Example usage for org.springframework.web.util UriComponentsBuilder fromUri

List of usage examples for org.springframework.web.util UriComponentsBuilder fromUri

Introduction

In this page you can find the example usage for org.springframework.web.util UriComponentsBuilder fromUri.

Prototype

public static UriComponentsBuilder fromUri(URI uri) 

Source Link

Document

Create a builder that is initialized with the given URI .

Usage

From source file:org.mitre.openid.connect.client.service.impl.TestSignedAuthRequestUrlBuilder.java

/**
 * This test takes the URI from the result of building a signed request
 * and checks that the JWS object parsed from the request URI matches up
 * with the expected claim values.// ww  w . j  av  a 2s  .  c  o m
 */
@Test
public void buildAuthRequestUrl() {

    String requestUri = urlBuilder.buildAuthRequestUrl(serverConfig, clientConfig, redirectUri, nonce, state,
            options, null);

    // parsing the result
    UriComponentsBuilder builder = null;

    try {
        builder = UriComponentsBuilder.fromUri(new URI(requestUri));
    } catch (URISyntaxException e1) {
        fail("URISyntaxException was thrown.");
    }

    UriComponents components = builder.build();
    String jwtString = components.getQueryParams().get("request").get(0);
    JWTClaimsSet claims = null;

    try {
        SignedJWT jwt = SignedJWT.parse(jwtString);
        claims = jwt.getJWTClaimsSet();
    } catch (ParseException e) {
        fail("ParseException was thrown.");
    }

    assertEquals(responseType, claims.getClaim("response_type"));
    assertEquals(clientConfig.getClientId(), claims.getClaim("client_id"));

    List<String> scopeList = Arrays.asList(((String) claims.getClaim("scope")).split(" "));
    assertTrue(scopeList.containsAll(clientConfig.getScope()));

    assertEquals(redirectUri, claims.getClaim("redirect_uri"));
    assertEquals(nonce, claims.getClaim("nonce"));
    assertEquals(state, claims.getClaim("state"));
    for (String claim : options.keySet()) {
        assertEquals(options.get(claim), claims.getClaim(claim));
    }
}

From source file:org.cloudfoundry.identity.uaa.login.feature.ImplicitGrantIT.java

@Test
public void testInvalidScopes() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

    LinkedMultiValueMap<String, String> postBody = new LinkedMultiValueMap<>();
    postBody.add("client_id", "cf");
    postBody.add("redirect_uri", "https://uaa.cloudfoundry.com/redirect/cf");
    postBody.add("response_type", "token");
    postBody.add("source", "credentials");
    postBody.add("username", testAccounts.getUserName());
    postBody.add("password", testAccounts.getPassword());
    postBody.add("scope", "read");

    ResponseEntity<Void> responseEntity = restOperations.exchange(baseUrl + "/oauth/authorize", HttpMethod.POST,
            new HttpEntity<>(postBody, headers), Void.class);

    Assert.assertEquals(HttpStatus.FOUND, responseEntity.getStatusCode());

    System.out.println(/*w  w  w . ja va  2  s.  c  o m*/
            "responseEntity.getHeaders().getLocation() = " + responseEntity.getHeaders().getLocation());

    UriComponents locationComponents = UriComponentsBuilder.fromUri(responseEntity.getHeaders().getLocation())
            .build();
    Assert.assertEquals("uaa.cloudfoundry.com", locationComponents.getHost());
    Assert.assertEquals("/redirect/cf", locationComponents.getPath());

    MultiValueMap<String, String> params = parseFragmentParams(locationComponents);

    Assert.assertThat(params.getFirst("error"), is("invalid_scope"));
    Assert.assertThat(params.getFirst("access_token"), isEmptyOrNullString());
    Assert.assertThat(params.getFirst("credentials"), isEmptyOrNullString());
}

From source file:org.mitre.openid.connect.client.service.impl.TestSignedAuthRequestUrlBuilder.java

@Test
public void buildAuthRequestUrl_withLoginHint() {

    String requestUri = urlBuilder.buildAuthRequestUrl(serverConfig, clientConfig, redirectUri, nonce, state,
            options, loginHint);//from  w  w  w . j ava 2s  . com

    // parsing the result
    UriComponentsBuilder builder = null;

    try {
        builder = UriComponentsBuilder.fromUri(new URI(requestUri));
    } catch (URISyntaxException e1) {
        fail("URISyntaxException was thrown.");
    }

    UriComponents components = builder.build();
    String jwtString = components.getQueryParams().get("request").get(0);
    JWTClaimsSet claims = null;

    try {
        SignedJWT jwt = SignedJWT.parse(jwtString);
        claims = jwt.getJWTClaimsSet();
    } catch (ParseException e) {
        fail("ParseException was thrown.");
    }

    assertEquals(responseType, claims.getClaim("response_type"));
    assertEquals(clientConfig.getClientId(), claims.getClaim("client_id"));

    List<String> scopeList = Arrays.asList(((String) claims.getClaim("scope")).split(" "));
    assertTrue(scopeList.containsAll(clientConfig.getScope()));

    assertEquals(redirectUri, claims.getClaim("redirect_uri"));
    assertEquals(nonce, claims.getClaim("nonce"));
    assertEquals(state, claims.getClaim("state"));
    for (String claim : options.keySet()) {
        assertEquals(options.get(claim), claims.getClaim(claim));
    }
    assertEquals(loginHint, claims.getClaim("login_hint"));
}

From source file:com.expedia.seiso.web.hateoas.link.ItemLinks.java

private UriComponentsBuilder repoUri(Class<?> itemClass, MultiValueMap<String, String> params) {
    // @formatter:off
    return UriComponentsBuilder.fromUri(versionUri).queryParams(params);
    // @formatter:on
}

From source file:org.cloudfoundry.identity.uaa.login.feature.OpenIdTokenGrantsIT.java

@Test
public void testImplicitGrant() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

    LinkedMultiValueMap<String, String> postBody = new LinkedMultiValueMap<>();
    postBody.add("client_id", "cf");
    postBody.add("redirect_uri", "https://uaa.cloudfoundry.com/redirect/cf");
    postBody.add("response_type", "token id_token");
    postBody.add("source", "credentials");
    postBody.add("username", user.getUserName());
    postBody.add("password", "secret");

    ResponseEntity<Void> responseEntity = restOperations.exchange(loginUrl + "/oauth/authorize",
            HttpMethod.POST, new HttpEntity<>(postBody, headers), Void.class);

    Assert.assertEquals(HttpStatus.FOUND, responseEntity.getStatusCode());

    UriComponents locationComponents = UriComponentsBuilder.fromUri(responseEntity.getHeaders().getLocation())
            .build();//from w  w  w.  jav  a2s  .c  o  m
    Assert.assertEquals("uaa.cloudfoundry.com", locationComponents.getHost());
    Assert.assertEquals("/redirect/cf", locationComponents.getPath());

    MultiValueMap<String, String> params = parseFragmentParams(locationComponents);

    Assert.assertThat(params.get("jti"), not(empty()));
    Assert.assertEquals("bearer", params.getFirst("token_type"));
    Assert.assertThat(Integer.parseInt(params.getFirst("expires_in")), Matchers.greaterThan(40000));

    String[] scopes = UriUtils.decode(params.getFirst("scope"), "UTF-8").split(" ");
    Assert.assertThat(Arrays.asList(scopes), containsInAnyOrder("scim.userids", "password.write",
            "cloud_controller.write", "openid", "cloud_controller.read"));

    validateToken("access_token", params.toSingleValueMap(), scopes);
    validateToken("id_token", params.toSingleValueMap(), scopes);
}

From source file:org.devefx.httpmapper.binding.MapperMethod.java

private URI appendUrlParams(URI uri, MultiValueMap<String, Object> body) throws URISyntaxException {
    UriComponentsBuilder builder = UriComponentsBuilder.fromUri(uri);
    for (Map.Entry<String, List<Object>> entry : body.entrySet()) {
        String key = entry.getKey();
        for (Object value : entry.getValue()) {
            if (value instanceof String) {
                builder.queryParam(key, (String) value);
            }/*from   w  ww.  j av  a2s.  c  o  m*/
        }
    }
    UriComponents uriComponents = builder.build();
    return uriComponents.toUri();
}

From source file:software.coolstuff.springframework.owncloud.service.impl.rest.OwncloudRestResourceServiceImpl.java

private URI resolveAsDirectoryURI(URI relativeTo) {
    URI userRoot = getUserRoot();
    if (relativeTo == null || StringUtils.isBlank(relativeTo.getPath())) {
        return userRoot;
    }/*ww  w  . ja v a2  s .com*/
    return URI
            .create(UriComponentsBuilder.fromUri(userRoot).path(relativeTo.getPath()).path(SLASH).toUriString())
            .normalize();
}

From source file:info.rmapproject.api.responsemgr.ResourceResponseManagerTestIT.java

/**
 * Test the RMap Resource RDF stmts.//from  w w w . ja  v a2 s  . co m
 */
@Test
public void getRMapResourceRdfStmtsWithLimit() {
    Response response = null;
    try {
        //create 1 disco
        RMapDiSCO rmapDisco = TestUtils.getRMapDiSCO(TestFile.DISCOA_XML);
        String discoURI = rmapDisco.getId().toString();
        assertNotNull(discoURI);
        rmapService.createDiSCO(rmapDisco, requestEventDetails);

        MultivaluedMap<String, String> params = new MultivaluedHashMap<String, String>();
        params.add(Constants.LIMIT_PARAM, "2");

        response = resourceResponseManager.getRMapResourceTriples(discoURI, RdfMediaType.APPLICATION_RDFXML,
                params);

        assertNotNull(response);
        String body = response.getEntity().toString();

        assertEquals(303, response.getStatus());
        assertTrue(body.contains("page number"));

        URI location = response.getLocation();
        MultiValueMap<String, String> parameters = UriComponentsBuilder.fromUri(location).build()
                .getQueryParams();
        String untildate = parameters.getFirst(Constants.UNTIL_PARAM);

        //check page 1 just has 2 statements
        params.add(Constants.PAGE_PARAM, "1");
        params.add(Constants.UNTIL_PARAM, untildate);

        response = resourceResponseManager.getRMapResourceTriples(discoURI, RdfMediaType.APPLICATION_RDFXML,
                params);
        assertEquals(200, response.getStatus());
        body = response.getEntity().toString();
        int numMatches = StringUtils.countMatches(body, "xmlns=");
        assertEquals(2, numMatches);

    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception thrown " + e.getMessage());
    }

}