public PartialMatchHelper(Set<RequestMappingInfo> infos, ServerWebExchange exchange) { this.partialMatches.addAll(infos.stream(). filter(info -> info.getPatternsCondition().getMatchingCondition(exchange) != null). map(info -> new PartialMatch(info, exchange)). collect(Collectors.toList())); }
/** * Checks if all conditions in this request mapping info match the provided request and returns * a potentially new request mapping info with conditions tailored to the current request. * <p>For example the returned instance may contain the subset of URL patterns that match to * the current request, sorted with best matching patterns on top. * @return a new instance in case all conditions match; or {@code null} otherwise */ @Override @Nullable public RequestMappingInfo getMatchingCondition(ServerWebExchange exchange) { RequestMethodsRequestCondition methods = this.methodsCondition.getMatchingCondition(exchange); ParamsRequestCondition params = this.paramsCondition.getMatchingCondition(exchange); HeadersRequestCondition headers = this.headersCondition.getMatchingCondition(exchange); ConsumesRequestCondition consumes = this.consumesCondition.getMatchingCondition(exchange); ProducesRequestCondition produces = this.producesCondition.getMatchingCondition(exchange); if (methods == null || params == null || headers == null || consumes == null || produces == null) { return null; } PatternsRequestCondition patterns = this.patternsCondition.getMatchingCondition(exchange); if (patterns == null) { return null; } RequestConditionHolder custom = this.customConditionHolder.getMatchingCondition(exchange); if (custom == null) { return null; } return new RequestMappingInfo(this.name, patterns, methods, params, headers, consumes, produces, custom.getCondition()); }
@Test public void compareNumberOfMatchingPatterns() throws Exception { ServerWebExchange exchange = MockServerWebExchange.from(get("/foo.html")); PatternsRequestCondition c1 = createPatternsCondition("/foo.*", "/foo.jpeg"); PatternsRequestCondition c2 = createPatternsCondition("/foo.*", "/foo.html"); PatternsRequestCondition match1 = c1.getMatchingCondition(exchange); PatternsRequestCondition match2 = c2.getMatchingCondition(exchange); assertNotNull(match1); assertEquals(1, match1.compareTo(match2, exchange)); }
@Test public void matchDirectPath() throws Exception { PatternsRequestCondition condition = createPatternsCondition("/foo"); MockServerWebExchange exchange = MockServerWebExchange.from(get("/foo")); PatternsRequestCondition match = condition.getMatchingCondition(exchange); assertNotNull(match); }
@Test public void matchPattern() throws Exception { PatternsRequestCondition condition = createPatternsCondition("/foo/*"); MockServerWebExchange exchange = MockServerWebExchange.from(get("/foo/bar")); PatternsRequestCondition match = condition.getMatchingCondition(exchange); assertNotNull(match); }
@Test public void matchPatternContainsExtension() throws Exception { PatternsRequestCondition condition = createPatternsCondition("/foo.jpg"); MockServerWebExchange exchange = MockServerWebExchange.from(get("/foo.html")); PatternsRequestCondition match = condition.getMatchingCondition(exchange); assertNull(match); }
@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); }
@Test public void matchSortPatterns() throws Exception { PatternsRequestCondition condition = createPatternsCondition("/*/*", "/foo/bar", "/foo/*"); MockServerWebExchange exchange = MockServerWebExchange.from(get("/foo/bar")); PatternsRequestCondition match = condition.getMatchingCondition(exchange); PatternsRequestCondition expected = createPatternsCondition("/foo/bar", "/foo/*", "/*/*"); assertEquals(expected, match); }
public PartialMatchHelper(Set<RequestMappingInfo> infos, ServerWebExchange exchange) { this.partialMatches.addAll(infos.stream(). filter(info -> info.getPatternsCondition().getMatchingCondition(exchange) != null). map(info -> new PartialMatch(info, exchange)). collect(Collectors.toList())); }
/** * Checks if all conditions in this request mapping info match the provided request and returns * a potentially new request mapping info with conditions tailored to the current request. * <p>For example the returned instance may contain the subset of URL patterns that match to * the current request, sorted with best matching patterns on top. * @return a new instance in case all conditions match; or {@code null} otherwise */ @Override public RequestMappingInfo getMatchingCondition(ServerWebExchange exchange) { RequestMethodsRequestCondition methods = this.methodsCondition.getMatchingCondition(exchange); ParamsRequestCondition params = this.paramsCondition.getMatchingCondition(exchange); HeadersRequestCondition headers = this.headersCondition.getMatchingCondition(exchange); ConsumesRequestCondition consumes = this.consumesCondition.getMatchingCondition(exchange); ProducesRequestCondition produces = this.producesCondition.getMatchingCondition(exchange); if (methods == null || params == null || headers == null || consumes == null || produces == null) { return null; } PatternsRequestCondition patterns = this.patternsCondition.getMatchingCondition(exchange); if (patterns == null) { return null; } RequestConditionHolder custom = this.customConditionHolder.getMatchingCondition(exchange); if (custom == null) { return null; } return new RequestMappingInfo(this.name, patterns, methods, params, headers, consumes, produces, custom.getCondition()); }