private PathPattern.PathMatchInfo matchAndExtract(PathPattern pp, String path) { return pp.matchAndExtract(PathPatternTests.toPathContainer(path)); }
private PathPattern.PathMatchInfo matchAndExtract(PathPattern p, String path) { return p.matchAndExtract(toPathContainer(path)); }
@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; } }
private PathPattern.PathMatchInfo matchAndExtract(String pattern, String path) { return parse(pattern).matchAndExtract(PathPatternTests.toPathContainer(path)); }
PathPattern.PathMatchInfo result = bestPattern.matchAndExtract(lookupPath); Assert.notNull(result, () -> "Expected bestPattern: " + bestPattern + " to match lookupPath " + lookupPath);
private PathPattern.PathMatchInfo checkCapture(String uriTemplate, String path, String... keyValues) { PathPatternParser parser = new PathPatternParser(); PathPattern pattern = parser.parse(uriTemplate); PathPattern.PathMatchInfo matchResult = pattern.matchAndExtract(toPathContainer(path)); Map<String, String> expectedKeyValues = new HashMap<>(); for (int i = 0; i < keyValues.length; i += 2) { expectedKeyValues.put(keyValues[i], keyValues[i + 1]); } for (Map.Entry<String, String> me : expectedKeyValues.entrySet()) { String value = matchResult.getUriVariables().get(me.getKey()); if (value == null) { fail("Did not find key '" + me.getKey() + "' in captured variables: " + matchResult.getUriVariables()); } if (!value.equals(me.getValue())) { fail("Expected value '" + me.getValue() + "' for key '" + me.getKey() + "' but was '" + value + "'"); } } return matchResult; }
@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; } }; }
@Test public void extractUriTemplateVarsRegexQualifiers() { PathPatternParser pp = new PathPatternParser(); PathPattern p = pp.parse("{symbolicName:[\\p{L}\\.]+}-sources-{version:[\\p{N}\\.]+}.jar"); PathPattern.PathMatchInfo result = p.matchAndExtract(toPathContainer("com.example-sources-1.0.0.jar")); assertEquals("com.example", result.getUriVariables().get("symbolicName")); assertEquals("1.0.0", result.getUriVariables().get("version")); p = pp.parse("{symbolicName:[\\w\\.]+}-sources-" + "{version:[\\d\\.]+}-{year:\\d{4}}{month:\\d{2}}{day:\\d{2}}.jar"); result = matchAndExtract(p,"com.example-sources-1.0.0-20100220.jar"); assertEquals("com.example", result.getUriVariables().get("symbolicName")); assertEquals("1.0.0", result.getUriVariables().get("version")); assertEquals("2010", result.getUriVariables().get("year")); assertEquals("02", result.getUriVariables().get("month")); assertEquals("20", result.getUriVariables().get("day")); p = pp.parse("{symbolicName:[\\p{L}\\.]+}-sources-{version:[\\p{N}\\.\\{\\}]+}.jar"); result = matchAndExtract(p, "com.example-sources-1.0.0.{12}.jar"); assertEquals("com.example", result.getUriVariables().get("symbolicName")); assertEquals("1.0.0.{12}", result.getUriVariables().get("version")); }
assertEquals("resource",pp.matchAndExtract(toPathContainer("/resource")).getUriVariables().get("var")); assertMatches(pp,"/resource/"); assertEquals("resource",pp.matchAndExtract(toPathContainer("/resource/")).getUriVariables().get("var")); assertNoMatch(pp,"/resource//"); pp = parse("/{var}/"); assertNoMatch(pp,"/resource"); assertMatches(pp,"/resource/"); assertEquals("resource",pp.matchAndExtract(toPathContainer("/resource/")).getUriVariables().get("var")); assertNoMatch(pp,"/resource//"); assertEquals("/resource",pp.matchAndExtract(toPathContainer("/resource")).getUriVariables().get("var")); assertMatches(pp,"/resource/"); assertEquals("/resource/",pp.matchAndExtract(toPathContainer("/resource/")).getUriVariables().get("var")); assertMatches(pp,"/resource//"); assertEquals("/resource//",pp.matchAndExtract(toPathContainer("/resource//")).getUriVariables().get("var")); assertMatches(pp,"//resource//"); assertEquals("//resource//",pp.matchAndExtract(toPathContainer("//resource//")).getUriVariables().get("var")); assertEquals("res1",pp.matchAndExtract(toPathContainer("/res1_res2")).getUriVariables().get("var1")); assertEquals("res2",pp.matchAndExtract(toPathContainer("/res1_res2")).getUriVariables().get("var2")); assertMatches(pp,"/res1_res2/"); assertEquals("res1",pp.matchAndExtract(toPathContainer("/res1_res2/")).getUriVariables().get("var1")); assertEquals("res2",pp.matchAndExtract(toPathContainer("/res1_res2/")).getUriVariables().get("var2")); assertNoMatch(pp,"/res1_res2//"); pp = parse("/{var1}_{var2}/"); assertNoMatch(pp,"/res1_res2"); assertMatches(pp,"/res1_res2/"); assertEquals("res1",pp.matchAndExtract(toPathContainer("/res1_res2/")).getUriVariables().get("var1")); assertEquals("res2",pp.matchAndExtract(toPathContainer("/res1_res2/")).getUriVariables().get("var2"));
PathPattern pp = parse("/{abc:foo(bar)}"); try { pp.matchAndExtract(toPSC("/foo")); fail("Should have raised exception"); pp.matchAndExtract(toPSC("/foobar")); fail("Should have raised exception");
@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); }
@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); }