Search in sources :

Example 26 with UriComponents

use of org.springframework.web.util.UriComponents in project dhis2-core by dhis2.

the class MobileController method populateContextPath.

private void populateContextPath(Model model, HttpServletRequest request) {
    UriComponents contextPath = ServletUriComponentsBuilder.fromContextPath(request).build();
    String contextPathString = contextPath.toString();
    String xForwardedProto = request.getHeader("X-Forwarded-Proto");
    if (xForwardedProto != null) {
        if (contextPathString.contains("http://") && xForwardedProto.equalsIgnoreCase("https")) {
            contextPathString = contextPathString.replace("http://", "https://");
        } else if (contextPathString.contains("https://") && xForwardedProto.equalsIgnoreCase("http")) {
            contextPathString = contextPathString.replace("https://", "http://");
        }
    }
    model.addAttribute("contextPath", contextPathString);
}
Also used : UriComponents(org.springframework.web.util.UriComponents)

Example 27 with UriComponents

use of org.springframework.web.util.UriComponents in project java-crud-api by kolchagov.

the class TestApi method getMockHttpServletRequest.

private MockHttpServletRequest getMockHttpServletRequest() throws UnsupportedEncodingException {
    final MockHttpServletRequest req = new MockHttpServletRequest();
    req.setServerName("localhost");
    req.setMethod(method);
    final UriComponents build = UriComponentsBuilder.fromUriString(this.baseUrl).build();
    req.setPathInfo(build.getPath());
    req.setQueryString(build.getQuery());
    setParamsFromBuild(req, build);
    if (data != null) {
        try {
            if (data.endsWith("__is_null"))
                throw new JsonParseException("");
            // invalid json test expects json content
            if (!"{\"}".equals(data)) {
                parser.parse(data);
            }
            req.setContentType("application/json");
        } catch (JsonParseException ignored) {
            req.setContentType("application/x-www-form-urlencoded");
            final String url = "/?" + URLDecoder.decode(data, "utf8");
            setParamsFromBuild(req, UriComponentsBuilder.fromUriString(url).build());
        }
        req.setContent(data.getBytes("utf8"));
    }
    return req;
}
Also used : UriComponents(org.springframework.web.util.UriComponents) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JsonParseException(com.google.gson.JsonParseException)

Example 28 with UriComponents

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

the class AbstractFlashMapManager method isFlashMapForRequest.

/**
	 * Whether the given FlashMap matches the current request.
	 * Uses the expected request path and query parameters saved in the FlashMap.
	 */
protected boolean isFlashMapForRequest(FlashMap flashMap, HttpServletRequest request) {
    String expectedPath = flashMap.getTargetRequestPath();
    if (expectedPath != null) {
        String requestUri = getUrlPathHelper().getOriginatingRequestUri(request);
        if (!requestUri.equals(expectedPath) && !requestUri.equals(expectedPath + "/")) {
            return false;
        }
    }
    UriComponents uriComponents = ServletUriComponentsBuilder.fromRequest(request).build();
    MultiValueMap<String, String> actualParams = uriComponents.getQueryParams();
    MultiValueMap<String, String> expectedParams = flashMap.getTargetRequestParams();
    for (String expectedName : expectedParams.keySet()) {
        List<String> actualValues = actualParams.get(expectedName);
        if (actualValues == null) {
            return false;
        }
        for (String expectedValue : expectedParams.get(expectedName)) {
            if (!actualValues.contains(expectedValue)) {
                return false;
            }
        }
    }
    return true;
}
Also used : UriComponents(org.springframework.web.util.UriComponents)

Example 29 with UriComponents

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

the class ServletUriComponentsBuilderTests method fromRequestWithForwardedPrefixTrailingSlash.

@Test
public void fromRequestWithForwardedPrefixTrailingSlash() {
    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 30 with UriComponents

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

the class ServletUriComponentsBuilderTests method fromRequestWithForwardedHostAndPort.

// Most X-Forwarded-* tests in UriComponentsBuilderTests
@Test
public void fromRequestWithForwardedHostAndPort() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(80);
    request.setRequestURI("/mvc-showcase");
    request.addHeader("X-Forwarded-Proto", "https");
    request.addHeader("X-Forwarded-Host", "84.198.58.199");
    request.addHeader("X-Forwarded-Port", "443");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertEquals("https://84.198.58.199/mvc-showcase", result.toString());
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) UriComponents(org.springframework.web.util.UriComponents) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

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