MatchableHandlerMapping extends HandlerMapping and lives in web.servlet.handler now

Issue: SPR-14321
This commit is contained in:
Juergen Hoeller 2016-06-30 14:38:26 +02:00
parent 232cfe5e98
commit 16949941f8
6 changed files with 30 additions and 55 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2016 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.
@ -29,9 +29,6 @@ import org.springframework.beans.BeansException;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.support.MatchableHandlerMapping;
import org.springframework.web.servlet.support.RequestMatchResult;
/**
* Abstract base class for URL-mapped {@link org.springframework.web.servlet.HandlerMapping}
@ -52,8 +49,7 @@ import org.springframework.web.servlet.support.RequestMatchResult;
* @author Arjen Poutsma
* @since 16.04.2003
*/
public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping
implements MatchableHandlerMapping {
public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping implements MatchableHandlerMapping {
private Object rootHandler;
@ -268,8 +264,8 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping
* @see #PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
*/
protected void exposePathWithinMapping(String bestMatchingPattern, String pathWithinMapping, HttpServletRequest request) {
request.setAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, bestMatchingPattern);
request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, pathWithinMapping);
request.setAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE, bestMatchingPattern);
request.setAttribute(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, pathWithinMapping);
}
/**
@ -279,7 +275,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping
* @see #PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
*/
protected void exposeUriTemplateVariables(Map<String, String> uriTemplateVariables, HttpServletRequest request) {
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVariables);
request.setAttribute(URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVariables);
}
@Override
@ -403,7 +399,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
exposePathWithinMapping(this.bestMatchingPattern, this.pathWithinMapping, request);
request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, supportsTypeLevelMappings());
request.setAttribute(INTROSPECT_TYPE_LEVEL_MAPPING, supportsTypeLevelMappings());
return true;
}

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.web.servlet.support;
package org.springframework.web.servlet.handler;
import java.io.IOException;
import java.util.ArrayList;
@ -70,16 +70,13 @@ public class HandlerMappingIntrospector implements CorsConfigurationSource {
private static List<HandlerMapping> initHandlerMappings(ApplicationContext context) {
Map<String, HandlerMapping> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
context, HandlerMapping.class, true, false);
if (!beans.isEmpty()) {
List<HandlerMapping> mappings = new ArrayList<HandlerMapping>(beans.values());
AnnotationAwareOrderComparator.sort(mappings);
return mappings;
}
return initDefaultHandlerMappings(context);
}
@ -118,14 +115,12 @@ public class HandlerMappingIntrospector implements CorsConfigurationSource {
return this.handlerMappings;
}
/**
* Find the {@link HandlerMapping} that would handle the given request and
* return it as a {@link MatchableHandlerMapping} that can be used to
* test request-matching criteria. If the matching HandlerMapping is not an
* instance of {@link MatchableHandlerMapping}, an IllegalStateException is
* raised.
*
* return it as a {@link MatchableHandlerMapping} that can be used to test
* request-matching criteria.
* <p>If the matching HandlerMapping is not an instance of
* {@link MatchableHandlerMapping}, an IllegalStateException is raised.
* @param request the current request
* @return the resolved matcher, or {@code null}
* @throws Exception if any of the HandlerMapping's raise an exception
@ -179,8 +174,7 @@ public class HandlerMappingIntrospector implements CorsConfigurationSource {
*/
private static class RequestAttributeChangeIgnoringWrapper extends HttpServletRequestWrapper {
private RequestAttributeChangeIgnoringWrapper(HttpServletRequest request) {
public RequestAttributeChangeIgnoringWrapper(HttpServletRequest request) {
super(request);
}
@ -190,4 +184,4 @@ public class HandlerMappingIntrospector implements CorsConfigurationSource {
}
}
}
}

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.web.servlet.support;
package org.springframework.web.servlet.handler;
import javax.servlet.http.HttpServletRequest;
@ -29,13 +29,13 @@ import org.springframework.web.servlet.HandlerMapping;
* @since 4.3.1
* @see HandlerMappingIntrospector
*/
public interface MatchableHandlerMapping {
public interface MatchableHandlerMapping extends HandlerMapping {
/**
* Whether the given request matches the request criteria.
* Determine whether the given request matches the request criteria.
* @param request the current request
* @param pattern the pattern to match
* @return the result from request matching or {@code null}
* @return the result from request matching, or {@code null} if none
*/
RequestMatchResult match(HttpServletRequest request, String pattern);

View File

@ -14,9 +14,8 @@
* limitations under the License.
*/
package org.springframework.web.servlet.support;
package org.springframework.web.servlet.handler;
import java.util.Collections;
import java.util.Map;
import org.springframework.util.Assert;
@ -24,8 +23,8 @@ import org.springframework.util.PathMatcher;
/**
* Container for the result from request pattern matching via
* {@link MatchableHandlerMapping} with a method to further extract URI template
* variables from the pattern.
* {@link MatchableHandlerMapping} with a method to further extract
* URI template variables from the pattern.
*
* @author Rossen Stoyanchev
* @since 4.3.1
@ -56,22 +55,12 @@ public class RequestMatchResult {
}
/**
* Whether the pattern was matched to the request.
*/
public boolean isMatch() {
return (this.matchingPattern != null);
}
/**
* Extract URI template variables from the matching pattern as defined in
* {@link PathMatcher#extractUriTemplateVariables}.
* @return a map with URI template variables
*/
public Map<String, String> extractUriTemplateVariables() {
if (!isMatch()) {
return Collections.<String, String>emptyMap();
}
return this.pathMatcher.extractUriTemplateVariables(this.matchingPattern, this.lookupPath);
}

View File

@ -35,13 +35,13 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.MatchableHandlerMapping;
import org.springframework.web.servlet.handler.RequestMatchResult;
import org.springframework.web.servlet.mvc.condition.AbstractRequestCondition;
import org.springframework.web.servlet.mvc.condition.CompositeRequestCondition;
import org.springframework.web.servlet.mvc.condition.RequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import org.springframework.web.servlet.support.MatchableHandlerMapping;
import org.springframework.web.servlet.support.RequestMatchResult;
/**
* Creates {@link RequestMappingInfo} instances from type and method-level
@ -54,7 +54,7 @@ import org.springframework.web.servlet.support.RequestMatchResult;
* @since 3.1
*/
public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMapping
implements EmbeddedValueResolverAware, MatchableHandlerMapping {
implements MatchableHandlerMapping, EmbeddedValueResolverAware {
private boolean useSuffixPatternMatch = true;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.web.servlet.support;
package org.springframework.web.servlet.handler;
import java.util.Arrays;
import java.util.Collections;
@ -36,17 +36,14 @@ import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.springframework.web.servlet.HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE;
import static org.junit.Assert.*;
import static org.springframework.web.servlet.HandlerMapping.*;
/**
* Unit tests for {@link HandlerMappingIntrospector}.
*
* @author Rossen Stoyanchev
* @since 4.3.1
*/
@ -97,10 +94,8 @@ public class HandlerMappingIntrospectorTests {
@Test
public void getMatchable() throws Exception {
MutablePropertyValues pvs = new MutablePropertyValues(
Collections.singletonMap("urlMap",
Collections.singletonMap("/path", new Object())));
Collections.singletonMap("urlMap", Collections.singletonMap("/path", new Object())));
StaticWebApplicationContext cxt = new StaticWebApplicationContext();
cxt.registerSingleton("hm", SimpleUrlHandlerMapping.class, pvs);
@ -165,6 +160,7 @@ public class HandlerMappingIntrospectorTests {
}
}
@Configuration @SuppressWarnings({"WeakerAccess", "unused"})
static class TestConfig {
@ -177,9 +173,9 @@ public class HandlerMappingIntrospectorTests {
public TestController testController() {
return new TestController();
}
}
@CrossOrigin("http://localhost:9000")
@Controller
private static class TestController {