Skip to content

Commit 1694994

Browse files
committed
MatchableHandlerMapping extends HandlerMapping and lives in web.servlet.handler now
Issue: SPR-14321
1 parent 232cfe5 commit 1694994

File tree

6 files changed

+30
-55
lines changed

6 files changed

+30
-55
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,9 +29,6 @@
2929
import org.springframework.util.Assert;
3030
import org.springframework.util.CollectionUtils;
3131
import org.springframework.web.servlet.HandlerExecutionChain;
32-
import org.springframework.web.servlet.HandlerMapping;
33-
import org.springframework.web.servlet.support.MatchableHandlerMapping;
34-
import org.springframework.web.servlet.support.RequestMatchResult;
3532

3633
/**
3734
* Abstract base class for URL-mapped {@link org.springframework.web.servlet.HandlerMapping}
@@ -52,8 +49,7 @@
5249
* @author Arjen Poutsma
5350
* @since 16.04.2003
5451
*/
55-
public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping
56-
implements MatchableHandlerMapping {
52+
public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping implements MatchableHandlerMapping {
5753

5854
private Object rootHandler;
5955

@@ -268,8 +264,8 @@ protected Object buildPathExposingHandler(Object rawHandler, String bestMatching
268264
* @see #PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
269265
*/
270266
protected void exposePathWithinMapping(String bestMatchingPattern, String pathWithinMapping, HttpServletRequest request) {
271-
request.setAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, bestMatchingPattern);
272-
request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, pathWithinMapping);
267+
request.setAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE, bestMatchingPattern);
268+
request.setAttribute(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, pathWithinMapping);
273269
}
274270

275271
/**
@@ -279,7 +275,7 @@ protected void exposePathWithinMapping(String bestMatchingPattern, String pathWi
279275
* @see #PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
280276
*/
281277
protected void exposeUriTemplateVariables(Map<String, String> uriTemplateVariables, HttpServletRequest request) {
282-
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVariables);
278+
request.setAttribute(URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVariables);
283279
}
284280

285281
@Override
@@ -403,7 +399,7 @@ public PathExposingHandlerInterceptor(String bestMatchingPattern, String pathWit
403399
@Override
404400
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
405401
exposePathWithinMapping(this.bestMatchingPattern, this.pathWithinMapping, request);
406-
request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, supportsTypeLevelMappings());
402+
request.setAttribute(INTROSPECT_TYPE_LEVEL_MAPPING, supportsTypeLevelMappings());
407403
return true;
408404
}
409405

spring-webmvc/src/main/java/org/springframework/web/servlet/support/HandlerMappingIntrospector.java renamed to spring-webmvc/src/main/java/org/springframework/web/servlet/handler/HandlerMappingIntrospector.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.web.servlet.support;
17+
package org.springframework.web.servlet.handler;
1818

1919
import java.io.IOException;
2020
import java.util.ArrayList;
@@ -70,16 +70,13 @@ public HandlerMappingIntrospector(ApplicationContext context) {
7070

7171

7272
private static List<HandlerMapping> initHandlerMappings(ApplicationContext context) {
73-
7473
Map<String, HandlerMapping> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
7574
context, HandlerMapping.class, true, false);
76-
7775
if (!beans.isEmpty()) {
7876
List<HandlerMapping> mappings = new ArrayList<HandlerMapping>(beans.values());
7977
AnnotationAwareOrderComparator.sort(mappings);
8078
return mappings;
8179
}
82-
8380
return initDefaultHandlerMappings(context);
8481
}
8582

@@ -118,14 +115,12 @@ public List<HandlerMapping> getHandlerMappings() {
118115
return this.handlerMappings;
119116
}
120117

121-
122118
/**
123119
* Find the {@link HandlerMapping} that would handle the given request and
124-
* return it as a {@link MatchableHandlerMapping} that can be used to
125-
* test request-matching criteria. If the matching HandlerMapping is not an
126-
* instance of {@link MatchableHandlerMapping}, an IllegalStateException is
127-
* raised.
128-
*
120+
* return it as a {@link MatchableHandlerMapping} that can be used to test
121+
* request-matching criteria.
122+
* <p>If the matching HandlerMapping is not an instance of
123+
* {@link MatchableHandlerMapping}, an IllegalStateException is raised.
129124
* @param request the current request
130125
* @return the resolved matcher, or {@code null}
131126
* @throws Exception if any of the HandlerMapping's raise an exception
@@ -179,8 +174,7 @@ public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
179174
*/
180175
private static class RequestAttributeChangeIgnoringWrapper extends HttpServletRequestWrapper {
181176

182-
183-
private RequestAttributeChangeIgnoringWrapper(HttpServletRequest request) {
177+
public RequestAttributeChangeIgnoringWrapper(HttpServletRequest request) {
184178
super(request);
185179
}
186180

@@ -190,4 +184,4 @@ public void setAttribute(String name, Object value) {
190184
}
191185
}
192186

193-
}
187+
}

spring-webmvc/src/main/java/org/springframework/web/servlet/support/MatchableHandlerMapping.java renamed to spring-webmvc/src/main/java/org/springframework/web/servlet/handler/MatchableHandlerMapping.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.web.servlet.support;
17+
package org.springframework.web.servlet.handler;
1818

1919
import javax.servlet.http.HttpServletRequest;
2020

@@ -29,13 +29,13 @@
2929
* @since 4.3.1
3030
* @see HandlerMappingIntrospector
3131
*/
32-
public interface MatchableHandlerMapping {
32+
public interface MatchableHandlerMapping extends HandlerMapping {
3333

3434
/**
35-
* Whether the given request matches the request criteria.
35+
* Determine whether the given request matches the request criteria.
3636
* @param request the current request
3737
* @param pattern the pattern to match
38-
* @return the result from request matching or {@code null}
38+
* @return the result from request matching, or {@code null} if none
3939
*/
4040
RequestMatchResult match(HttpServletRequest request, String pattern);
4141

spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestMatchResult.java renamed to spring-webmvc/src/main/java/org/springframework/web/servlet/handler/RequestMatchResult.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.web.servlet.support;
17+
package org.springframework.web.servlet.handler;
1818

19-
import java.util.Collections;
2019
import java.util.Map;
2120

2221
import org.springframework.util.Assert;
2322
import org.springframework.util.PathMatcher;
2423

2524
/**
2625
* Container for the result from request pattern matching via
27-
* {@link MatchableHandlerMapping} with a method to further extract URI template
28-
* variables from the pattern.
26+
* {@link MatchableHandlerMapping} with a method to further extract
27+
* URI template variables from the pattern.
2928
*
3029
* @author Rossen Stoyanchev
3130
* @since 4.3.1
@@ -56,22 +55,12 @@ public RequestMatchResult(String matchingPattern, String lookupPath, PathMatcher
5655
}
5756

5857

59-
/**
60-
* Whether the pattern was matched to the request.
61-
*/
62-
public boolean isMatch() {
63-
return (this.matchingPattern != null);
64-
}
65-
6658
/**
6759
* Extract URI template variables from the matching pattern as defined in
6860
* {@link PathMatcher#extractUriTemplateVariables}.
6961
* @return a map with URI template variables
7062
*/
7163
public Map<String, String> extractUriTemplateVariables() {
72-
if (!isMatch()) {
73-
return Collections.<String, String>emptyMap();
74-
}
7564
return this.pathMatcher.extractUriTemplateVariables(this.matchingPattern, this.lookupPath);
7665
}
7766

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@
3535
import org.springframework.web.bind.annotation.RequestMethod;
3636
import org.springframework.web.cors.CorsConfiguration;
3737
import org.springframework.web.method.HandlerMethod;
38+
import org.springframework.web.servlet.handler.MatchableHandlerMapping;
39+
import org.springframework.web.servlet.handler.RequestMatchResult;
3840
import org.springframework.web.servlet.mvc.condition.AbstractRequestCondition;
3941
import org.springframework.web.servlet.mvc.condition.CompositeRequestCondition;
4042
import org.springframework.web.servlet.mvc.condition.RequestCondition;
4143
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
4244
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
43-
import org.springframework.web.servlet.support.MatchableHandlerMapping;
44-
import org.springframework.web.servlet.support.RequestMatchResult;
4545

4646
/**
4747
* Creates {@link RequestMappingInfo} instances from type and method-level
@@ -54,7 +54,7 @@
5454
* @since 3.1
5555
*/
5656
public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMapping
57-
implements EmbeddedValueResolverAware, MatchableHandlerMapping {
57+
implements MatchableHandlerMapping, EmbeddedValueResolverAware {
5858

5959
private boolean useSuffixPatternMatch = true;
6060

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.web.servlet.support;
17+
package org.springframework.web.servlet.handler;
1818

1919
import java.util.Arrays;
2020
import java.util.Collections;
@@ -36,17 +36,14 @@
3636
import org.springframework.web.cors.CorsConfiguration;
3737
import org.springframework.web.servlet.HandlerExecutionChain;
3838
import org.springframework.web.servlet.HandlerMapping;
39-
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
40-
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
4139
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
4240

43-
import static org.junit.Assert.assertEquals;
44-
import static org.junit.Assert.assertNotNull;
45-
import static org.junit.Assert.assertNull;
46-
import static org.springframework.web.servlet.HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE;
41+
import static org.junit.Assert.*;
42+
import static org.springframework.web.servlet.HandlerMapping.*;
4743

4844
/**
4945
* Unit tests for {@link HandlerMappingIntrospector}.
46+
*
5047
* @author Rossen Stoyanchev
5148
* @since 4.3.1
5249
*/
@@ -97,10 +94,8 @@ public void defaultHandlerMappings() throws Exception {
9794

9895
@Test
9996
public void getMatchable() throws Exception {
100-
10197
MutablePropertyValues pvs = new MutablePropertyValues(
102-
Collections.singletonMap("urlMap",
103-
Collections.singletonMap("/path", new Object())));
98+
Collections.singletonMap("urlMap", Collections.singletonMap("/path", new Object())));
10499

105100
StaticWebApplicationContext cxt = new StaticWebApplicationContext();
106101
cxt.registerSingleton("hm", SimpleUrlHandlerMapping.class, pvs);
@@ -165,6 +160,7 @@ public HandlerExecutionChain getHandler(HttpServletRequest request) throws Excep
165160
}
166161
}
167162

163+
168164
@Configuration @SuppressWarnings({"WeakerAccess", "unused"})
169165
static class TestConfig {
170166

@@ -177,9 +173,9 @@ public RequestMappingHandlerMapping handlerMapping() {
177173
public TestController testController() {
178174
return new TestController();
179175
}
180-
181176
}
182177

178+
183179
@CrossOrigin("http://localhost:9000")
184180
@Controller
185181
private static class TestController {

0 commit comments

Comments
 (0)