@Override public String toString() { return this.pattern.getPatternString(); } }
@Override public void accept(Visitor visitor) { visitor.path(this.pattern.getPatternString()); }
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); }
@Override public boolean matches(Object item) { if (item != null && item instanceof PathPattern) { return ((PathPattern) item).getPatternString().equals(pattern); } 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); }
@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; } }
@Test public void equallyMatchingPatternsAreBothPresent() throws Exception { PatternsRequestCondition c = createPatternsCondition("/a", "/b"); assertEquals(2, c.getPatterns().size()); Iterator<PathPattern> itr = c.getPatterns().iterator(); assertEquals("/a", itr.next().getPatternString()); assertEquals("/b", itr.next().getPatternString()); }
@Test public void prependNonEmptyPatternsOnly() { PatternsRequestCondition c = createPatternsCondition(""); assertEquals("Do not prepend empty patterns (SPR-8255)", "", c.getPatterns().iterator().next().getPatternString()); }
public String combine(String string1, String string2) { PathPattern pattern1 = pp.parse(string1); PathPattern pattern2 = pp.parse(string2); return pattern1.combine(pattern2).getPatternString(); }
public Mono<ServerResponse> pattern(ServerRequest request) { String pattern = matchingPattern(request).getPatternString(); return ServerResponse.ok().syncBody(pattern); }
private RequestMappingInfo assertComposedAnnotationMapping(String methodName, String path, RequestMethod requestMethod) throws Exception { Class<?> clazz = ComposedAnnotationController.class; Method method = clazz.getMethod(methodName); RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, clazz); assertNotNull(info); Set<PathPattern> paths = info.getPatternsCondition().getPatterns(); assertEquals(1, paths.size()); assertEquals(path, paths.iterator().next().getPatternString()); Set<RequestMethod> methods = info.getMethodsCondition().getMethods(); assertEquals(1, methods.size()); assertEquals(requestMethod, methods.iterator().next()); return info; }
@SuppressWarnings("unchecked") public Mono<ServerResponse> variables(ServerRequest request) { Map<String, String> pathVariables = request.pathVariables(); Map<String, String> attributePathVariables = (Map<String, String>) request.attributes().get(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE); assertTrue( (pathVariables.equals(attributePathVariables)) || (pathVariables.isEmpty() && (attributePathVariables == null))); PathPattern pathPattern = matchingPattern(request); String pattern = pathPattern != null ? pathPattern.getPatternString() : ""; Flux<String> responseBody; if (!pattern.isEmpty()) { responseBody = Flux.just(pattern, "\n", pathVariables.toString()); } else { responseBody = Flux.just(pathVariables.toString()); } return ServerResponse.ok().body(responseBody, String.class); }
@Test public void prependPatternWithSlash() { RequestMappingInfo actual = paths("foo").build(); List<PathPattern> patterns = new ArrayList<>(actual.getPatternsCondition().getPatterns()); assertEquals(1, patterns.size()); assertEquals("/foo", patterns.get(0).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();
@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; } }; }
@SuppressWarnings("unchecked") public Mono<ServerResponse> attributes(ServerRequest request) { assertTrue(request.attributes().containsKey(RouterFunctions.REQUEST_ATTRIBUTE)); assertTrue(request.attributes().containsKey(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE)); Map<String, String> pathVariables = (Map<String, String>) request.attributes().get(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE); assertNotNull(pathVariables); assertEquals(1, pathVariables.size()); assertEquals("bar", pathVariables.get("foo")); pathVariables = (Map<String, String>) request.attributes().get(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); assertNotNull(pathVariables); assertEquals(1, pathVariables.size()); assertEquals("bar", pathVariables.get("foo")); PathPattern pattern = (PathPattern) request.attributes().get(RouterFunctions.MATCHING_PATTERN_ATTRIBUTE); assertNotNull(pattern); assertEquals("/attributes/{foo}", pattern.getPatternString()); pattern = (PathPattern) request.attributes() .get(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); assertNotNull(pattern); assertEquals("/attributes/{foo}", pattern.getPatternString()); return ServerResponse.ok().build(); } }
@Test public void handleMatchBestMatchingPatternAttribute() { RequestMappingInfo key = paths("/{path1}/2", "/**").build(); ServerWebExchange exchange = MockServerWebExchange.from(get("/1/2")); this.handlerMapping.handleMatch(key, handlerMethod, exchange); PathPattern bestMatch = (PathPattern) exchange.getAttributes().get(BEST_MATCHING_PATTERN_ATTRIBUTE); assertEquals("/{path1}/2", bestMatch.getPatternString()); HandlerMethod mapped = (HandlerMethod) exchange.getAttributes().get(BEST_MATCHING_HANDLER_ATTRIBUTE); assertSame(handlerMethod, mapped); }
@Test public void handleMatchBestMatchingPatternAttributeNoPatternsDefined() { RequestMappingInfo key = paths().build(); ServerWebExchange exchange = MockServerWebExchange.from(get("/1/2")); this.handlerMapping.handleMatch(key, handlerMethod, exchange); PathPattern bestMatch = (PathPattern) exchange.getAttributes().get(BEST_MATCHING_PATTERN_ATTRIBUTE); assertEquals("/1/2", bestMatch.getPatternString()); }
@Test public void matchTrailingSlash() throws Exception { MockServerWebExchange exchange = MockServerWebExchange.from(get("/foo/")); PatternsRequestCondition condition = createPatternsCondition("/foo"); PatternsRequestCondition match = condition.getMatchingCondition(exchange); assertNotNull(match); assertEquals("Should match by default", "/foo", match.getPatterns().iterator().next().getPatternString()); condition = createPatternsCondition("/foo"); match = condition.getMatchingCondition(exchange); assertNotNull(match); assertEquals("Trailing slash should be insensitive to useSuffixPatternMatch settings (SPR-6164, SPR-5636)", "/foo", match.getPatterns().iterator().next().getPatternString()); PathPatternParser parser = new PathPatternParser(); parser.setMatchOptionalTrailingSeparator(false); condition = new PatternsRequestCondition(parser.parse("/foo")); match = condition.getMatchingCondition(MockServerWebExchange.from(get("/foo/"))); assertNull(match); }
private DispatcherHandlerMappingDescription describe( Entry<PathPattern, Object> mapping) { return new DispatcherHandlerMappingDescription( mapping.getKey().getPatternString(), mapping.getValue().toString(), null); }