Search in sources :

Example 66 with UriComponents

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

the class MvcUriComponentsBuilderTests method fromMethodCallWithPathVariable.

@Test
public void fromMethodCallWithPathVariable() {
    UriComponents uriComponents = fromMethodCall(on(ControllerWithMethods.class).methodWithPathVariable("1")).build();
    assertThat(uriComponents.toUriString()).startsWith("http://localhost");
    assertThat(uriComponents.toUriString()).endsWith("/something/1/foo");
}
Also used : UriComponents(org.springframework.web.util.UriComponents) Test(org.junit.jupiter.api.Test)

Example 67 with UriComponents

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

the class ServletUriComponentsBuilderTests method fromRequestWithForwardedPrefix.

// SPR-16650
@Test
public void fromRequestWithForwardedPrefix() throws Exception {
    this.request.addHeader("X-Forwarded-Prefix", "/prefix");
    this.request.setContextPath("/mvc-showcase");
    this.request.setRequestURI("/mvc-showcase/bar");
    HttpServletRequest requestToUse = adaptFromForwardedHeaders(this.request);
    UriComponents result = ServletUriComponentsBuilder.fromRequest(requestToUse).build();
    assertThat(result.toUriString()).isEqualTo("http://localhost/prefix/bar");
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) UriComponents(org.springframework.web.util.UriComponents) Test(org.junit.jupiter.api.Test)

Example 68 with UriComponents

use of org.springframework.web.util.UriComponents in project ontrack by nemerosa.

the class DefaultURIBuilder method build.

@Override
public URI build(Object methodInvocation) {
    // Default builder
    UriComponentsBuilder builder = MvcUriComponentsBuilder.fromMethodCall(methodInvocation);
    // Default URI
    UriComponents uriComponents = builder.build();
    // TODO #251 Workaround for SPR-12771
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    String portHeader = httpRequest.getHeaders().getFirst("X-Forwarded-Port");
    if (StringUtils.hasText(portHeader)) {
        int port = Integer.parseInt(portHeader);
        String scheme = uriComponents.getScheme();
        if (("https".equals(scheme) && port == 443) || ("http".equals(scheme) && port == 80)) {
            port = -1;
        }
        builder.port(port);
    }
    // OK
    return builder.build().toUri();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) UriComponents(org.springframework.web.util.UriComponents) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ServletUriComponentsBuilder(org.springframework.web.servlet.support.ServletUriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 69 with UriComponents

use of org.springframework.web.util.UriComponents in project lavagna by digitalfondue.

the class Redirector method sendRedirect.

public static void sendRedirect(HttpServletRequest req, HttpServletResponse resp, String page, Map<String, List<String>> params) throws IOException {
    UriComponents urlToRedirect = UriComponentsBuilder.fromPath(page).queryParams(new LinkedMultiValueMap<>(params)).build();
    resp.sendRedirect(urlToRedirect.toUriString());
}
Also used : UriComponents(org.springframework.web.util.UriComponents) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Example 70 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)

Aggregations

UriComponents (org.springframework.web.util.UriComponents)133 Test (org.junit.jupiter.api.Test)43 Test (org.junit.Test)37 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)26 MvcUriComponentsBuilder (org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder)7 URI (java.net.URI)5 HashMap (java.util.HashMap)5 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 MvcResult (org.springframework.test.web.servlet.MvcResult)4 ServletUriComponentsBuilder (org.springframework.web.servlet.support.ServletUriComponentsBuilder)4 MethodParameter (org.springframework.core.MethodParameter)3 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)3 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Matchers.anyString (org.mockito.Matchers.anyString)2