SPR-8247
This commit is contained in:
@@ -30,27 +30,18 @@ import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.context.ApplicationContextException;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.ReflectionUtils.MethodFilter;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.method.HandlerMethodSelector;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
/**
|
||||
* Abstract base class for {@link org.springframework.web.servlet.HandlerMapping HandlerMapping} implementations that
|
||||
* support mapping requests to {@link HandlerMethod}s rather than to handlers.
|
||||
*
|
||||
* <p>Each {@link HandlerMethod} is registered with a unique key. Subclasses define the key type and how to create it
|
||||
* for a given handler method. Keys represent conditions for matching a handler method to a request.
|
||||
*
|
||||
* <p>Subclasses must also define how to create a key for an incoming request. The resulting key is used to perform
|
||||
* a {@link HandlerMethod} lookup possibly resulting in a direct match. However, when a map lookup is insufficient,
|
||||
* the keys of all handler methods are iterated and subclasses are allowed to make an exhaustive check of key
|
||||
* conditions against the request.
|
||||
*
|
||||
* <p>Since there can be more than one matching key for a request, subclasses must define a comparator for sorting
|
||||
* the keys of matching handler methods in order to find the most specific match.
|
||||
*
|
||||
* @param <T> A unique key for the registration of mapped {@link HandlerMethod}s representing the conditions to
|
||||
* match a handler method to a request.
|
||||
* @param <T> Represents a mapping key with conditions for mapping a request to a {@link HandlerMethod}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
@@ -58,8 +49,52 @@ import org.springframework.web.method.HandlerMethodSelector;
|
||||
*/
|
||||
public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMapping {
|
||||
|
||||
private UrlPathHelper urlPathHelper = new UrlPathHelper();
|
||||
|
||||
private final MultiValueMap<String, T> urlMap = new LinkedMultiValueMap<String, T>();
|
||||
|
||||
private final Map<T, HandlerMethod> handlerMethods = new LinkedHashMap<T, HandlerMethod>();
|
||||
|
||||
/**
|
||||
* Set if URL lookup should always use the full path within the current servlet context. Else, the path within the
|
||||
* current servlet mapping is used if applicable (that is, in the case of a ".../*" servlet mapping in web.xml).
|
||||
* <p>Default is "false".
|
||||
*
|
||||
* @see org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath
|
||||
*/
|
||||
public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
|
||||
this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if context path and request URI should be URL-decoded. Both are returned <i>undecoded</i> by the Servlet API, in
|
||||
* contrast to the servlet path. <p>Uses either the request encoding or the default encoding according to the Servlet
|
||||
* spec (ISO-8859-1).
|
||||
*
|
||||
* @see org.springframework.web.util.UrlPathHelper#setUrlDecode
|
||||
*/
|
||||
public void setUrlDecode(boolean urlDecode) {
|
||||
this.urlPathHelper.setUrlDecode(urlDecode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the UrlPathHelper to use for resolution of lookup paths. <p>Use this to override the default UrlPathHelper
|
||||
* with a custom subclass, or to share common UrlPathHelper settings across multiple HandlerMappings and
|
||||
* MethodNameResolvers.
|
||||
*
|
||||
*/
|
||||
public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
|
||||
Assert.notNull(urlPathHelper, "UrlPathHelper must not be null");
|
||||
this.urlPathHelper = urlPathHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link UrlPathHelper} to use for resolution of lookup paths.
|
||||
*/
|
||||
public UrlPathHelper getUrlPathHelper() {
|
||||
return urlPathHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the initialization of the superclass and detects handlers.
|
||||
*/
|
||||
@@ -71,14 +106,12 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
|
||||
|
||||
/**
|
||||
* Register handler methods found in beans of the current ApplicationContext.
|
||||
* <p>The actual key determination for a handler is up to the concrete
|
||||
* {@link #getKeyForMethod(Method)} implementation. A method in a bean for which no key
|
||||
* could be determined is simply not considered a handler method.
|
||||
* @see #getKeyForMethod(Method)
|
||||
* <p>The actual mapping for a handler is up to the concrete {@link #getMappingKeyForMethod(String, Method)}
|
||||
* implementation.
|
||||
*/
|
||||
protected void initHandlerMethods() {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Looking for URL mappings in application context: " + getApplicationContext());
|
||||
logger.debug("Looking for request mappings in application context: " + getApplicationContext());
|
||||
}
|
||||
for (String beanName : getApplicationContext().getBeanNamesForType(Object.class)) {
|
||||
if (isHandler(beanName)){
|
||||
@@ -102,81 +135,79 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
|
||||
|
||||
Set<Method> methods = HandlerMethodSelector.selectMethods(handlerType, new MethodFilter() {
|
||||
public boolean matches(Method method) {
|
||||
return getKeyForMethod(beanName, method) != null;
|
||||
return getMappingKeyForMethod(beanName, method) != null;
|
||||
}
|
||||
});
|
||||
for (Method method : methods) {
|
||||
T key = getKeyForMethod(beanName, method);
|
||||
HandlerMethod handlerMethod = new HandlerMethod(beanName, getApplicationContext(), method);
|
||||
registerHandlerMethod(key, handlerMethod);
|
||||
T mapping = getMappingKeyForMethod(beanName, method);
|
||||
Set<String> paths = getMappingPaths(mapping);
|
||||
registerHandlerMethod(paths, mapping, handlerMethod);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a lookup key for the given bean method. A method for which no key can be determined is
|
||||
* Provides a mapping key for the given bean method. A method for which no mapping can be determined is
|
||||
* not considered a handler method.
|
||||
*
|
||||
* @param beanName the name of the bean the method belongs to
|
||||
* @param method the method to create a key for
|
||||
* @return the lookup key, or {@code null} if the method has none
|
||||
* @param method the method to create a mapping for
|
||||
* @return the mapping key, or {@code null} if the method is not mapped
|
||||
*/
|
||||
protected abstract T getKeyForMethod(String beanName, Method method);
|
||||
protected abstract T getMappingKeyForMethod(String beanName, Method method);
|
||||
|
||||
/**
|
||||
* Registers a {@link HandlerMethod} under the given key.
|
||||
*
|
||||
* @param key the key to register the method under
|
||||
* Registers a {@link HandlerMethod} with the given mapping.
|
||||
*
|
||||
* @param paths URL paths mapped to this method
|
||||
* @param mappingKey the mapping key for the method
|
||||
* @param handlerMethod the handler method to register
|
||||
* @throws IllegalStateException if another method was already register under the key
|
||||
* @throws IllegalStateException if another method was already register under the same mapping
|
||||
*/
|
||||
protected void registerHandlerMethod(T key, HandlerMethod handlerMethod) {
|
||||
Assert.notNull(key, "'key' must not be null");
|
||||
protected void registerHandlerMethod(Set<String> paths, T mappingKey, HandlerMethod handlerMethod) {
|
||||
Assert.notNull(mappingKey, "'mapping' must not be null");
|
||||
Assert.notNull(handlerMethod, "'handlerMethod' must not be null");
|
||||
HandlerMethod mappedHandlerMethod = handlerMethods.get(key);
|
||||
HandlerMethod mappedHandlerMethod = handlerMethods.get(mappingKey);
|
||||
if (mappedHandlerMethod != null && !mappedHandlerMethod.equals(handlerMethod)) {
|
||||
throw new IllegalStateException("Ambiguous mapping found. Cannot map '" + handlerMethod.getBean()
|
||||
+ "' bean method \n" + handlerMethod + "\nto " + key + ": There is already '"
|
||||
+ "' bean method \n" + handlerMethod + "\nto " + mappingKey + ": There is already '"
|
||||
+ mappedHandlerMethod.getBean() + "' bean method\n" + mappedHandlerMethod + " mapped.");
|
||||
}
|
||||
handlerMethods.put(key, handlerMethod);
|
||||
handlerMethods.put(mappingKey, handlerMethod);
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Mapped \"" + key + "\" onto " + handlerMethod);
|
||||
logger.info("Mapped \"" + mappingKey + "\" onto " + handlerMethod);
|
||||
}
|
||||
for (String path : paths) {
|
||||
urlMap.add(path, mappingKey);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL paths for the given mapping.
|
||||
*/
|
||||
protected abstract Set<String> getMappingPaths(T mappingKey);
|
||||
|
||||
@Override
|
||||
protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
|
||||
T key = getKeyForRequest(request);
|
||||
if (key == null) {
|
||||
return null;
|
||||
}
|
||||
String lookupPath = urlPathHelper.getLookupPathForRequest(request);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Looking up handler method with key [" + key + "]");
|
||||
logger.debug("Looking up handler method for path " + lookupPath);
|
||||
}
|
||||
|
||||
HandlerMethod handlerMethod = lookupHandlerMethod(key, request);
|
||||
HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request);
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
if (handlerMethod != null) {
|
||||
logger.debug("Returning handler method [" + handlerMethod + "]");
|
||||
}
|
||||
else {
|
||||
logger.debug("Did not find handler method for [" + key + "]");
|
||||
logger.debug("Did not find handler method for [" + lookupPath + "]");
|
||||
}
|
||||
}
|
||||
|
||||
return (handlerMethod != null) ? handlerMethod.createWithResolvedBean() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract template method that returns the lookup key for the given HTTP servlet request.
|
||||
*
|
||||
* @param request the request to look up the key for
|
||||
* @return the key, or {@code null} if the request does not have one
|
||||
* @throws Exception in case of errors
|
||||
*/
|
||||
protected abstract T getKeyForRequest(HttpServletRequest request) throws Exception;
|
||||
|
||||
/**
|
||||
* Looks up the best-matching {@link HandlerMethod} for the given request.
|
||||
*
|
||||
@@ -185,119 +216,126 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
|
||||
* returns the 1st entry, if any. If no matches are found, {@link #handleNoMatch(Set, HttpServletRequest)} is
|
||||
* invoked.
|
||||
*
|
||||
* @param lookupKey current lookup key
|
||||
* @param lookupPath mapping lookup path within the current servlet mapping if applicable
|
||||
* @param request the current HTTP servlet request
|
||||
* @return the best-matching handler method, or {@code null} if there is no match
|
||||
*/
|
||||
protected HandlerMethod lookupHandlerMethod(T lookupKey, HttpServletRequest request) throws Exception {
|
||||
if (handlerMethods.containsKey(lookupKey)) {
|
||||
protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {
|
||||
List<T> keys = urlMap.get(lookupPath);
|
||||
if (keys == null) {
|
||||
keys = new ArrayList<T>(handlerMethods.keySet());
|
||||
}
|
||||
|
||||
List<Match> matches = new ArrayList<Match>();
|
||||
|
||||
for (T key : keys) {
|
||||
T match = getMatchingMappingKey(key, lookupPath, request);
|
||||
if (match != null) {
|
||||
matches.add(new Match(match, handlerMethods.get(key)));
|
||||
}
|
||||
}
|
||||
|
||||
if (!matches.isEmpty()) {
|
||||
Comparator<Match> comparator = new MatchComparator(getMappingKeyComparator(lookupPath, request));
|
||||
Collections.sort(matches, comparator);
|
||||
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Found direct match for [" + lookupKey + "]");
|
||||
logger.trace("Found " + matches.size() + " matching mapping(s) for [" + lookupPath + "] : " + matches);
|
||||
}
|
||||
|
||||
handleMatch(lookupKey, request);
|
||||
return handlerMethods.get(lookupKey);
|
||||
Match bestMatch = matches.get(0);
|
||||
if (matches.size() > 1) {
|
||||
Match secondBestMatch = matches.get(1);
|
||||
if (comparator.compare(bestMatch, secondBestMatch) == 0) {
|
||||
Method m1 = bestMatch.handlerMethod.getMethod();
|
||||
Method m2 = secondBestMatch.handlerMethod.getMethod();
|
||||
throw new IllegalStateException(
|
||||
"Ambiguous handler methods mapped for HTTP path '" + request.getRequestURL() + "': {" +
|
||||
m1 + ", " + m2 + "}");
|
||||
}
|
||||
}
|
||||
|
||||
handleMatch(bestMatch.mappingKey, lookupPath, request);
|
||||
return bestMatch.handlerMethod;
|
||||
}
|
||||
else {
|
||||
List<Match> matches = new ArrayList<Match>();
|
||||
|
||||
for (Map.Entry<T, HandlerMethod> entry : handlerMethods.entrySet()) {
|
||||
T match = getMatchingKey(entry.getKey(), request);
|
||||
if (match != null) {
|
||||
matches.add(new Match(match, entry.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
if (!matches.isEmpty()) {
|
||||
Comparator<Match> comparator = getMatchComparator(request);
|
||||
Collections.sort(matches, comparator);
|
||||
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Found " + matches.size() + " matching key(s) for [" + lookupKey + "] : " + matches);
|
||||
}
|
||||
|
||||
Match bestMatch = matches.get(0);
|
||||
if (matches.size() > 1) {
|
||||
Match secondBestMatch = matches.get(1);
|
||||
if (comparator.compare(bestMatch, secondBestMatch) == 0) {
|
||||
Method m1 = bestMatch.handlerMethod.getMethod();
|
||||
Method m2 = secondBestMatch.handlerMethod.getMethod();
|
||||
throw new IllegalStateException(
|
||||
"Ambiguous handler methods mapped for HTTP path '" + request.getRequestURL() + "': {" +
|
||||
m1 + ", " + m2 + "}");
|
||||
}
|
||||
}
|
||||
|
||||
handleMatch(bestMatch.key, request);
|
||||
return bestMatch.handlerMethod;
|
||||
}
|
||||
else {
|
||||
return handleNoMatch(handlerMethods.keySet(), request);
|
||||
}
|
||||
return handleNoMatch(handlerMethods.keySet(), lookupPath, request);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoked when a key matching to a request has been identified.
|
||||
* Invoked when a request has been matched to a mapping.
|
||||
*
|
||||
* @param key the key selected for the request returned by {@link #getMatchingKey(Object, HttpServletRequest)}.
|
||||
* @param mappingKey the mapping selected for the request returned by
|
||||
* {@link #getMatchingMappingKey(Object, String, HttpServletRequest)}.
|
||||
* @param lookupPath mapping lookup path within the current servlet mapping if applicable
|
||||
* @param request the current request
|
||||
*/
|
||||
protected void handleMatch(T key, HttpServletRequest request) {
|
||||
protected void handleMatch(T mappingKey, String lookupPath, HttpServletRequest request) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the matching variant of the given key, given the current HTTP servlet request.
|
||||
* Checks if the mapping matches the current request and returns a mapping updated to contain only conditions
|
||||
* relevant to the current request (for example a mapping may have several HTTP methods, the matching mapping
|
||||
* will contain only 1).
|
||||
*
|
||||
* @param key the key to get the matches for
|
||||
* @param mappingKey the mapping key to get a match for
|
||||
* @param lookupPath mapping lookup path within the current servlet mapping if applicable
|
||||
* @param request the current HTTP servlet request
|
||||
* @return the matching key, or {@code null} if the given key does not match against the servlet request
|
||||
* @return a matching mapping, or {@code null} if the given mapping does not match the request
|
||||
*/
|
||||
protected abstract T getMatchingKey(T key, HttpServletRequest request);
|
||||
|
||||
private Comparator<Match> getMatchComparator(HttpServletRequest request) {
|
||||
final Comparator<T> keyComparator = getKeyComparator(request);
|
||||
return new Comparator<Match>() {
|
||||
public int compare(Match m1, Match m2) {
|
||||
return keyComparator.compare(m1.key, m2.key);
|
||||
}
|
||||
};
|
||||
}
|
||||
protected abstract T getMatchingMappingKey(T mappingKey, String lookupPath, HttpServletRequest request);
|
||||
|
||||
/**
|
||||
* Returns a comparator to sort the keys with. The returned comparator should sort 'better' matches higher.
|
||||
* Returns a comparator to sort mapping keys with. The returned comparator should sort 'better' matches higher.
|
||||
*
|
||||
* @param lookupPath mapping lookup path within the current servlet mapping if applicable
|
||||
* @param request the current HTTP servlet request
|
||||
* @return the comparator
|
||||
*/
|
||||
protected abstract Comparator<T> getKeyComparator(HttpServletRequest request);
|
||||
protected abstract Comparator<T> getMappingKeyComparator(String lookupPath, HttpServletRequest request);
|
||||
|
||||
/**
|
||||
* Invoked when no match was found. Default implementation returns {@code null}.
|
||||
*
|
||||
* @param requestKeys the registered request keys
|
||||
* @param mappingKeys all registered mappings
|
||||
* @param lookupPath mapping lookup path within the current servlet mapping if applicable
|
||||
* @param request the current HTTP request
|
||||
* @throws ServletException in case of errors
|
||||
*/
|
||||
protected HandlerMethod handleNoMatch(Set<T> requestKeys, HttpServletRequest request) throws Exception {
|
||||
protected HandlerMethod handleNoMatch(Set<T> mappingKeys, String lookupPath, HttpServletRequest request)
|
||||
throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
private class Match {
|
||||
|
||||
private final T key;
|
||||
private final T mappingKey;
|
||||
|
||||
private final HandlerMethod handlerMethod;
|
||||
|
||||
private Match(T key, HandlerMethod handlerMethod) {
|
||||
this.key = key;
|
||||
private Match(T mapping, HandlerMethod handlerMethod) {
|
||||
this.mappingKey = mapping;
|
||||
this.handlerMethod = handlerMethod;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return key.toString();
|
||||
return mappingKey.toString();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
private class MatchComparator implements Comparator<Match> {
|
||||
|
||||
private final Comparator<T> comparator;
|
||||
|
||||
public MatchComparator(Comparator<T> comparator) {
|
||||
this.comparator = comparator;
|
||||
}
|
||||
|
||||
public int compare(Match match1, Match match2) {
|
||||
return comparator.compare(match1.mappingKey, match2.mappingKey);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.web.servlet.mvc.method.annotation;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
@@ -42,71 +43,22 @@ import org.springframework.web.servlet.HandlerMapping;
|
||||
import org.springframework.web.servlet.handler.AbstractHandlerMethodMapping;
|
||||
import org.springframework.web.servlet.handler.MappedInterceptor;
|
||||
import org.springframework.web.servlet.handler.MappedInterceptors;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
import org.springframework.web.servlet.mvc.method.condition.RequestConditionFactory;
|
||||
|
||||
/**
|
||||
* An {@link AbstractHandlerMethodMapping} variant that uses {@link RequestKey}s for the registration and the lookup
|
||||
* of {@link HandlerMethod}s.
|
||||
*
|
||||
* <p>A {@link RequestKey} for an incoming request contains the URL and the HTTP method of the request.
|
||||
* A {@link RequestKey} for a handler method contains all conditions found in the method @{@link RequestMapping}
|
||||
* annotation combined with all conditions found in the type @{@link RequestMapping} annotation, if present.
|
||||
*
|
||||
* <p>An incoming request matches to a handler method directly when a @{@link RequestMapping} annotation contains
|
||||
* a single, non-pattern URL and a single HTTP method. When a {@link RequestKey} contains additional conditions
|
||||
* (e.g. more URL patterns, request parameters, headers, etc) those conditions must be checked against the
|
||||
* request rather than against the key that represents it. This results in the creation of a new handler method
|
||||
* {@link RequestKey} with the subset of conditions relevant to the current request (see
|
||||
* {@link RequestKey#getMatchingKey(HttpServletRequest, PathMatcher, UrlPathHelper)}).
|
||||
* Such keys can then be compared against each other, in the context of the current request, making it possible
|
||||
* to select to the best matching {@link RequestKey} in case of multiple matches and also the best matching
|
||||
* pattern within the selected key.
|
||||
* An {@link AbstractHandlerMethodMapping} variant that uses {@link RequestMappingKey}s for the registration and
|
||||
* the lookup of {@link HandlerMethod}s.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 3.1.0
|
||||
*/
|
||||
public class RequestMappingHandlerMethodMapping extends AbstractHandlerMethodMapping<RequestKey> {
|
||||
|
||||
private UrlPathHelper urlPathHelper = new UrlPathHelper();
|
||||
public class RequestMappingHandlerMethodMapping extends AbstractHandlerMethodMapping<RequestMappingKey> {
|
||||
|
||||
private PathMatcher pathMatcher = new AntPathMatcher();
|
||||
|
||||
private MappedInterceptors mappedInterceptors;
|
||||
|
||||
/**
|
||||
* Set if URL lookup should always use the full path within the current servlet context. Else, the path within the
|
||||
* current servlet mapping is used if applicable (that is, in the case of a ".../*" servlet mapping in web.xml).
|
||||
* <p>Default is "false".
|
||||
*
|
||||
* @see org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath
|
||||
*/
|
||||
public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
|
||||
this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if context path and request URI should be URL-decoded. Both are returned <i>undecoded</i> by the Servlet API, in
|
||||
* contrast to the servlet path. <p>Uses either the request encoding or the default encoding according to the Servlet
|
||||
* spec (ISO-8859-1).
|
||||
*
|
||||
* @see org.springframework.web.util.UrlPathHelper#setUrlDecode
|
||||
*/
|
||||
public void setUrlDecode(boolean urlDecode) {
|
||||
this.urlPathHelper.setUrlDecode(urlDecode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the UrlPathHelper to use for resolution of lookup paths. <p>Use this to override the default UrlPathHelper
|
||||
* with a custom subclass, or to share common UrlPathHelper settings across multiple HandlerMappings and
|
||||
* MethodNameResolvers.
|
||||
*
|
||||
*/
|
||||
public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
|
||||
Assert.notNull(urlPathHelper, "UrlPathHelper must not be null");
|
||||
this.urlPathHelper = urlPathHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the PathMatcher implementation to use for matching URL paths against registered URL patterns. Default is
|
||||
* AntPathMatcher.
|
||||
@@ -145,18 +97,7 @@ public class RequestMappingHandlerMethodMapping extends AbstractHandlerMethodMap
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link RequestKey} instances that represents the given HTTP servlet request.
|
||||
*
|
||||
* @param request the request to look up the key for
|
||||
* @return the key, never null
|
||||
*/
|
||||
@Override
|
||||
protected RequestKey getKeyForRequest(HttpServletRequest request) {
|
||||
return RequestKey.createFromServletRequest(request, urlPathHelper);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a {@link RequestKey} for the given method.
|
||||
* Provides a {@link RequestMappingKey} for the given method.
|
||||
* <p>Only {@link RequestMapping @RequestMapping}-annotated methods are considered.
|
||||
* Type-level {@link RequestMapping @RequestMapping} annotations are also detected and their
|
||||
* attributes combined with method-level {@link RequestMapping @RequestMapping} attributes.
|
||||
@@ -164,16 +105,16 @@ public class RequestMappingHandlerMethodMapping extends AbstractHandlerMethodMap
|
||||
* @param beanName the name of the bean the method belongs to
|
||||
* @param method the method to create a key for
|
||||
* @return the key, or {@code null}
|
||||
* @see RequestKey#combine(RequestKey, PathMatcher)
|
||||
* @see RequestMappingKey#combine(RequestMappingKey, PathMatcher)
|
||||
*/
|
||||
@Override
|
||||
protected RequestKey getKeyForMethod(String beanName, Method method) {
|
||||
protected RequestMappingKey getMappingKeyForMethod(String beanName, Method method) {
|
||||
RequestMapping annotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
|
||||
if (annotation != null) {
|
||||
RequestKey methodKey = RequestKey.createFromRequestMapping(annotation);
|
||||
RequestMappingKey methodKey = createFromRequestMapping(annotation);
|
||||
RequestMapping typeAnnot = getApplicationContext().findAnnotationOnBean(beanName, RequestMapping.class);
|
||||
if (typeAnnot != null) {
|
||||
RequestKey typeKey = RequestKey.createFromRequestMapping(typeAnnot);
|
||||
RequestMappingKey typeKey = createFromRequestMapping(typeAnnot);
|
||||
return typeKey.combine(methodKey, pathMatcher);
|
||||
}
|
||||
else {
|
||||
@@ -185,41 +126,52 @@ public class RequestMappingHandlerMethodMapping extends AbstractHandlerMethodMap
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new {@link RequestKey} with attributes matching to the current request or {@code null}.
|
||||
* @see RequestKey#getMatchingKey(HttpServletRequest, PathMatcher, UrlPathHelper)
|
||||
*/
|
||||
private static RequestMappingKey createFromRequestMapping(RequestMapping annotation) {
|
||||
return new RequestMappingKey(Arrays.asList(annotation.value()), Arrays.asList(annotation.method()),
|
||||
RequestConditionFactory.parseParams(annotation.params()),
|
||||
RequestConditionFactory.parseHeaders(annotation.headers()),
|
||||
RequestConditionFactory.parseConsumes(annotation.consumes())
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RequestKey getMatchingKey(RequestKey key, HttpServletRequest request) {
|
||||
return key.getMatchingKey(request, pathMatcher, urlPathHelper);
|
||||
protected Set<String> getMappingPaths(RequestMappingKey key) {
|
||||
return key.getPatterns();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link Comparator} that can be used to sort and select the best matching {@link RequestKey}.
|
||||
* Returns a new {@link RequestMappingKey} with attributes matching to the current request or {@code null}.
|
||||
* @see RequestMappingKey#getMatchingKey(String, HttpServletRequest, PathMatcher)
|
||||
*/
|
||||
@Override
|
||||
protected Comparator<RequestKey> getKeyComparator(HttpServletRequest request) {
|
||||
return new RequestKeyComparator(request);
|
||||
protected RequestMappingKey getMatchingMappingKey(RequestMappingKey key, String lookupPath, HttpServletRequest request) {
|
||||
return key.getMatchingKey(lookupPath, request, pathMatcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link Comparator} that can be used to sort and select the best matching {@link RequestMappingKey}.
|
||||
*/
|
||||
@Override
|
||||
protected Comparator<RequestMappingKey> getMappingKeyComparator(String lookupPath, HttpServletRequest request) {
|
||||
return new RequestKeyComparator(lookupPath, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleMatch(RequestKey key, HttpServletRequest request) {
|
||||
protected void handleMatch(RequestMappingKey key, String lookupPath, HttpServletRequest request) {
|
||||
String pattern = key.getPatterns().iterator().next();
|
||||
String lookupPath = urlPathHelper.getLookupPathForRequest(request);
|
||||
Map<String, String> uriTemplateVariables = pathMatcher.extractUriTemplateVariables(pattern, lookupPath);
|
||||
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVariables);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterates all {@link RequestKey}s looking for keys that match by URL but not by HTTP method.
|
||||
* Iterates all {@link RequestMappingKey}s looking for keys that match by URL but not by HTTP method.
|
||||
* @exception HttpRequestMethodNotSupportedException if there are matches by URL but not by HTTP method
|
||||
*/
|
||||
@Override
|
||||
protected HandlerMethod handleNoMatch(Set<RequestKey> requestKeys, HttpServletRequest request)
|
||||
protected HandlerMethod handleNoMatch(Set<RequestMappingKey> requestKeys, String lookupPath, HttpServletRequest request)
|
||||
throws HttpRequestMethodNotSupportedException {
|
||||
String lookupPath = urlPathHelper.getLookupPathForRequest(request);
|
||||
Set<String> allowedMethods = new HashSet<String>(6);
|
||||
for (RequestKey requestKey : requestKeys) {
|
||||
for (RequestMappingKey requestKey : requestKeys) {
|
||||
for (String pattern : requestKey.getPatterns()) {
|
||||
if (pathMatcher.match(pattern, lookupPath)) {
|
||||
for (RequestMethod method : requestKey.getMethods()) {
|
||||
@@ -244,7 +196,7 @@ public class RequestMappingHandlerMethodMapping extends AbstractHandlerMethodMap
|
||||
protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request) {
|
||||
HandlerExecutionChain chain = super.getHandlerExecutionChain(handler, request);
|
||||
if (this.mappedInterceptors != null) {
|
||||
String lookupPath = urlPathHelper.getLookupPathForRequest(request);
|
||||
String lookupPath = getUrlPathHelper().getLookupPathForRequest(request);
|
||||
HandlerInterceptor[] handlerInterceptors = mappedInterceptors.getInterceptors(lookupPath, pathMatcher);
|
||||
if (handlerInterceptors.length > 0) {
|
||||
chain.addInterceptors(handlerInterceptors);
|
||||
@@ -254,31 +206,30 @@ public class RequestMappingHandlerMethodMapping extends AbstractHandlerMethodMap
|
||||
}
|
||||
|
||||
/**
|
||||
* A comparator for {@link RequestKey}s. Effective comparison can only be done in the context of a
|
||||
* specific request. For example not all {@link RequestKey} patterns may apply to the current request.
|
||||
* A comparator for {@link RequestMappingKey}s. Effective comparison can only be done in the context of a
|
||||
* specific request. For example not all {@link RequestMappingKey} patterns may apply to the current request.
|
||||
* Therefore an HttpServletRequest is required as input.
|
||||
*
|
||||
* <p>Furthermore, the following assumptions are made about the input RequestKeys:
|
||||
* <ul><li>Each RequestKey has been fully matched to the request <li>The RequestKey contains matched
|
||||
* patterns only <li>Patterns are ordered with the best matching pattern at the top </ul>
|
||||
*
|
||||
* @see RequestMappingHandlerMethodMapping#getMatchingKey(RequestKey, HttpServletRequest)
|
||||
* @see RequestMappingHandlerMethodMapping#getMatchingKey(RequestMappingKey, HttpServletRequest)
|
||||
*/
|
||||
private class RequestKeyComparator implements Comparator<RequestKey> {
|
||||
private class RequestKeyComparator implements Comparator<RequestMappingKey> {
|
||||
|
||||
private Comparator<String> patternComparator;
|
||||
|
||||
private List<MediaType> requestAcceptHeader;
|
||||
|
||||
public RequestKeyComparator(HttpServletRequest request) {
|
||||
String lookupPath = urlPathHelper.getLookupPathForRequest(request);
|
||||
public RequestKeyComparator(String lookupPath, HttpServletRequest request) {
|
||||
this.patternComparator = pathMatcher.getPatternComparator(lookupPath);
|
||||
String acceptHeader = request.getHeader("Accept");
|
||||
this.requestAcceptHeader = MediaType.parseMediaTypes(acceptHeader);
|
||||
MediaType.sortByQualityValue(this.requestAcceptHeader);
|
||||
}
|
||||
|
||||
public int compare(RequestKey key, RequestKey otherKey) {
|
||||
public int compare(RequestMappingKey key, RequestMappingKey otherKey) {
|
||||
int result = comparePatterns(key.getPatterns(), otherKey.getPatterns());
|
||||
if (result != 0) {
|
||||
return result;
|
||||
|
||||
@@ -17,37 +17,35 @@
|
||||
package org.springframework.web.servlet.mvc.method.annotation;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.servlet.mvc.method.condition.RequestCondition;
|
||||
import org.springframework.web.servlet.mvc.method.condition.RequestConditionFactory;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
/**
|
||||
* Contains a set of conditions to match to a given request such as URL patterns, HTTP methods, request
|
||||
* parameters and headers.
|
||||
*
|
||||
* <p>A {@link RequestKey} can be combined with another {@link RequestKey} resulting in a new {@link RequestKey}
|
||||
* with conditions from both (see {@link #combine(RequestKey, PathMatcher)}).
|
||||
* <p>A {@link RequestMappingKey} can be combined with another {@link RequestMappingKey} resulting in a new {@link RequestMappingKey}
|
||||
* with conditions from both (see {@link #combine(RequestMappingKey, PathMatcher)}).
|
||||
*
|
||||
* <p>A {@link RequestKey} can be matched to a request resulting in a new {@link RequestKey} with the subset of
|
||||
* conditions relevant to the request (see {@link #getMatchingKey(HttpServletRequest, PathMatcher, UrlPathHelper)}).
|
||||
* <p>A {@link RequestMappingKey} can be matched to a request resulting in a new {@link RequestMappingKey} with the subset of
|
||||
* conditions relevant to the request (see {@link #getMatchingKey(String, HttpServletRequest, PathMatcher)}).
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 3.1
|
||||
*/
|
||||
public final class RequestKey {
|
||||
public final class RequestMappingKey {
|
||||
|
||||
private final Set<String> patterns;
|
||||
|
||||
@@ -66,20 +64,18 @@ public final class RequestKey {
|
||||
*
|
||||
* <p>Package protected for testing purposes.
|
||||
*/
|
||||
RequestKey(Collection<String> patterns, Collection<RequestMethod> methods) {
|
||||
RequestMappingKey(Collection<String> patterns, Collection<RequestMethod> methods) {
|
||||
this(patterns, methods, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code RequestKey} instance with a full set of conditions.
|
||||
*
|
||||
* <p>Package protected for testing purposes.
|
||||
*/
|
||||
RequestKey(Collection<String> patterns,
|
||||
Collection<RequestMethod> methods,
|
||||
RequestCondition paramsCondition,
|
||||
RequestCondition headersCondition,
|
||||
RequestCondition consumesCondition) {
|
||||
public RequestMappingKey(Collection<String> patterns,
|
||||
Collection<RequestMethod> methods,
|
||||
RequestCondition paramsCondition,
|
||||
RequestCondition headersCondition,
|
||||
RequestCondition consumesCondition) {
|
||||
this.patterns = asUnmodifiableSet(prependLeadingSlash(patterns));
|
||||
this.methods = asUnmodifiableSet(methods);
|
||||
this.paramsCondition = paramsCondition != null ? paramsCondition : RequestConditionFactory.trueCondition();
|
||||
@@ -109,34 +105,6 @@ public final class RequestKey {
|
||||
return Collections.unmodifiableSet(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code RequestKey} from a {@link RequestMapping @RequestMapping} annotation.
|
||||
*
|
||||
* @param annotation the annotation
|
||||
* @return the request key created from the annotation
|
||||
*/
|
||||
public static RequestKey createFromRequestMapping(RequestMapping annotation) {
|
||||
return new RequestKey(Arrays.asList(annotation.value()), Arrays.asList(annotation.method()),
|
||||
RequestConditionFactory.parseParams(annotation.params()),
|
||||
RequestConditionFactory.parseHeaders(annotation.headers()),
|
||||
RequestConditionFactory.parseConsumes(annotation.consumes())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code RequestKey} from a {@link HttpServletRequest}.
|
||||
*
|
||||
* @param request the servlet request
|
||||
* @param urlPathHelper to create the {@linkplain UrlPathHelper#getLookupPathForRequest(HttpServletRequest) lookup
|
||||
* path}
|
||||
* @return the request key created from the servlet request
|
||||
*/
|
||||
public static RequestKey createFromServletRequest(HttpServletRequest request, UrlPathHelper urlPathHelper) {
|
||||
String lookupPath = urlPathHelper.getLookupPathForRequest(request);
|
||||
RequestMethod method = RequestMethod.valueOf(request.getMethod());
|
||||
return new RequestKey(Collections.singleton(lookupPath), Collections.singleton(method));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the patterns of this request key.
|
||||
*/
|
||||
@@ -183,14 +151,14 @@ public final class RequestKey {
|
||||
* @param pathMatcher to {@linkplain PathMatcher#combine(String, String) combine} the patterns
|
||||
* @return a new request key containing conditions from both keys
|
||||
*/
|
||||
public RequestKey combine(RequestKey methodKey, PathMatcher pathMatcher) {
|
||||
public RequestMappingKey combine(RequestMappingKey methodKey, PathMatcher pathMatcher) {
|
||||
Set<String> patterns = combinePatterns(this.patterns, methodKey.patterns, pathMatcher);
|
||||
Set<RequestMethod> methods = union(this.methods, methodKey.methods);
|
||||
RequestCondition params = RequestConditionFactory.and(this.paramsCondition, methodKey.paramsCondition);
|
||||
RequestCondition headers = RequestConditionFactory.and(this.headersCondition, methodKey.headersCondition);
|
||||
RequestCondition consumes = RequestConditionFactory.mostSpecific(methodKey.consumesCondition, this.consumesCondition);
|
||||
|
||||
return new RequestKey(patterns, methods, params, headers, consumes);
|
||||
return new RequestMappingKey(patterns, methods, params, headers, consumes);
|
||||
}
|
||||
|
||||
private static Set<String> combinePatterns(Collection<String> typePatterns,
|
||||
@@ -231,21 +199,21 @@ public final class RequestKey {
|
||||
* <li>Request parameter and request header conditions are included in full.
|
||||
* <li>The list of consumes conditions is trimmed and sorted to match the request "Content-Type" header.
|
||||
* </ul>
|
||||
* @param lookupPath mapping lookup path within the current servlet mapping if applicable
|
||||
* @param request the current request
|
||||
* @param pathMatcher to check for matching patterns
|
||||
* @param urlPathHelper to derive the lookup path for the request
|
||||
* @return a new request key that contains all matching attributes, or {@code null} if not all conditions match
|
||||
*/
|
||||
public RequestKey getMatchingKey(HttpServletRequest request, PathMatcher pathMatcher, UrlPathHelper urlPathHelper) {
|
||||
public RequestMappingKey getMatchingKey(String lookupPath, HttpServletRequest request, PathMatcher pathMatcher) {
|
||||
if (!checkMethod(request) || !paramsCondition.match(request) || !headersCondition.match(request) ||
|
||||
!consumesCondition.match(request)) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
List<String> matchingPatterns = getMatchingPatterns(request, pathMatcher, urlPathHelper);
|
||||
List<String> matchingPatterns = getMatchingPatterns(lookupPath, request, pathMatcher);
|
||||
if (!matchingPatterns.isEmpty()) {
|
||||
Set<RequestMethod> matchingMethods = getMatchingMethod(request);
|
||||
return new RequestKey(matchingPatterns, matchingMethods, this.paramsCondition, this.headersCondition,
|
||||
return new RequestMappingKey(matchingPatterns, matchingMethods, this.paramsCondition, this.headersCondition,
|
||||
this.consumesCondition);
|
||||
}
|
||||
else {
|
||||
@@ -254,10 +222,9 @@ public final class RequestKey {
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> getMatchingPatterns(HttpServletRequest request,
|
||||
PathMatcher pathMatcher,
|
||||
UrlPathHelper urlPathHelper) {
|
||||
String lookupPath = urlPathHelper.getLookupPathForRequest(request);
|
||||
private List<String> getMatchingPatterns(String lookupPath,
|
||||
HttpServletRequest request,
|
||||
PathMatcher pathMatcher) {
|
||||
|
||||
List<String> matchingPatterns = new ArrayList<String>();
|
||||
for (String pattern : this.patterns) {
|
||||
@@ -308,8 +275,8 @@ public final class RequestKey {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj != null && obj instanceof RequestKey) {
|
||||
RequestKey other = (RequestKey) obj;
|
||||
if (obj != null && obj instanceof RequestMappingKey) {
|
||||
RequestMappingKey other = (RequestMappingKey) obj;
|
||||
return (this.patterns.equals(other.patterns) && this.methods.equals(other.methods) &&
|
||||
this.paramsCondition.equals(other.paramsCondition) &&
|
||||
this.headersCondition.equals(other.headersCondition));
|
||||
@@ -20,7 +20,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* Defines the contract for conditions that must be met before an incoming request matches a {@link
|
||||
* org.springframework.web.servlet.mvc.method.annotation.RequestKey RequestKey}.
|
||||
* org.springframework.web.servlet.mvc.method.annotation.RequestMappingKey RequestKey}.
|
||||
*
|
||||
* <p>Implementations of this interface are created by the {@link RequestConditionFactory}.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user