Remove ParsingPathMatcher

Now that we also have RequestPath and PathContainer with the latter as
the required input, the ParsingPathMatcher adapter can be removed.
This commit is contained in:
Rossen Stoyanchev
2017-08-01 13:35:21 +02:00
parent 4fc0ce1206
commit b1440b6816
7 changed files with 29 additions and 260 deletions

View File

@@ -1,170 +0,0 @@
/*
* Copyright 2002-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.util.pattern;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
import org.springframework.http.server.PathContainer;
import org.springframework.lang.Nullable;
import org.springframework.util.PathMatcher;
import org.springframework.web.util.pattern.PathPattern.PathMatchResult;
/**
* {@link PathMatcher} implementation for path patterns parsed
* as {@link PathPatternParser} and compiled as {@link PathPattern}s.
*
* <p>Once parsed, {@link PathPattern}s are tailored for fast matching
* and quick comparison.
*
* <p>Calls this {@link PathMatcher} implementation can lead to
* {@link PatternParseException} if the provided patterns are
* illegal.
*
* @author Andy Clement
* @author Juergen Hoeller
* @since 5.0
* @see PathPattern
*/
public class ParsingPathMatcher implements PathMatcher {
private final PathPatternParser parser = new PathPatternParser();
private final ConcurrentMap<String, PathPattern> cache = new ConcurrentHashMap<>(256);
@Override
public boolean isPattern(String path) {
// TODO crude, should be smarter, lookup pattern and ask it
return (path.indexOf('*') != -1 || path.indexOf('?') != -1);
}
@Override
public boolean match(String pattern, String path) {
PathPattern pathPattern = getPathPattern(pattern);
return pathPattern.matches(PathContainer.parsePath(path));
}
@Override
public boolean matchStart(String pattern, String path) {
PathPattern pathPattern = getPathPattern(pattern);
return pathPattern.matchStart(PathContainer.parsePath(path));
}
@Override
public String extractPathWithinPattern(String pattern, String path) {
PathPattern pathPattern = getPathPattern(pattern);
PathContainer pathContainer = PathContainer.parsePath(path);
return pathPattern.extractPathWithinPattern(pathContainer).value();
}
@Override
public Map<String, String> extractUriTemplateVariables(String pattern, String path) {
PathPattern pathPattern = getPathPattern(pattern);
PathContainer pathContainer = PathContainer.parsePath(path);
PathMatchResult results = pathPattern.matchAndExtract(pathContainer);
// Collapse PathMatchResults to simple value results
// TODO: (path parameters are lost in this translation)
if (results.getUriVariables().size() == 0) {
return Collections.emptyMap();
}
else {
return results.getUriVariables().entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
}
@Override
public Comparator<String> getPatternComparator(String path) {
return new PathPatternStringComparatorConsideringPath(path);
}
@Override
public String combine(String pattern1, String pattern2) {
PathPattern pathPattern = getPathPattern(pattern1);
return pathPattern.combine(getPathPattern(pattern2)).getPatternString();
}
private PathPattern getPathPattern(String pattern) {
PathPattern pathPattern = this.cache.get(pattern);
if (pathPattern == null) {
pathPattern = this.parser.parse(pattern);
this.cache.put(pattern, pathPattern);
}
return pathPattern;
}
private class PathPatternStringComparatorConsideringPath implements Comparator<String> {
private final PatternComparatorConsideringPath ppcp;
public PathPatternStringComparatorConsideringPath(String path) {
this.ppcp = new PatternComparatorConsideringPath(path);
}
@Override
public int compare(@Nullable String o1, @Nullable String o2) {
if (o1 == null) {
return (o2 == null ? 0 : +1);
}
else if (o2 == null) {
return -1;
}
PathPattern p1 = getPathPattern(o1);
PathPattern p2 = getPathPattern(o2);
return this.ppcp.compare(p1, p2);
}
}
/**
* {@link PathPattern} comparator that takes account of a specified
* path and sorts anything that exactly matches it to be first.
*/
static class PatternComparatorConsideringPath implements Comparator<PathPattern> {
private final String path;
public PatternComparatorConsideringPath(String path) {
this.path = path;
}
@Override
public int compare(@Nullable PathPattern o1, @Nullable PathPattern o2) {
// Nulls get sorted to the end
if (o1 == null) {
return (o2 == null ? 0 : +1);
}
else if (o2 == null) {
return -1;
}
if (o1.getPatternString().equals(this.path)) {
return (o2.getPatternString().equals(this.path)) ? 0 : -1;
}
else if (o2.getPatternString().equals(this.path)) {
return +1;
}
return o1.compareTo(o2);
}
}
}