private void addPaginationParams(UriComponentsBuilder builder, int offset, int limit) { if (offset > 0) { builder.queryParam("offset", offset); } if (limit > 0) { builder.queryParam("limit", limit); } }
@RequestMapping("/drug/enforcements") @ResponseBody public JsonNode getDrugRecallsForUnii( @RequestParam(value = "unii", defaultValue = "") String unii, @RequestParam(value = "limit", defaultValue = "0") int limit) throws Exception { JsonNode node; HttpHeaders headers = new HttpHeaders(); ObjectMapper mapper = new ObjectMapper(); headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(searchDrugEnfrcmntUrl) .queryParam("limit", uniiRecallsLimit); this.apiKey.addToUriComponentsBuilder(builder); if (unii != null && unii.trim().length() > 0) { builder.queryParam("search", "openfda.unii:" + unii); try { node = mapper.readTree(rest.getForObject(builder.build().toUri(), String.class)); ((ObjectNode) node).set("results", getSortedResults(node.get("results"), limit)); } catch (HttpClientErrorException ex) { if (ex.getStatusCode().value() == 404) { node = new ObjectMapper() .readTree( "{\"error\":{\"code\":\"NOT_FOUND\", \"message\":\"No matches found!\"}}"); } else { throw ex; } } } else { node = getDrugRecalls(limit == 0 ? 10 : limit, null, null); } return node; }
private void addGeoQueryParams(UriComponentsBuilder builder, GeoQuery geoQuery) { if (geoQuery != null) { StringBuilder georel = new StringBuilder(geoQuery.getRelation().name()); if (geoQuery.getRelation() == GeoQuery.Relation.near) { georel.append(';').append(geoQuery.getModifier()); georel.append(':').append(geoQuery.getDistance()); } builder.queryParam("georel", georel.toString()); builder.queryParam("geometry", geoQuery.getGeometry()); builder.queryParam( "coords", geoQuery .getCoordinates() .stream() .map(Coordinate::toString) .collect(Collectors.joining(";"))); } }
private String append( String base, Map<String, ?> query, Map<String, String> keys, boolean fragment) { UriComponentsBuilder template = UriComponentsBuilder.newInstance(); UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(base); URI redirectUri; try { // assume it's encoded to start with (if it came in over the wire) redirectUri = builder.build(true).toUri(); } catch (Exception e) { // ... but allow client registrations to contain hard-coded non-encoded values redirectUri = builder.build().toUri(); builder = UriComponentsBuilder.fromUri(redirectUri); } template .scheme(redirectUri.getScheme()) .port(redirectUri.getPort()) .host(redirectUri.getHost()) .userInfo(redirectUri.getUserInfo()) .path(redirectUri.getPath()); if (fragment) { StringBuilder values = new StringBuilder(); if (redirectUri.getFragment() != null) { String append = redirectUri.getFragment(); values.append(append); } for (String key : query.keySet()) { if (values.length() > 0) { values.append("&"); } String name = key; if (keys != null && keys.containsKey(key)) { name = keys.get(key); } values.append(name + "={" + key + "}"); } if (values.length() > 0) { template.fragment(values.toString()); } UriComponents encoded = template.build().expand(query).encode(); builder.fragment(encoded.getFragment()); } else { for (String key : query.keySet()) { String name = key; if (keys != null && keys.containsKey(key)) { name = keys.get(key); } template.queryParam(name, "{" + key + "}"); } template.fragment(redirectUri.getFragment()); UriComponents encoded = template.build().expand(query).encode(); builder.query(encoded.getQuery()); } return builder.build().toUriString(); }
@RequestMapping("/completion") public String completion( @Validated OrderForm form, @RequestParam(value = "productCode") String productCode, UriComponentsBuilder uriBuilder) { orderService.order(productCode, form.getQuantity()); uriBuilder.path("/menu"); uriBuilder.queryParam("orderedCode", productCode); return "redirect:" + uriBuilder.build().toUriString(); }
/* * (non-Javadoc) * @see org.springframework.hateoas.MethodLinkBuilderFactory#linkTo(java.lang.Object) */ @Override public ControllerLinkBuilder linkTo(Object invocationValue) { Assert.isInstanceOf(LastInvocationAware.class, invocationValue); LastInvocationAware invocations = (LastInvocationAware) invocationValue; MethodInvocation invocation = invocations.getLastInvocation(); Iterator<Object> classMappingParameters = invocations.getObjectParameters(); Method method = invocation.getMethod(); String mapping = DISCOVERER.getMapping(method); UriComponentsBuilder builder = ControllerLinkBuilder.getBuilder().path(mapping); UriTemplate template = new UriTemplate(mapping); Map<String, Object> values = new HashMap<String, Object>(); Iterator<String> names = template.getVariableNames().iterator(); while (classMappingParameters.hasNext()) { values.put(names.next(), classMappingParameters.next()); } for (BoundMethodParameter parameter : PATH_VARIABLE_ACCESSOR.getBoundParameters(invocation)) { values.put(parameter.getVariableName(), parameter.asString()); } for (BoundMethodParameter parameter : REQUEST_PARAM_ACCESSOR.getBoundParameters(invocation)) { Object value = parameter.getValue(); String key = parameter.getVariableName(); if (value instanceof Collection) { for (Object element : (Collection<?>) value) { builder.queryParam(key, element); } } else { builder.queryParam(key, parameter.asString()); } } UriComponents components = applyUriComponentsContributer(builder, invocation).buildAndExpand(values); return new ControllerLinkBuilder(UriComponentsBuilder.fromUri(components.toUri())); }
private String getHref(int pageParameter) { UriComponentsBuilder localBuilder = baseComponent.cloneBuilder(); if (queryString == null) { return localBuilder .queryParam(HalResourceMetadata.PARAM_PAGE, pageParameter) .build() .toString(); } else { return localBuilder .query(queryString) .queryParam(HalResourceMetadata.PARAM_PAGE, pageParameter) .build() .toString(); } }
/* * (non-Javadoc) * @see org.springframework.hateoas.mvc.UriComponentsContributor#enhance(org.springframework.web.util.UriComponentsBuilder, org.springframework.core.MethodParameter, java.lang.Object) */ @Override public void enhance(UriComponentsBuilder builder, MethodParameter parameter, Object value) { if (!(value instanceof Sort)) { return; } Sort sort = (Sort) value; String sortParameter = getSortParameter(parameter); builder.replaceQueryParam(sortParameter); for (String expression : foldIntoExpressions(sort)) { builder.queryParam(sortParameter, expression); } }
/** * Augments a {@link UriComponentsBuilder} with queries based on the methods annotated with {@link * FilterParameter} * * @param builder the builder to augment * @param instance the instance to inspect and invoke */ @SuppressWarnings("unchecked") public static void augment(UriComponentsBuilder builder, Object instance) { Method[] methods = ReflectionUtils.getAllDeclaredMethods(instance.getClass()); Arrays.sort(methods, MethodNameComparator.INSTANCE); for (Method method : methods) { FilterParameter filterParameter = AnnotationUtils.getAnnotation(method, FilterParameter.class); if (filterParameter == null) { continue; } String value = getValue(method, instance); if (StringUtils.hasText(value)) { builder.queryParam(filterParameter.value(), value); } } }
private void addParam( UriComponentsBuilder builder, String key, Collection<? extends CharSequence> value) { if (!nullOrEmpty(value)) { builder.queryParam(key, String.join(",", value)); } }
private void addParam(UriComponentsBuilder builder, String key, String value) { if (!nullOrEmpty(value)) { builder.queryParam(key, value); } }