Search in sources :

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

Example 2 with UriComponents

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

the class AuthorizationEndpoint method append.

private String append(String base, Map<String, ?> query, Map<String, String> keys, boolean fragment) {
    UriComponentsBuilder template = UriComponentsBuilder.newInstance();
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(base);
    URI redirectUri;
    try {
        // assume it's encoded to start with (if it came in over the wire)
        redirectUri = builder.build(true).toUri();
    } catch (Exception e) {
        // ... but allow client registrations to contain hard-coded non-encoded values
        redirectUri = builder.build().toUri();
        builder = UriComponentsBuilder.fromUri(redirectUri);
    }
    template.scheme(redirectUri.getScheme()).port(redirectUri.getPort()).host(redirectUri.getHost()).userInfo(redirectUri.getUserInfo()).path(redirectUri.getPath());
    if (fragment) {
        StringBuilder values = new StringBuilder();
        if (redirectUri.getFragment() != null) {
            String append = redirectUri.getFragment();
            values.append(append);
        }
        for (String key : query.keySet()) {
            if (values.length() > 0) {
                values.append("&");
            }
            String name = key;
            if (keys != null && keys.containsKey(key)) {
                name = keys.get(key);
            }
            values.append(name + "={" + key + "}");
        }
        if (values.length() > 0) {
            template.fragment(values.toString());
        }
        UriComponents encoded = template.build().expand(query).encode();
        builder.fragment(encoded.getFragment());
    } else {
        for (String key : query.keySet()) {
            String name = key;
            if (keys != null && keys.containsKey(key)) {
                name = keys.get(key);
            }
            template.queryParam(name, "{" + key + "}");
        }
        template.fragment(redirectUri.getFragment());
        UriComponents encoded = template.build().expand(query).encode();
        builder.query(encoded.getQuery());
    }
    return builder.build().toUriString();
}
Also used : UriComponents(org.springframework.web.util.UriComponents) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) URI(java.net.URI) OAuth2Exception(org.springframework.security.oauth2.common.exceptions.OAuth2Exception) UserDeniedAuthorizationException(org.springframework.security.oauth2.common.exceptions.UserDeniedAuthorizationException) RedirectMismatchException(org.springframework.security.oauth2.common.exceptions.RedirectMismatchException) AuthenticationException(org.springframework.security.core.AuthenticationException) BadClientCredentialsException(org.springframework.security.oauth2.common.exceptions.BadClientCredentialsException) HttpSessionRequiredException(org.springframework.web.HttpSessionRequiredException) InvalidRequestException(org.springframework.security.oauth2.common.exceptions.InvalidRequestException) UnsupportedResponseTypeException(org.springframework.security.oauth2.common.exceptions.UnsupportedResponseTypeException) ClientRegistrationException(org.springframework.security.oauth2.provider.ClientRegistrationException) ClientAuthenticationException(org.springframework.security.oauth2.common.exceptions.ClientAuthenticationException) InvalidClientException(org.springframework.security.oauth2.common.exceptions.InvalidClientException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) InsufficientAuthenticationException(org.springframework.security.authentication.InsufficientAuthenticationException) UnapprovedClientAuthenticationException(org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException)

Example 3 with UriComponents

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

the class ServletUriComponentsBuilder method initFromRequest.

/**
	 * Initialize a builder with a scheme, host,and port (but not path and query).
	 */
private static ServletUriComponentsBuilder initFromRequest(HttpServletRequest request) {
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents uriComponents = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    String scheme = uriComponents.getScheme();
    String host = uriComponents.getHost();
    int port = uriComponents.getPort();
    ServletUriComponentsBuilder builder = new ServletUriComponentsBuilder();
    builder.scheme(scheme);
    builder.host(host);
    if (("http".equals(scheme) && port != 80) || ("https".equals(scheme) && port != 443)) {
        builder.port(port);
    }
    return builder;
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) UriComponents(org.springframework.web.util.UriComponents)

Example 4 with UriComponents

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

the class MvcUriComponentsBuilderTests method testFromMethodNamePathVariable.

@Test
public void testFromMethodNamePathVariable() throws Exception {
    UriComponents uriComponents = fromMethodName(ControllerWithMethods.class, "methodWithPathVariable", new Object[] { "1" }).build();
    assertThat(uriComponents.toUriString(), is("http://localhost/something/1/foo"));
}
Also used : UriComponents(org.springframework.web.util.UriComponents) Test(org.junit.Test)

Example 5 with UriComponents

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

the class MvcUriComponentsBuilderTests method testFromControllerNotMapped.

@Test
public void testFromControllerNotMapped() {
    UriComponents uriComponents = fromController(UnmappedController.class).build();
    assertThat(uriComponents.toUriString(), is("http://localhost/"));
}
Also used : UriComponents(org.springframework.web.util.UriComponents) Test(org.junit.Test)

Aggregations

UriComponents (org.springframework.web.util.UriComponents)127 Test (org.junit.jupiter.api.Test)42 Test (org.junit.Test)37 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)24 MvcUriComponentsBuilder (org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder)7 URI (java.net.URI)5 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 HashMap (java.util.HashMap)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 HttpHeaders (org.springframework.http.HttpHeaders)3 ResponseEntity (org.springframework.http.ResponseEntity)3 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)3 LinkedHashSet (java.util.LinkedHashSet)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Matchers.anyString (org.mockito.Matchers.anyString)2