/** * Replace, add or remove query parameters in the current request and return the new query part of * the request. You can remove a query parameter by providing an empty string or null as value. * * @param hr * @param params array of name/value like params[0]="name" and params[1] = "values". Length of the * array must be even. * @return query part only e.g. view=table&filter=auto or null if the params array is odd. */ public static String replaceCurrentQueryParams(HttpRequestHashModel hr, String... params) { UriComponentsBuilder bldr = ServletUriComponentsBuilder.fromRequest(hr.getRequest()); if (params.length % 2 != 0) { return null; } for (int i = 0; i < params.length; i += 2) { // if the value is blank, remove the parameter if (StringUtils.isBlank(params[i + 1])) { bldr.replaceQueryParam(params[i]); } else { bldr.replaceQueryParam(params[i], params[i + 1]); } } return bldr.build().getQuery(); }
final String constructFirstPageUri(final UriComponentsBuilder uriBuilder, final int size) { return uriBuilder .replaceQueryParam(PAGE, 0) .replaceQueryParam("size", size) .build() .encode() .toUriString(); }
/* * (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); } }
/** * Replace or add a query parameter in the current request and return the new query part of the * request. * * @param hr HttpRequestHashModel from Freemarker template (variable Request) * @param name query string parameter to add or change * @param value value of the query string parameter * @return query part only e.g. view=table&filter=auto */ public static String replaceCurrentQueryParam( HttpRequestHashModel hr, String name, String value) { UriComponentsBuilder bldr = ServletUriComponentsBuilder.fromRequest(hr.getRequest()); bldr.replaceQueryParam(name, value); return bldr.build().getQuery(); }
/** * Get absolute URL from HttpRequestHashModel and remove a query string parameter. * * @param hr from Freemarker template (variable Request) * @param name query string parameter to remove * @return absolute URL as String */ public static String getURLRemoveQueryParam(HttpRequestHashModel hr, String name) { UriComponentsBuilder bldr = ServletUriComponentsBuilder.fromRequest(hr.getRequest()); bldr.replaceQueryParam(name); return bldr.build().toUriString(); }
/** * Get an URI with a provided language as query parameter. Then parameter will be added or * replaced. e.g. -getUriWithLanguage("/search?q=cal","fr") will produce /search?q=cal&lang=fr * -getUriWithLanguage("/search?q=cal&lang=en","fr") will produce /search?q=cal&lang=fr * * @param uri * @param lang * @return */ public static String getUriWithLanguage(String uri, String lang) { UriComponentsBuilder bldr = UriComponentsBuilder.fromUriString(uri); bldr.replaceQueryParam(LANG_PARAM, lang); return bldr.build().toUriString(); }
private EntityCollectionResponseV2 createEntityCollectionResponse( String entityName, EntityCollectionRequestV2 request, HttpServletRequest httpRequest) { EntityMetaData meta = dataService.getEntityMetaData(entityName); Query q = request.getQ() != null ? request.getQ().createQuery(meta) : new QueryImpl(); q.pageSize(request.getNum()).offset(request.getStart()).sort(request.getSort()); Fetch fetch = AttributeFilterToFetchConverter.convert( request.getAttrs(), meta, languageService.getCurrentUserLanguageCode()); if (fetch != null) { q.fetch(fetch); } if (request.getAggs() != null) { // return aggregates for aggregate query AggregateQuery aggsQ = request.getAggs().createAggregateQuery(meta, q); AttributeMetaData xAttr = aggsQ.getAttributeX(); AttributeMetaData yAttr = aggsQ.getAttributeY(); if (xAttr == null && yAttr == null) { throw new MolgenisQueryException("Aggregate query is missing 'x' or 'y' attribute"); } AggregateResult aggs = dataService.aggregate(entityName, aggsQ); AttributeMetaDataResponseV2 xAttrResponse = xAttr != null ? new AttributeMetaDataResponseV2( entityName, meta, xAttr, fetch, permissionService, dataService, languageService) : null; AttributeMetaDataResponseV2 yAttrResponse = yAttr != null ? new AttributeMetaDataResponseV2( entityName, meta, yAttr, fetch, permissionService, dataService, languageService) : null; return new EntityAggregatesResponse( aggs, xAttrResponse, yAttrResponse, BASE_URI + '/' + entityName); } else { Long count = dataService.count(entityName, q); Iterable<Entity> it; if (count > 0) { it = new Iterable<Entity>() { @Override public Iterator<Entity> iterator() { return dataService.findAll(entityName, q).iterator(); } }; } else { it = Collections.emptyList(); } EntityPager pager = new EntityPager(request.getStart(), request.getNum(), count, it); List<Map<String, Object>> entities = new ArrayList<>(); for (Entity entity : it) { Map<String, Object> responseData = new LinkedHashMap<String, Object>(); createEntityValuesResponse(entity, fetch, responseData); entities.add(responseData); } UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(getFullURL(httpRequest)); String prevHref = null; if (pager.getPrevStart() != null) { builder.replaceQueryParam("start", pager.getPrevStart()); prevHref = builder.build(false).toUriString(); } String nextHref = null; if (pager.getNextStart() != null) { builder.replaceQueryParam("start", pager.getNextStart()); nextHref = builder.build(false).toUriString(); } return new EntityCollectionResponseV2( pager, entities, fetch, BASE_URI + '/' + entityName, meta, permissionService, dataService, languageService, prevHref, nextHref); } }