Example usage for org.springframework.web.util UriComponents getQueryParams

List of usage examples for org.springframework.web.util UriComponents getQueryParams

Introduction

In this page you can find the example usage for org.springframework.web.util UriComponents getQueryParams.

Prototype

public abstract MultiValueMap<String, String> getQueryParams();

Source Link

Document

Return the map of query parameters.

Usage

From source file:info.joseluismartin.gtc.AbstractTileCache.java

/**
 * Create a parameter map from a query string
 * @param uri the query string/*from ww w  . j a v  a  2  s .  c  o m*/
 * @return a map with parameters
 */
protected Map<String, String> getParameterMap(String uri) {
    UriComponentsBuilder b = UriComponentsBuilder.newInstance();
    b.query(uri);
    UriComponents c = b.build();
    return c.getQueryParams().toSingleValueMap();
}

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./* www .java 2  s  .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.mitre.openid.connect.client.service.impl.TestSignedAuthRequestUrlBuilder.java

@Test
public void buildAuthRequestUrl_withLoginHint() {

    String requestUri = urlBuilder.buildAuthRequestUrl(serverConfig, clientConfig, redirectUri, nonce, state,
            options, loginHint);// w w w . j  a  v  a 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:de.blizzy.documentr.markdown.macro.impl.GoogleDocsMacro.java

@Override
public String getHtml(IMacroContext macroContext) {
    String macroParams = macroContext.getParameters();
    String googleUrl = StringUtils.substringBefore(macroParams, " ").trim(); //$NON-NLS-1$
    String width = StringUtils.substringAfter(macroParams, " ").trim(); //$NON-NLS-1$

    UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(googleUrl).build();
    String path = uriComponents.getPath();
    MultiValueMap<String, String> params = uriComponents.getQueryParams();
    if (path.startsWith("/spreadsheet/")) { //$NON-NLS-1$
        String key = params.get("key").get(0); //$NON-NLS-1$
        UriComponents components = UriComponentsBuilder.fromHttpUrl("https://docs.google.com/spreadsheet/pub") //$NON-NLS-1$
                .queryParam("key", key) //$NON-NLS-1$
                .queryParam("output", "html") //$NON-NLS-1$ //$NON-NLS-2$
                .queryParam("widget", "true") //$NON-NLS-1$ //$NON-NLS-2$
                .build();/*w w  w. j  av a  2s .com*/
        return buildIframe(components);
    } else if (path.startsWith("/document/")) { //$NON-NLS-1$
        String id = params.get("id").get(0); //$NON-NLS-1$
        UriComponents components = UriComponentsBuilder.fromHttpUrl("https://docs.google.com/document/pub") //$NON-NLS-1$
                .queryParam("id", id) //$NON-NLS-1$
                .queryParam("embedded", "true") //$NON-NLS-1$ //$NON-NLS-2$
                .build();
        return buildIframe(components);
    } else if (path.startsWith("/presentation/")) { //$NON-NLS-1$
        String id = params.get("id").get(0); //$NON-NLS-1$
        UriComponents components = UriComponentsBuilder
                .fromHttpUrl("https://docs.google.com/presentation/embed") //$NON-NLS-1$
                .queryParam("id", id) //$NON-NLS-1$
                .queryParam("start", "false") //$NON-NLS-1$ //$NON-NLS-2$
                .queryParam("loop", "false") //$NON-NLS-1$ //$NON-NLS-2$
                .queryParam("delayms", String.valueOf(TimeUnit.MILLISECONDS.convert(3, TimeUnit.SECONDS))) //$NON-NLS-1$
                .build();
        return buildIframe(components);
    } else if (path.startsWith("/drawings/")) { //$NON-NLS-1$
        String id = params.get("id").get(0); //$NON-NLS-1$
        if (StringUtils.isBlank(width)) {
            width = "960"; //$NON-NLS-1$
        }
        UriComponents components = UriComponentsBuilder.fromHttpUrl("https://docs.google.com/drawings/pub") //$NON-NLS-1$
                .queryParam("id", id) //$NON-NLS-1$
                .queryParam("w", width) //$NON-NLS-1$
                .build();
        return buildImg(components);
    } else {
        return null;
    }
}

From source file:info.joseluismartin.gtc.WmsCache.java

/**
 * {@inheritDoc}//from   ww  w.  j a  v  a2s.  c  o  m
 * @throws IOException 
 */
@Override
public InputStream parseResponse(InputStream serverStream, String remoteUri, String localUri)
        throws IOException {
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(remoteUri);
    UriComponents remoteComponents = builder.build();
    String localUrl = localUri + "/" + getPath();

    InputStream is = serverStream;
    MultiValueMap<String, String> params = remoteComponents.getQueryParams();

    if (GET_CAPABILITIES.equals(params.getFirst(REQUEST))) {
        String response = IOUtils.toString(serverStream);

        Document doc = XMLUtils.newDocument(response);

        if (log.isDebugEnabled())
            XMLUtils.prettyDocumentToString(doc);

        // Fix GetMapUrl
        Element getMapElement = (Element) doc.getElementsByTagName(GET_MAP).item(0);
        if (getMapElement != null) {
            if (log.isDebugEnabled()) {
                log.debug("Found GetMapUrl: " + this.getMapUrl);
            }

            NodeList nl = getMapElement.getElementsByTagName(ONLINE_RESOURCE_ELEMENT);
            if (nl.getLength() > 0) {
                Element resource = (Element) nl.item(0);
                if (resource.hasAttributeNS(XLINK_NS, HREF_ATTRIBUTE)) {
                    this.getMapUrl = resource.getAttributeNS(XLINK_NS, HREF_ATTRIBUTE).replace("?", "");
                    resource.setAttributeNS(XLINK_NS, HREF_ATTRIBUTE, localUrl);
                }
            }
        }

        // Fix GetFeatureInfoUrl
        Element getFeatureElement = (Element) doc.getElementsByTagName(GET_FEATURE_INFO).item(0);
        if (getFeatureElement != null) {
            if (log.isDebugEnabled()) {
                log.debug("Found GetFeatureInfoUrl: " + this.getFeatureInfoUrl);
            }

            NodeList nl = getFeatureElement.getElementsByTagName(ONLINE_RESOURCE_ELEMENT);
            if (nl.getLength() > 0) {
                Element resource = (Element) nl.item(0);
                if (resource.hasAttributeNS(XLINK_NS, HREF_ATTRIBUTE)) {
                    this.getFeatureInfoUrl = resource.getAttributeNS(XLINK_NS, HREF_ATTRIBUTE).replace("?", "");
                    resource.setAttributeNS(XLINK_NS, HREF_ATTRIBUTE, localUrl);
                }
            }
        }

        response = XMLUtils.documentToString(doc);
        response = response.replaceAll(getServerUrl(), localUrl);
        //   response = "<?xml version='1.0' encoding='UTF-8'>" + response; 

        is = IOUtils.toInputStream(response);
    }

    return is;
}

From source file:io.pivotal.cla.webdriver.AuthenticationTests.java

@Test
public void requiresAuthenticationAndCreatesValidOAuthTokenRequest() throws Exception {
    String redirect = mockMvc.perform(get("/sign/pivotal")).andExpect(status().is3xxRedirection()).andReturn()
            .getResponse().getRedirectedUrl();

    UriComponents redirectComponent = UriComponentsBuilder.fromHttpUrl(redirect).build();

    assertThat(redirectComponent.getScheme()).isEqualTo("https");
    assertThat(redirectComponent.getHost()).isEqualTo("github.com");
    MultiValueMap<String, String> params = redirectComponent.getQueryParams();
    assertThat(params.getFirst("client_id")).isEqualTo(config.getMain().getClientId());
    assertThat(urlDecode(params.getFirst("redirect_uri"))).isEqualTo("http://localhost/login/oauth2/github");
    assertThat(params.getFirst("state")).isNotNull();

    String[] scopes = urlDecode(params.getFirst("scope")).split(",");
    assertThat(scopes).containsOnly("user:email");
}

From source file:io.pivotal.cla.webdriver.AuthenticationTests.java

@Test
public void adminRequiresAuthenticationAndCreatesValidOAuthTokenRequest() throws Exception {
    String redirect = mockMvc.perform(get("/admin/cla/link")).andExpect(status().is3xxRedirection()).andReturn()
            .getResponse().getRedirectedUrl();

    UriComponents redirectComponent = UriComponentsBuilder.fromHttpUrl(redirect).build();

    assertThat(redirectComponent.getScheme()).isEqualTo("https");
    assertThat(redirectComponent.getHost()).isEqualTo("github.com");
    MultiValueMap<String, String> params = redirectComponent.getQueryParams();
    assertThat(params.getFirst("client_id")).isEqualTo(config.getMain().getClientId());
    assertThat(urlDecode(params.getFirst("redirect_uri"))).isEqualTo("http://localhost/login/oauth2/github");
    assertThat(params.getFirst("state")).isNotNull();

    String[] scopes = urlDecode(params.getFirst("scope")).split(",");
    assertThat(scopes).containsOnly("user:email", "repo:status", "admin:repo_hook", "admin:org_hook",
            "read:org");
}

From source file:de.blizzy.documentr.markdown.macro.impl.FlattrMacroTest.java

@Test
public void getHtml() {
    String html = macro.getHtml(macroContext);
    @SuppressWarnings("nls")
    String re = "^<a href=\"([^\"]+)\">"
            + "<img src=\"https://api\\.flattr\\.com/button/flattr-badge-large\\.png\"/>" + "</a>$"; //$NON-NLS-2$
    assertRE(re, html);/* w  w w . j  a  v  a  2  s .c o  m*/

    Matcher matcher = Pattern.compile(re, Pattern.DOTALL).matcher(html);
    matcher.find();
    String url = StringEscapeUtils.unescapeHtml4(matcher.group(1));
    UriComponents components = UriComponentsBuilder.fromHttpUrl(url).build();
    assertEquals("https", components.getScheme()); //$NON-NLS-1$
    assertEquals("flattr.com", components.getHost()); //$NON-NLS-1$
    assertEquals(-1, components.getPort());
    assertEquals("/submit/auto", components.getPath()); //$NON-NLS-1$
    MultiValueMap<String, String> params = components.getQueryParams();
    assertEquals(FLATTR_USER_ID, params.getFirst("user_id")); //$NON-NLS-1$
    assertEquals(PAGE_URL, params.getFirst("url")); //$NON-NLS-1$
    assertEquals(PAGE_TITLE, params.getFirst("title")); //$NON-NLS-1$
    assertEquals("text", params.getFirst("category")); //$NON-NLS-1$ //$NON-NLS-2$
    assertTrue(params.getFirst("tags").equals(TAG_1 + "," + TAG_2) || //$NON-NLS-1$ //$NON-NLS-2$
            params.getFirst("tags").equals(TAG_2 + "," + TAG_1)); //$NON-NLS-1$ //$NON-NLS-2$
}

From source file:org.cateproject.test.functional.mockmvc.HtmlUnitRequestBuilder.java

private void params(MockHttpServletRequest result, UriComponents uriComponents) {
    for (Entry<String, List<String>> values : uriComponents.getQueryParams().entrySet()) {
        String name = values.getKey();
        for (String value : values.getValue()) {
            try {
                result.addParameter(name, URLDecoder.decode(value, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }//w w w.  j  a  v  a2 s  .com
        }
    }
    for (NameValuePair param : webRequest.getRequestParameters()) {
        result.addParameter(param.getName(), param.getValue());
    }
}

From source file:org.cloudfoundry.identity.uaa.mock.token.TokenMvcMockTests.java

@Test
public void invalidScopeErrorMessageIsNotShowingAllClientScopes() throws Exception {
    String clientId = "testclient" + generator.generate();
    String scopes = "openid";
    setUpClients(clientId, scopes, scopes, "authorization_code", true);

    String username = "testuser" + generator.generate();
    ScimUser developer = setUpUser(username, "scim.write", OriginKeys.UAA,
            IdentityZoneHolder.getUaaZone().getId());
    MockHttpSession session = getAuthenticatedSession(developer);

    String basicDigestHeaderValue = "Basic " + new String(
            org.apache.commons.codec.binary.Base64.encodeBase64((clientId + ":" + SECRET).getBytes()));

    String state = generator.generate();
    MockHttpServletRequestBuilder authRequest = get("/oauth/authorize")
            .header("Authorization", basicDigestHeaderValue).session(session)
            .param(OAuth2Utils.RESPONSE_TYPE, "code").param(OAuth2Utils.SCOPE, "scim.write")
            .param(OAuth2Utils.STATE, state).param(OAuth2Utils.CLIENT_ID, clientId)
            .param(OAuth2Utils.REDIRECT_URI, TEST_REDIRECT_URI);

    MvcResult mvcResult = getMockMvc().perform(authRequest).andExpect(status().is3xxRedirection()).andReturn();

    UriComponents locationComponents = UriComponentsBuilder
            .fromUri(URI.create(mvcResult.getResponse().getHeader("Location"))).build();
    MultiValueMap<String, String> queryParams = locationComponents.getQueryParams();
    String errorMessage = URIUtil
            .encodeQuery("scim.write is invalid. Please use a valid scope name in the request");
    assertTrue(!queryParams.containsKey("scope"));
    assertEquals(errorMessage, queryParams.getFirst("error_description"));
}