Skip to content

Commit febed19

Browse files
committed
Use PathPatternParser in function.server
Use new PathPatternParser instead of PathMatcher in web.reactive.function.server.
1 parent a31429b commit febed19

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultHandlerStrategiesBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public void applicationContext(ApplicationContext applicationContext) {
112112
applicationContext.getBeansOfType(HttpMessageReader.class).values().forEach(this::messageReader);
113113
applicationContext.getBeansOfType(HttpMessageWriter.class).values().forEach(this::messageWriter);
114114
applicationContext.getBeansOfType(ViewResolver.class).values().forEach(this::viewResolver);
115+
localeResolver(DEFAULT_LOCALE_RESOLVER);
115116
}
116117

117118
@Override

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Map;
2424
import java.util.Optional;
2525
import java.util.Set;
26+
import java.util.function.Function;
2627
import java.util.function.Predicate;
2728

2829
import reactor.core.publisher.Flux;
@@ -32,11 +33,11 @@
3233
import org.springframework.http.MediaType;
3334
import org.springframework.http.server.reactive.ServerHttpRequest;
3435
import org.springframework.util.Assert;
35-
import org.springframework.util.PathMatcher;
3636
import org.springframework.web.reactive.function.BodyExtractor;
3737
import org.springframework.web.server.WebSession;
38-
import org.springframework.web.util.ParsingPathMatcher;
3938
import org.springframework.web.util.UriUtils;
39+
import org.springframework.web.util.patterns.PathPattern;
40+
import org.springframework.web.util.patterns.PathPatternParser;
4041

4142
/**
4243
* Implementations of {@link RequestPredicate} that implement various useful request matching operations, such as
@@ -47,7 +48,7 @@
4748
*/
4849
public abstract class RequestPredicates {
4950

50-
private static final PathMatcher DEFAULT_PATH_MATCHER = new ParsingPathMatcher();
51+
private static final PathPatternParser DEFAULT_PATTERN_PARSER = new PathPatternParser();
5152

5253
/**
5354
* Returns a {@code RequestPredicate} that always matches.
@@ -75,21 +76,22 @@ public static RequestPredicate method(HttpMethod httpMethod) {
7576
* @return a predicate that tests against the given path pattern
7677
*/
7778
public static RequestPredicate path(String pattern) {
78-
return path(pattern, DEFAULT_PATH_MATCHER);
79+
Assert.notNull(pattern, "'pattern' must not be null");
80+
return new PathPatternPredicate(DEFAULT_PATTERN_PARSER.parse(pattern));
7981
}
8082

8183
/**
82-
* Return a {@code RequestPredicate} that tests against the given path pattern using the given matcher.
84+
* Return a function that creates new path-matching {@code RequestPredicates} from pattern
85+
* Strings using the given {@link PathPatternParser}. This method can be used to specify a
86+
* non-default, customized {@code PathPatternParser} when resolving path patterns.
8387
*
84-
* @param pattern the pattern to match to
85-
* @param pathMatcher the path matcher to use
86-
* @return a predicate that tests against the given path pattern
88+
* @param patternParser the parser used to parse patterns given to the returned function
89+
* @return a function that resolves patterns Strings into path-matching
90+
* {@code RequestPredicate}s
8791
*/
88-
public static RequestPredicate path(String pattern, PathMatcher pathMatcher) {
89-
Assert.notNull(pattern, "'pattern' must not be null");
90-
Assert.notNull(pathMatcher, "'pathMatcher' must not be null");
91-
92-
return new PathMatchingPredicate(pattern, pathMatcher);
92+
public static Function<String, RequestPredicate> pathPredicates(PathPatternParser patternParser) {
93+
Assert.notNull(patternParser, "'patternParser' must not be null");
94+
return pattern -> new PathPatternPredicate(patternParser.parse(pattern));
9395
}
9496

9597
/**
@@ -324,26 +326,22 @@ public boolean test(ServerRequest request) {
324326
}
325327
}
326328

327-
private static class PathMatchingPredicate implements RequestPredicate {
328-
329-
private final String pattern;
329+
private static class PathPatternPredicate implements RequestPredicate {
330330

331-
private final PathMatcher pathMatcher;
331+
private final PathPattern pattern;
332332

333-
public PathMatchingPredicate(String pattern, PathMatcher pathMatcher) {
333+
public PathPatternPredicate(PathPattern pattern) {
334334
Assert.notNull(pattern, "'pattern' must not be null");
335-
Assert.notNull(pathMatcher, "'pathMatcher' must not be null");
336335
this.pattern = pattern;
337-
this.pathMatcher = pathMatcher;
338336
}
339337

340338
@Override
341339
public boolean test(ServerRequest request) {
342340
String path = request.path();
343-
if (this.pathMatcher.match(this.pattern, path)) {
341+
if (this.pattern.matches(path)) {
344342
if (request instanceof DefaultServerRequest) {
345343
DefaultServerRequest defaultRequest = (DefaultServerRequest) request;
346-
Map<String, String> uriTemplateVariables = this.pathMatcher.extractUriTemplateVariables(this.pattern, path);
344+
Map<String, String> uriTemplateVariables = this.pattern.matchAndExtract(path);
347345
defaultRequest.exchange().getAttributes().put(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVariables);
348346
}
349347
return true;
@@ -356,7 +354,7 @@ public boolean test(ServerRequest request) {
356354
@Override
357355
public ServerRequest subRequest(ServerRequest request) {
358356
String requestPath = request.path();
359-
String subPath = this.pathMatcher.extractPathWithinPattern(this.pattern, requestPath);
357+
String subPath = this.pattern.extractPathWithinPattern(requestPath);
360358
return new SubPathServerRequestWrapper(request, subPath);
361359
}
362360
}

spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818

1919
import java.net.URI;
2020
import java.util.Collections;
21+
import java.util.function.Function;
2122

2223
import org.junit.Test;
2324

2425
import org.springframework.http.HttpMethod;
2526
import org.springframework.http.MediaType;
27+
import org.springframework.web.util.patterns.PathPatternParser;
2628

2729
import static org.junit.Assert.assertFalse;
2830
import static org.junit.Assert.assertTrue;
@@ -94,6 +96,18 @@ public void path() throws Exception {
9496
assertFalse(predicate.test(request));
9597
}
9698

99+
@Test
100+
public void pathPredicates() throws Exception {
101+
PathPatternParser parser = new PathPatternParser();
102+
parser.setCaseSensitive(false);
103+
Function<String, RequestPredicate> pathPredicates = RequestPredicates.pathPredicates(parser);
104+
105+
URI uri = URI.create("http://localhost/path");
106+
RequestPredicate predicate = pathPredicates.apply("/P*");
107+
MockServerRequest request = MockServerRequest.builder().uri(uri).build();
108+
assertTrue(predicate.test(request));
109+
}
110+
97111
@Test
98112
public void headers() throws Exception {
99113
String name = "MyHeader";

0 commit comments

Comments
 (0)