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);
}
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;
}
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);
}
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());
}
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;
}
Aggregations