Reduce API Surface in PathPatternRegistry

This commit reduces the exposition of `PathPattern` instances throughout
the `HandlerMapping` API and removes some methods from its public API.

Issue: SPR-14544
This commit is contained in:
Brian Clozel
2017-02-09 22:09:46 +01:00
parent 4999898617
commit 8d43f45515
13 changed files with 135 additions and 127 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.web.cors.reactive;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
@@ -71,12 +72,14 @@ public class UrlBasedCorsConfigurationSource implements CorsConfigurationSource
/**
* Set CORS configuration based on URL patterns.
*/
public void setCorsConfigurations(Map<PathPattern, CorsConfiguration> corsConfigurations) {
public void setCorsConfigurations(Map<String, CorsConfiguration> corsConfigurations) {
this.patternRegistry.clear();
this.corsConfigurations.clear();
if (corsConfigurations != null) {
this.patternRegistry.addAll(corsConfigurations.keySet());
this.corsConfigurations.putAll(corsConfigurations);
corsConfigurations.forEach((pattern, config) -> {
List<PathPattern> registered = this.patternRegistry.register(pattern);
registered.forEach(p -> this.corsConfigurations.put(p, config));
});
}
}

View File

@@ -17,7 +17,6 @@
package org.springframework.web.util.patterns;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
@@ -63,6 +62,11 @@ public class PathPatternRegistry {
this.patterns = new HashSet<>();
}
public PathPatternRegistry(Set<PathPattern> patterns) {
this();
this.patterns.addAll(patterns);
}
/**
* Whether to match to paths irrespective of the presence of a trailing slash.
*/
@@ -150,30 +154,6 @@ public class PathPatternRegistry {
return this.pathPatternParser.parse(pathPattern);
}
/**
* Add a {@link PathPattern} instance to this registry
* @return true if this registry did not already contain the specified {@code PathPattern}
*/
public boolean add(PathPattern pathPattern) {
return this.patterns.add(pathPattern);
}
/**
* Add all {@link PathPattern}s instance to this registry
* @return true if this registry did not already contain at least one of the given {@code PathPattern}s
*/
public boolean addAll(Collection<PathPattern> pathPatterns) {
return this.patterns.addAll(pathPatterns);
}
/**
* Remove the given {@link PathPattern} from this registry
* @return true if this registry contained the given {@code PathPattern}
*/
public boolean remove(PathPattern pathPattern) {
return this.patterns.remove(pathPattern);
}
/**
* Remove all {@link PathPattern}s from this registry
*/
@@ -197,25 +177,7 @@ public class PathPatternRegistry {
* @return the list of {@link PathPattern} that were registered as a result
*/
public List<PathPattern> register(String rawPattern) {
String fixedPattern = prependLeadingSlash(rawPattern);
List<PathPattern> newPatterns = new ArrayList<>();
PathPattern pattern = this.pathPatternParser.parse(fixedPattern);
newPatterns.add(pattern);
if (StringUtils.hasLength(fixedPattern) && !pattern.isCatchAll()) {
if (this.useSuffixPatternMatch) {
if (this.fileExtensions != null && !this.fileExtensions.isEmpty()) {
for (String extension : this.fileExtensions) {
newPatterns.add(this.pathPatternParser.parse(fixedPattern + extension));
}
}
else {
newPatterns.add(this.pathPatternParser.parse(fixedPattern + ".*"));
}
}
if (this.useTrailingSlashMatch && !fixedPattern.endsWith("/")) {
newPatterns.add(this.pathPatternParser.parse(fixedPattern + "/"));
}
}
List<PathPattern> newPatterns = generatePathPatterns(rawPattern);
this.patterns.addAll(newPatterns);
return newPatterns;
}
@@ -229,6 +191,44 @@ public class PathPatternRegistry {
}
}
private List<PathPattern> generatePathPatterns(String rawPattern) {
String fixedPattern = prependLeadingSlash(rawPattern);
List<PathPattern> patterns = new ArrayList<>();
PathPattern pattern = this.pathPatternParser.parse(fixedPattern);
patterns.add(pattern);
if (StringUtils.hasLength(fixedPattern) && !pattern.isCatchAll()) {
if (this.useSuffixPatternMatch) {
if (this.fileExtensions != null && !this.fileExtensions.isEmpty()) {
for (String extension : this.fileExtensions) {
patterns.add(this.pathPatternParser.parse(fixedPattern + extension));
}
}
else {
patterns.add(this.pathPatternParser.parse(fixedPattern + ".*"));
}
}
if (this.useTrailingSlashMatch && !fixedPattern.endsWith("/")) {
patterns.add(this.pathPatternParser.parse(fixedPattern + "/"));
}
}
return patterns;
}
/**
* Parse the given {@code rawPattern} and removes it to this registry,
* as well as pattern variants, depending on the given options and
* the nature of the input pattern.
*
* @param rawPattern raw path pattern to parse and unregister
* @return the list of {@link PathPattern} that were unregistered as a result
*/
public List<PathPattern> unregister(String rawPattern) {
List<PathPattern> unregisteredPatterns = generatePathPatterns(rawPattern);
this.patterns.removeAll(unregisteredPatterns);
return unregisteredPatterns;
}
/**
* Combine the patterns contained in the current registry
* with the ones in the other, into a new {@code PathPatternRegistry} instance.