@Override public DefaultUriBuilder pathSegment(String... pathSegments) { this.uriComponentsBuilder.pathSegment(pathSegments); return this; }
@Override public DefaultUriBuilder pathSegment(String... pathSegments) { this.uriComponentsBuilder.pathSegment(pathSegments); return this; }
public URI getTransportUrl(TransportType transportType) { return UriComponentsBuilder.fromUri(this.sockJsUrl) .scheme(getScheme(transportType)) .pathSegment(getServerId()) .pathSegment(getSessionId()) .pathSegment(transportType.toString()) .build(true).toUri(); }
public URI getInfoUrl() { return UriComponentsBuilder.fromUri(this.sockJsUrl) .scheme(getScheme(TransportType.XHR)) .pathSegment("info") .build(true).toUri(); }
@Override public void copyToUriComponentsBuilder(UriComponentsBuilder builder) { builder.pathSegment(StringUtils.toStringArray(getPathSegments())); }
private void parsePathIfNecessary(UriComponentsBuilder result) { if (parsePath && encodingMode.equals(EncodingMode.URI_COMPONENT)) { UriComponents uric = result.build(); String path = uric.getPath(); result.replacePath(null); for (String segment : uric.getPathSegments()) { result.pathSegment(segment); } if (path != null && path.endsWith("/")) { result.path("/"); } } }
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(); }
/** * Create a {@code UriComponentsBuilder} from the URI template string. * This implementation also breaks up the path into path segments depending * on whether {@link #setParsePath parsePath} is enabled. */ protected UriComponentsBuilder initUriComponentsBuilder(String uriTemplate) { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(uriTemplate); if (shouldParsePath() && !isStrictEncoding()) { List<String> pathSegments = builder.build().getPathSegments(); builder.replacePath(null); for (String pathSegment : pathSegments) { builder.pathSegment(pathSegment); } } return builder; }
private void parsePathIfNecessary(UriComponentsBuilder result) { if (parsePath && encodingMode.equals(EncodingMode.URI_COMPONENT)) { UriComponents uric = result.build(); String path = uric.getPath(); result.replacePath(null); for (String segment : uric.getPathSegments()) { result.pathSegment(segment); } if (path != null && path.endsWith("/")) { result.path("/"); } } }
@Test public void pathSegments() { UriComponentsBuilder builder = UriComponentsBuilder.newInstance(); UriComponents result = builder.pathSegment("foo").pathSegment("bar").build(); assertEquals("/foo/bar", result.getPath()); assertEquals(Arrays.asList("foo", "bar"), result.getPathSegments()); }
@Test public void pathSegmentsThenPathSegments() { UriComponentsBuilder builder = UriComponentsBuilder.newInstance().pathSegment("foo").pathSegment("bar"); UriComponents result = builder.build(); assertEquals("/foo/bar", result.getPath()); assertEquals(Arrays.asList("foo", "bar"), result.getPathSegments()); }
@Test public void pathSegmentsSomeEmpty() { UriComponentsBuilder builder = UriComponentsBuilder.newInstance().pathSegment("", "foo", "", "bar"); UriComponents result = builder.build(); assertEquals("/foo/bar", result.getPath()); assertEquals(Arrays.asList("foo", "bar"), result.getPathSegments()); }
@Test public void emptySegments() { String baseUrl = "http://example.com/abc/"; assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("/x/y/z").build().toString(), equalTo("http://example.com/abc/x/y/z")); assertThat(UriComponentsBuilder.fromUriString(baseUrl).pathSegment("x", "y", "z").build().toString(), equalTo("http://example.com/abc/x/y/z")); assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("/x/").path("/y/z").build().toString(), equalTo("http://example.com/abc/x/y/z")); assertThat(UriComponentsBuilder.fromUriString(baseUrl).pathSegment("x").path("y").build().toString(), equalTo("http://example.com/abc/x/y")); }
@Test public void fromControllerSubResource() { UriComponents uriComponents = fromController(PersonControllerImpl.class).pathSegment("something").build(); assertThat(uriComponents.toUriString(), endsWith("/people/something")); }
@Test public void pathThenPathSegments() { UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/foo/bar").pathSegment("ba/z"); UriComponents result = builder.build().encode(); assertEquals("/foo/bar/ba%2Fz", result.getPath()); assertEquals(Arrays.asList("foo", "bar", "ba%2Fz"), result.getPathSegments()); }
@Test public void pathSegmentsThenPath() { UriComponentsBuilder builder = UriComponentsBuilder.newInstance().pathSegment("foo").path("/"); UriComponents result = builder.build(); assertEquals("/foo/", result.getPath()); assertEquals(Collections.singletonList("foo"), result.getPathSegments()); }
@Test public void copyToUriComponentsBuilder() { UriComponents source = UriComponentsBuilder.fromPath("/foo/bar").pathSegment("ba/z").build(); UriComponentsBuilder targetBuilder = UriComponentsBuilder.newInstance(); source.copyToUriComponentsBuilder(targetBuilder); UriComponents result = targetBuilder.build().encode(); assertEquals("/foo/bar/ba%2Fz", result.getPath()); assertEquals(Arrays.asList("foo", "bar", "ba%2Fz"), result.getPathSegments()); }
@Test public void test() throws Exception { String id = "a/b"; URI url = UriComponentsBuilder.fromUriString("/circuit").pathSegment(id).build().encode().toUri(); ResultActions result = mockMvc.perform(get(url)); result.andExpect(status().isOk()).andExpect(model().attribute("receivedId", is(id))); }
@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()); }
@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()); }