Remove PathPatternParser from PatternsRequestCondition
Since `PathPattern.combine` now returns another `PathPattern` instance (it was previously returning a String instance), we can now safely remove the parser instance included in `PatternsRequestCondition`. Issue: SPR-15663
This commit is contained in:
@@ -19,7 +19,6 @@ package org.springframework.web.reactive.result.condition;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@@ -27,14 +26,11 @@ import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.http.server.reactive.PathContainer;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.util.pattern.PathPattern;
|
||||
import org.springframework.web.util.pattern.PathPatternParser;
|
||||
|
||||
/**
|
||||
* A logical disjunction (' || ') request condition that matches a request
|
||||
@@ -46,48 +42,32 @@ import org.springframework.web.util.pattern.PathPatternParser;
|
||||
*/
|
||||
public final class PatternsRequestCondition extends AbstractRequestCondition<PatternsRequestCondition> {
|
||||
|
||||
private final Set<PathPattern> patterns;
|
||||
|
||||
private final PathPatternParser parser;
|
||||
private final SortedSet<PathPattern> patterns;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new instance with the given URL patterns.
|
||||
* Each pattern is prepended with "/" if not already.
|
||||
* @param patterns 0 or more URL patterns; if 0 the condition will match to every request.
|
||||
*/
|
||||
public PatternsRequestCondition(String... patterns) {
|
||||
this(patterns, null);
|
||||
public PatternsRequestCondition(PathPattern... patterns) {
|
||||
this(Arrays.asList(patterns));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with the given URL patterns.
|
||||
* Each pattern that is not empty and does not start with "/" is pre-pended with "/".
|
||||
* @param patterns the URL patterns to use; if 0, the condition will match to every request.
|
||||
* @param patternParser for parsing string patterns
|
||||
* Creates a new instance with the given {@code Stream} of URL patterns.
|
||||
*/
|
||||
public PatternsRequestCondition(String[] patterns, PathPatternParser patternParser) {
|
||||
this(Arrays.asList(patterns), patternParser);
|
||||
public PatternsRequestCondition(List<PathPattern> patterns) {
|
||||
this(toSortedSet(patterns));
|
||||
}
|
||||
|
||||
/**
|
||||
* Private constructor accepting a collection of raw patterns.
|
||||
*/
|
||||
private PatternsRequestCondition(Collection<String> patterns, PathPatternParser parser) {
|
||||
this.parser = (parser != null ? parser : new PathPatternParser());
|
||||
this.patterns = toSortedSet(patterns.stream().map(pattern -> parse(pattern, this.parser)));
|
||||
private static SortedSet<PathPattern> toSortedSet(Collection<PathPattern> patterns) {
|
||||
TreeSet<PathPattern> sorted = new TreeSet<>(getPatternComparator());
|
||||
sorted.addAll(patterns);
|
||||
return sorted;
|
||||
}
|
||||
|
||||
private static PathPattern parse(String pattern, PathPatternParser parser) {
|
||||
if (StringUtils.hasText(pattern) && !pattern.startsWith("/")) {
|
||||
pattern = "/" + pattern;
|
||||
}
|
||||
return parser.parse(pattern);
|
||||
}
|
||||
|
||||
private static Set<PathPattern> toSortedSet(Stream<PathPattern> stream) {
|
||||
return Collections.unmodifiableSet(stream.sorted()
|
||||
.collect(Collectors.toCollection(() -> new TreeSet<>(getPatternComparator()))));
|
||||
private PatternsRequestCondition(SortedSet<PathPattern> patterns) {
|
||||
this.patterns = patterns;
|
||||
}
|
||||
|
||||
private static Comparator<PathPattern> getPatternComparator() {
|
||||
@@ -97,14 +77,6 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Private constructor accepting a list of path patterns.
|
||||
*/
|
||||
private PatternsRequestCondition(List<PathPattern> patterns, PathPatternParser patternParser) {
|
||||
this.patterns = toSortedSet(patterns.stream());
|
||||
this.parser = patternParser;
|
||||
}
|
||||
|
||||
public Set<PathPattern> getPatterns() {
|
||||
return this.patterns;
|
||||
}
|
||||
@@ -145,10 +117,7 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
|
||||
else if (!other.patterns.isEmpty()) {
|
||||
combined.addAll(other.patterns);
|
||||
}
|
||||
else {
|
||||
combined.add(this.parser.parse(""));
|
||||
}
|
||||
return new PatternsRequestCondition(combined, this.parser);
|
||||
return new PatternsRequestCondition(combined);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -167,7 +136,7 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
|
||||
}
|
||||
SortedSet<PathPattern> matches = getMatchingPatterns(exchange);
|
||||
return matches.isEmpty() ? null :
|
||||
new PatternsRequestCondition(new ArrayList<PathPattern>(matches), this.parser);
|
||||
new PatternsRequestCondition(matches);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,6 +16,10 @@
|
||||
|
||||
package org.springframework.web.reactive.result.method;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
@@ -29,6 +33,7 @@ import org.springframework.web.reactive.result.condition.RequestCondition;
|
||||
import org.springframework.web.reactive.result.condition.RequestConditionHolder;
|
||||
import org.springframework.web.reactive.result.condition.RequestMethodsRequestCondition;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.util.pattern.PathPattern;
|
||||
import org.springframework.web.util.pattern.PathPatternParser;
|
||||
|
||||
/**
|
||||
@@ -478,8 +483,9 @@ public final class RequestMappingInfo implements RequestCondition<RequestMapping
|
||||
public RequestMappingInfo build() {
|
||||
RequestedContentTypeResolver contentTypeResolver = this.options.getContentTypeResolver();
|
||||
|
||||
PatternsRequestCondition patternsCondition = new PatternsRequestCondition(
|
||||
this.paths, this.options.getPatternParser());
|
||||
PathPatternParser parser = this.options.getPatternParser() != null ?
|
||||
this.options.getPatternParser() : new PathPatternParser();
|
||||
PatternsRequestCondition patternsCondition = new PatternsRequestCondition(parse(this.paths, parser));
|
||||
|
||||
return new RequestMappingInfo(this.mappingName, patternsCondition,
|
||||
new RequestMethodsRequestCondition(methods),
|
||||
@@ -489,6 +495,18 @@ public final class RequestMappingInfo implements RequestCondition<RequestMapping
|
||||
new ProducesRequestCondition(this.produces, this.headers, contentTypeResolver),
|
||||
this.customCondition);
|
||||
}
|
||||
|
||||
private static List<PathPattern> parse(String[] paths, PathPatternParser parser) {
|
||||
return Arrays
|
||||
.stream(paths)
|
||||
.map(path -> {
|
||||
if (StringUtils.hasText(path) && !path.startsWith("/")) {
|
||||
path = "/" + path;
|
||||
}
|
||||
return parser.parse(path);
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user