@Override public DefaultUriBuilder path(String path) { this.uriComponentsBuilder.path(path); return this; }
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("/"); } } }
/** * Create a builder that is initialized with the given path. * @param path the path to initialize with * @return the new {@code UriComponentsBuilder} */ public static UriComponentsBuilder fromPath(String path) { UriComponentsBuilder builder = new UriComponentsBuilder(); builder.path(path); return builder; }
@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")); }
@Override public void copyToUriComponentsBuilder(UriComponentsBuilder builder) { builder.path(getPath()); }
@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()); }
/** * An alternative to {@link #fromController(Class)} that accepts a * {@code UriComponentsBuilder} representing the base URL. This is useful * when using MvcUriComponentsBuilder outside the context of processing a * request or to apply a custom baseUrl not matching the current request. * <p><strong>Note:</strong> This method extracts values from "Forwarded" * and "X-Forwarded-*" headers if found. See class-level docs. * @param builder the builder for the base URL; the builder will be cloned * and therefore not modified and may be re-used for further calls. * @param controllerType the controller to build a URI for * @return a UriComponentsBuilder instance (never {@code null}) */ public static UriComponentsBuilder fromController(@Nullable UriComponentsBuilder builder, Class<?> controllerType) { builder = getBaseUrlToUse(builder); // Externally configured prefix via PathConfigurer.. String prefix = getPathPrefix(controllerType); builder.path(prefix); String mapping = getClassMapping(controllerType); builder.path(mapping); return builder; }
@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()); }
private static UriComponentsBuilder fromMethodInternal(@Nullable UriComponentsBuilder builder, Class<?> controllerType, Method method, Object... args) { builder = getBaseUrlToUse(builder); // Externally configured prefix via PathConfigurer.. String prefix = getPathPrefix(controllerType); builder.path(prefix); String typePath = getClassMapping(controllerType); String methodPath = getMethodMapping(method); String path = pathMatcher.combine(typePath, methodPath); builder.path(path); return applyContributors(builder, method, args); }
private Mono<DetectedEndpoint> detectEndpoint(Instance instance, EndpointDefinition endpoint) { String managementUrl = instance.getRegistration().getManagementUrl(); if (managementUrl == null) { return Mono.empty(); } URI uri = UriComponentsBuilder.fromUriString(managementUrl) .path("/") .path(endpoint.getPath()) .build() .toUri(); return instanceWebClient.instance(instance).options().uri(uri).exchange().flatMap(this.convert(endpoint, uri)); }
@Test public void serializable() throws Exception { UriComponents uriComponents = UriComponentsBuilder.fromUriString( "http://example.com").path("/{foo}").query("bar={baz}").build(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(uriComponents); ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray())); UriComponents readObject = (UriComponents) ois.readObject(); assertThat(uriComponents.toString(), equalTo(readObject.toString())); }
@Test public void equalsHierarchicalUriComponents() { String url = "http://example.com"; UriComponents uric1 = UriComponentsBuilder.fromUriString(url).path("/{foo}").query("bar={baz}").build(); UriComponents uric2 = UriComponentsBuilder.fromUriString(url).path("/{foo}").query("bar={baz}").build(); UriComponents uric3 = UriComponentsBuilder.fromUriString(url).path("/{foo}").query("bin={baz}").build(); assertThat(uric1, instanceOf(HierarchicalUriComponents.class)); assertThat(uric1, equalTo(uric1)); assertThat(uric1, equalTo(uric2)); assertThat(uric1, not(equalTo(uric3))); }
protected String getServiceUrl() { if (instance.getServiceUrl() != null) { return instance.getServiceUrl(); } return UriComponentsBuilder.fromUriString(getServiceBaseUrl()).path("/").toUriString(); }
@Test public void relativeUrls() { String baseUrl = "http://example.com"; assertThat(UriComponentsBuilder.fromUriString(baseUrl + "/foo/../bar").build().toString(), equalTo(baseUrl + "/foo/../bar")); assertThat(UriComponentsBuilder.fromUriString(baseUrl + "/foo/../bar").build().toUriString(), equalTo(baseUrl + "/foo/../bar")); assertThat(UriComponentsBuilder.fromUriString(baseUrl + "/foo/../bar").build().toUri().getPath(), equalTo("/foo/../bar")); assertThat(UriComponentsBuilder.fromUriString("../../").build().toString(), equalTo("../../")); assertThat(UriComponentsBuilder.fromUriString("../../").build().toUriString(), equalTo("../../")); assertThat(UriComponentsBuilder.fromUriString("../../").build().toUri().getPath(), equalTo("../../")); assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("foo/../bar").build().toString(), equalTo(baseUrl + "/foo/../bar")); assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("foo/../bar").build().toUriString(), equalTo(baseUrl + "/foo/../bar")); assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("foo/../bar").build().toUri().getPath(), equalTo("/foo/../bar")); }
@Test public void pathThenPath() { UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/foo/bar").path("ba/z"); UriComponents result = builder.build().encode(); assertEquals("/foo/barba/z", result.getPath()); assertEquals(Arrays.asList("foo", "barba", "z"), result.getPathSegments()); }
@Test public void resolveUriComponentsBuilder() { MethodParameter param = this.testMethod.arg(UriComponentsBuilder.class); Object value = this.resolver.resolveArgument(param, new BindingContext(), this.exchange).block(); assertNotNull(value); assertEquals(UriComponentsBuilder.class, value.getClass()); assertEquals("http://example.org:9999/next", ((UriComponentsBuilder) value).path("/next").toUriString()); }
@Test public void expand() { UriComponents uriComponents = UriComponentsBuilder.fromUriString( "http://example.com").path("/{foo} {bar}").build(); uriComponents = uriComponents.expand("1 2", "3 4"); assertEquals("/1 2 3 4", uriComponents.getPath()); assertEquals("http://example.com/1 2 3 4", uriComponents.toUriString()); }
@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()); }