@Override @Nullable public CorsConfiguration getCorsConfiguration(ServerWebExchange exchange) { PathContainer lookupPath = exchange.getRequest().getPath().pathWithinApplication(); return this.corsConfigurations.entrySet().stream() .filter(entry -> entry.getKey().matches(lookupPath)) .map(Map.Entry::getValue) .findFirst() .orElse(null); }
@Override public String toString() { return this.pattern.getPatternString(); } }
@Override public boolean test(ServerRequest request) { PathContainer pathContainer = request.pathContainer(); PathPattern.PathMatchInfo info = this.pattern.matchAndExtract(pathContainer); traceMatch("Pattern", this.pattern.getPatternString(), request.path(), info != null); if (info != null) { mergeAttributes(request, info.getUriVariables(), this.pattern); return true; } else { return false; } }
public boolean equals(Object other) { if (!(other instanceof PathPattern)) { return false; } PathPattern otherPattern = (PathPattern) other; return (this.patternString.equals(otherPattern.getPatternString()) && this.separator == otherPattern.getSeparator() && this.caseSensitive == otherPattern.caseSensitive); }
/** * Whether this pattern matches the given path. * @param pathContainer the candidate path to attempt to match against * @return {@code true} if the path matches this pattern */ public boolean matches(PathContainer pathContainer) { if (this.head == null) { return !hasLength(pathContainer) || (this.matchOptionalTrailingSeparator && pathContainerIsJustSeparator(pathContainer)); } else if (!hasLength(pathContainer)) { if (this.head instanceof WildcardTheRestPathElement || this.head instanceof CaptureTheRestPathElement) { pathContainer = EMPTY_PATH; // Will allow CaptureTheRest to bind the variable to empty } else { return false; } } MatchingContext matchingContext = new MatchingContext(pathContainer, false); return this.head.matches(0, matchingContext); }
/** * Look up a handler instance for the given URL lookup path. * <p>Supports direct matches, e.g. a registered "/test" matches "/test", * and various path pattern matches, e.g. a registered "/t*" matches * both "/test" and "/team". For details, see the PathPattern class. * @param lookupPath the URL the handler is mapped to * @param exchange the current exchange * @return the associated handler instance, or {@code null} if not found * @see org.springframework.web.util.pattern.PathPattern */ @Nullable protected Object lookupHandler(PathContainer lookupPath, ServerWebExchange exchange) throws Exception { List<PathPattern> matches = this.handlerMap.keySet().stream() .filter(key -> key.matches(lookupPath)) .collect(Collectors.toList()); if (matches.isEmpty()) { return null; } if (matches.size() > 1) { matches.sort(PathPattern.SPECIFICITY_COMPARATOR); if (logger.isTraceEnabled()) { logger.debug(exchange.getLogPrefix() + "Matching patterns " + matches); } } PathPattern pattern = matches.get(0); PathContainer pathWithinMapping = pattern.extractPathWithinPattern(lookupPath); return handleMatch(this.handlerMap.get(pattern), pattern, pathWithinMapping, exchange); }
@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; } }; }
matches(PathContainer.parsePath(pattern2string.patternString))) { return pattern2string; return this.parser.parse(concat( this.patternString.substring(0, this.patternString.length() - 2), pattern2string.patternString)); return this.parser.parse(concat(this.patternString, pattern2string.patternString));
@Override public Mono<MatchResult> matches(ServerWebExchange exchange) { ServerHttpRequest request = exchange.getRequest(); if(this.method != null && !this.method.equals(request.getMethod())) { return MatchResult.notMatch(); } PathContainer path = request.getPath().pathWithinApplication(); boolean match = this.pattern.matches(path); if(!match) { return MatchResult.notMatch(); } Map<String,String> pathVariables = this.pattern.matchAndExtract(path).getUriVariables(); Map<String,Object> variables = new HashMap<>(pathVariables); return MatchResult.match(variables); }
private PathPattern.PathMatchInfo matchAndExtract(PathPattern pp, String path) { return pp.matchAndExtract(PathPatternTests.toPathContainer(path)); }
public String combine(String string1, String string2) { PathPattern pattern1 = pp.parse(string1); PathPattern pattern2 = pp.parse(string2); return pattern1.combine(pattern2).getPatternString(); }
paths.add(pp.parse("/hotels/new")); paths.sort(comparator); assertEquals("/hotels/new", paths.get(0).getPatternString()); assertNull(paths.get(1)); paths.clear(); paths.add(pp.parse("/hotels/new")); paths.sort(comparator); assertEquals("/hotels/new", paths.get(0).getPatternString()); assertEquals("/hotels/*", paths.get(1).getPatternString()); paths.clear(); paths.add(pp.parse("/hotels/*")); paths.sort(comparator); assertEquals("/hotels/new", paths.get(0).getPatternString()); assertEquals("/hotels/*", paths.get(1).getPatternString()); paths.clear(); paths.add(pp.parse("/hotels/*")); paths.sort(comparator); assertEquals("/hotels/*", paths.get(0).getPatternString()); assertEquals("/hotels/**", paths.get(1).getPatternString()); paths.clear(); paths.add(pp.parse("/hotels/**")); paths.sort(comparator); assertEquals("/hotels/*", paths.get(0).getPatternString()); assertEquals("/hotels/**", paths.get(1).getPatternString()); paths.clear();
private static int scoreByNormalizedLength(PathPattern pattern) { return -pattern.getNormalizedLength(); }
return new PathRemainingMatchInfo(pathContainer); else if (!hasLength(pathContainer)) { return null;
pushPathElement(createPathElement()); return new PathPattern(pathPattern, this.parser, this.headPE);
matches(PathContainer.parsePath(pattern2string.patternString))) { return pattern2string; return this.parser.parse(concat( this.patternString.substring(0, this.patternString.length() - 2), pattern2string.patternString)); return this.parser.parse(concat(this.patternString, pattern2string.patternString));
@Override public Mono<MatchResult> matches(ServerWebExchange exchange) { ServerHttpRequest request = exchange.getRequest(); if (this.method != null && !this.method.equals(request.getMethod())) { return MatchResult.notMatch(); } PathContainer path = request.getPath().pathWithinApplication(); boolean match = this.pattern.matches(path); if (!match) { return MatchResult.notMatch(); } Map<String, String> pathVariables = this.pattern.matchAndExtract(path).getUriVariables(); Map<String, Object> variables = new HashMap<>(pathVariables); return MatchResult.match(variables); }
private Mono<String> resolveResourceUrl(ServerWebExchange exchange, PathContainer lookupPath) { return this.handlerMap.entrySet().stream() .filter(entry -> entry.getKey().matches(lookupPath)) .min((entry1, entry2) -> PathPattern.SPECIFICITY_COMPARATOR.compare(entry1.getKey(), entry2.getKey())) .map(entry -> { PathContainer path = entry.getKey().extractPathWithinPattern(lookupPath); int endIndex = lookupPath.elements().size() - path.elements().size(); PathContainer mapping = lookupPath.subPath(0, endIndex); ResourceWebHandler handler = entry.getValue(); List<ResourceResolver> resolvers = handler.getResourceResolvers(); ResourceResolverChain chain = new DefaultResourceResolverChain(resolvers); return chain.resolveUrlPath(path.value(), handler.getLocations()) .map(resolvedPath -> mapping.value() + resolvedPath); }) .orElseGet(() ->{ if (logger.isTraceEnabled()) { logger.trace(exchange.getLogPrefix() + "No match for \"" + lookupPath + "\""); } return Mono.empty(); }); }
public boolean equals(Object other) { if (!(other instanceof PathPattern)) { return false; } PathPattern otherPattern = (PathPattern) other; return (this.patternString.equals(otherPattern.getPatternString()) && this.separator == otherPattern.getSeparator() && this.caseSensitive == otherPattern.caseSensitive); }
/** * Match this pattern to the given URI path and return extracted URI template * variables as well as path parameters (matrix variables). * @param pathContainer the candidate path to attempt to match against * @return info object with the extracted variables, or {@code null} for no match */ @Nullable public PathMatchInfo matchAndExtract(PathContainer pathContainer) { if (this.head == null) { return hasLength(pathContainer) && !(this.matchOptionalTrailingSeparator && pathContainerIsJustSeparator(pathContainer)) ? null : PathMatchInfo.EMPTY; } else if (!hasLength(pathContainer)) { if (this.head instanceof WildcardTheRestPathElement || this.head instanceof CaptureTheRestPathElement) { pathContainer = EMPTY_PATH; // Will allow CaptureTheRest to bind the variable to empty } else { return null; } } MatchingContext matchingContext = new MatchingContext(pathContainer, true); return this.head.matches(0, matchingContext) ? matchingContext.getPathMatchResult() : null; }