9 changed files with 131 additions and 446 deletions
@ -1,77 +0,0 @@
@@ -1,77 +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.reactive.handler; |
||||
|
||||
import org.jetbrains.annotations.NotNull; |
||||
|
||||
import org.springframework.util.Assert; |
||||
import org.springframework.web.util.pattern.PathPattern; |
||||
|
||||
/** |
||||
* Result of path match performed through a {@link PathPatternRegistry}. |
||||
* Each result contains the matched pattern and handler of type {@code T}. |
||||
* |
||||
* @author Brian Clozel |
||||
* @since 5.0 |
||||
* @see PathPatternRegistry |
||||
*/ |
||||
public class PathMatchResult<T> implements Comparable<PathMatchResult<T>> { |
||||
|
||||
private final PathPattern pattern; |
||||
|
||||
private final T handler; |
||||
|
||||
|
||||
public PathMatchResult(PathPattern pattern, T handler) { |
||||
Assert.notNull(pattern, "PathPattern must not be null"); |
||||
Assert.notNull(handler, "Handler must not be null"); |
||||
this.pattern = pattern; |
||||
this.handler = handler; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Return the {@link PathPattern} that matched the incoming request. |
||||
*/ |
||||
public PathPattern getPattern() { |
||||
return this.pattern; |
||||
} |
||||
|
||||
/** |
||||
* Return the request handler associated with the {@link PathPattern}. |
||||
*/ |
||||
public T getHandler() { |
||||
return this.handler; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public int compareTo(@NotNull PathMatchResult<T> other) { |
||||
int index = this.pattern.compareTo(other.pattern); |
||||
return (index != 0 ? index : this.getPatternString().compareTo(other.getPatternString())); |
||||
} |
||||
|
||||
private String getPatternString() { |
||||
return this.pattern.getPatternString(); |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "PathMatchResult{pattern=" + this.pattern + ", handler=" + this.handler + "]"; |
||||
} |
||||
|
||||
} |
||||
@ -1,141 +0,0 @@
@@ -1,141 +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.reactive.handler; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.Comparator; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
import java.util.SortedSet; |
||||
import java.util.TreeSet; |
||||
import java.util.stream.Collectors; |
||||
|
||||
import org.springframework.http.server.reactive.PathContainer; |
||||
import org.springframework.lang.Nullable; |
||||
import org.springframework.util.StringUtils; |
||||
import org.springframework.web.util.pattern.PathPattern; |
||||
import org.springframework.web.util.pattern.PathPatternParser; |
||||
|
||||
/** |
||||
* Registry that holds {@code PathPattern}s instances |
||||
* and allows matching against them with a lookup path. |
||||
* |
||||
* @author Brian Clozel |
||||
* @since 5.0 |
||||
*/ |
||||
public class PathPatternRegistry<T> { |
||||
|
||||
private final PathPatternParser pathPatternParser; |
||||
|
||||
private final Map<PathPattern, T> patternsMap; |
||||
|
||||
|
||||
/** |
||||
* Create a new {@code PathPatternRegistry} with |
||||
* a default instance of {@link PathPatternParser}. |
||||
*/ |
||||
public PathPatternRegistry() { |
||||
this(new PathPatternParser()); |
||||
} |
||||
|
||||
/** |
||||
* Create a new {@code PathPatternRegistry} using |
||||
* the provided instance of {@link PathPatternParser}. |
||||
* @param patternParser the {@link PathPatternParser} to use |
||||
*/ |
||||
public PathPatternRegistry(PathPatternParser patternParser) { |
||||
this(patternParser, Collections.emptyMap()); |
||||
} |
||||
|
||||
/** |
||||
* Create a new {@code PathPatternRegistry} using |
||||
* the provided instance of {@link PathPatternParser} |
||||
* and the given map of {@link PathPattern}. |
||||
* @param patternParser the {@link PathPatternParser} to use |
||||
* @param patternsMap the map of {@link PathPattern} to use |
||||
*/ |
||||
public PathPatternRegistry(PathPatternParser patternParser, Map<PathPattern, T> patternsMap) { |
||||
this.pathPatternParser = patternParser; |
||||
this.patternsMap = new HashMap<>(patternsMap); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Return a (read-only) map of all patterns and associated values. |
||||
*/ |
||||
public Map<PathPattern, T> getPatternsMap() { |
||||
return Collections.unmodifiableMap(this.patternsMap); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Parse the given {@code rawPattern} and adds it to this registry. |
||||
* @param rawPattern raw path pattern to parse and register |
||||
* @param handler the associated handler object |
||||
*/ |
||||
public void register(String rawPattern, T handler) { |
||||
String fixedPattern = prependLeadingSlash(rawPattern); |
||||
PathPattern newPattern = this.pathPatternParser.parse(fixedPattern); |
||||
this.patternsMap.put(newPattern, handler); |
||||
} |
||||
|
||||
private static String prependLeadingSlash(String pattern) { |
||||
if (StringUtils.hasLength(pattern) && !pattern.startsWith("/")) { |
||||
return "/" + pattern; |
||||
} |
||||
else { |
||||
return pattern; |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Return patterns matching the given {@code lookupPath}. |
||||
* <p>The returned set sorted with the most specific |
||||
* patterns first, according to the given {@code lookupPath}. |
||||
* @param lookupPath the URL lookup path to be matched against |
||||
*/ |
||||
public SortedSet<PathMatchResult<T>> findMatches(PathContainer lookupPath) { |
||||
return this.patternsMap.entrySet().stream() |
||||
.filter(entry -> entry.getKey().matches(lookupPath)) |
||||
.sorted(Comparator.comparing(Map.Entry::getKey)) |
||||
.map(entry -> new PathMatchResult<>(entry.getKey(), entry.getValue())) |
||||
.collect(Collectors.toCollection(TreeSet::new)); |
||||
} |
||||
|
||||
/** |
||||
* Return, if any, the most specific {@code PathPattern} matching the given {@code lookupPath}. |
||||
* @param lookupPath the URL lookup path to be matched against |
||||
*/ |
||||
@Nullable |
||||
public PathMatchResult<T> findFirstMatch(PathContainer lookupPath) { |
||||
return this.patternsMap.entrySet().stream() |
||||
.filter(entry -> entry.getKey().matches(lookupPath)) |
||||
.sorted(Comparator.comparing(Map.Entry::getKey)) |
||||
.findFirst() |
||||
.map(entry -> new PathMatchResult<>(entry.getKey(), entry.getValue())) |
||||
.orElse(null); |
||||
} |
||||
|
||||
/** |
||||
* Remove all {@link PathPattern}s from this registry |
||||
*/ |
||||
public void clear() { |
||||
this.patternsMap.clear(); |
||||
} |
||||
|
||||
} |
||||
@ -1,133 +0,0 @@
@@ -1,133 +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.reactive.handler; |
||||
|
||||
import java.nio.charset.StandardCharsets; |
||||
import java.util.Collection; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
import java.util.stream.Collectors; |
||||
|
||||
import org.hamcrest.BaseMatcher; |
||||
import org.hamcrest.Description; |
||||
import org.hamcrest.Matchers; |
||||
import org.junit.Rule; |
||||
import org.junit.Test; |
||||
import org.junit.rules.ExpectedException; |
||||
|
||||
import org.springframework.http.server.reactive.PathContainer; |
||||
import org.springframework.web.util.pattern.PathPattern; |
||||
import org.springframework.web.util.pattern.PathPatternParser; |
||||
import org.springframework.web.util.pattern.PatternParseException; |
||||
|
||||
import static org.hamcrest.Matchers.contains; |
||||
import static org.hamcrest.Matchers.hasSize; |
||||
import static org.hamcrest.Matchers.is; |
||||
import static org.junit.Assert.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link PathPatternRegistry} |
||||
* |
||||
* @author Brian Clozel |
||||
*/ |
||||
public class PathPatternRegistryTests { |
||||
|
||||
private final PathPatternRegistry<Object> registry = new PathPatternRegistry(); |
||||
|
||||
private final PathPatternParser parser = new PathPatternParser(); |
||||
|
||||
@Rule |
||||
public ExpectedException thrown = ExpectedException.none(); |
||||
|
||||
|
||||
@Test |
||||
public void shouldPrependPatternsWithSlash() { |
||||
this.registry.register("foo/bar", new Object()); |
||||
assertThat(this.registry.getPatternsMap().keySet(), contains(pattern("/foo/bar"))); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldNotRegisterInvalidPatterns() { |
||||
this.thrown.expect(PatternParseException.class); |
||||
this.thrown.expectMessage(Matchers.containsString("Expected close capture character after variable name")); |
||||
this.registry.register("/{invalid", new Object()); |
||||
} |
||||
|
||||
@Test |
||||
public void registerPatternsWithSameSpecificity() { |
||||
PathPattern fooOne = this.parser.parse("/fo?"); |
||||
PathPattern fooTwo = this.parser.parse("/f?o"); |
||||
assertThat(fooOne.compareTo(fooTwo), is(0)); |
||||
|
||||
this.registry.register("/fo?", new Object()); |
||||
this.registry.register("/f?o", new Object()); |
||||
|
||||
PathContainer path = PathContainer.parse("/foo", StandardCharsets.UTF_8); |
||||
Set<PathMatchResult<Object>> matches = this.registry.findMatches(path); |
||||
assertThat(toPatterns(matches), contains(pattern("/f?o"), pattern("/fo?"))); |
||||
} |
||||
|
||||
@Test |
||||
public void findNoMatch() { |
||||
this.registry.register("/foo/{bar}", new Object()); |
||||
PathContainer path = PathContainer.parse("/other", StandardCharsets.UTF_8); |
||||
assertThat(this.registry.findMatches(path), hasSize(0)); |
||||
} |
||||
|
||||
@Test |
||||
public void orderMatchesBySpecificity() { |
||||
this.registry.register("/foo/{*baz}", new Object()); |
||||
this.registry.register("/foo/bar/baz", new Object()); |
||||
this.registry.register("/foo/bar/{baz}", new Object()); |
||||
PathContainer path = PathContainer.parse("/foo/bar/baz", StandardCharsets.UTF_8); |
||||
Set<PathMatchResult<Object>> matches = this.registry.findMatches(path); |
||||
assertThat(toPatterns(matches), contains(pattern("/foo/bar/baz"), pattern("/foo/bar/{baz}"), |
||||
pattern("/foo/{*baz}"))); |
||||
} |
||||
|
||||
|
||||
private List<PathPattern> toPatterns(Collection<PathMatchResult<Object>> results) { |
||||
return results.stream().map(PathMatchResult::getPattern).collect(Collectors.toList()); |
||||
} |
||||
|
||||
private static PathPatternMatcher pattern(String pattern) { |
||||
return new PathPatternMatcher(pattern); |
||||
} |
||||
|
||||
private static class PathPatternMatcher extends BaseMatcher<PathPattern> { |
||||
|
||||
private final String pattern; |
||||
|
||||
public PathPatternMatcher(String pattern) { |
||||
this.pattern = pattern; |
||||
} |
||||
|
||||
@Override |
||||
public boolean matches(Object item) { |
||||
if(item != null && item instanceof PathPattern) { |
||||
return ((PathPattern) item).getPatternString().equals(pattern); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
@Override |
||||
public void describeTo(Description description) { |
||||
|
||||
} |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue