Java Code Examples for org.springframework.web.util.UriComponentsBuilder#replaceQueryParam()

The following examples show how to use org.springframework.web.util.UriComponentsBuilder#replaceQueryParam() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WxApiExecutor.java    From FastBootWeixin with Apache License 2.0 6 votes vote down vote up
private RequestEntity buildHttpRequestEntity(WxApiMethodInfo wxApiMethodInfo, Object[] args) {
        UriComponentsBuilder builder = wxApiMethodInfo.fromArgs(args);
        // 替换accessToken
        builder.replaceQueryParam(WX_ACCESS_TOKEN_PARAM_NAME, wxTokenManager.get());
        HttpHeaders httpHeaders = null;
        Object body = null;
        if (wxApiMethodInfo.getRequestMethod() == WxApiRequest.Method.JSON) {
            httpHeaders = buildJsonHeaders();
            // 由于内置的AbstractJackson2HttpMessageConverter中writeInternal方法,对json的解析使用默认的UTF-8编码
            // 导致对于多字节的UTF8编码EMOJI表情进行了转义,产生了不正确的结果
            // 但是内置功能无法被简单修改,所以这里自己转了一下
            body = getStringBody(wxApiMethodInfo, args);
//            body = getObjectBody(wxApiMethodInfo, args);
        } else if (wxApiMethodInfo.getRequestMethod() == WxApiRequest.Method.XML) {
            httpHeaders = buildXmlHeaders();
            // 暂时不支持xml转换。。。
            body = getObjectBody(wxApiMethodInfo, args);
        } else if (wxApiMethodInfo.getRequestMethod() == WxApiRequest.Method.FORM) {
            body = getFormBody(wxApiMethodInfo, args);
        }
        return new RequestEntity(body, httpHeaders, wxApiMethodInfo.getRequestMethod().getHttpMethod(), builder.build().toUri());
    }
 
Example 2
Source File: Util.java    From airsonic-advanced with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Return an URL for the given HTTP request, with anonymized sensitive parameters.
 *
 * @param request An HTTP request instance
 * @return The associated anonymized URL
 */
public static String getAnonymizedURLForRequest(HttpServletRequest request) {

    String url = getURLForRequest(request);
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url);
    MultiValueMap<String, String> components = builder.build().getQueryParams();

    // Subsonic REST API authentication (see RESTRequestParameterProcessingFilter)
    if (components.containsKey("p")) builder.replaceQueryParam("p", URL_SENSITIVE_REPLACEMENT_STRING);  // Cleartext password
    if (components.containsKey("t")) builder.replaceQueryParam("t", URL_SENSITIVE_REPLACEMENT_STRING);  // Token
    if (components.containsKey("s")) builder.replaceQueryParam("s", URL_SENSITIVE_REPLACEMENT_STRING);  // Salt
    if (components.containsKey("u")) builder.replaceQueryParam("u", URL_SENSITIVE_REPLACEMENT_STRING);  // Username

    return builder.build().toUriString();
}
 
Example 3
Source File: Util.java    From airsonic with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Return an URL for the given HTTP request, with anonymized sensitive parameters.
 *
 * @param request An HTTP request instance
 * @return The associated anonymized URL
 */
public static String getAnonymizedURLForRequest(HttpServletRequest request) {

    String url = getURLForRequest(request);
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url);
    MultiValueMap<String, String> components = builder.build().getQueryParams();

    // Subsonic REST API authentication (see RESTRequestParameterProcessingFilter)
    if (components.containsKey("p")) builder.replaceQueryParam("p", URL_SENSITIVE_REPLACEMENT_STRING);  // Cleartext password
    if (components.containsKey("t")) builder.replaceQueryParam("t", URL_SENSITIVE_REPLACEMENT_STRING);  // Token
    if (components.containsKey("s")) builder.replaceQueryParam("s", URL_SENSITIVE_REPLACEMENT_STRING);  // Salt
    if (components.containsKey("u")) builder.replaceQueryParam("u", URL_SENSITIVE_REPLACEMENT_STRING);  // Username

    return builder.build().toUriString();
}
 
Example 4
Source File: LinksUtils.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static URI createEntitiesResponseUri(@Nullable @CheckForNull Integer pageNumber) {
  UriComponentsBuilder builder =
      MolgenisServletUriComponentsBuilder.fromCurrentRequestDecodedQuery();
  if (pageNumber != null) {
    builder.replaceQueryParam(PageUtils.PAGE_QUERY_PARAMETER_NAME, pageNumber);
  }
  return builder.build().toUri();
}
 
Example 5
Source File: EntityMapperImpl.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private URI createEntitiesResponseUri(Integer pageNumber) {
  UriComponentsBuilder builder =
      MolgenisServletUriComponentsBuilder.fromCurrentRequestDecodedQuery();
  if (pageNumber != null) {
    builder.replaceQueryParam("page", pageNumber);
  }
  return builder.build().toUri();
}
 
Example 6
Source File: PageLinksAspect.java    From Showcase with Apache License 2.0 4 votes vote down vote up
private UriComponentsBuilder replacePageParams(UriComponentsBuilder origional, Pageable pageable) {
    UriComponentsBuilder builder = origional.cloneBuilder();
    builder.replaceQueryParam("page", pageable.getPageNumber());
    builder.replaceQueryParam("size", pageable.getPageSize());
    return builder;
}
 
Example 7
Source File: RestControllerV2.java    From molgenis with GNU Lesser General Public License v3.0 4 votes vote down vote up
private EntityCollectionResponseV2 createEntityCollectionResponse(
    ServletUriComponentsBuilder uriBuilder,
    String entityTypeId,
    EntityCollectionRequestV2 request,
    HttpServletRequest httpRequest,
    boolean includeCategories) {
  Repository<Entity> repository = dataService.getRepository(entityTypeId);
  EntityType entityType = repository.getEntityType();

  Query<Entity> q =
      request.getQ() != null ? request.getQ().createQuery(repository) : new QueryImpl<>();
  q.pageSize(request.getNum()).offset(request.getStart()).sort(request.getSort());
  Fetch fetch =
      AttributeFilterToFetchConverter.convert(
          request.getAttrs(), entityType, LocaleContextHolder.getLocale().getLanguage());
  if (fetch != null) {
    q.fetch(fetch);
  }

  if (request.getAggs() != null) {
    // return aggregates for aggregate query
    AggregateQuery aggsQ = request.getAggs().createAggregateQuery(entityType, q);
    Attribute xAttr = aggsQ.getAttributeX();
    Attribute yAttr = aggsQ.getAttributeY();
    if (xAttr == null && yAttr == null) {
      throw new MolgenisQueryException("Aggregate query is missing 'x' or 'y' attribute");
    }
    AggregateResult aggs = dataService.aggregate(entityTypeId, aggsQ);
    AttributeResponseV2 xAttrResponse =
        xAttr != null
            ? new AttributeResponseV2(
                uriBuilder,
                entityTypeId,
                entityType,
                xAttr,
                fetch,
                permissionService,
                dataService)
            : null;
    AttributeResponseV2 yAttrResponse =
        yAttr != null
            ? new AttributeResponseV2(
                uriBuilder,
                entityTypeId,
                entityType,
                yAttr,
                fetch,
                permissionService,
                dataService)
            : null;
    return new EntityAggregatesResponse(
        aggs,
        xAttrResponse,
        yAttrResponse,
        UriUtils.createEntityCollectionUriPath(uriBuilder, entityTypeId));
  } else {
    Long count = dataService.count(entityTypeId, new QueryImpl<>(q).setOffset(0).setPageSize(0));
    Iterable<Entity> it;
    if (count > 0 && q.getPageSize() > 0) {
      it = () -> dataService.findAll(entityTypeId, 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<>();
      createEntityValuesResponse(uriBuilder, 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(
        uriBuilder,
        pager,
        entities,
        fetch,
        UriUtils.createEntityCollectionUriPath(uriBuilder, entityTypeId),
        entityType,
        permissionService,
        dataService,
        prevHref,
        nextHref,
        includeCategories);
  }
}