From 1e255560492a864800a71752394fe06e6a3bf9a1 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 3 Jun 2020 05:58:10 +0100 Subject: [PATCH] Shared PathPatternParser instance See gh-25143 --- .../UrlBasedCorsConfigurationSource.java | 4 +-- .../web/util/pattern/PathPatternParser.java | 31 +++++++++++++++++++ .../server/PathResourceLookupFunction.java | 6 ++-- .../function/server/RequestPredicates.java | 5 +-- .../resource/ResourceUrlProvider.java | 6 ++-- .../condition/PatternsRequestCondition.java | 2 +- .../result/method/RequestMappingInfo.java | 2 +- .../function/PathResourceLookupFunction.java | 6 ++-- .../servlet/function/RequestPredicates.java | 5 +-- 9 files changed, 43 insertions(+), 24 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/cors/reactive/UrlBasedCorsConfigurationSource.java b/spring-web/src/main/java/org/springframework/web/cors/reactive/UrlBasedCorsConfigurationSource.java index 2b97a1c7a8..6a454c64dd 100644 --- a/spring-web/src/main/java/org/springframework/web/cors/reactive/UrlBasedCorsConfigurationSource.java +++ b/spring-web/src/main/java/org/springframework/web/cors/reactive/UrlBasedCorsConfigurationSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,7 +50,7 @@ public class UrlBasedCorsConfigurationSource implements CorsConfigurationSource * @since 5.0.6 */ public UrlBasedCorsConfigurationSource() { - this(new PathPatternParser()); + this(PathPatternParser.defaultInstance); } /** diff --git a/spring-web/src/main/java/org/springframework/web/util/pattern/PathPatternParser.java b/spring-web/src/main/java/org/springframework/web/util/pattern/PathPatternParser.java index d083514f09..f7122a5318 100644 --- a/spring-web/src/main/java/org/springframework/web/util/pattern/PathPatternParser.java +++ b/spring-web/src/main/java/org/springframework/web/util/pattern/PathPatternParser.java @@ -122,4 +122,35 @@ public class PathPatternParser { return new InternalPathPatternParser(this).parse(pathPattern); } + + /** + * Shared, read-only instance of {@code PathPatternParser}. Uses default settings: + * + */ + public final static PathPatternParser defaultInstance = new PathPatternParser() { + + @Override + public void setMatchOptionalTrailingSeparator(boolean matchOptionalTrailingSeparator) { + raiseError(); + } + + @Override + public void setCaseSensitive(boolean caseSensitive) { + raiseError(); + } + + @Override + public void setPathOptions(PathContainer.Options pathOptions) { + raiseError(); + } + + private void raiseError() { + throw new UnsupportedOperationException( + "This is a read-only, shared instance that cannot be modified"); + } + }; } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/PathResourceLookupFunction.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/PathResourceLookupFunction.java index 0e2b719cc1..eb4109c223 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/PathResourceLookupFunction.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/PathResourceLookupFunction.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,8 +41,6 @@ import org.springframework.web.util.pattern.PathPatternParser; */ class PathResourceLookupFunction implements Function> { - private static final PathPatternParser PATTERN_PARSER = new PathPatternParser(); - private final PathPattern pattern; private final Resource location; @@ -51,7 +49,7 @@ class PathResourceLookupFunction implements Function handlerMap = new LinkedHashMap<>(); @@ -78,7 +76,7 @@ public class ResourceUrlProvider implements ApplicationListener { rawPattern = prependLeadingSlash(rawPattern); - PathPattern pattern = this.patternParser.parse(rawPattern); + PathPattern pattern = PathPatternParser.defaultInstance.parse(rawPattern); this.handlerMap.put(pattern, resourceWebHandler); }); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/PatternsRequestCondition.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/PatternsRequestCondition.java index e0035a7248..0fe4803e66 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/PatternsRequestCondition.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/PatternsRequestCondition.java @@ -43,7 +43,7 @@ import org.springframework.web.util.pattern.PathPatternParser; public final class PatternsRequestCondition extends AbstractRequestCondition { private static final SortedSet EMPTY_PATH_PATTERN = - new TreeSet<>(Collections.singleton(new PathPatternParser().parse(""))); + new TreeSet<>(Collections.singleton(PathPatternParser.defaultInstance.parse(""))); private final SortedSet patterns; diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java index 344df2f3af..f6f2de2ad4 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java @@ -527,7 +527,7 @@ public final class RequestMappingInfo implements RequestCondition> { - private static final PathPatternParser PATTERN_PARSER = new PathPatternParser(); - private final PathPattern pattern; private final Resource location; @@ -50,7 +48,7 @@ class PathResourceLookupFunction implements Function