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(); }
private void contextPath(MockHttpServletRequest request, UriComponents uriComponents) { if (this.contextPath == null) { List<String> pathSegments = uriComponents.getPathSegments(); if (pathSegments.isEmpty()) { request.setContextPath(""); } else { request.setContextPath("/" + pathSegments.get(0)); } } else { String path = uriComponents.getPath(); Assert.isTrue(path != null && path.startsWith(this.contextPath), () -> "\"" + uriComponents.getPath() + "\" should start with context path \"" + this.contextPath + "\""); request.setContextPath(this.contextPath); } }
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 {@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 // SPR-16364 public void uriComponentsNotEqualAfterNormalization() { UriComponents uri1 = UriComponentsBuilder.fromUriString("http://test.com").build().normalize(); UriComponents uri2 = UriComponentsBuilder.fromUriString("http://test.com/").build(); assertTrue(uri1.getPathSegments().isEmpty()); assertTrue(uri2.getPathSegments().isEmpty()); assertNotEquals(uri1, uri2); }
/** * 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; }
@Test public void path() { UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/foo/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 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 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 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 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 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 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()); }
if (oldUrl.getPathSegments().isEmpty()) { return Mono.error(new ResolveEndpointException("No endpoint specified")); String endpointId = oldUrl.getPathSegments().get(0); Optional<Endpoint> endpoint = instance.getEndpoints().get(endpointId);
/** * Returns the mapping path segments request mapping. * * @param method must not be {@literal null}. * @return */ public static List<String> getPathSegments(Method method) { Assert.notNull(method, "Method must not be null!"); String mapping = DISCOVERER.getMapping(method.getDeclaringClass(), method); return UriComponentsBuilder.fromPath(mapping).build().getPathSegments(); } }
assertEquals(-1, result.getPort()); assertEquals("/rfc/rfc3986.txt", result.getPath()); assertEquals(Arrays.asList("rfc", "rfc3986.txt"), result.getPathSegments()); assertNull(result.getQuery()); assertNull(result.getFragment());
List<String> baseUriSegments = UriComponentsBuilder.fromUri(baseUri).build().getPathSegments(); Collections.reverse(baseUriSegments); String tail = "";
protected T slash(UriComponents components, boolean encoded) { String uriString = uriComponents.toUriString(); UriComponentsBuilder builder = uriString.isEmpty() ? fromUri(uriComponents.toUri()) : fromUriString(uriString); for (String pathSegment : components.getPathSegments()) { builder.pathSegment(encoded ? pathSegment : encodePath(pathSegment)); } String fragment = components.getFragment(); if (StringUtils.hasText(fragment)) { builder.fragment(encoded ? fragment : encodeFragment(fragment)); } return createNewInstance(builder.query(components.getQuery())); }