Example usage for org.springframework.web.util UriTemplate expand

List of usage examples for org.springframework.web.util UriTemplate expand

Introduction

In this page you can find the example usage for org.springframework.web.util UriTemplate expand.

Prototype

public URI expand(Object... uriVariableValues) 

Source Link

Document

Given an array of variables, expand this template into a full URI.

Usage

From source file:tv.arte.resteventapi.core.presentation.decoration.RestEventApiControllerLinkBuilder.java

public static String linkTo(Class<?> controller, Method method, Map<String, ?> parameters,
        ConversionService conversionService) {

    Assert.notNull(controller, "Controller type must not be null!");
    Assert.notNull(method, "Method must not be null!");

    RestEventApiControllerLinkBuilder linkBuilder = new RestEventApiControllerLinkBuilder(getBuilder());

    if (parameters == null) {
        parameters = new HashMap<String, Object>(1);
    }//from   w  ww.  j a v a2 s. co m

    UriTemplate template = new UriTemplate(DISCOVERER.getMapping(controller, method));
    URI uri = template.expand(parameters);
    linkBuilder = linkBuilder.slash(uri);
    List<String> templateVariables = template.getVariableNames();

    for (Entry<String, ?> paramEnt : parameters.entrySet()) {
        if (!templateVariables.contains(paramEnt.getKey())) {
            String queryParamName = paramEnt.getKey();
            Object queryParamValue = null;

            if (paramEnt.getValue() != null) {
                queryParamValue = conversionService.convert(paramEnt.getValue(), String.class);
            }

            if (queryParamValue != null) {
                linkBuilder.uriBuilder.queryParam(queryParamName, queryParamValue);
            }
        }
    }

    return linkBuilder.uriBuilder.build().toUriString();
}

From source file:com.ge.predix.acs.commons.web.UriTemplateUtils.java

/**
 * Generates an instance of the URI according to the template given by uriTemplate, by expanding the variables
 * with the values provided by keyValues.
 *
 * @param uriTemplate/*w w  w.  j a  va2  s . c o m*/
 *            The URI template
 * @param keyValues
 *            Dynamic list of string of the form "key:value"
 * @return The corresponding URI instance
 */
public static URI expand(final String uriTemplate, final String... keyValues) {

    UriTemplate template = new UriTemplate(uriTemplate);
    Map<String, String> uriVariables = new HashMap<>();

    for (String kv : keyValues) {
        String[] keyValue = kv.split(":");
        uriVariables.put(keyValue[0], keyValue[1]);
    }
    return template.expand(uriVariables);
}

From source file:com.hemou.android.util.StrUtils.java

public static String toUri(String url, Object... urlVariables) {
    try {/* ww  w .  j  a va  2 s  .c om*/
        UriTemplate uriTemplate = new UriTemplate(url);
        URI expanded = uriTemplate.expand(urlVariables);
        return expanded.toURL().toString();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return url;
}

From source file:net.eusashead.hateoas.header.impl.LocationHeaderImpl.java

public LocationHeaderImpl(String uriTemplate, Object... objects) {
    UriTemplate uri = new UriTemplate(uriTemplate);
    this.value = uri.expand(objects);
}

From source file:com.sambrannen.samples.events.web.RestEventController.java

private String buildNewLocation(HttpServletRequest request, Long id) {
    String url = request.getRequestURL().append("/{id}").toString();
    UriTemplate uriTemplate = new UriTemplate(url);
    return uriTemplate.expand(id).toASCIIString();
}

From source file:org.openwms.common.location.LocationGroupController.java

private String getLocationForCreatedResource(javax.servlet.http.HttpServletRequest req, String objId) {
    StringBuffer url = req.getRequestURL();
    UriTemplate template = new UriTemplate(url.append("/{objId}/").toString());
    return template.expand(objId).toASCIIString();
}

From source file:org.openwms.tms.api.TransportationController.java

private String getCreatedResourceURI(HttpServletRequest req, String objId) {
    StringBuffer url = req.getRequestURL();
    UriTemplate template = new UriTemplate(url.append("/{objId}/").toString());
    return template.expand(objId).toASCIIString();
}

From source file:cz.zcu.kiv.eegdatabase.webservices.rest.datafile.DataFileServiceController.java

/**
 * Builds URL for file download./*w  w  w . ja v a  2  s.c  o m*/
 *
 * @param request HTTP request
 * @param id      data file id
 * @return URL string
 */
private String buildLocation(HttpServletRequest request, Object id) {
    StringBuffer url = request.getRequestURL();
    UriTemplate ut = new UriTemplate(url.append("/{id}").toString());
    return ut.expand(id).toASCIIString();
}

From source file:org.openwms.core.http.AbstractWebController.java

/**
 * Append the ID of the object that was created to the original request URL and return it.
 *
 * @param req The HttpServletRequest object
 * @param objId The ID to append//w  w  w  .j av  a 2 s.c  om
 * @return The complete appended URL
 */
protected String getLocationForCreatedResource(HttpServletRequest req, String objId) {
    StringBuffer url = req.getRequestURL();
    UriTemplate template = new UriTemplate(url.append("/{objId}/").toString());
    return template.expand(objId).toASCIIString();
}

From source file:com.github.alexfalappa.nbspringboot.projects.initializr.InitializrService.java

public JsonNode getDependencies(String bootVersion) throws Exception {
    if (!dependencyMetaMap.containsKey(bootVersion)) {
        // set connection timeouts
        timeoutFromPrefs();/*w ww . j  a va  2 s.  c  om*/
        // prepare request
        final String serviceUrl = NbPreferences.forModule(PrefConstants.class).get(PREF_INITIALIZR_URL,
                "http://start.spring.io");
        UriTemplate template = new UriTemplate(serviceUrl.concat("/dependencies?{bootVersion}"));
        RequestEntity<Void> req = RequestEntity.get(template.expand(bootVersion))
                .accept(MediaType.valueOf("application/vnd.initializr.v2.1+json"))
                .header("User-Agent", REST_USER_AGENT).build();
        // connect
        logger.log(INFO, "Getting Spring Initializr dependencies metadata from: {0}", template);
        logger.log(INFO, "Asking metadata as: {0}", REST_USER_AGENT);
        long start = System.currentTimeMillis();
        ResponseEntity<String> respEntity = rt.exchange(req, String.class);
        // analyze response
        final HttpStatus statusCode = respEntity.getStatusCode();
        if (statusCode == OK) {
            ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
            final JsonNode depMeta = mapper.readTree(respEntity.getBody());
            logger.log(INFO,
                    "Retrieved Spring Initializr dependencies metadata for boot version {0}. Took {1} msec",
                    new Object[] { bootVersion, System.currentTimeMillis() - start });
            if (logger.isLoggable(FINE)) {
                logger.fine(mapper.writeValueAsString(depMeta));
            }
            dependencyMetaMap.put(bootVersion, depMeta);
        } else {
            // log status code
            final String errMessage = String.format(
                    "Spring initializr service connection problem. HTTP status code: %s",
                    statusCode.toString());
            logger.severe(errMessage);
            // throw exception in order to set error message
            throw new RuntimeException(errMessage);
        }
    }
    return dependencyMetaMap.get(bootVersion);
}