Example usage for org.springframework.web.util UriComponentsBuilder query

List of usage examples for org.springframework.web.util UriComponentsBuilder query

Introduction

In this page you can find the example usage for org.springframework.web.util UriComponentsBuilder query.

Prototype

@Override
public UriComponentsBuilder query(@Nullable String query) 

Source Link

Document

Append the given query to the existing query of this builder.

Usage

From source file:org.mitre.discovery.util.WebfingerURLNormalizer.java

/**
 * Normalize the resource string as per OIDC Discovery.
 * @param identifier//from  w  w  w  . jav a  2s .  co  m
 * @return the normalized string, or null if the string can't be normalized
 */
public static UriComponents normalizeResource(String identifier) {
    // try to parse the URI
    // NOTE: we can't use the Java built-in URI class because it doesn't split the parts appropriately

    if (Strings.isNullOrEmpty(identifier)) {
        logger.warn("Can't normalize null or empty URI: " + identifier);
        return null; // nothing we can do
    } else {

        //UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(identifier);
        UriComponentsBuilder builder = UriComponentsBuilder.newInstance();

        Matcher m = pattern.matcher(identifier);
        if (m.matches()) {
            builder.scheme(m.group(2));
            builder.userInfo(m.group(6));
            builder.host(m.group(8));
            String port = m.group(10);
            if (!Strings.isNullOrEmpty(port)) {
                builder.port(Integer.parseInt(port));
            }
            builder.path(m.group(11));
            builder.query(m.group(13));
            builder.fragment(m.group(15)); // we throw away the hash, but this is the group it would be if we kept it
        } else {
            // doesn't match the pattern, throw it out
            logger.warn("Parser couldn't match input: " + identifier);
            return null;
        }

        UriComponents n = builder.build();

        if (Strings.isNullOrEmpty(n.getScheme())) {
            if (!Strings.isNullOrEmpty(n.getUserInfo()) && Strings.isNullOrEmpty(n.getPath())
                    && Strings.isNullOrEmpty(n.getQuery()) && n.getPort() < 0) {

                // scheme empty, userinfo is not empty, path/query/port are empty
                // set to "acct" (rule 2)
                builder.scheme("acct");

            } else {
                // scheme is empty, but rule 2 doesn't apply
                // set scheme to "https" (rule 3)
                builder.scheme("https");
            }
        }

        // fragment must be stripped (rule 4)
        builder.fragment(null);

        return builder.build();
    }

}

From source file:info.joseluismartin.gtc.AbstractTileCache.java

/**
 * Create a parameter map from a query string
 * @param uri the query string/*  w ww. j a  v  a  2  s .c  om*/
 * @return a map with parameters
 */
protected Map<String, String> getParameterMap(String uri) {
    UriComponentsBuilder b = UriComponentsBuilder.newInstance();
    b.query(uri);
    UriComponents c = b.build();
    return c.getQueryParams().toSingleValueMap();
}

From source file:org.cloudfoundry.identity.uaa.oauth.UaaAuthorizationEndpoint.java

private String getUnsuccessfulRedirect(AuthorizationRequest authorizationRequest, OAuth2Exception failure,
        boolean fragment) {

    if (authorizationRequest == null || authorizationRequest.getRedirectUri() == null) {
        // we have no redirect for the user. very sad.
        throw new UnapprovedClientAuthenticationException("Authorization failure, and no redirect URI.",
                failure);//  www.ja va2 s.c o  m
    }

    UriComponentsBuilder template = UriComponentsBuilder.fromUriString(authorizationRequest.getRedirectUri());
    StringBuilder values = new StringBuilder();

    values.append("error=" + encode(failure.getOAuth2ErrorCode()));
    values.append("&error_description=" + encode(failure.getMessage()));

    if (authorizationRequest.getState() != null) {
        values.append("&state=" + encode(authorizationRequest.getState()));
    }

    if (failure.getAdditionalInformation() != null) {
        for (Map.Entry<String, String> additionalInfo : failure.getAdditionalInformation().entrySet()) {
            values.append("&" + encode(additionalInfo.getKey()) + "=" + encode(additionalInfo.getValue()));
        }
    }

    if (fragment) {
        template.fragment(values.toString());
    } else {
        template.query(values.toString());
    }

    return template.build(true).toUriString();

}

From source file:org.joyrest.oauth2.endpoint.AuthorizationEndpoint.java

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;/*from   w  ww .  j  a  va2s .  co m*/
    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 (nonNull(keys) && 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();
}

From source file:org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.java

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;//from  w  w  w. ja  v a2  s .com
    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();

}