private static MultiValueMap<String, String> getQueryParams(ClientHttpRequest request) { return UriComponentsBuilder.fromUri(request.getURI()).build().getQueryParams(); }
@Nullable protected final String getCallbackParam(ServerHttpRequest request) { String query = request.getURI().getQuery(); MultiValueMap<String, String> params = UriComponentsBuilder.newInstance().query(query).build().getQueryParams(); String value = params.getFirst("c"); if (StringUtils.isEmpty(value)) { return null; } String result = UriUtils.decode(value, StandardCharsets.UTF_8); return (CALLBACK_PARAM_PATTERN.matcher(result).matches() ? result : null); }
private URI getUrlToUse() { MultiValueMap<String, String> params = this.queryParamsBuilder.buildAndExpand().encode().getQueryParams(); if (!params.isEmpty()) { return UriComponentsBuilder.fromUri(this.url).queryParams(params).build(true).toUri(); } return this.url; } }
private MultiValueMap<String, String> getOriginatingRequestParams(HttpServletRequest request) { String query = getUrlPathHelper().getOriginatingQueryString(request); return ServletUriComponentsBuilder.fromPath("/").query(query).build().getQueryParams(); }
/** * Convenience method that retrieves the {@link #getOutputFlashMap "output" * FlashMap}, updates it with the path and query params of the target URL, * and then saves it using the {@link #getFlashMapManager FlashMapManager}. * @param location the target URL for the redirect * @param request the current request * @param response the current response * @since 5.0 */ public static void saveOutputFlashMap(String location, HttpServletRequest request, HttpServletResponse response) { FlashMap flashMap = getOutputFlashMap(request); if (CollectionUtils.isEmpty(flashMap)) { return; } UriComponents uriComponents = UriComponentsBuilder.fromUriString(location).build(); flashMap.setTargetRequestPath(uriComponents.getPath()); flashMap.addTargetRequestParams(uriComponents.getQueryParams()); FlashMapManager manager = getFlashMapManager(request); Assert.state(manager != null, "No FlashMapManager. Is this a DispatcherServlet handled request?"); manager.saveOutputFlashMap(flashMap, request, response); }
@Test // SPR-9317 public void fromUriEncodedQuery() throws URISyntaxException { URI uri = new URI("http://www.example.org/?param=aGVsbG9Xb3JsZA%3D%3D"); String fromUri = UriComponentsBuilder.fromUri(uri).build().getQueryParams().get("param").get(0); String fromUriString = UriComponentsBuilder.fromUriString(uri.toString()) .build().getQueryParams().get("param").get(0); assertEquals(fromUri, fromUriString); }
private MultiValueMap<String, String> getOriginatingRequestParams(HttpServletRequest request) { String query = getUrlPathHelper().getOriginatingQueryString(request); return ServletUriComponentsBuilder.fromPath("/").query(query).build().getQueryParams(); }
private void params(MockHttpServletRequest request, UriComponents uriComponents) { uriComponents.getQueryParams().forEach((name, values) -> { String urlDecodedName = urlDecode(name); values.forEach(value -> { value = (value != null ? urlDecode(value) : ""); request.addParameter(urlDecodedName, value); }); }); for (NameValuePair param : this.webRequest.getRequestParameters()) { request.addParameter(param.getName(), param.getValue()); } }
@Test public void queryParams() { UriComponentsBuilder builder = UriComponentsBuilder.newInstance(); UriComponents result = builder.queryParam("baz", "qux", 42).build(); assertEquals("baz=qux&baz=42", result.getQuery()); MultiValueMap<String, String> expectedQueryParams = new LinkedMultiValueMap<>(2); expectedQueryParams.add("baz", "qux"); expectedQueryParams.add("baz", "42"); assertEquals(expectedQueryParams, result.getQueryParams()); }
@Test public void queryParamWithoutValueWithoutEquals() { UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://example.com/foo?bar").build(); assertThat(uriComponents.toUriString(), equalTo("http://example.com/foo?bar")); // TODO [SPR-13537] Change equalTo(null) to equalTo(""). assertThat(uriComponents.getQueryParams().get("bar").get(0), equalTo(null)); }
@Test public void queryParamWithValueWithEquals() { UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://example.com/foo?bar=baz").build(); assertThat(uriComponents.toUriString(), equalTo("http://example.com/foo?bar=baz")); assertThat(uriComponents.getQueryParams().get("bar").get(0), equalTo("baz")); }
@Test public void queryParamWithoutValueWithEquals() { UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://example.com/foo?bar=").build(); assertThat(uriComponents.toUriString(), equalTo("http://example.com/foo?bar=")); assertThat(uriComponents.getQueryParams().get("bar").get(0), equalTo("")); }
@Test public void emptyQueryParam() { UriComponentsBuilder builder = UriComponentsBuilder.newInstance(); UriComponents result = builder.queryParam("baz").build(); assertEquals("baz", result.getQuery()); MultiValueMap<String, String> expectedQueryParams = new LinkedMultiValueMap<>(2); expectedQueryParams.add("baz", null); assertEquals(expectedQueryParams, result.getQueryParams()); }
private URI getUrlToUse() { MultiValueMap<String, String> params = this.queryParamsBuilder.buildAndExpand().encode().getQueryParams(); if (!params.isEmpty()) { return UriComponentsBuilder.fromUri(this.url).queryParams(params).build(true).toUri(); } return this.url; } }
@Test public void fromMethodNameWithPathVarAndRequestParam() { UriComponents uriComponents = fromMethodName( ControllerWithMethods.class, "methodForNextPage", "1", 10, 5).build(); assertThat(uriComponents.getPath(), is("/something/1/foo")); MultiValueMap<String, String> queryParams = uriComponents.getQueryParams(); assertThat(queryParams.get("limit"), contains("5")); assertThat(queryParams.get("offset"), contains("10")); }
@Test // SPR-11970 public void fromUriStringNoPathWithReservedCharInQuery() { UriComponents result = UriComponentsBuilder.fromUriString("http://example.com?foo=bar@baz").build(); assertTrue(StringUtils.isEmpty(result.getUserInfo())); assertEquals("example.com", result.getHost()); assertTrue(result.getQueryParams().containsKey("foo")); assertEquals("bar@baz", result.getQueryParams().getFirst("foo")); }
@Test public void fromMethodCallWithPathVariableAndMultiValueRequestParams() { UriComponents uriComponents = fromMethodCall( on(ControllerWithMethods.class).methodWithMultiValueRequestParams("1", Arrays.asList(3, 7), 5)).build(); assertThat(uriComponents.getPath(), is("/something/1/foo")); MultiValueMap<String, String> queryParams = uriComponents.getQueryParams(); assertThat(queryParams.get("limit"), contains("5")); assertThat(queryParams.get("items"), containsInAnyOrder("3", "7")); }
@Test public void fromMethodCallWithPathVariableAndRequestParams() { UriComponents uriComponents = fromMethodCall( on(ControllerWithMethods.class).methodForNextPage("1", 10, 5)).build(); assertThat(uriComponents.getPath(), is("/something/1/foo")); MultiValueMap<String, String> queryParams = uriComponents.getQueryParams(); assertThat(queryParams.get("limit"), contains("5")); assertThat(queryParams.get("offset"), contains("10")); }
@Test // SPR-9832 public void fromUriStringQueryParamWithReservedCharInValue() { String uri = "http://www.google.com/ig/calculator?q=1USD=?EUR"; UriComponents result = UriComponentsBuilder.fromUriString(uri).build(); assertEquals("q=1USD=?EUR", result.getQuery()); assertEquals("1USD=?EUR", result.getQueryParams().getFirst("q")); }
MultiValueMap<String, String> expectedQueryParams = new LinkedMultiValueMap<>(1); expectedQueryParams.add("foo", "bar"); assertEquals(expectedQueryParams, result.getQueryParams()); assertEquals("and(java.util.BitSet)", result.getFragment());