/** * Get the request path as a {@code PathContainer}. */ default PathContainer pathContainer() { return PathContainer.parsePath(path()); }
public static PathContainer toPathContainer(String path) { if (path == null) { return null; } return PathContainer.parsePath(path); }
DefaultRequestPath(URI uri, @Nullable String contextPath) { this.fullPath = PathContainer.parsePath(uri.getRawPath()); this.contextPath = initContextPath(this.fullPath, contextPath); this.pathWithinApplication = extractPathWithinApplication(this.fullPath, this.contextPath); }
DefaultRequestPath(URI uri, @Nullable String contextPath) { this.fullPath = PathContainer.parsePath(uri.getRawPath()); this.contextPath = initContextPath(this.fullPath, contextPath); this.pathWithinApplication = extractPathWithinApplication(this.fullPath, this.contextPath); }
private static PathContainer initContextPath(PathContainer path, @Nullable String contextPath) { if (!StringUtils.hasText(contextPath) || "/".equals(contextPath)) { return PathContainer.parsePath(""); } validateContextPath(path.value(), contextPath); int length = contextPath.length(); int counter = 0; for (int i=0; i < path.elements().size(); i++) { PathContainer.Element element = path.elements().get(i); counter += element.value().length(); if (length == counter) { return path.subPath(0, i + 1); } } // Should not happen.. throw new IllegalStateException("Failed to initialize contextPath '" + contextPath + "'" + " for requestPath '" + path.value() + "'"); }
private void setPathWithinHandlerMapping(ServerWebExchange exchange, String path) { exchange.getAttributes().put(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, PathContainer.parsePath(path)); }
matches(PathContainer.parsePath(pattern2string.patternString))) { return pattern2string;
/** * Get the public resource URL for the given URI string. * <p>The URI string is expected to be a path and if it contains a query or * fragment those will be preserved in the resulting public resource URL. * @param uriString the URI string to transform * @param exchange the current exchange * @return the resolved public resource URL path, or empty if unresolved */ public final Mono<String> getForUriString(String uriString, ServerWebExchange exchange) { ServerHttpRequest request = exchange.getRequest(); int queryIndex = getQueryIndex(uriString); String lookupPath = uriString.substring(0, queryIndex); String query = uriString.substring(queryIndex); PathContainer parsedLookupPath = PathContainer.parsePath(lookupPath); return resolveResourceUrl(exchange, parsedLookupPath).map(resolvedPath -> request.getPath().contextPath().value() + resolvedPath + query); }
private static PathContainer initContextPath(PathContainer path, @Nullable String contextPath) { if (!StringUtils.hasText(contextPath) || "/".equals(contextPath)) { return PathContainer.parsePath(""); } validateContextPath(path.value(), contextPath); int length = contextPath.length(); int counter = 0; for (int i=0; i < path.elements().size(); i++) { PathContainer.Element element = path.elements().get(i); counter += element.value().length(); if (length == counter) { return path.subPath(0, i + 1); } } // Should not happen.. throw new IllegalStateException("Failed to initialize contextPath '" + contextPath + "'" + " for requestPath '" + path.value() + "'"); }
private void testPath(String input, String value, List<String> expectedElements) { PathContainer path = PathContainer.parsePath(input); assertEquals("value: '" + input + "'", value, path.value()); assertEquals("elements: " + input, expectedElements, path.elements().stream() .map(PathContainer.Element::value).collect(Collectors.toList())); }
matches(PathContainer.parsePath(pattern2string.patternString))) { return pattern2string;
@Override public Predicate<ServerWebExchange> apply(Config config) { final ArrayList<PathPattern> pathPatterns = new ArrayList<>(); synchronized (this.pathPatternParser) { pathPatternParser.setMatchOptionalTrailingSeparator(config.isMatchOptionalTrailingSeparator()); config.getPatterns().forEach(pattern -> { PathPattern pathPattern = this.pathPatternParser.parse(pattern); pathPatterns.add(pathPattern); }); } return exchange -> { PathContainer path = parsePath(exchange.getRequest().getURI().getPath()); Optional<PathPattern> optionalPathPattern = pathPatterns.stream() .filter(pattern -> pattern.matches(path)) .findFirst(); if (optionalPathPattern.isPresent()) { PathPattern pathPattern = optionalPathPattern.get(); traceMatch("Pattern", pathPattern.getPatternString(), path, true); PathMatchInfo pathMatchInfo = pathPattern.matchAndExtract(path); putUriTemplateVariables(exchange, pathMatchInfo.getUriVariables()); return true; } else { traceMatch("Pattern", config.getPatterns(), path, false); return false; } }; }
private void testPathSegment(String rawValue, String valueToMatch, MultiValueMap<String, String> params) { PathContainer container = PathContainer.parsePath(rawValue); if ("".equals(rawValue)) { assertEquals(0, container.elements().size()); return; } assertEquals(1, container.elements().size()); PathSegment segment = (PathSegment) container.elements().get(0); assertEquals("value: '" + rawValue + "'", rawValue, segment.value()); assertEquals("valueToMatch: '" + rawValue + "'", valueToMatch, segment.valueToMatch()); assertEquals("params: '" + rawValue + "'", params, segment.parameters()); }
@Test public void subPath() throws Exception { // basic PathContainer path = PathContainer.parsePath("/a/b/c"); assertSame(path, path.subPath(0)); assertEquals("/b/c", path.subPath(2).value()); assertEquals("/c", path.subPath(4).value()); // root path path = PathContainer.parsePath("/"); assertEquals("/", path.subPath(0).value()); // trailing slash path = PathContainer.parsePath("/a/b/"); assertEquals("/b/", path.subPath(2).value()); }
@Test public void mapPathToLocation() throws Exception { MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("")); exchange.getAttributes().put(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, PathContainer.parsePath("/testStylesheet.css")); ResourceWebHandler handler = getHandler("/resources/**"); handler.handle(exchange).block(Duration.ofSeconds(5)); StepVerifier.create(exchange.getResponse().getBody()) .consumeNextWith(buf -> assertEquals("test stylesheet content", DataBufferTestUtils.dumpString(buf, StandardCharsets.UTF_8))) .expectComplete() .verify(); }
DefaultRequestPath(URI uri, @Nullable String contextPath) { this.fullPath = PathContainer.parsePath(uri.getRawPath()); this.contextPath = initContextPath(this.fullPath, contextPath); this.pathWithinApplication = extractPathWithinApplication(this.fullPath, this.contextPath); }
DefaultRequestPath(URI uri, @Nullable String contextPath) { this.fullPath = PathContainer.parsePath(uri.getRawPath()); this.contextPath = initContextPath(this.fullPath, contextPath); this.pathWithinApplication = extractPathWithinApplication(this.fullPath, this.contextPath); }
private static PathContainer initContextPath(PathContainer path, @Nullable String contextPath) { if (!StringUtils.hasText(contextPath) || "/".equals(contextPath)) { return PathContainer.parsePath(""); } validateContextPath(path.value(), contextPath); int length = contextPath.length(); int counter = 0; for (int i=0; i < path.elements().size(); i++) { PathContainer.Element element = path.elements().get(i); counter += element.value().length(); if (length == counter) { return path.subPath(0, i + 1); } } // Should not happen.. throw new IllegalStateException("Failed to initialize contextPath '" + contextPath + "'" + " for requestPath '" + path.value() + "'"); }