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

The following examples show how to use org.springframework.web.util.UriComponentsBuilder#build() . These examples are extracted from open source projects. 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
/**
 * Helper method to build the url if the url is static and doesn't change
 *
 * @param httpUrl
 * @param pathComplete
 * @param queryParams
 * @return
 */
protected static String buildUrl(String httpUrl, String pathComplete, HashMap<String, String> queryParams) {
	UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(httpUrl);

	builder.path(pathComplete);

	if (null != queryParams) {
		// build the URL with all the params it needs
		builder = buildUrlParams(builder, queryParams);
	}

	UriComponents uriComponents = builder.build();
	return uriComponents.toString();
}
 
Example 2
@Override
public void sendRedirect(String location) throws IOException {

	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(location);
	UriComponents uriComponents = builder.build();

	// Absolute location
	if (uriComponents.getScheme() != null) {
		super.sendRedirect(location);
		return;
	}

	// Network-path reference
	if (location.startsWith("//")) {
		String scheme = this.request.getScheme();
		super.sendRedirect(builder.scheme(scheme).toUriString());
		return;
	}

	String path = uriComponents.getPath();
	if (path != null) {
		// Relative to Servlet container root or to current request
		path = (path.startsWith(FOLDER_SEPARATOR) ? path :
				StringUtils.applyRelativePath(this.request.getRequestURI(), path));
	}

	String result = UriComponentsBuilder
			.fromHttpRequest(new ServletServerHttpRequest(this.request))
			.replacePath(path)
			.replaceQuery(uriComponents.getQuery())
			.fragment(uriComponents.getFragment())
			.build().normalize().toUriString();

	super.sendRedirect(result);
}
 
Example 3
@Override
public void sendRedirect(String location) throws IOException {

	UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(location);
	UriComponents uriComponents = builder.build();

	// Absolute location
	if (uriComponents.getScheme() != null) {
		super.sendRedirect(location);
		return;
	}

	// Network-path reference
	if (location.startsWith("//")) {
		String scheme = this.request.getScheme();
		super.sendRedirect(builder.scheme(scheme).toUriString());
		return;
	}

	String path = uriComponents.getPath();
	if (path != null) {
		// Relative to Servlet container root or to current request
		path = (path.startsWith(FOLDER_SEPARATOR) ? path :
				StringUtils.applyRelativePath(this.request.getRequestURI(), path));
	}

	String result = UriComponentsBuilder
			.fromHttpRequest(new ServletServerHttpRequest(this.request))
			.replacePath(path)
			.replaceQuery(uriComponents.getQuery())
			.fragment(uriComponents.getFragment())
			.build().normalize().toUriString();

	super.sendRedirect(result);
}
 
Example 4
@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();

    Route route = routeLocator.getMatchingRoute(
            urlPathHelper.getPathWithinApplication(ctx.getRequest()));
    if (route != null) {
        Pair<String, String> lh = locationHeader(ctx);
        if (lh != null) {
            String location = lh.second();
            URI originalRequestUri = UriComponentsBuilder
                    .fromHttpRequest(new ServletServerHttpRequest(ctx.getRequest()))
                    .build().toUri();
            UriComponentsBuilder redirectedUriBuilder = UriComponentsBuilder
                    .fromUriString(location);

            UriComponents redirectedUriComps = redirectedUriBuilder.build();

            String modifiedLocation = redirectedUriBuilder
                    .scheme(scheme)
                    .host(host)
                    .port(port).replacePath(redirectedUriComps.getPath())
                    .queryParam("redirect", true)
                    .queryParam("redirect.scheme", redirectedUriComps.getScheme())
                    .queryParam("redirect.host", redirectedUriComps.getHost())
                    .queryParam("redirect.port", redirectedUriComps.getPort())
                    .toUriString();
            lh.setSecond(modifiedLocation);
        }
    }
    return null;
}
 
Example 5
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
    UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(request.getServletPath());
    uriComponentsBuilder.queryParam(queryParam);
    uriComponentsBuilder.queryParam(StringUtils.defaultIfEmpty(request.getQueryString(), StringUtils.EMPTY));
    UriComponents uriComponents = uriComponentsBuilder.build();
    String returnUrlWithQueryParameter = uriComponents.toUriString();
    LOGGER.debug(returnUrlWithQueryParameter);
    redirectStrategy.sendRedirect(request, response, returnUrlWithQueryParameter);
}
 
Example 6
Source Project: zstack   File: URLBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public static String hideUrlPassword(String url){
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url);
    UriComponents u = builder.build();
    if (u.getUserInfo() == null) {
        return url;
    } else {
        return builder.userInfo(u.getUserInfo().split(":")[0]).build().toUriString();
    }
}
 
Example 7
private UriComponents uriComponents() {
	URL url = this.webRequest.getUrl();
	UriComponentsBuilder uriBldr = UriComponentsBuilder.fromUriString(url.toExternalForm());
	return uriBldr.build();
}