Java Code Examples for org.springframework.web.util.UriUtils#encodeQuery()

The following examples show how to use org.springframework.web.util.UriUtils#encodeQuery() . 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: OauthController.java    From kaif with Apache License 2.0 6 votes vote down vote up
private RedirectView redirectViewWithQuery(String redirectUri, String state, String query) {
  if (!Strings.isNullOrEmpty(state)) {
    query += "&state=" + state;
  }
  String encoded = UriUtils.encodeQuery(query, Charsets.UTF_8.name());
  String locationUri = redirectUri;
  if (redirectUri.contains("?")) {
    locationUri += "&" + encoded;
  } else {
    locationUri += "?" + encoded;
  }
  RedirectView redirectView = new RedirectView(locationUri);
  redirectView.setStatusCode(HttpStatus.FOUND);
  redirectView.setExposeModelAttributes(false);
  redirectView.setPropagateQueryParams(false);
  return redirectView;
}
 
Example 2
Source File: ActiveServerFilter.java    From atlas with Apache License 2.0 5 votes vote down vote up
private void handleRedirect(HttpServletRequest servletRequest, HttpServletResponse httpServletResponse,
                            String activeServerAddress) throws IOException {
    String requestURI = servletRequest.getRequestURI();
    String queryString = servletRequest.getQueryString();

    if (queryString != null && (!queryString.isEmpty())) {
        queryString = UriUtils.encodeQuery(queryString, "UTF-8");
    }

    if ((queryString != null) && (!queryString.isEmpty())) {
        requestURI += "?" + queryString;
    }

    if (requestURI == null) {
        requestURI = "/";
    }
    String redirectLocation = activeServerAddress + requestURI;
    LOG.info("Not active. Redirecting to {}", redirectLocation);
    // A POST/PUT/DELETE require special handling by sending HTTP 307 instead of the regular 301/302.
    // Reference: http://stackoverflow.com/questions/2068418/whats-the-difference-between-a-302-and-a-307-redirect
    if (isUnsafeHttpMethod(servletRequest)) {
        httpServletResponse.setHeader(HttpHeaders.LOCATION, redirectLocation);
        httpServletResponse.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
    } else {
        httpServletResponse.sendRedirect(redirectLocation);
    }
}