Search in sources :

Example 21 with UriComponents

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)));
}
Also used : UriComponents(org.springframework.web.util.UriComponents) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 22 with UriComponents

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());
}
Also used : UriComponents(org.springframework.web.util.UriComponents) Test(org.junit.Test)

Example 23 with UriComponents

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));
}
Also used : UriComponents(org.springframework.web.util.UriComponents) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder)

Example 24 with UriComponents

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;
        }
    });
}
Also used : SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) UriComponents(org.springframework.web.util.UriComponents) CompositeUriComponentsContributor(org.springframework.web.method.support.CompositeUriComponentsContributor) HashMap(java.util.HashMap) MethodParameter(org.springframework.core.MethodParameter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter)

Example 25 with UriComponents

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);
}
Also used : UriComponents(org.springframework.web.util.UriComponents) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Charset(java.nio.charset.Charset)

Aggregations

UriComponents (org.springframework.web.util.UriComponents)48 Test (org.junit.Test)32 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)10 MvcUriComponentsBuilder (org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder)6 HttpRequest (org.springframework.http.HttpRequest)2 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 InsufficientAuthenticationException (org.springframework.security.authentication.InsufficientAuthenticationException)2 AuthenticationException (org.springframework.security.core.AuthenticationException)2 JsonParseException (com.google.gson.JsonParseException)1 URI (java.net.URI)1 Charset (java.nio.charset.Charset)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 DateTime (org.joda.time.DateTime)1 LocalDate (org.joda.time.LocalDate)1 MethodParameter (org.springframework.core.MethodParameter)1 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)1 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)1