23
23
import java .util .Map ;
24
24
import java .util .Optional ;
25
25
import java .util .Set ;
26
+ import java .util .function .Function ;
26
27
import java .util .function .Predicate ;
27
28
28
29
import reactor .core .publisher .Flux ;
32
33
import org .springframework .http .MediaType ;
33
34
import org .springframework .http .server .reactive .ServerHttpRequest ;
34
35
import org .springframework .util .Assert ;
35
- import org .springframework .util .PathMatcher ;
36
36
import org .springframework .web .reactive .function .BodyExtractor ;
37
37
import org .springframework .web .server .WebSession ;
38
- import org .springframework .web .util .ParsingPathMatcher ;
39
38
import org .springframework .web .util .UriUtils ;
39
+ import org .springframework .web .util .patterns .PathPattern ;
40
+ import org .springframework .web .util .patterns .PathPatternParser ;
40
41
41
42
/**
42
43
* Implementations of {@link RequestPredicate} that implement various useful request matching operations, such as
47
48
*/
48
49
public abstract class RequestPredicates {
49
50
50
- private static final PathMatcher DEFAULT_PATH_MATCHER = new ParsingPathMatcher ();
51
+ private static final PathPatternParser DEFAULT_PATTERN_PARSER = new PathPatternParser ();
51
52
52
53
/**
53
54
* Returns a {@code RequestPredicate} that always matches.
@@ -75,21 +76,22 @@ public static RequestPredicate method(HttpMethod httpMethod) {
75
76
* @return a predicate that tests against the given path pattern
76
77
*/
77
78
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 ));
79
81
}
80
82
81
83
/**
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.
83
87
*
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
87
91
*/
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 ));
93
95
}
94
96
95
97
/**
@@ -324,26 +326,22 @@ public boolean test(ServerRequest request) {
324
326
}
325
327
}
326
328
327
- private static class PathMatchingPredicate implements RequestPredicate {
328
-
329
- private final String pattern ;
329
+ private static class PathPatternPredicate implements RequestPredicate {
330
330
331
- private final PathMatcher pathMatcher ;
331
+ private final PathPattern pattern ;
332
332
333
- public PathMatchingPredicate ( String pattern , PathMatcher pathMatcher ) {
333
+ public PathPatternPredicate ( PathPattern pattern ) {
334
334
Assert .notNull (pattern , "'pattern' must not be null" );
335
- Assert .notNull (pathMatcher , "'pathMatcher' must not be null" );
336
335
this .pattern = pattern ;
337
- this .pathMatcher = pathMatcher ;
338
336
}
339
337
340
338
@ Override
341
339
public boolean test (ServerRequest request ) {
342
340
String path = request .path ();
343
- if (this .pathMatcher . match ( this . pattern , path )) {
341
+ if (this .pattern . matches ( path )) {
344
342
if (request instanceof DefaultServerRequest ) {
345
343
DefaultServerRequest defaultRequest = (DefaultServerRequest ) request ;
346
- Map <String , String > uriTemplateVariables = this .pathMatcher . extractUriTemplateVariables ( this . pattern , path );
344
+ Map <String , String > uriTemplateVariables = this .pattern . matchAndExtract ( path );
347
345
defaultRequest .exchange ().getAttributes ().put (RouterFunctions .URI_TEMPLATE_VARIABLES_ATTRIBUTE , uriTemplateVariables );
348
346
}
349
347
return true ;
@@ -356,7 +354,7 @@ public boolean test(ServerRequest request) {
356
354
@ Override
357
355
public ServerRequest subRequest (ServerRequest request ) {
358
356
String requestPath = request .path ();
359
- String subPath = this .pathMatcher .extractPathWithinPattern (this . pattern , requestPath );
357
+ String subPath = this .pattern .extractPathWithinPattern (requestPath );
360
358
return new SubPathServerRequestWrapper (request , subPath );
361
359
}
362
360
}
0 commit comments