private static URI rewriteUrl(UriComponents oldUrl, String targetUrl) { String[] newPathSegments = oldUrl.getPathSegments() .subList(1, oldUrl.getPathSegments().size()) .toArray(new String[]{}); return UriComponentsBuilder.fromUriString(targetUrl) .pathSegment(newPathSegments) .query(oldUrl.getQuery()) .build(true) .toUri(); }
@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 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()); }
/** * Configure a base URL to prepend URI templates with. The base URL must * have a scheme and host but may optionally contain a port and a path. * The base URL must be fully expanded and encoded which can be done via * {@link UriComponentsBuilder}. * @param baseUrl the base URL. */ public void setBaseUrl(@Nullable String baseUrl) { if (baseUrl != null) { UriComponents uriComponents = UriComponentsBuilder.fromUriString(baseUrl).build(); Assert.hasText(uriComponents.getScheme(), "'baseUrl' must have a scheme"); Assert.hasText(uriComponents.getHost(), "'baseUrl' must have a host"); Assert.isNull(uriComponents.getQuery(), "'baseUrl' cannot have a query"); Assert.isNull(uriComponents.getFragment(), "'baseUrl' cannot have a fragment"); } this.baseUrl = baseUrl; }
@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")); }
/** * Configure a base URL to prepend URI templates with. The base URL must * have a scheme and host but may optionally contain a port and a path. * The base URL must be fully expanded and encoded which can be done via * {@link UriComponentsBuilder}. * @param baseUrl the base URL. */ public void setBaseUrl(@Nullable String baseUrl) { if (baseUrl != null) { UriComponents uriComponents = UriComponentsBuilder.fromUriString(baseUrl).build(); Assert.hasText(uriComponents.getScheme(), "'baseUrl' must have a scheme"); Assert.hasText(uriComponents.getHost(), "'baseUrl' must have a host"); Assert.isNull(uriComponents.getQuery(), "'baseUrl' cannot have a query"); Assert.isNull(uriComponents.getFragment(), "'baseUrl' cannot have a fragment"); } this.baseUrl = baseUrl; }
assertEquals("/rfc/rfc3986.txt", result.getPath()); assertEquals(Arrays.asList("rfc", "rfc3986.txt"), result.getPathSegments()); assertNull(result.getQuery()); assertNull(result.getFragment()); assertEquals(80, result.getPort()); assertEquals("/javase/6/docs/api/java/util/BitSet.html", result.getPath()); assertEquals("foo=bar", result.getQuery()); MultiValueMap<String, String> expectedQueryParams = new LinkedMultiValueMap<>(1); expectedQueryParams.add("foo", "bar"); assertEquals("java-net@java.sun.com", result.getSchemeSpecificPart()); assertNull(result.getPath()); assertNull(result.getQuery()); assertEquals("baz", result.getFragment()); assertEquals(-1, result.getPort()); assertEquals("docs/guide/collections/designfaq.html", result.getPath()); assertNull(result.getQuery()); assertEquals("28", result.getFragment());
String query = uri.getQuery(); if (legalSpaces) { query = query.replace("%20", "+");
builder.query(encoded.getQuery());
@Override public void sendRedirect(String location) throws IOException { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(location); UriComponents uriComponents = builder.build(); // Absolute location if (uriComponents.getScheme() != null) { super.sendRedirect(location); return; } // Network-path reference if (location.startsWith("//")) { String scheme = this.request.getScheme(); super.sendRedirect(builder.scheme(scheme).toUriString()); return; } String path = uriComponents.getPath(); if (path != null) { // Relative to Servlet container root or to current request path = (path.startsWith(FOLDER_SEPARATOR) ? path : StringUtils.applyRelativePath(this.request.getRequestURI(), path)); } String result = UriComponentsBuilder .fromHttpRequest(new ServletServerHttpRequest(this.request)) .replacePath(path) .replaceQuery(uriComponents.getQuery()) .fragment(uriComponents.getFragment()) .build().normalize().toUriString(); super.sendRedirect(result); } }
@Test public void replaceQueryParam() { UriComponentsBuilder builder = UriComponentsBuilder.newInstance().queryParam("baz", "qux", 42); builder.replaceQueryParam("baz", "xuq", 24); UriComponents result = builder.build(); assertEquals("baz=xuq&baz=24", result.getQuery()); builder = UriComponentsBuilder.newInstance().queryParam("baz", "qux", 42); builder.replaceQueryParam("baz"); result = builder.build(); assertNull("Query param should have been deleted", result.getQuery()); }
String query = uri.getQuery(); if (query != null) { uriBuilder.append('?');
@Test public void fromPath() throws URISyntaxException { UriComponents result = UriComponentsBuilder.fromPath("foo").queryParam("bar").fragment("baz").build(); assertEquals("foo", result.getPath()); assertEquals("bar", result.getQuery()); assertEquals("baz", result.getFragment()); assertEquals("Invalid result URI String", "foo?bar#baz", result.toUriString()); URI expected = new URI("foo?bar#baz"); assertEquals("Invalid result URI", expected, result.toUri()); result = UriComponentsBuilder.fromPath("/foo").build(); assertEquals("/foo", result.getPath()); expected = new URI("/foo"); assertEquals("Invalid result URI", expected, result.toUri()); }
@Test public void fromHierarchicalUri() throws URISyntaxException { URI uri = new URI("http://example.com/foo?bar#baz"); UriComponents result = UriComponentsBuilder.fromUri(uri).build(); assertEquals("http", result.getScheme()); assertEquals("example.com", result.getHost()); assertEquals("/foo", result.getPath()); assertEquals("bar", result.getQuery()); assertEquals("baz", result.getFragment()); assertEquals("Invalid result URI", uri, result.toUri()); }
@Override public void sendRedirect(String location) throws IOException { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(location); UriComponents uriComponents = builder.build(); // Absolute location if (uriComponents.getScheme() != null) { super.sendRedirect(location); return; } // Network-path reference if (location.startsWith("//")) { String scheme = this.request.getScheme(); super.sendRedirect(builder.scheme(scheme).toUriString()); return; } String path = uriComponents.getPath(); if (path != null) { // Relative to Servlet container root or to current request path = (path.startsWith(FOLDER_SEPARATOR) ? path : StringUtils.applyRelativePath(this.request.getRequestURI(), path)); } String result = UriComponentsBuilder .fromHttpRequest(new ServletServerHttpRequest(this.request)) .replacePath(path) .replaceQuery(uriComponents.getQuery()) .fragment(uriComponents.getFragment()) .build().normalize().toUriString(); super.sendRedirect(result); } }
@Test public void plain() throws URISyntaxException { UriComponentsBuilder builder = UriComponentsBuilder.newInstance(); UriComponents result = builder.scheme("http").host("example.com") .path("foo").queryParam("bar").fragment("baz") .build(); assertEquals("http", result.getScheme()); assertEquals("example.com", result.getHost()); assertEquals("foo", result.getPath()); assertEquals("bar", result.getQuery()); assertEquals("baz", result.getFragment()); URI expected = new URI("http://example.com/foo?bar#baz"); assertEquals("Invalid result URI", expected, result.toUri()); }
@Test public void multipleFromSameBuilder() throws URISyntaxException { UriComponentsBuilder builder = UriComponentsBuilder.newInstance() .scheme("http").host("example.com").pathSegment("foo"); UriComponents result1 = builder.build(); builder = builder.pathSegment("foo2").queryParam("bar").fragment("baz"); UriComponents result2 = builder.build(); assertEquals("http", result1.getScheme()); assertEquals("example.com", result1.getHost()); assertEquals("/foo", result1.getPath()); URI expected = new URI("http://example.com/foo"); assertEquals("Invalid result URI", expected, result1.toUri()); assertEquals("http", result2.getScheme()); assertEquals("example.com", result2.getHost()); assertEquals("/foo/foo2", result2.getPath()); assertEquals("bar", result2.getQuery()); assertEquals("baz", result2.getFragment()); expected = new URI("http://example.com/foo/foo2?bar#baz"); assertEquals("Invalid result URI", expected, result2.toUri()); }
public MockHttpServletRequest buildRequest(ServletContext servletContext) { Charset charset = getCharset(); String httpMethod = this.webRequest.getHttpMethod().name(); UriComponents uriComponents = uriComponents(); String path = uriComponents.getPath(); MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest( servletContext, httpMethod, (path != null ? path : "")); parent(request, this.parentBuilder); String host = uriComponents.getHost(); request.setServerName(host != null ? host : ""); // needs to be first for additional headers 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()); String scheme = uriComponents.getScheme(); request.setScheme(scheme != null ? scheme : ""); request.setPathInfo(null); return postProcess(request); }
@Test public void fromHttpRequest() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setScheme("http"); request.setServerName("localhost"); request.setServerPort(-1); request.setRequestURI("/path"); request.setQueryString("a=1"); UriComponents result = UriComponentsBuilder.fromHttpRequest(new ServletServerHttpRequest(request)).build(); assertEquals("http", result.getScheme()); assertEquals("localhost", result.getHost()); assertEquals(-1, result.getPort()); assertEquals("/path", result.getPath()); assertEquals("a=1", result.getQuery()); }
@Test public void testClone() { UriComponentsBuilder builder1 = UriComponentsBuilder.newInstance(); builder1.scheme("http").host("e1.com").path("/p1").pathSegment("ps1").queryParam("q1").fragment("f1").encode(); UriComponentsBuilder builder2 = (UriComponentsBuilder) builder1.clone(); builder2.scheme("https").host("e2.com").path("p2").pathSegment("{ps2}").queryParam("q2").fragment("f2"); UriComponents result1 = builder1.build(); assertEquals("http", result1.getScheme()); assertEquals("e1.com", result1.getHost()); assertEquals("/p1/ps1", result1.getPath()); assertEquals("q1", result1.getQuery()); assertEquals("f1", result1.getFragment()); UriComponents result2 = builder2.buildAndExpand("ps2;a"); assertEquals("https", result2.getScheme()); assertEquals("e2.com", result2.getHost()); assertEquals("/p1/ps1/p2/ps2%3Ba", result2.getPath()); assertEquals("q1&q2", result2.getQuery()); assertEquals("f2", result2.getFragment()); }