Skip to content

Commit 1e25556

Browse files
committed
Shared PathPatternParser instance
See gh-25143
1 parent 1ced937 commit 1e25556

File tree

9 files changed

+43
-24
lines changed

9 files changed

+43
-24
lines changed

spring-web/src/main/java/org/springframework/web/cors/reactive/UrlBasedCorsConfigurationSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,7 +50,7 @@ public class UrlBasedCorsConfigurationSource implements CorsConfigurationSource
5050
* @since 5.0.6
5151
*/
5252
public UrlBasedCorsConfigurationSource() {
53-
this(new PathPatternParser());
53+
this(PathPatternParser.defaultInstance);
5454
}
5555

5656
/**

spring-web/src/main/java/org/springframework/web/util/pattern/PathPatternParser.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,35 @@ public PathPattern parse(String pathPattern) throws PatternParseException {
122122
return new InternalPathPatternParser(this).parse(pathPattern);
123123
}
124124

125+
126+
/**
127+
* Shared, read-only instance of {@code PathPatternParser}. Uses default settings:
128+
* <ul>
129+
* <li>{@code matchOptionalTrailingSeparator=true}
130+
* <li>{@code caseSensitivetrue}
131+
* <li>{@code pathOptions=PathContainer.Options.HTTP_PATH}
132+
* </ul>
133+
*/
134+
public final static PathPatternParser defaultInstance = new PathPatternParser() {
135+
136+
@Override
137+
public void setMatchOptionalTrailingSeparator(boolean matchOptionalTrailingSeparator) {
138+
raiseError();
139+
}
140+
141+
@Override
142+
public void setCaseSensitive(boolean caseSensitive) {
143+
raiseError();
144+
}
145+
146+
@Override
147+
public void setPathOptions(PathContainer.Options pathOptions) {
148+
raiseError();
149+
}
150+
151+
private void raiseError() {
152+
throw new UnsupportedOperationException(
153+
"This is a read-only, shared instance that cannot be modified");
154+
}
155+
};
125156
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,8 +41,6 @@
4141
*/
4242
class PathResourceLookupFunction implements Function<ServerRequest, Mono<Resource>> {
4343

44-
private static final PathPatternParser PATTERN_PARSER = new PathPatternParser();
45-
4644
private final PathPattern pattern;
4745

4846
private final Resource location;
@@ -51,7 +49,7 @@ class PathResourceLookupFunction implements Function<ServerRequest, Mono<Resourc
5149
public PathResourceLookupFunction(String pattern, Resource location) {
5250
Assert.hasLength(pattern, "'pattern' must not be empty");
5351
Assert.notNull(location, "'location' must not be null");
54-
this.pattern = PATTERN_PARSER.parse(pattern);
52+
this.pattern = PathPatternParser.defaultInstance.parse(pattern);
5553
this.location = location;
5654
}
5755

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ public abstract class RequestPredicates {
7272

7373
private static final Log logger = LogFactory.getLog(RequestPredicates.class);
7474

75-
private static final PathPatternParser DEFAULT_PATTERN_PARSER = new PathPatternParser();
76-
77-
7875
/**
7976
* Return a {@code RequestPredicate} that always matches.
8077
* @return a predicate that always matches
@@ -115,7 +112,7 @@ public static RequestPredicate path(String pattern) {
115112
if (!pattern.isEmpty() && !pattern.startsWith("/")) {
116113
pattern = "/" + pattern;
117114
}
118-
return pathPredicates(DEFAULT_PATTERN_PARSER).apply(pattern);
115+
return pathPredicates(PathPatternParser.defaultInstance).apply(pattern);
119116
}
120117

121118
/**

spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceUrlProvider.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,8 +54,6 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
5454
private static final Log logger = LogFactory.getLog(ResourceUrlProvider.class);
5555

5656

57-
private final PathPatternParser patternParser = new PathPatternParser();
58-
5957
private final Map<PathPattern, ResourceWebHandler> handlerMap = new LinkedHashMap<>();
6058

6159

@@ -78,7 +76,7 @@ public void registerHandlers(Map<String, ResourceWebHandler> handlerMap) {
7876
this.handlerMap.clear();
7977
handlerMap.forEach((rawPattern, resourceWebHandler) -> {
8078
rawPattern = prependLeadingSlash(rawPattern);
81-
PathPattern pattern = this.patternParser.parse(rawPattern);
79+
PathPattern pattern = PathPatternParser.defaultInstance.parse(rawPattern);
8280
this.handlerMap.put(pattern, resourceWebHandler);
8381
});
8482
}

spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/PatternsRequestCondition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
public final class PatternsRequestCondition extends AbstractRequestCondition<PatternsRequestCondition> {
4444

4545
private static final SortedSet<PathPattern> EMPTY_PATH_PATTERN =
46-
new TreeSet<>(Collections.singleton(new PathPatternParser().parse("")));
46+
new TreeSet<>(Collections.singleton(PathPatternParser.defaultInstance.parse("")));
4747

4848

4949
private final SortedSet<PathPattern> patterns;

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ public Builder options(BuilderConfiguration options) {
527527
public RequestMappingInfo build() {
528528

529529
PathPatternParser parser = (this.options.getPatternParser() != null ?
530-
this.options.getPatternParser() : new PathPatternParser());
530+
this.options.getPatternParser() : PathPatternParser.defaultInstance);
531531

532532
RequestedContentTypeResolver contentTypeResolver = this.options.getContentTypeResolver();
533533

spring-webmvc/src/main/java/org/springframework/web/servlet/function/PathResourceLookupFunction.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,8 +40,6 @@
4040
*/
4141
class PathResourceLookupFunction implements Function<ServerRequest, Optional<Resource>> {
4242

43-
private static final PathPatternParser PATTERN_PARSER = new PathPatternParser();
44-
4543
private final PathPattern pattern;
4644

4745
private final Resource location;
@@ -50,7 +48,7 @@ class PathResourceLookupFunction implements Function<ServerRequest, Optional<Res
5048
public PathResourceLookupFunction(String pattern, Resource location) {
5149
Assert.hasLength(pattern, "'pattern' must not be empty");
5250
Assert.notNull(location, "'location' must not be null");
53-
this.pattern = PATTERN_PARSER.parse(pattern);
51+
this.pattern = PathPatternParser.defaultInstance.parse(pattern);
5452
this.location = location;
5553
}
5654

spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ public abstract class RequestPredicates {
7171

7272
private static final Log logger = LogFactory.getLog(RequestPredicates.class);
7373

74-
private static final PathPatternParser DEFAULT_PATTERN_PARSER = new PathPatternParser();
75-
76-
7774
/**
7875
* Return a {@code RequestPredicate} that always matches.
7976
* @return a predicate that always matches
@@ -113,7 +110,7 @@ public static RequestPredicate path(String pattern) {
113110
if (!pattern.isEmpty() && !pattern.startsWith("/")) {
114111
pattern = "/" + pattern;
115112
}
116-
return pathPredicates(DEFAULT_PATTERN_PARSER).apply(pattern);
113+
return pathPredicates(PathPatternParser.defaultInstance).apply(pattern);
117114
}
118115

119116
/**

0 commit comments

Comments
 (0)