Search in sources :

Example 1 with UriComponentsBuilder

use of org.springframework.web.util.UriComponentsBuilder 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 2 with UriComponentsBuilder

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

the class MvcUriComponentsBuilderTests method testFromMethodNameWithCustomBaseUrlViaInstance.

@Test
public void testFromMethodNameWithCustomBaseUrlViaInstance() throws Exception {
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base");
    MvcUriComponentsBuilder mvcBuilder = MvcUriComponentsBuilder.relativeTo(builder);
    UriComponents uriComponents = mvcBuilder.withMethodName(ControllerWithMethods.class, "methodWithPathVariable", new Object[] { "1" }).build();
    assertEquals("http://example.org:9090/base/something/1/foo", uriComponents.toString());
    assertEquals("http://example.org:9090/base", builder.toUriString());
}
Also used : UriComponents(org.springframework.web.util.UriComponents) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) Test(org.junit.Test)

Example 3 with UriComponentsBuilder

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

the class MvcUriComponentsBuilderTests method testFromControllerWithCustomBaseUrlViaInstance.

@Test
public void testFromControllerWithCustomBaseUrlViaInstance() {
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base");
    MvcUriComponentsBuilder mvcBuilder = MvcUriComponentsBuilder.relativeTo(builder);
    UriComponents uriComponents = mvcBuilder.withController(PersonControllerImpl.class).build();
    assertEquals("http://example.org:9090/base/people", uriComponents.toString());
    assertEquals("http://example.org:9090/base", builder.toUriString());
}
Also used : UriComponents(org.springframework.web.util.UriComponents) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) Test(org.junit.Test)

Example 4 with UriComponentsBuilder

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

the class MvcUriComponentsBuilderTests method testFromMappingNameWithCustomBaseUrl.

@Test
public void testFromMappingNameWithCustomBaseUrl() throws Exception {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(WebConfig.class);
    context.refresh();
    this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
    UriComponentsBuilder baseUrl = UriComponentsBuilder.fromUriString("http://example.org:9999/base");
    MvcUriComponentsBuilder mvcBuilder = MvcUriComponentsBuilder.relativeTo(baseUrl);
    String url = mvcBuilder.withMappingName("PAC#getAddressesForCountry").arg(0, "DE").buildAndExpand(123);
    assertEquals("http://example.org:9999/base/people/123/addresses/DE", url);
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 5 with UriComponentsBuilder

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

the class MvcUriComponentsBuilderTests method testFromMethodCallWithCustomBaseUrlViaInstance.

@Test
public void testFromMethodCallWithCustomBaseUrlViaInstance() {
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base");
    MvcUriComponentsBuilder mvcBuilder = MvcUriComponentsBuilder.relativeTo(builder);
    UriComponents result = mvcBuilder.withMethodCall(on(ControllerWithMethods.class).myMethod(null)).build();
    assertEquals("http://example.org:9090/base/something/else", result.toString());
    assertEquals("http://example.org:9090/base", builder.toUriString());
}
Also used : UriComponents(org.springframework.web.util.UriComponents) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) Test(org.junit.Test)

Aggregations

UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)155 Test (org.junit.Test)34 UriComponents (org.springframework.web.util.UriComponents)23 HttpEntity (org.springframework.http.HttpEntity)18 ServletUriComponentsBuilder (org.springframework.web.servlet.support.ServletUriComponentsBuilder)13 URI (java.net.URI)10 Test (org.junit.jupiter.api.Test)9 HttpHeaders (org.springframework.http.HttpHeaders)9 MvcUriComponentsBuilder (org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder)9 SearchRequest (org.nzbhydra.searching.searchrequests.SearchRequest)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)7 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)6 User (com.serotonin.m2m2.vo.User)5 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)5 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5