Search in sources :

Example 6 with UriTemplate

use of org.springframework.web.util.UriTemplate in project spring-framework by spring-projects.

the class RequestContext method getContextUrl.

/**
	 * Return a context-aware URl for the given relative URL with placeholders --
	 * named keys with braces {@code {}}. For example, send in a relative URL
	 * {@code foo/{bar}?spam={spam}} and a parameter map {@code {bar=baz,spam=nuts}}
	 * and the result will be {@code [contextpath]/foo/baz?spam=nuts}.
	 * @param relativeUrl the relative URL part
	 * @param params a map of parameters to insert as placeholders in the url
	 * @return a URL that points back to the current web application with an
	 * absolute path also URL-encoded accordingly
	 */
public String getContextUrl(String relativeUrl, Map<String, ?> params) {
    String url = getContextPath() + relativeUrl;
    UriTemplate template = new UriTemplate(url);
    url = template.expand(params).toASCIIString();
    return getExchange().getResponse().encodeUrl(url);
}
Also used : UriTemplate(org.springframework.web.util.UriTemplate)

Example 7 with UriTemplate

use of org.springframework.web.util.UriTemplate in project spring-framework by spring-projects.

the class RequestContext method getContextUrl.

/**
	 * Return a context-aware URl for the given relative URL with placeholders (named keys with braces {@code {}}).
	 * For example, send in a relative URL {@code foo/{bar}?spam={spam}} and a parameter map
	 * {@code {bar=baz,spam=nuts}} and the result will be {@code [contextpath]/foo/baz?spam=nuts}.
	 * @param relativeUrl the relative URL part
	 * @param params a map of parameters to insert as placeholders in the url
	 * @return a URL that points back to the server with an absolute path (also URL-encoded accordingly)
	 */
public String getContextUrl(String relativeUrl, Map<String, ?> params) {
    String url = getContextPath() + relativeUrl;
    UriTemplate template = new UriTemplate(url);
    url = template.expand(params).toASCIIString();
    if (this.response != null) {
        url = this.response.encodeURL(url);
    }
    return url;
}
Also used : UriTemplate(org.springframework.web.util.UriTemplate)

Example 8 with UriTemplate

use of org.springframework.web.util.UriTemplate in project spring-data-document-examples by spring-projects.

the class RestTemplate method execute.

// general execution
public <T> T execute(String url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor, Object... urlVariables) throws RestClientException {
    UriTemplate uriTemplate = new HttpUrlTemplate(url);
    URI expanded = uriTemplate.expand(urlVariables);
    return doExecute(expanded, method, requestCallback, responseExtractor);
}
Also used : UriTemplate(org.springframework.web.util.UriTemplate) URI(java.net.URI)

Example 9 with UriTemplate

use of org.springframework.web.util.UriTemplate in project spring-framework by spring-projects.

the class RequestEntityTests method uriVariablesExpansion.

@Test
public void uriVariablesExpansion() throws URISyntaxException {
    URI uri = new UriTemplate("http://example.com/{foo}").expand("bar");
    RequestEntity.get(uri).accept(MediaType.TEXT_PLAIN).build();
    String url = "http://www.{host}.com/{path}";
    String host = "example";
    String path = "foo/bar";
    URI expected = new URI("http://www.example.com/foo/bar");
    uri = new UriTemplate(url).expand(host, path);
    RequestEntity<?> entity = RequestEntity.get(uri).build();
    assertEquals(expected, entity.getUrl());
    Map<String, String> uriVariables = new HashMap<>(2);
    uriVariables.put("host", host);
    uriVariables.put("path", path);
    uri = new UriTemplate(url).expand(uriVariables);
    entity = RequestEntity.get(uri).build();
    assertEquals(expected, entity.getUrl());
}
Also used : HashMap(java.util.HashMap) UriTemplate(org.springframework.web.util.UriTemplate) URI(java.net.URI) Test(org.junit.Test)

Example 10 with UriTemplate

use of org.springframework.web.util.UriTemplate in project geode by apache.

the class RestHttpOperationInvoker method resolveLink.

/**
   * Resolves one Link from a Collection of Links based on the command invocation matching multiple
   * relations from the Link Index.
   * 
   * @param command the CommandRequest object encapsulating details of the command invocation.
   * @param links a Collection of Links for the command matching the relation.
   * @return the resolved Link matching the command exactly as entered by the user.
   * @see #findLink(org.apache.geode.management.internal.cli.CommandRequest)
   * @see org.apache.geode.management.internal.cli.CommandRequest
   * @see org.apache.geode.management.internal.web.domain.Link
   * @see org.springframework.web.util.UriTemplate
   */
// Find and use the Link with the greatest number of path variables that can be expanded!
protected Link resolveLink(final CommandRequest command, final List<Link> links) {
    // NOTE, Gfsh's ParseResult contains a Map entry for all command options whether or not the user
    // set the option
    // with a value on the command-line, argh!
    Map<String, String> commandParametersCopy = CollectionUtils.removeKeys(new HashMap<>(command.getParameters()), NoValueFilter.INSTANCE);
    Link resolvedLink = null;
    int pathVariableCount = 0;
    for (Link link : links) {
        final List<String> pathVariables = new UriTemplate(decode(link.getHref().toString())).getVariableNames();
        // to even be considered...
        if (commandParametersCopy.keySet().containsAll(pathVariables)) {
            // for the last Link
            if (resolvedLink == null || (pathVariables.size() > pathVariableCount)) {
                resolvedLink = link;
                pathVariableCount = pathVariables.size();
            }
        }
    }
    if (resolvedLink == null) {
        throw new RestApiCallForCommandNotFoundException(String.format("No REST API call for command (%1$s) was found!", command.getInput()));
    }
    return resolvedLink;
}
Also used : UriTemplate(org.springframework.web.util.UriTemplate) Link(org.apache.geode.management.internal.web.domain.Link)

Aggregations

UriTemplate (org.springframework.web.util.UriTemplate)11 URI (java.net.URI)4 ContentStoreService (org.craftercms.core.service.ContentStoreService)2 Test (org.junit.Test)2 Statement (org.junit.runners.model.Statement)2 RestClientException (org.springframework.web.client.RestClientException)2 RestTemplate (org.springframework.web.client.RestTemplate)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Matcher (java.util.regex.Matcher)1 Link (org.apache.geode.management.internal.web.domain.Link)1 Context (org.craftercms.core.service.Context)1 ScriptFactory (org.craftercms.engine.scripting.ScriptFactory)1 SiteContext (org.craftercms.engine.service.context.SiteContext)1 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)1 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)1 RestOperations (org.springframework.web.client.RestOperations)1