Add PathPatternRegistry for handler mapping matching
Previously `HandlerMapping` implementation were heavily relying on `String` path patterns, `PathMatcher` implementations and dedicated maps for matching incoming request URL to an actual request handler. This commit adds the `PathPatternRegistry` that holds `PathPattern` instances and the associated request handler — matching results are then shared as `PathMatchResult` instances. `AbstractUrlHandlerMapping` will use this registry directly, but other components dealing with request matching (like `PatternsRequestCondition`) will directly use ordered `PathPattern` collections since ordering is important there. This opens the door for faster request matching and simplifies the design of this part. Issue: SPR-15608
This commit is contained in:
@@ -17,34 +17,27 @@
|
||||
package org.springframework.web.reactive.config;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.web.util.pattern.ParsingPathMatcher;
|
||||
|
||||
/**
|
||||
* Assist with configuring {@code HandlerMapping}'s with path matching options.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Brian Clozel
|
||||
* @since 5.0
|
||||
*/
|
||||
public class PathMatchConfigurer {
|
||||
|
||||
private Boolean suffixPatternMatch;
|
||||
|
||||
private Boolean trailingSlashMatch;
|
||||
|
||||
private Boolean registeredSuffixPatternMatch;
|
||||
|
||||
private PathMatcher pathMatcher;
|
||||
|
||||
private Boolean caseSensitiveMatch;
|
||||
|
||||
/**
|
||||
* Whether to use suffix pattern match (".*") when matching patterns to
|
||||
* requests. If enabled a method mapped to "/users" also matches to "/users.*".
|
||||
* <p>By default this is set to {@code true}.
|
||||
* @see #registeredSuffixPatternMatch
|
||||
* Whether to match to URLs irrespective of their case.
|
||||
* If enabled a method mapped to "/users" won't match to "/Users/".
|
||||
* <p>The default value is {@code false}.
|
||||
*/
|
||||
public PathMatchConfigurer setUseSuffixPatternMatch(Boolean suffixPatternMatch) {
|
||||
this.suffixPatternMatch = suffixPatternMatch;
|
||||
public PathMatchConfigurer setUseCaseSensitiveMatch(Boolean caseSensitiveMatch) {
|
||||
this.caseSensitiveMatch = caseSensitiveMatch;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -58,49 +51,14 @@ public class PathMatchConfigurer {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether suffix pattern matching should work only against path extensions
|
||||
* that are explicitly registered. This is generally recommended to reduce
|
||||
* ambiguity and to avoid issues such as when a "." (dot) appears in the path
|
||||
* for other reasons.
|
||||
* <p>By default this is set to "true".
|
||||
*/
|
||||
public PathMatchConfigurer setUseRegisteredSuffixPatternMatch(Boolean registeredSuffixPatternMatch) {
|
||||
this.registeredSuffixPatternMatch = registeredSuffixPatternMatch;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the PathMatcher for matching URL paths against registered URL patterns.
|
||||
* <p>The default is a {@link org.springframework.web.util.pattern.ParsingPathMatcher}.
|
||||
*/
|
||||
public PathMatchConfigurer setPathMatcher(PathMatcher pathMatcher) {
|
||||
this.pathMatcher = pathMatcher;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected Boolean isUseSuffixPatternMatch() {
|
||||
return this.suffixPatternMatch;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected Boolean isUseTrailingSlashMatch() {
|
||||
return this.trailingSlashMatch;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected Boolean isUseRegisteredSuffixPatternMatch() {
|
||||
return this.registeredSuffixPatternMatch;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public PathMatcher getPathMatcher() {
|
||||
if (this.pathMatcher instanceof ParsingPathMatcher && (this.trailingSlashMatch || this.suffixPatternMatch)) {
|
||||
throw new IllegalStateException("When using a ParsingPathMatcher, useTrailingSlashMatch" +
|
||||
" and useSuffixPatternMatch should be set to 'false'.");
|
||||
}
|
||||
return this.pathMatcher;
|
||||
protected Boolean isUseCaseSensitiveMatch() {
|
||||
return this.caseSensitiveMatch;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ import org.springframework.beans.factory.BeanInitializationException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.reactive.handler.AbstractHandlerMapping;
|
||||
import org.springframework.web.reactive.handler.AbstractUrlHandlerMapping;
|
||||
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
|
||||
import org.springframework.web.reactive.resource.ResourceWebHandler;
|
||||
import org.springframework.web.server.WebHandler;
|
||||
@@ -76,7 +76,7 @@ public class ResourceHandlerRegistry {
|
||||
* URL path patterns. The handler will be invoked for every incoming request
|
||||
* that matches to one of the specified path patterns.
|
||||
* <p>Patterns like {@code "/static/**"} or {@code "/css/{filename:\\w+\\.css}"}
|
||||
* are allowed. See {@link org.springframework.web.util.pattern.ParsingPathMatcher}
|
||||
* are allowed. See {@link org.springframework.web.util.pattern.PathPattern}
|
||||
* for more details on the syntax.
|
||||
* @return A {@link ResourceHandlerRegistration} to use to further
|
||||
* configure the registered resource handler
|
||||
@@ -115,7 +115,7 @@ public class ResourceHandlerRegistry {
|
||||
* of no registrations.
|
||||
*/
|
||||
@Nullable
|
||||
protected AbstractHandlerMapping getHandlerMapping() {
|
||||
protected AbstractUrlHandlerMapping getHandlerMapping() {
|
||||
if (this.registrations.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -36,7 +36,6 @@ import org.springframework.format.support.FormattingConversionService;
|
||||
import org.springframework.http.codec.ServerCodecConfigurer;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.MessageCodesResolver;
|
||||
import org.springframework.validation.Validator;
|
||||
@@ -73,13 +72,6 @@ import org.springframework.web.server.handler.ResponseStatusExceptionHandler;
|
||||
*/
|
||||
public class WebFluxConfigurationSupport implements ApplicationContextAware {
|
||||
|
||||
static final boolean jackson2Present =
|
||||
ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper",
|
||||
WebFluxConfigurationSupport.class.getClassLoader()) &&
|
||||
ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator",
|
||||
WebFluxConfigurationSupport.class.getClassLoader());
|
||||
|
||||
|
||||
private Map<String, CorsConfiguration> corsConfigurations;
|
||||
|
||||
private PathMatchConfigurer pathMatchConfigurer;
|
||||
@@ -118,21 +110,11 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware {
|
||||
mapping.setCorsConfigurations(getCorsConfigurations());
|
||||
|
||||
PathMatchConfigurer configurer = getPathMatchConfigurer();
|
||||
Boolean useSuffixPatternMatch = configurer.isUseSuffixPatternMatch();
|
||||
Boolean useRegisteredSuffixPatternMatch = configurer.isUseRegisteredSuffixPatternMatch();
|
||||
Boolean useTrailingSlashMatch = configurer.isUseTrailingSlashMatch();
|
||||
if (useSuffixPatternMatch != null) {
|
||||
mapping.setUseSuffixPatternMatch(useSuffixPatternMatch);
|
||||
if (configurer.isUseTrailingSlashMatch() != null) {
|
||||
mapping.setUseTrailingSlashMatch(configurer.isUseTrailingSlashMatch());
|
||||
}
|
||||
if (useRegisteredSuffixPatternMatch != null) {
|
||||
mapping.setUseRegisteredSuffixPatternMatch(useRegisteredSuffixPatternMatch);
|
||||
}
|
||||
if (useTrailingSlashMatch != null) {
|
||||
mapping.setUseTrailingSlashMatch(useTrailingSlashMatch);
|
||||
}
|
||||
PathMatcher pathMatcher = configurer.getPathMatcher();
|
||||
if (pathMatcher != null) {
|
||||
mapping.setPathMatcher(pathMatcher);
|
||||
if (configurer.isUseCaseSensitiveMatch() != null) {
|
||||
mapping.setUseCaseSensitiveMatch(configurer.isUseCaseSensitiveMatch());
|
||||
}
|
||||
return mapping;
|
||||
}
|
||||
@@ -224,9 +206,12 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware {
|
||||
|
||||
AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
|
||||
if (handlerMapping != null) {
|
||||
PathMatchConfigurer pathMatchConfigurer = getPathMatchConfigurer();
|
||||
if (pathMatchConfigurer.getPathMatcher() != null) {
|
||||
handlerMapping.setPathMatcher(pathMatchConfigurer.getPathMatcher());
|
||||
PathMatchConfigurer configurer = getPathMatchConfigurer();
|
||||
if (configurer.isUseTrailingSlashMatch() != null) {
|
||||
handlerMapping.setUseTrailingSlashMatch(configurer.isUseTrailingSlashMatch());
|
||||
}
|
||||
if (configurer.isUseCaseSensitiveMatch() != null) {
|
||||
handlerMapping.setUseCaseSensitiveMatch(configurer.isUseCaseSensitiveMatch());
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -58,13 +58,6 @@ public interface WebFluxConfigurer {
|
||||
/**
|
||||
* Configure path matching options.
|
||||
*
|
||||
* <p>Note that if a {@link org.springframework.web.util.pattern.ParsingPathMatcher}
|
||||
* is configured here,
|
||||
* the {@link PathMatchConfigurer#setUseTrailingSlashMatch(Boolean)} and
|
||||
* {@link PathMatchConfigurer#setUseSuffixPatternMatch(Boolean)} options must be set
|
||||
* to {@literal false}as they can lead to illegal patterns,
|
||||
* see {@link org.springframework.web.util.pattern.ParsingPathMatcher}.
|
||||
*
|
||||
* {@code HandlerMapping}s with path matching options.
|
||||
* @param configurer the {@link PathMatchConfigurer} instance
|
||||
*/
|
||||
|
||||
@@ -24,7 +24,6 @@ import org.springframework.context.support.ApplicationObjectSupport;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.cors.reactive.CorsConfigurationSource;
|
||||
import org.springframework.web.cors.reactive.CorsProcessor;
|
||||
@@ -34,7 +33,7 @@ import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
|
||||
import org.springframework.web.reactive.HandlerMapping;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebHandler;
|
||||
import org.springframework.web.util.pattern.ParsingPathMatcher;
|
||||
import org.springframework.web.util.pattern.PathPatternParser;
|
||||
|
||||
/**
|
||||
* Abstract base class for {@link org.springframework.web.reactive.HandlerMapping}
|
||||
@@ -52,7 +51,7 @@ public abstract class AbstractHandlerMapping extends ApplicationObjectSupport im
|
||||
|
||||
private int order = Integer.MAX_VALUE; // default: same as non-Ordered
|
||||
|
||||
private PathMatcher pathMatcher = new ParsingPathMatcher();
|
||||
private PathPatternParser patternParser = new PathPatternParser();
|
||||
|
||||
private final UrlBasedCorsConfigurationSource globalCorsConfigSource = new UrlBasedCorsConfigurationSource();
|
||||
|
||||
@@ -74,22 +73,28 @@ public abstract class AbstractHandlerMapping extends ApplicationObjectSupport im
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the PathMatcher implementation to use for matching URL paths
|
||||
* against registered URL patterns.
|
||||
* <p>The default is a {@link ParsingPathMatcher}.
|
||||
* Whether to match to URLs irrespective of their case.
|
||||
* If enabled a method mapped to "/users" won't match to "/Users/".
|
||||
* <p>The default value is {@code false}.
|
||||
*/
|
||||
public void setPathMatcher(PathMatcher pathMatcher) {
|
||||
Assert.notNull(pathMatcher, "PathMatcher must not be null");
|
||||
this.pathMatcher = pathMatcher;
|
||||
this.globalCorsConfigSource.setPathMatcher(pathMatcher);
|
||||
public void setUseCaseSensitiveMatch(boolean caseSensitiveMatch) {
|
||||
this.patternParser.setCaseSensitive(caseSensitiveMatch);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the PathMatcher implementation to use for matching URL paths
|
||||
* against registered URL patterns.
|
||||
* Whether to match to URLs irrespective of the presence of a trailing slash.
|
||||
* If enabled a method mapped to "/users" also matches to "/users/".
|
||||
* <p>The default value is {@code true}.
|
||||
*/
|
||||
public PathMatcher getPathMatcher() {
|
||||
return this.pathMatcher;
|
||||
public void setUseTrailingSlashMatch(boolean trailingSlashMatch) {
|
||||
this.patternParser.setMatchOptionalTrailingSlash(trailingSlashMatch);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link PathPatternParser} instance.
|
||||
*/
|
||||
public PathPatternParser getPathPatternParser() {
|
||||
return this.patternParser;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,12 +16,8 @@
|
||||
|
||||
package org.springframework.web.reactive.handler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@@ -29,20 +25,21 @@ import org.springframework.beans.BeansException;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.util.pattern.PathPattern;
|
||||
|
||||
/**
|
||||
* Abstract base class for URL-mapped
|
||||
* {@link org.springframework.web.reactive.HandlerMapping} implementations.
|
||||
*
|
||||
* <p>Supports direct matches, e.g. a registered "/test" matches "/test", and
|
||||
* various Ant-style pattern matches, e.g. a registered "/t*" pattern matches
|
||||
* various path pattern matches, e.g. a registered "/t*" pattern matches
|
||||
* both "/test" and "/team", "/test/*" matches all paths under "/test",
|
||||
* "/test/**" matches all paths below "/test". For details, see the
|
||||
* {@link org.springframework.web.util.pattern.ParsingPathMatcher} javadoc.
|
||||
* {@link org.springframework.web.util.pattern.PathPattern} javadoc.
|
||||
*
|
||||
* <p>Will search all path patterns to find the most exact match for the
|
||||
* current request path. The most exact match is defined as the longest
|
||||
* path pattern that matches the current request path.
|
||||
* <p>Will search all path patterns to find the most specific match for the
|
||||
* current request path. The most specific pattern is defined as the longest
|
||||
* path pattern with the fewest captured variables and wildcards.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Juergen Hoeller
|
||||
@@ -51,27 +48,12 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
*/
|
||||
public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
|
||||
|
||||
private boolean useTrailingSlashMatch = false;
|
||||
|
||||
private boolean lazyInitHandlers = false;
|
||||
|
||||
private final Map<String, Object> handlerMap = new LinkedHashMap<>();
|
||||
private PathPatternRegistry<Object> patternRegistry;
|
||||
|
||||
|
||||
/**
|
||||
* Whether to match to URLs irrespective of the presence of a trailing slash.
|
||||
* If enabled a URL pattern such as "/users" also matches to "/users/".
|
||||
* <p>The default value is {@code false}.
|
||||
*/
|
||||
public void setUseTrailingSlashMatch(boolean useTrailingSlashMatch) {
|
||||
this.useTrailingSlashMatch = useTrailingSlashMatch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to match to URLs irrespective of the presence of a trailing slash.
|
||||
*/
|
||||
public boolean useTrailingSlashMatch() {
|
||||
return this.useTrailingSlashMatch;
|
||||
public PathPatternRegistry<Object> getPatternRegistry() {
|
||||
return patternRegistry;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,11 +72,11 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
|
||||
|
||||
/**
|
||||
* Return the registered handlers as an unmodifiable Map, with the registered path
|
||||
* as key and the handler object (or handler bean name in case of a lazy-init handler)
|
||||
* pattern as key and the handler object (or handler bean name in case of a lazy-init handler)
|
||||
* as value.
|
||||
*/
|
||||
public final Map<String, Object> getHandlerMap() {
|
||||
return Collections.unmodifiableMap(this.handlerMap);
|
||||
public final Map<PathPattern, Object> getHandlerMap() {
|
||||
return this.patternRegistry.getPatternsMap();
|
||||
}
|
||||
|
||||
|
||||
@@ -129,58 +111,30 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
|
||||
* @param lookupPath URL the handler is mapped to
|
||||
* @param exchange the current exchange
|
||||
* @return the associated handler instance, or {@code null} if not found
|
||||
* @see org.springframework.web.util.pattern.ParsingPathMatcher
|
||||
* @see org.springframework.web.util.pattern.PathPattern
|
||||
*/
|
||||
@Nullable
|
||||
protected Object lookupHandler(String lookupPath, ServerWebExchange exchange) throws Exception {
|
||||
// Direct match?
|
||||
Object handler = this.handlerMap.get(lookupPath);
|
||||
if (handler != null) {
|
||||
return handleMatch(handler, lookupPath, lookupPath, exchange);
|
||||
}
|
||||
|
||||
// Pattern match?
|
||||
List<String> matches = new ArrayList<>();
|
||||
for (String pattern : this.handlerMap.keySet()) {
|
||||
if (getPathMatcher().match(pattern, lookupPath)) {
|
||||
matches.add(pattern);
|
||||
}
|
||||
else if (useTrailingSlashMatch()) {
|
||||
if (!pattern.endsWith("/") && getPathMatcher().match(pattern + "/", lookupPath)) {
|
||||
matches.add(pattern +"/");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String bestMatch = null;
|
||||
Comparator<String> comparator = getPathMatcher().getPatternComparator(lookupPath);
|
||||
if (!matches.isEmpty()) {
|
||||
Collections.sort(matches, comparator);
|
||||
Optional<PathMatchResult<Object>> matches = this.patternRegistry.findFirstMatch(lookupPath);
|
||||
if (matches.isPresent()) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Matching patterns for request [" + lookupPath + "] are " + matches);
|
||||
}
|
||||
bestMatch = matches.get(0);
|
||||
}
|
||||
if (bestMatch != null) {
|
||||
handler = this.handlerMap.get(bestMatch);
|
||||
PathMatchResult<Object> bestMatch = matches.get();
|
||||
String pathWithinMapping = bestMatch.getPattern().extractPathWithinPattern(lookupPath);
|
||||
Object handler = bestMatch.getHandler();
|
||||
if (handler == null) {
|
||||
if (bestMatch.endsWith("/")) {
|
||||
handler = this.handlerMap.get(bestMatch.substring(0, bestMatch.length() - 1));
|
||||
}
|
||||
if (handler == null) {
|
||||
throw new IllegalStateException(
|
||||
"Could not find handler for best pattern match [" + bestMatch + "]");
|
||||
}
|
||||
throw new IllegalStateException(
|
||||
"Could not find handler for best pattern match [" + bestMatch + "]");
|
||||
}
|
||||
String pathWithinMapping = getPathMatcher().extractPathWithinPattern(bestMatch, lookupPath);
|
||||
return handleMatch(handler, bestMatch, pathWithinMapping, exchange);
|
||||
return handleMatch(handler, bestMatch.getPattern(), pathWithinMapping, exchange);
|
||||
}
|
||||
|
||||
// No handler found...
|
||||
return null;
|
||||
}
|
||||
|
||||
private Object handleMatch(Object handler, String bestMatch, String pathWithinMapping,
|
||||
private Object handleMatch(Object handler, PathPattern bestMatch, String pathWithinMapping,
|
||||
ServerWebExchange exchange) throws Exception {
|
||||
|
||||
// Bean name or resolved handler?
|
||||
@@ -236,6 +190,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
|
||||
Assert.notNull(handler, "Handler object must not be null");
|
||||
Object resolvedHandler = handler;
|
||||
|
||||
|
||||
// Eagerly resolve handler if referencing singleton via name.
|
||||
if (!this.lazyInitHandlers && handler instanceof String) {
|
||||
String handlerName = (String) handler;
|
||||
@@ -243,20 +198,27 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
|
||||
resolvedHandler = obtainApplicationContext().getBean(handlerName);
|
||||
}
|
||||
}
|
||||
if (this.patternRegistry == null) {
|
||||
this.patternRegistry = new PathPatternRegistry<>(getPathPatternParser());
|
||||
}
|
||||
|
||||
Object mappedHandler = this.handlerMap.get(urlPath);
|
||||
if (mappedHandler != null) {
|
||||
if (mappedHandler != resolvedHandler) {
|
||||
throw new IllegalStateException(
|
||||
"Cannot map " + getHandlerDescription(handler) + " to URL path [" + urlPath +
|
||||
"]: There is already " + getHandlerDescription(mappedHandler) + " mapped.");
|
||||
Map<PathPattern, Object> patternsMap = this.patternRegistry.getPatternsMap();
|
||||
if (patternsMap.containsKey(urlPath)) {
|
||||
Object mappedHandler = patternsMap.get(urlPath);
|
||||
if (mappedHandler != null) {
|
||||
if (mappedHandler != resolvedHandler) {
|
||||
throw new IllegalStateException(
|
||||
"Cannot map " + getHandlerDescription(handler) + " to URL path [" + urlPath +
|
||||
"]: There is already " + getHandlerDescription(mappedHandler) + " mapped.");
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
this.handlerMap.put(urlPath, resolvedHandler);
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Mapped URL path [" + urlPath + "] onto " + getHandlerDescription(handler));
|
||||
}
|
||||
this.patternRegistry.register(urlPath, resolvedHandler);
|
||||
}
|
||||
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Mapped URL path [" + urlPath + "] onto " + getHandlerDescription(handler));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.util.pattern.PathPattern;
|
||||
|
||||
/**
|
||||
* Result of matching an request lookup path against {@link PathPattern} instances.
|
||||
* <p>Each result optionnally associates the matching {@link PathPattern}
|
||||
* with a request handler of type {@code T}.
|
||||
* @author Brian Clozel
|
||||
* @since 5.0
|
||||
* @see PathPatternRegistry
|
||||
*/
|
||||
public class PathMatchResult<T> {
|
||||
|
||||
private final PathPattern pattern;
|
||||
|
||||
private final T handler;
|
||||
|
||||
public PathMatchResult(PathPattern pattern, T handler) {
|
||||
Assert.notNull(pattern, "PathPattern should not be null");
|
||||
this.pattern = pattern;
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link PathPattern} that matched the incoming request.
|
||||
*/
|
||||
public PathPattern getPattern() {
|
||||
return pattern;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the request handler associated with the {@link PathPattern}.
|
||||
*/
|
||||
@Nullable
|
||||
public T getHandler() {
|
||||
return handler;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
* 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.Optional;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.util.pattern.PathPattern;
|
||||
import org.springframework.web.util.pattern.PathPatternComparator;
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a {@code SortedSet} of {@code PathPattern}s 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(String lookupPath) {
|
||||
return this.patternsMap.entrySet().stream()
|
||||
.filter(entry -> entry.getKey().matches(lookupPath))
|
||||
.map(entry -> new PathMatchResult<>(entry.getKey(), entry.getValue()))
|
||||
.collect(Collectors.toCollection(() ->
|
||||
new TreeSet<>(new PathMatchResultComparator<T>(lookupPath))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return, if any, the most specific {@code PathPattern} matching the given {@code lookupPath}.
|
||||
*
|
||||
* @param lookupPath the URL lookup path to be matched against
|
||||
*/
|
||||
public Optional<PathMatchResult<T>> findFirstMatch(String lookupPath) {
|
||||
PathPatternComparator comparator = new PathPatternComparator(lookupPath);
|
||||
return this.patternsMap.entrySet().stream()
|
||||
.filter(entry -> entry.getKey().matches(lookupPath))
|
||||
.reduce((e1, e2) -> comparator.compare(e1.getKey(), e2.getKey()) < 0 ? e1 : e2)
|
||||
.map(entry -> new PathMatchResult<>(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all {@link PathPattern}s from this registry
|
||||
*/
|
||||
public void clear() {
|
||||
this.patternsMap.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the given {@code rawPattern} and adds it to this registry.
|
||||
* @param rawPattern raw path pattern to parse and register
|
||||
* @param handler
|
||||
*/
|
||||
public void register(String rawPattern, T handler) {
|
||||
String fixedPattern = prependLeadingSlash(rawPattern);
|
||||
PathPattern newPattern = this.pathPatternParser.parse(fixedPattern);
|
||||
this.patternsMap.put(newPattern, handler);
|
||||
}
|
||||
|
||||
private String prependLeadingSlash(String pattern) {
|
||||
if (StringUtils.hasLength(pattern) && !pattern.startsWith("/")) {
|
||||
return "/" + pattern;
|
||||
}
|
||||
else {
|
||||
return pattern;
|
||||
}
|
||||
}
|
||||
|
||||
private class PathMatchResultComparator<T> implements Comparator<PathMatchResult<T>> {
|
||||
|
||||
private final String path;
|
||||
|
||||
public PathMatchResultComparator(String path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(PathMatchResult<T> o1, PathMatchResult<T> o2) {
|
||||
// Nulls get sorted to the end
|
||||
if (o1 == null) {
|
||||
return (o2 == null ? 0 : +1);
|
||||
}
|
||||
else if (o2 == null) {
|
||||
return -1;
|
||||
}
|
||||
PathPattern p1 = o1.getPattern();
|
||||
PathPattern p2 = o2.getPattern();
|
||||
// exact matches get sorted first
|
||||
if (p1.getPatternString().equals(path)) {
|
||||
return (p2.getPatternString().equals(path)) ? 0 : -1;
|
||||
}
|
||||
else if (p2.getPatternString().equals(path)) {
|
||||
return +1;
|
||||
}
|
||||
// compare pattern specificity
|
||||
int result = p1.compareTo(p2);
|
||||
// if equal specificity, sort using pattern string
|
||||
if (result == 0) {
|
||||
return p1.getPatternString().compareTo(p2.getPatternString());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -46,7 +46,7 @@ import org.springframework.util.CollectionUtils;
|
||||
* various Ant-style pattern matches, e.g. a registered "/t*" pattern matches
|
||||
* both "/test" and "/team", "/test/*" matches all paths under "/test",
|
||||
* "/test/**" matches all paths below "/test". For details, see the
|
||||
* {@link org.springframework.web.util.pattern.ParsingPathMatcher} javadoc.
|
||||
* {@link org.springframework.web.util.pattern.PathPattern} javadoc.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
@@ -60,7 +60,7 @@ public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {
|
||||
* Map URL paths to handler bean names.
|
||||
* This is the typical way of configuring this HandlerMapping.
|
||||
* <p>Supports direct URL matches and Ant-style pattern matches. For syntax details,
|
||||
* see the {@link org.springframework.web.util.pattern.ParsingPathMatcher} javadoc.
|
||||
* see the {@link org.springframework.web.util.pattern.PathPattern} javadoc.
|
||||
* @param mappings properties with URLs as keys and bean names as values
|
||||
* @see #setUrlMap
|
||||
*/
|
||||
@@ -72,7 +72,7 @@ public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {
|
||||
* Set a Map with URL paths as keys and handler beans (or handler bean names)
|
||||
* as values. Convenient for population with bean references.
|
||||
* <p>Supports direct URL matches and Ant-style pattern matches. For syntax details,
|
||||
* see the {@link org.springframework.web.util.pattern.ParsingPathMatcher} javadoc.
|
||||
* see the {@link org.springframework.web.util.pattern.PathPattern} javadoc.
|
||||
* @param urlMap map with URLs as keys and beans as values
|
||||
* @see #setMappings
|
||||
*/
|
||||
|
||||
@@ -17,11 +17,9 @@
|
||||
package org.springframework.web.reactive.resource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -34,10 +32,12 @@ import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.util.pattern.ParsingPathMatcher;
|
||||
import org.springframework.web.reactive.handler.PathMatchResult;
|
||||
import org.springframework.web.util.pattern.PathPattern;
|
||||
import org.springframework.web.util.pattern.PathPatternParser;
|
||||
import org.springframework.web.reactive.handler.PathPatternRegistry;
|
||||
|
||||
/**
|
||||
* A central component to use to obtain the public URL path that clients should
|
||||
@@ -54,26 +54,17 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private PathMatcher pathMatcher = new ParsingPathMatcher();
|
||||
|
||||
private final Map<String, ResourceWebHandler> handlerMap = new LinkedHashMap<>();
|
||||
private PathPatternRegistry<ResourceWebHandler> patternRegistry = new PathPatternRegistry<>();
|
||||
|
||||
private boolean autodetect = true;
|
||||
|
||||
|
||||
/**
|
||||
* Configure a {@code PathMatcher} to use when comparing target lookup path
|
||||
* Configure a {@code PathPatternParser} to use when comparing target lookup path
|
||||
* against resource mappings.
|
||||
*/
|
||||
public void setPathMatcher(PathMatcher pathMatcher) {
|
||||
this.pathMatcher = pathMatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the configured {@code PathMatcher}.
|
||||
*/
|
||||
public PathMatcher getPathMatcher() {
|
||||
return this.pathMatcher;
|
||||
public void setPathPatternParser(PathPatternParser patternParser) {
|
||||
this.patternRegistry = new PathPatternRegistry<>(patternParser);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,8 +75,8 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
|
||||
*/
|
||||
public void setHandlerMap(@Nullable Map<String, ResourceWebHandler> handlerMap) {
|
||||
if (handlerMap != null) {
|
||||
this.handlerMap.clear();
|
||||
this.handlerMap.putAll(handlerMap);
|
||||
this.patternRegistry.clear();
|
||||
handlerMap.forEach(this.patternRegistry::register);
|
||||
this.autodetect = false;
|
||||
}
|
||||
}
|
||||
@@ -94,8 +85,8 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
|
||||
* Return the resource mappings, either manually configured or auto-detected
|
||||
* when the Spring {@code ApplicationContext} is refreshed.
|
||||
*/
|
||||
public Map<String, ResourceWebHandler> getHandlerMap() {
|
||||
return this.handlerMap;
|
||||
public Map<PathPattern, ResourceWebHandler> getHandlerMap() {
|
||||
return this.patternRegistry.getPatternsMap();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,14 +100,14 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
|
||||
@Override
|
||||
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||
if (isAutodetect()) {
|
||||
this.handlerMap.clear();
|
||||
this.patternRegistry.clear();
|
||||
detectResourceHandlers(event.getApplicationContext());
|
||||
if (this.handlerMap.isEmpty() && logger.isDebugEnabled()) {
|
||||
logger.debug("No resource handling mappings found");
|
||||
}
|
||||
if (!this.handlerMap.isEmpty()) {
|
||||
if (!this.patternRegistry.getPatternsMap().isEmpty()) {
|
||||
this.autodetect = false;
|
||||
}
|
||||
else if(logger.isDebugEnabled()) {
|
||||
logger.debug("No resource handling mappings found");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,7 +120,7 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
|
||||
AnnotationAwareOrderComparator.sort(handlerMappings);
|
||||
|
||||
for (SimpleUrlHandlerMapping hm : handlerMappings) {
|
||||
for (String pattern : hm.getHandlerMap().keySet()) {
|
||||
for (PathPattern pattern : hm.getHandlerMap().keySet()) {
|
||||
Object handler = hm.getHandlerMap().get(pattern);
|
||||
if (handler instanceof ResourceWebHandler) {
|
||||
ResourceWebHandler resourceHandler = (ResourceWebHandler) handler;
|
||||
@@ -138,7 +129,7 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
|
||||
"locations=" + resourceHandler.getLocations() + ", " +
|
||||
"resolvers=" + resourceHandler.getResourceResolvers());
|
||||
}
|
||||
this.handlerMap.put(pattern, resourceHandler);
|
||||
this.patternRegistry.register(pattern.getPatternString(), resourceHandler);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -173,11 +164,11 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
|
||||
private int getEndPathIndex(String lookupPath) {
|
||||
int suffixIndex = lookupPath.length();
|
||||
int queryIndex = lookupPath.indexOf("?");
|
||||
if(queryIndex > 0) {
|
||||
if (queryIndex > 0) {
|
||||
suffixIndex = queryIndex;
|
||||
}
|
||||
int hashIndex = lookupPath.indexOf("#");
|
||||
if(hashIndex > 0) {
|
||||
if (hashIndex > 0) {
|
||||
suffixIndex = Math.min(suffixIndex, hashIndex);
|
||||
}
|
||||
return suffixIndex;
|
||||
@@ -200,28 +191,20 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
|
||||
logger.trace("Getting resource URL for lookup path \"" + lookupPath + "\"");
|
||||
}
|
||||
|
||||
List<String> matchingPatterns = new ArrayList<>();
|
||||
for (String pattern : this.handlerMap.keySet()) {
|
||||
if (getPathMatcher().match(pattern, lookupPath)) {
|
||||
matchingPatterns.add(pattern);
|
||||
}
|
||||
}
|
||||
Set<PathMatchResult<ResourceWebHandler>> matchResults = this.patternRegistry.findMatches(lookupPath);
|
||||
|
||||
if (matchingPatterns.isEmpty()) {
|
||||
if (matchResults.isEmpty()) {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
Comparator<String> patternComparator = getPathMatcher().getPatternComparator(lookupPath);
|
||||
Collections.sort(matchingPatterns, patternComparator);
|
||||
|
||||
return Flux.fromIterable(matchingPatterns)
|
||||
.concatMap(pattern -> {
|
||||
String pathWithinMapping = getPathMatcher().extractPathWithinPattern(pattern, lookupPath);
|
||||
return Flux.fromIterable(matchResults)
|
||||
.concatMap(result -> {
|
||||
String pathWithinMapping = result.getPattern().extractPathWithinPattern(lookupPath);
|
||||
String pathMapping = lookupPath.substring(0, lookupPath.indexOf(pathWithinMapping));
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Invoking ResourceResolverChain for URL pattern \"" + pattern + "\"");
|
||||
logger.trace("Invoking ResourceResolverChain for URL pattern \"" + result.getPattern() + "\"");
|
||||
}
|
||||
ResourceWebHandler handler = this.handlerMap.get(pattern);
|
||||
ResourceWebHandler handler = result.getHandler();
|
||||
ResourceResolverChain chain = new DefaultResourceResolverChain(handler.getResourceResolvers());
|
||||
return chain.resolveUrlPath(pathWithinMapping, handler.getLocations())
|
||||
.map(resolvedPath -> {
|
||||
|
||||
@@ -20,82 +20,74 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.util.pattern.ParsingPathMatcher;
|
||||
import org.springframework.web.util.pattern.PathPattern;
|
||||
import org.springframework.web.util.pattern.PathPatternComparator;
|
||||
import org.springframework.web.util.pattern.PathPatternParser;
|
||||
|
||||
/**
|
||||
* A logical disjunction (' || ') request condition that matches a request
|
||||
* against a set of URL path patterns.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Brian Clozel
|
||||
* @since 5.0
|
||||
*/
|
||||
public final class PatternsRequestCondition extends AbstractRequestCondition<PatternsRequestCondition> {
|
||||
|
||||
private final Set<String> patterns;
|
||||
|
||||
private final PathMatcher pathMatcher;
|
||||
|
||||
private final boolean useSuffixPatternMatch;
|
||||
|
||||
private final boolean useTrailingSlashMatch;
|
||||
|
||||
private final Set<String> fileExtensions = new HashSet<>();
|
||||
private final List<PathPattern> patterns;
|
||||
|
||||
private final PathPatternParser parser;
|
||||
|
||||
/**
|
||||
* Creates a new instance with the given URL patterns.
|
||||
* Each pattern that is not empty and does not start with "/" is prepended with "/".
|
||||
* 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(asList(patterns), null, true, true, null);
|
||||
this(asList(patterns), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 pathMatcher for pattern path matching
|
||||
* @param useSuffixPatternMatch whether to enable matching by suffix (".*")
|
||||
* @param useTrailingSlashMatch whether to match irrespective of a trailing slash
|
||||
* @param extensions file extensions to consider for path matching
|
||||
* @param patternParser for parsing string patterns
|
||||
*/
|
||||
public PatternsRequestCondition(String[] patterns, PathMatcher pathMatcher,
|
||||
boolean useSuffixPatternMatch, boolean useTrailingSlashMatch,
|
||||
Set<String> extensions) {
|
||||
public PatternsRequestCondition(String[] patterns, PathPatternParser patternParser) {
|
||||
|
||||
this(asList(patterns), pathMatcher, useSuffixPatternMatch, useTrailingSlashMatch, extensions);
|
||||
this(asList(patterns), patternParser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Private constructor accepting a collection of patterns.
|
||||
* Private constructor accepting a collection of raw patterns.
|
||||
*/
|
||||
private PatternsRequestCondition(Collection<String> patterns, PathMatcher pathMatcher,
|
||||
boolean useSuffixPatternMatch, boolean useTrailingSlashMatch,
|
||||
Set<String> fileExtensions) {
|
||||
|
||||
this.patterns = Collections.unmodifiableSet(prependLeadingSlash(patterns));
|
||||
this.pathMatcher = (pathMatcher != null ? pathMatcher : new ParsingPathMatcher());
|
||||
this.useSuffixPatternMatch = useSuffixPatternMatch;
|
||||
this.useTrailingSlashMatch = useTrailingSlashMatch;
|
||||
if (fileExtensions != null) {
|
||||
for (String fileExtension : fileExtensions) {
|
||||
if (fileExtension.charAt(0) != '.') {
|
||||
fileExtension = "." + fileExtension;
|
||||
}
|
||||
this.fileExtensions.add(fileExtension);
|
||||
private PatternsRequestCondition(Collection<String> patterns, PathPatternParser patternParser) {
|
||||
this.parser = patternParser != null ? patternParser : new PathPatternParser();
|
||||
this.patterns = new ArrayList<>();
|
||||
patterns.forEach(pattern -> {
|
||||
if (StringUtils.hasText(pattern) && !pattern.startsWith("/")) {
|
||||
pattern = "/" + pattern;
|
||||
}
|
||||
}
|
||||
this.patterns.add(this.parser.parse(pattern));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Private constructor accepting a list of path patterns.
|
||||
*/
|
||||
private PatternsRequestCondition(List<PathPattern> patterns, PathPatternParser patternParser) {
|
||||
this.patterns = patterns;
|
||||
this.parser = patternParser;
|
||||
}
|
||||
|
||||
|
||||
@@ -103,26 +95,17 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
|
||||
return (patterns != null ? Arrays.asList(patterns) : Collections.emptyList());
|
||||
}
|
||||
|
||||
private static Set<String> prependLeadingSlash(Collection<String> patterns) {
|
||||
if (patterns == null) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
Set<String> result = new LinkedHashSet<>(patterns.size());
|
||||
for (String pattern : patterns) {
|
||||
if (StringUtils.hasLength(pattern) && !pattern.startsWith("/")) {
|
||||
pattern = "/" + pattern;
|
||||
}
|
||||
result.add(pattern);
|
||||
}
|
||||
return result;
|
||||
public Set<PathPattern> getPatterns() {
|
||||
return new TreeSet<>(this.patterns);
|
||||
}
|
||||
|
||||
public Set<String> getPatterns() {
|
||||
return this.patterns;
|
||||
public Set<String> getPatternStrings() {
|
||||
return this.patterns.stream()
|
||||
.map(PathPattern::toString).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Collection<String> getContent() {
|
||||
protected Collection<PathPattern> getContent() {
|
||||
return this.patterns;
|
||||
}
|
||||
|
||||
@@ -136,62 +119,54 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
|
||||
* the "other" instance as follows:
|
||||
* <ul>
|
||||
* <li>If there are patterns in both instances, combine the patterns in "this" with
|
||||
* the patterns in "other" using {@link PathMatcher#combine(String, String)}.
|
||||
* the patterns in "other" using {@link PathPattern#combine(String)}.
|
||||
* <li>If only one instance has patterns, use them.
|
||||
* <li>If neither instance has patterns, use an empty String (i.e. "").
|
||||
* </ul>
|
||||
*/
|
||||
@Override
|
||||
public PatternsRequestCondition combine(PatternsRequestCondition other) {
|
||||
Set<String> result = new LinkedHashSet<>();
|
||||
List<PathPattern> combined = new ArrayList<>();
|
||||
if (!this.patterns.isEmpty() && !other.patterns.isEmpty()) {
|
||||
for (String pattern1 : this.patterns) {
|
||||
for (String pattern2 : other.patterns) {
|
||||
result.add(this.pathMatcher.combine(pattern1, pattern2));
|
||||
for (PathPattern pattern1 : this.patterns) {
|
||||
for (PathPattern pattern2 : other.patterns) {
|
||||
String combinedPattern = pattern1.combine(pattern2.getPatternString());
|
||||
combined.add(this.parser.parse(combinedPattern));
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (!this.patterns.isEmpty()) {
|
||||
result.addAll(this.patterns);
|
||||
combined.addAll(this.patterns);
|
||||
}
|
||||
else if (!other.patterns.isEmpty()) {
|
||||
result.addAll(other.patterns);
|
||||
combined.addAll(other.patterns);
|
||||
}
|
||||
else {
|
||||
result.add("");
|
||||
combined.add(this.parser.parse(""));
|
||||
}
|
||||
return new PatternsRequestCondition(result, this.pathMatcher, this.useSuffixPatternMatch,
|
||||
this.useTrailingSlashMatch, this.fileExtensions);
|
||||
|
||||
return new PatternsRequestCondition(combined, this.parser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if any of the patterns match the given request and returns an instance
|
||||
* that is guaranteed to contain matching patterns, sorted via
|
||||
* {@link PathMatcher#getPatternComparator(String)}.
|
||||
* <p>A matching pattern is obtained by making checks in the following order:
|
||||
* <ul>
|
||||
* <li>Direct match
|
||||
* <li>Pattern match with ".*" appended if the pattern doesn't already contain a "."
|
||||
* <li>Pattern match
|
||||
* <li>Pattern match with "/" appended if the pattern doesn't already end in "/"
|
||||
* </ul>
|
||||
* that is guaranteed to contain matching patterns, sorted with a
|
||||
* {@link PathPatternComparator}.
|
||||
* @param exchange the current exchange
|
||||
* @return the same instance if the condition contains no patterns;
|
||||
* or a new condition with sorted matching patterns;
|
||||
* or {@code null} if no patterns match.
|
||||
*/
|
||||
@Override
|
||||
@Nullable
|
||||
public PatternsRequestCondition getMatchingCondition(ServerWebExchange exchange) {
|
||||
if (this.patterns.isEmpty()) {
|
||||
return this;
|
||||
}
|
||||
|
||||
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
|
||||
List<String> matches = getMatchingPatterns(lookupPath);
|
||||
|
||||
return matches.isEmpty() ? null :
|
||||
new PatternsRequestCondition(matches, this.pathMatcher, this.useSuffixPatternMatch,
|
||||
this.useTrailingSlashMatch, this.fileExtensions);
|
||||
SortedSet<PathPattern> matches = getMatchingPatterns(lookupPath);
|
||||
return matches.isEmpty() ? null : new PatternsRequestCondition(new ArrayList<>(matches), this.parser);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -201,39 +176,19 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
|
||||
* This method is provided as an alternative to be used if no request is available
|
||||
* (e.g. introspection, tooling, etc).
|
||||
* @param lookupPath the lookup path to match to existing patterns
|
||||
* @return a collection of matching patterns sorted with the closest match at the top
|
||||
* @return a sorted set of matching patterns sorted with the closest match first
|
||||
*/
|
||||
public List<String> getMatchingPatterns(String lookupPath) {
|
||||
List<String> matches = new ArrayList<>();
|
||||
for (String pattern : this.patterns) {
|
||||
String match = getMatchingPattern(pattern, lookupPath);
|
||||
if (match != null) {
|
||||
matches.add(match);
|
||||
}
|
||||
}
|
||||
Collections.sort(matches, this.pathMatcher.getPatternComparator(lookupPath));
|
||||
return matches;
|
||||
}
|
||||
|
||||
private String getMatchingPattern(String pattern, String lookupPath) {
|
||||
if (pattern.equals(lookupPath)) {
|
||||
return pattern;
|
||||
}
|
||||
if (this.pathMatcher.match(pattern, lookupPath)) {
|
||||
return pattern;
|
||||
}
|
||||
if (this.useTrailingSlashMatch) {
|
||||
if (!pattern.endsWith("/") && this.pathMatcher.match(pattern + "/", lookupPath)) {
|
||||
return pattern +"/";
|
||||
}
|
||||
}
|
||||
return null;
|
||||
public SortedSet<PathPattern> getMatchingPatterns(String lookupPath) {
|
||||
return patterns.stream()
|
||||
.filter(pattern -> pattern.matches(lookupPath))
|
||||
.collect(Collectors.toCollection(() ->
|
||||
new TreeSet<>(new PathPatternComparator(lookupPath))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare the two conditions based on the URL patterns they contain.
|
||||
* Patterns are compared one at a time, from top to bottom via
|
||||
* {@link PathMatcher#getPatternComparator(String)}. If all compared
|
||||
* {@link PathPatternComparator}. If all compared
|
||||
* patterns match equally, but one instance has more patterns, it is
|
||||
* considered a closer match.
|
||||
* <p>It is assumed that both instances have been obtained via
|
||||
@@ -244,11 +199,13 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
|
||||
@Override
|
||||
public int compareTo(PatternsRequestCondition other, ServerWebExchange exchange) {
|
||||
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
|
||||
Comparator<String> patternComparator = this.pathMatcher.getPatternComparator(lookupPath);
|
||||
Iterator<String> iterator = this.patterns.iterator();
|
||||
Iterator<String> iteratorOther = other.patterns.iterator();
|
||||
PathPatternComparator comparator = new PathPatternComparator(lookupPath);
|
||||
Iterator<PathPattern> iterator = this.patterns.stream()
|
||||
.sorted(comparator).collect(Collectors.toList()).iterator();
|
||||
Iterator<PathPattern> iteratorOther = other.getPatterns().stream()
|
||||
.sorted(comparator).collect(Collectors.toList()).iterator();
|
||||
while (iterator.hasNext() && iteratorOther.hasNext()) {
|
||||
int result = patternComparator.compare(iterator.next(), iteratorOther.next());
|
||||
int result = comparator.compare(iterator.next(), iteratorOther.next());
|
||||
if (result != 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -37,8 +37,6 @@ import org.springframework.core.MethodIntrospector;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.cors.reactive.CorsUtils;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
@@ -294,7 +292,7 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
|
||||
* @param lookupPath the lookup path within the current mapping
|
||||
* @param exchange the current exchange
|
||||
* @return the best-matching handler method, or {@code null} if no match
|
||||
* @see #handleMatch(Object, String, ServerWebExchange)
|
||||
* @see #handleMatch(Object, String, ServerWebExchange)
|
||||
* @see #handleNoMatch(Set, String, ServerWebExchange)
|
||||
*/
|
||||
@Nullable
|
||||
@@ -302,14 +300,7 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
|
||||
throws Exception {
|
||||
|
||||
List<Match> matches = new ArrayList<>();
|
||||
List<T> directPathMatches = this.mappingRegistry.getMappingsByUrl(lookupPath);
|
||||
if (directPathMatches != null) {
|
||||
addMatchingMappings(directPathMatches, matches, exchange);
|
||||
}
|
||||
if (matches.isEmpty()) {
|
||||
// No choice but to go through all mappings...
|
||||
addMatchingMappings(this.mappingRegistry.getMappings().keySet(), matches, exchange);
|
||||
}
|
||||
addMatchingMappings(this.mappingRegistry.getMappings().keySet(), matches, exchange);
|
||||
|
||||
if (!matches.isEmpty()) {
|
||||
Comparator<Match> comparator = new MatchComparator(getMappingComparator(exchange));
|
||||
@@ -443,8 +434,6 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
|
||||
|
||||
private final Map<T, HandlerMethod> mappingLookup = new LinkedHashMap<>();
|
||||
|
||||
private final MultiValueMap<String, T> urlLookup = new LinkedMultiValueMap<>();
|
||||
|
||||
private final Map<HandlerMethod, CorsConfiguration> corsLookup = new ConcurrentHashMap<>();
|
||||
|
||||
private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
|
||||
@@ -457,15 +446,6 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
|
||||
return this.mappingLookup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return matches for the given URL path. Not thread-safe.
|
||||
* @see #acquireReadLock()
|
||||
*/
|
||||
@Nullable
|
||||
public List<T> getMappingsByUrl(String urlPath) {
|
||||
return this.urlLookup.get(urlPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return CORS configuration. Thread-safe for concurrent use.
|
||||
*/
|
||||
@@ -499,17 +479,12 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
|
||||
}
|
||||
this.mappingLookup.put(mapping, handlerMethod);
|
||||
|
||||
List<String> directUrls = getDirectUrls(mapping);
|
||||
for (String url : directUrls) {
|
||||
this.urlLookup.add(url, mapping);
|
||||
}
|
||||
|
||||
CorsConfiguration corsConfig = initCorsConfiguration(handler, method, mapping);
|
||||
if (corsConfig != null) {
|
||||
this.corsLookup.put(handlerMethod, corsConfig);
|
||||
}
|
||||
|
||||
this.registry.put(mapping, new MappingRegistration<>(mapping, handlerMethod, directUrls));
|
||||
this.registry.put(mapping, new MappingRegistration<>(mapping, handlerMethod));
|
||||
}
|
||||
finally {
|
||||
this.readWriteLock.writeLock().unlock();
|
||||
@@ -520,22 +495,12 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
|
||||
HandlerMethod handlerMethod = this.mappingLookup.get(mapping);
|
||||
if (handlerMethod != null && !handlerMethod.equals(newHandlerMethod)) {
|
||||
throw new IllegalStateException(
|
||||
"Ambiguous mapping. Cannot map '" + newHandlerMethod.getBean() + "' method \n" +
|
||||
newHandlerMethod + "\nto " + mapping + ": There is already '" +
|
||||
handlerMethod.getBean() + "' bean method\n" + handlerMethod + " mapped.");
|
||||
"Ambiguous mapping. Cannot map '" + newHandlerMethod.getBean() + "' method \n" +
|
||||
newHandlerMethod + "\nto " + mapping + ": There is already '" +
|
||||
handlerMethod.getBean() + "' bean method\n" + handlerMethod + " mapped.");
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> getDirectUrls(T mapping) {
|
||||
List<String> urls = new ArrayList<>(1);
|
||||
for (String path : getMappingPathPatterns(mapping)) {
|
||||
if (!getPathMatcher().isPattern(path)) {
|
||||
urls.add(path);
|
||||
}
|
||||
}
|
||||
return urls;
|
||||
}
|
||||
|
||||
public void unregister(T mapping) {
|
||||
this.readWriteLock.writeLock().lock();
|
||||
try {
|
||||
@@ -545,16 +510,6 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
|
||||
}
|
||||
|
||||
this.mappingLookup.remove(definition.getMapping());
|
||||
|
||||
for (String url : definition.getDirectUrls()) {
|
||||
List<T> list = this.urlLookup.get(url);
|
||||
if (list != null) {
|
||||
list.remove(definition.getMapping());
|
||||
if (list.isEmpty()) {
|
||||
this.urlLookup.remove(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.corsLookup.remove(definition.getHandlerMethod());
|
||||
}
|
||||
finally {
|
||||
@@ -570,14 +525,11 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
|
||||
|
||||
private final HandlerMethod handlerMethod;
|
||||
|
||||
private final List<String> directUrls;
|
||||
|
||||
public MappingRegistration(T mapping, HandlerMethod handlerMethod, @Nullable List<String> directUrls) {
|
||||
public MappingRegistration(T mapping, HandlerMethod handlerMethod) {
|
||||
Assert.notNull(mapping, "Mapping must not be null");
|
||||
Assert.notNull(handlerMethod, "HandlerMethod must not be null");
|
||||
this.mapping = mapping;
|
||||
this.handlerMethod = handlerMethod;
|
||||
this.directUrls = (directUrls != null ? directUrls : Collections.emptyList());
|
||||
}
|
||||
|
||||
public T getMapping() {
|
||||
@@ -588,9 +540,6 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
|
||||
return this.handlerMethod;
|
||||
}
|
||||
|
||||
public List<String> getDirectUrls() {
|
||||
return this.directUrls;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -16,10 +16,7 @@
|
||||
|
||||
package org.springframework.web.reactive.result.method;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
|
||||
@@ -32,6 +29,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.PathPatternParser;
|
||||
|
||||
/**
|
||||
* Encapsulates the following request mapping conditions:
|
||||
@@ -473,8 +471,7 @@ public final class RequestMappingInfo implements RequestCondition<RequestMapping
|
||||
RequestedContentTypeResolver contentTypeResolver = this.options.getContentTypeResolver();
|
||||
|
||||
PatternsRequestCondition patternsCondition = new PatternsRequestCondition(
|
||||
this.paths, this.options.getPathMatcher(), this.options.useSuffixPatternMatch(),
|
||||
this.options.useTrailingSlashMatch(), this.options.getFileExtensions());
|
||||
this.paths, this.options.getPatternParser());
|
||||
|
||||
return new RequestMappingInfo(this.mappingName, patternsCondition,
|
||||
new RequestMethodsRequestCondition(methods),
|
||||
@@ -495,77 +492,17 @@ public final class RequestMappingInfo implements RequestCondition<RequestMapping
|
||||
*/
|
||||
public static class BuilderConfiguration {
|
||||
|
||||
private PathMatcher pathMatcher;
|
||||
|
||||
private boolean trailingSlashMatch = true;
|
||||
|
||||
private boolean suffixPatternMatch = true;
|
||||
|
||||
private boolean registeredSuffixPatternMatch = false;
|
||||
private PathPatternParser patternParser;
|
||||
|
||||
private RequestedContentTypeResolver contentTypeResolver;
|
||||
|
||||
/**
|
||||
* Set a custom PathMatcher to use for the PatternsRequestCondition.
|
||||
* <p>By default this is not set.
|
||||
*/
|
||||
public void setPathMatcher(PathMatcher pathMatcher) {
|
||||
this.pathMatcher = pathMatcher;
|
||||
}
|
||||
|
||||
public PathMatcher getPathMatcher() {
|
||||
return this.pathMatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to apply trailing slash matching in PatternsRequestCondition.
|
||||
* <p>By default this is set to 'true'.
|
||||
*/
|
||||
public void setTrailingSlashMatch(boolean trailingSlashMatch) {
|
||||
this.trailingSlashMatch = trailingSlashMatch;
|
||||
}
|
||||
|
||||
public boolean useTrailingSlashMatch() {
|
||||
return this.trailingSlashMatch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to apply suffix pattern matching in PatternsRequestCondition.
|
||||
* <p>By default this is set to 'true'.
|
||||
* @see #setRegisteredSuffixPatternMatch(boolean)
|
||||
*/
|
||||
public void setSuffixPatternMatch(boolean suffixPatternMatch) {
|
||||
this.suffixPatternMatch = suffixPatternMatch;
|
||||
}
|
||||
|
||||
public boolean useSuffixPatternMatch() {
|
||||
return this.suffixPatternMatch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether suffix pattern matching should be restricted to registered
|
||||
* file extensions only. Setting this property also sets
|
||||
* suffixPatternMatch=true and requires that a
|
||||
* {@link #setContentTypeResolver} is also configured in order to
|
||||
* obtain the registered file extensions.
|
||||
*/
|
||||
public void setRegisteredSuffixPatternMatch(boolean registeredSuffixPatternMatch) {
|
||||
this.registeredSuffixPatternMatch = registeredSuffixPatternMatch;
|
||||
this.suffixPatternMatch = (registeredSuffixPatternMatch || this.suffixPatternMatch);
|
||||
}
|
||||
|
||||
public boolean useRegisteredSuffixPatternMatch() {
|
||||
return this.registeredSuffixPatternMatch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the file extensions to use for suffix pattern matching. If
|
||||
* {@code registeredSuffixPatternMatch=true}, the extensions are obtained
|
||||
* from the configured {@code contentTypeResolver}.
|
||||
*/
|
||||
@Nullable
|
||||
public Set<String> getFileExtensions() {
|
||||
return null;
|
||||
public PathPatternParser getPatternParser() {
|
||||
return this.patternParser;
|
||||
}
|
||||
|
||||
public void setPatternParser(PathPatternParser patternParser) {
|
||||
this.patternParser = patternParser;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -47,6 +47,7 @@ import org.springframework.web.server.NotAcceptableStatusException;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.ServerWebInputException;
|
||||
import org.springframework.web.server.UnsupportedMediaTypeStatusException;
|
||||
import org.springframework.web.util.pattern.PathPattern;
|
||||
|
||||
|
||||
/**
|
||||
@@ -76,7 +77,7 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
|
||||
*/
|
||||
@Override
|
||||
protected Set<String> getMappingPathPatterns(RequestMappingInfo info) {
|
||||
return info.getPatternsCondition().getPatterns();
|
||||
return info.getPatternsCondition().getPatternStrings();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,17 +109,17 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
|
||||
protected void handleMatch(RequestMappingInfo info, String lookupPath, ServerWebExchange exchange) {
|
||||
super.handleMatch(info, lookupPath, exchange);
|
||||
|
||||
String bestPattern;
|
||||
PathPattern bestPattern;
|
||||
Map<String, String> uriVariables;
|
||||
|
||||
Set<String> patterns = info.getPatternsCondition().getPatterns();
|
||||
Set<PathPattern> patterns = info.getPatternsCondition().getPatterns();
|
||||
if (patterns.isEmpty()) {
|
||||
bestPattern = lookupPath;
|
||||
bestPattern = getPathPatternParser().parse(lookupPath);
|
||||
uriVariables = Collections.emptyMap();
|
||||
}
|
||||
else {
|
||||
bestPattern = patterns.iterator().next();
|
||||
uriVariables = getPathMatcher().extractUriTemplateVariables(bestPattern, lookupPath);
|
||||
uriVariables = bestPattern.matchAndExtract(lookupPath);
|
||||
}
|
||||
|
||||
// Let URI vars be stripped of semicolon content..
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.web.reactive.result.method.annotation;
|
||||
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.context.EmbeddedValueResolverAware;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
@@ -49,12 +48,6 @@ import org.springframework.web.reactive.result.method.RequestMappingInfoHandlerM
|
||||
public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMapping
|
||||
implements EmbeddedValueResolverAware {
|
||||
|
||||
private boolean useSuffixPatternMatch = true;
|
||||
|
||||
private boolean useRegisteredSuffixPatternMatch = true;
|
||||
|
||||
private boolean useTrailingSlashMatch = true;
|
||||
|
||||
private RequestedContentTypeResolver contentTypeResolver = new RequestedContentTypeResolverBuilder().build();
|
||||
|
||||
private StringValueResolver embeddedValueResolver;
|
||||
@@ -62,40 +55,6 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
|
||||
private RequestMappingInfo.BuilderConfiguration config = new RequestMappingInfo.BuilderConfiguration();
|
||||
|
||||
|
||||
/**
|
||||
* Whether to use suffix pattern matching. If enabled a method mapped to
|
||||
* "/path" also matches to "/path.*".
|
||||
* <p>The default value is {@code true}.
|
||||
* <p><strong>Note:</strong> when using suffix pattern matching it's usually
|
||||
* preferable to be explicit about what is and isn't an extension so rather
|
||||
* than setting this property consider using
|
||||
* {@link #setUseRegisteredSuffixPatternMatch} instead.
|
||||
*/
|
||||
public void setUseSuffixPatternMatch(boolean useSuffixPatternMatch) {
|
||||
this.useSuffixPatternMatch = useSuffixPatternMatch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether suffix pattern matching should work only against path extensions
|
||||
* explicitly registered with the configured {@link RequestedContentTypeResolver}. This
|
||||
* is generally recommended to reduce ambiguity and to avoid issues such as
|
||||
* when a "." appears in the path for other reasons.
|
||||
* <p>By default this is set to "true".
|
||||
*/
|
||||
public void setUseRegisteredSuffixPatternMatch(boolean useRegisteredSuffixPatternMatch) {
|
||||
this.useRegisteredSuffixPatternMatch = useRegisteredSuffixPatternMatch;
|
||||
this.useSuffixPatternMatch = (useRegisteredSuffixPatternMatch || this.useSuffixPatternMatch);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to match to URLs irrespective of the presence of a trailing slash.
|
||||
* If enabled a method mapped to "/users" also matches to "/users/".
|
||||
* <p>The default value is {@code true}.
|
||||
*/
|
||||
public void setUseTrailingSlashMatch(boolean useTrailingSlashMatch) {
|
||||
this.useTrailingSlashMatch = useTrailingSlashMatch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link RequestedContentTypeResolver} to use to determine requested media types.
|
||||
* If not set, the default constructor is used.
|
||||
@@ -113,36 +72,13 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
this.config = new RequestMappingInfo.BuilderConfiguration();
|
||||
this.config.setPathMatcher(getPathMatcher());
|
||||
this.config.setSuffixPatternMatch(this.useSuffixPatternMatch);
|
||||
this.config.setRegisteredSuffixPatternMatch(this.useRegisteredSuffixPatternMatch);
|
||||
this.config.setPatternParser(getPathPatternParser());
|
||||
this.config.setContentTypeResolver(getContentTypeResolver());
|
||||
|
||||
super.afterPropertiesSet();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Whether to use suffix pattern matching.
|
||||
*/
|
||||
public boolean useSuffixPatternMatch() {
|
||||
return this.useSuffixPatternMatch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to use registered suffixes for pattern matching.
|
||||
*/
|
||||
public boolean useRegisteredSuffixPatternMatch() {
|
||||
return this.useRegisteredSuffixPatternMatch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to match to URLs irrespective of the presence of a trailing slash.
|
||||
*/
|
||||
public boolean useTrailingSlashMatch() {
|
||||
return this.useTrailingSlashMatch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the configured {@link RequestedContentTypeResolver}.
|
||||
*/
|
||||
@@ -150,14 +86,6 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
|
||||
return this.contentTypeResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the file extensions to use for suffix pattern matching.
|
||||
*/
|
||||
@Nullable
|
||||
public Set<String> getFileExtensions() {
|
||||
return this.config.getFileExtensions();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* Expects a handler to have a type-level @{@link Controller} annotation.
|
||||
|
||||
Reference in New Issue
Block a user