use of org.springframework.web.util.UriComponents in project spring-framework by spring-projects.
the class MvcUriComponentsBuilderTests method testFromMethodNameTwoPathVariables.
@Test
public void testFromMethodNameTwoPathVariables() throws Exception {
DateTime now = DateTime.now();
UriComponents uriComponents = fromMethodName(ControllerWithMethods.class, "methodWithTwoPathVariables", 1, now).build();
assertThat(uriComponents.getPath(), is("/something/1/foo/" + ISODateTimeFormat.date().print(now)));
}
use of org.springframework.web.util.UriComponents in project spring-framework by spring-projects.
the class ServletUriComponentsBuilderTests method fromRequestWithForwardedPrefix.
@Test
public void fromRequestWithForwardedPrefix() {
this.request.setRequestURI("/bar");
this.request.addHeader("X-Forwarded-Prefix", "/foo");
UriComponents result = ServletUriComponentsBuilder.fromRequest(this.request).build();
assertEquals("http://localhost/foo/bar", result.toUriString());
}
use of org.springframework.web.util.UriComponents in project spring-framework by spring-projects.
the class CorsUtils method isSameOrigin.
/**
* Check if the request is a same-origin one, based on {@code Origin}, {@code Host},
* {@code Forwarded} and {@code X-Forwarded-Host} headers.
* @return {@code true} if the request is a same-origin one, {@code false} in case
* of cross-origin request.
*/
public static boolean isSameOrigin(ServerHttpRequest request) {
String origin = request.getHeaders().getOrigin();
if (origin == null) {
return true;
}
UriComponentsBuilder urlBuilder = UriComponentsBuilder.fromHttpRequest(request);
UriComponents actualUrl = urlBuilder.build();
String actualHost = actualUrl.getHost();
int actualPort = getPort(actualUrl);
Assert.notNull(actualHost, "Actual request host must not be null");
Assert.isTrue(actualPort != -1, "Actual request port must not be undefined");
UriComponents originUrl = UriComponentsBuilder.fromOriginHeader(origin).build();
return (actualHost.equals(originUrl.getHost()) && actualPort == getPort(originUrl));
}
use of org.springframework.web.util.UriComponents in project spring-framework by spring-projects.
the class MvcUriComponentsBuilder method applyContributors.
private static UriComponents applyContributors(UriComponentsBuilder builder, Method method, Object... args) {
CompositeUriComponentsContributor contributor = getConfiguredUriComponentsContributor();
if (contributor == null) {
logger.debug("Using default CompositeUriComponentsContributor");
contributor = defaultUriComponentsContributor;
}
int paramCount = method.getParameterCount();
int argCount = args.length;
if (paramCount != argCount) {
throw new IllegalArgumentException("Number of method parameters " + paramCount + " does not match number of argument values " + argCount);
}
final Map<String, Object> uriVars = new HashMap<>();
for (int i = 0; i < paramCount; i++) {
MethodParameter param = new SynthesizingMethodParameter(method, i);
param.initParameterNameDiscovery(parameterNameDiscoverer);
contributor.contributeMethodArgument(param, args[i], builder, uriVars);
}
// We may not have all URI var values, expand only what we have
return builder.build().expand(new UriComponents.UriTemplateVariables() {
@Override
public Object getValue(String name) {
return uriVars.containsKey(name) ? uriVars.get(name) : UriComponents.UriTemplateVariables.SKIP_VALUE;
}
});
}
use of org.springframework.web.util.UriComponents in project spring-framework by spring-projects.
the class HtmlUnitRequestBuilder method buildRequest.
public MockHttpServletRequest buildRequest(ServletContext servletContext) {
Charset charset = getCharset();
String httpMethod = this.webRequest.getHttpMethod().name();
UriComponents uriComponents = uriComponents();
MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest(servletContext, httpMethod, uriComponents.getPath());
parent(request, this.parentBuilder);
// needs to be first for additional headers
request.setServerName(uriComponents.getHost());
authType(request);
request.setCharacterEncoding(charset.name());
content(request, charset);
contextPath(request, uriComponents);
contentType(request);
cookies(request);
headers(request);
locales(request);
servletPath(uriComponents, request);
params(request, uriComponents);
ports(uriComponents, request);
request.setProtocol("HTTP/1.1");
request.setQueryString(uriComponents.getQuery());
request.setScheme(uriComponents.getScheme());
request.setPathInfo(null);
return postProcess(request);
}
Aggregations