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

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

Introduction

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

Prototype

public UriComponents build() 

Source Link

Document

Build a UriComponents instance from the various components contained in this builder.

Usage

From source file:de.blizzy.documentr.web.util.FacadeHostRequestWrapper.java

public static String buildFacadeUrl(String url, String contextPath, String documentrHost) {
    contextPath = StringUtils.defaultIfBlank(contextPath, StringUtils.EMPTY);
    if (contextPath.equals("/")) { //$NON-NLS-1$
        contextPath = StringUtils.EMPTY;
    }//from   w ww.j a  v  a2  s .  c om

    String newUrl;
    if (StringUtils.isNotBlank(contextPath)) {
        int pos = url.indexOf(contextPath);
        newUrl = documentrHost + url.substring(pos + contextPath.length());
    } else {
        UriComponentsBuilder builder;
        try {
            builder = UriComponentsBuilder.fromHttpUrl(url);
        } catch (IllegalArgumentException e) {
            builder = UriComponentsBuilder.fromUriString(url);
        }
        String path = StringUtils.defaultIfBlank(builder.build().getPath(), StringUtils.EMPTY);
        if (StringUtils.isNotBlank(path)) {
            int pos = StringUtils.lastIndexOf(url, path);
            newUrl = documentrHost + url.substring(pos);
        } else {
            newUrl = documentrHost;
        }
    }
    return newUrl;
}

From source file:com.rsa.redchallenge.standaloneapp.utils.RestInteractor.java

/**
 * Helper function to create the URL to be called
 *
 * @param params   parameter for the call
 * @param restPath path of the interface in the REST server to be called
 * @return URL to be called//from w w  w .jav a 2  s.c  om
 */
private static String populatePath(Map<String, Object> params, String restPath) {
    String uri = null;
    if (params != null && !params.isEmpty()) {
        String url = restPath;
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);

        for (Map.Entry<String, Object> param : params.entrySet())
            builder.queryParam(param.getKey(), param.getValue());

        uri = builder.build().toUriString();
    } else {
        uri = restPath;
    }

    return uri;
}

From source file:cherry.example.web.util.ViewNameUtil.java

public static String fromMethodCall(Object invocationInfo) {

    MethodInvocationInfo info = (MethodInvocationInfo) invocationInfo;
    Method method = info.getControllerMethod();
    Class<?> type = method.getDeclaringClass();

    UriComponentsBuilder ucb = UriComponentsBuilder.newInstance();
    String typePath = getMappedPath(type.getAnnotation(RequestMapping.class));
    if (StringUtils.isNotEmpty(typePath)) {
        ucb.path(typePath);/*  w w  w . j  a v a 2s  . co  m*/
    }

    String methodPath = getMappedPath(method.getAnnotation(RequestMapping.class));
    if (StringUtils.isNotEmpty(methodPath)) {
        ucb.pathSegment(methodPath);
    }

    String path = ucb.build().getPath();
    if (path.startsWith("/")) {
        return path.substring(1);
    }
    return path;
}

From source file:org.terasoluna.gfw.web.el.Functions.java

/**
 * build query string from map./*from  w w w  .  j  av  a 2  s.  c  o  m*/
 * <p>
 * query string is encoded with "UTF-8".
 * </p>
 * @see ObjectToMapConverter
 * @param map map
 * @return query string. if map is not empty, return query string. ex) name1=value&amp;name2=value&amp;...
 */
public static String mapToQuery(Map<String, Object> map) {
    if (map == null || map.isEmpty()) {
        return "";
    }
    UriComponentsBuilder builder = UriComponentsBuilder.fromPath("");
    for (Map.Entry<String, Object> e : map.entrySet()) {
        String name = e.getKey();
        Object value = e.getValue();
        builder.queryParam(name, value);
    }
    String query = builder.build().encode().toString();
    // remove the beginning symbol character('?') of the query string.
    return query.substring(1);
}

From source file:com.neiljbrown.brighttalk.channels.reportingapi.client.spring.SpringApiClientImpl.java

/**
 * Builds an absolute HTTP URL from a supplied base URI, a relative path an an optional map of request parameters.
 * <p>//from   www .  ja  v a2  s  .c o m
 * Supports building URLs before URL template variables have been expanded - template variable placeholders ({...})
 * will _not_ be encoded.
 * 
 * @param baseUri The {@link URL base URI}.
 * @param relativeUrlPath The relative path to be appended to the base URI.
 * @param requestParams An optional, map representation of request parameters to be appended to the URL. Can be null.
 * @return A String representation of the absolute URL. A return type of String rather than {@link java.net.URI} is
 * used to avoid encoding the URL before any template variables have been replaced.
 */
private static String buildAbsoluteHttpUrl(URI baseUri, String relativeUrlPath,
        Map<String, List<String>> requestParams) {
    UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUri(baseUri);
    uriBuilder.path(relativeUrlPath);
    if (requestParams != null) {
        for (String paramName : requestParams.keySet()) {
            for (String paramValue : requestParams.get(paramName)) {
                uriBuilder.queryParam(paramName, paramValue);
            }
        }
    }
    UriComponents uriComponents = uriBuilder.build();
    return uriComponents.toUriString();
}

From source file:org.terasoluna.gfw.web.el.Functions.java

/**
 * build query string from map with the specified {@link BeanWrapper}.
 * <p>//from w  ww . j  av  a2s.c om
 * query string is encoded with "UTF-8".<br>
 * <strong>Use {@link #mapToQuery(Map)} instead of this method.</strong>
 * </p>
 * @see ObjectToMapConverter
 * @param map map
 * @param beanWrapper beanWrapper which has the definition of each field.
 * @return query string. if map is not empty, return query string. ex) name1=value&amp;name2=value&amp;...
 * @deprecated (since 5.0.1, to support nested fields in f:query, Use {@link #mapToQuery(Map)} instead of this method.)
 */
@Deprecated
public static String mapToQuery(Map<String, Object> map, BeanWrapper beanWrapper) {
    if (map == null || map.isEmpty()) {
        return "";
    }
    UriComponentsBuilder builder = UriComponentsBuilder.fromPath("");
    for (Map.Entry<String, Object> e : map.entrySet()) {
        String name = e.getKey();
        Object value = e.getValue();
        TypeDescriptor sourceType;
        if (beanWrapper != null) {
            sourceType = beanWrapper.getPropertyTypeDescriptor(name);
        } else {
            sourceType = TypeDescriptor.forObject(value);
        }
        builder.queryParam(name, CONVERSION_SERVICE.convert(value, sourceType, STRING_DESC));
    }
    String query = builder.build().encode().toString();
    // remove the beginning symbol character('?') of the query string.
    return query.substring(1);
}

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

/**
 * Normalize the resource string as per OIDC Discovery.
 * @param identifier//from w  w  w . ja  va 2 s. 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:com.pepaproch.gtswsdlclient.impl.AvailabilityCheckByPhoneQueryBuilderImpl.java

@Override
public UriComponents buildQuery(String phoneNumber) {
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
    if (phoneNumber != null) {
        builder.queryParam(PHONE_FIELD_NAME, phoneNumber);
    }// www  .  j  a va2  s  .c o  m

    return builder.build();
}

From source file:com.pepaproch.gtswsdlclient.impl.AvailabilityCheckQueryBuilderImpl.java

@Override
public UriComponents buildQuery(String addrId) {
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
    if (addrId != null && !addrId.isEmpty()) {
        builder.queryParam("uirAdr", addrId);
    }//from   www  .  ja v a2 s .  c  o m

    return builder.build();
}

From source file:ph.com.fsoft.temp.service.red.service.PersonServiceImpl.java

@Override
public PersonDto findById(long id) {
    UriComponentsBuilder uri = UriComponentsBuilder.fromHttpUrl("http://blue/blue/api/rest/person")
            .queryParam("id", id);
    return restTemplate.getForObject(uri.build().toUri(), PersonDto.class);
}