Skip to content

Commit b572f76

Browse files
committed
HandlerMappings expose usesPathPatterns() method
See gh-24945
1 parent bb7e5f0 commit b572f76

File tree

5 files changed

+41
-35
lines changed

5 files changed

+41
-35
lines changed

Diff for: spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java

+4-21
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
import org.apache.commons.logging.Log;
4141
import org.apache.commons.logging.LogFactory;
4242

43-
import org.springframework.aop.framework.AopProxyUtils;
44-
import org.springframework.aop.support.AopUtils;
4543
import org.springframework.beans.factory.BeanFactoryUtils;
4644
import org.springframework.beans.factory.BeanInitializationException;
4745
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@@ -65,7 +63,6 @@
6563
import org.springframework.web.multipart.MultipartException;
6664
import org.springframework.web.multipart.MultipartHttpServletRequest;
6765
import org.springframework.web.multipart.MultipartResolver;
68-
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
6966
import org.springframework.web.util.NestedServletException;
7067
import org.springframework.web.util.ServletRequestPathUtils;
7168
import org.springframework.web.util.WebUtils;
@@ -630,26 +627,12 @@ private void initHandlerMappings(ApplicationContext context) {
630627
}
631628
}
632629

633-
this.parseRequestPath = initRequestPathParsing(this.handlerMappings);
634-
}
635-
636-
private boolean initRequestPathParsing(List<HandlerMapping> mappings) {
637-
for (HandlerMapping mapping : mappings) {
638-
if (mapping instanceof AbstractHandlerMapping) {
639-
if (((AbstractHandlerMapping) mapping).getPatternParser() != null) {
640-
return true;
641-
}
642-
}
643-
if (AopUtils.isAopProxy(mapping)) {
644-
Object target = AopProxyUtils.getSingletonTarget(mapping);
645-
if (target instanceof AbstractHandlerMapping) {
646-
if (((AbstractHandlerMapping) target).getPatternParser() != null) {
647-
return true;
648-
}
649-
}
630+
for (HandlerMapping mapping : this.handlerMappings) {
631+
if (mapping.usesPathPatterns()) {
632+
this.parseRequestPath = true;
633+
break;
650634
}
651635
}
652-
return false;
653636
}
654637

655638
/**

Diff for: spring-webmvc/src/main/java/org/springframework/web/servlet/HandlerMapping.java

+16
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,22 @@ public interface HandlerMapping {
136136
*/
137137
String PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE = HandlerMapping.class.getName() + ".producibleMediaTypes";
138138

139+
140+
/**
141+
* Whether this {@code HandlerMapping} instance has been enabled to use parsed
142+
* {@link org.springframework.web.util.pattern.PathPattern}s in which case
143+
* the {@link DispatcherServlet} automatically
144+
* {@link org.springframework.web.util.ServletRequestPathUtils#parseAndCache parses}
145+
* the {@code RequestPath} to make it available for
146+
* {@link org.springframework.web.util.ServletRequestPathUtils#getParsedRequestPath
147+
* access} in {@code HandlerMapping}s, {@code HandlerInterceptor}s, and
148+
* other components.
149+
* @since 5.3
150+
*/
151+
default boolean usesPathPatterns() {
152+
return false;
153+
}
154+
139155
/**
140156
* Return a handler and any interceptors for this request. The choice may be made
141157
* on request URL, session state, or any factor the implementing class chooses.

Diff for: spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMapping.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,15 @@ protected final MappedInterceptor[] getMappedInterceptors() {
469469
}
470470

471471

472+
/**
473+
* Return "true" if this {@code HandlerMapping} has been
474+
* {@link #setPatternParser enabled} to use parsed {@code PathPattern}s.
475+
*/
476+
@Override
477+
public boolean usesPathPatterns() {
478+
return getPatternParser() != null;
479+
}
480+
472481
/**
473482
* Look up a handler for the given request, falling back to the default
474483
* handler if no specific one is found.
@@ -534,8 +543,8 @@ else if (logger.isDebugEnabled() && !request.getDispatcherType().equals(Dispatch
534543

535544
/**
536545
* Initialize the path to use for request mapping.
537-
* <p>When parsed patterns are {@link #setPatternParser(PathPatternParser)
538-
* enabled} a parsed {@code RequestPath} is expected to have been
546+
* <p>When parsed patterns are {@link #usesPathPatterns() enabled} a parsed
547+
* {@code RequestPath} is expected to have been
539548
* {@link ServletRequestPathUtils#parseAndCache(HttpServletRequest) parsed}
540549
* externally by the {@link org.springframework.web.servlet.DispatcherServlet}
541550
* or {@link org.springframework.web.filter.ServletRequestPathFilter}.
@@ -545,7 +554,7 @@ else if (logger.isDebugEnabled() && !request.getDispatcherType().equals(Dispatch
545554
* @since 5.3
546555
*/
547556
protected String initLookupPath(HttpServletRequest request) {
548-
if (getPatternParser() != null) {
557+
if (usesPathPatterns()) {
549558
request.removeAttribute(UrlPathHelper.PATH_ATTRIBUTE);
550559
RequestPath requestPath = ServletRequestPathUtils.getParsedRequestPath(request);
551560
String lookupPath = requestPath.pathWithinApplication().value();

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

+6-8
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@
4040
import org.springframework.web.util.ServletRequestPathUtils;
4141
import org.springframework.web.util.UrlPathHelper;
4242
import org.springframework.web.util.pattern.PathPattern;
43-
import org.springframework.web.util.pattern.PathPatternParser;
4443

4544
/**
4645
* Abstract base class for URL-mapped {@link HandlerMapping} implementations.
4746
*
4847
* <p>Supports literal matches and pattern matches such as "/test/*", "/test/**",
4948
* and others. For details on pattern syntax refer to {@link PathPattern} when
50-
* parsed patterns are {@link #setPatternParser(PathPatternParser) enabled} or
51-
* see {@link AntPathMatcher} otherwise. The syntax is largely the same but the
49+
* parsed patterns are {@link #usesPathPatterns() enabled} or see
50+
* {@link AntPathMatcher} otherwise. The syntax is largely the same but the
5251
* {@code PathPattern} syntax is more tailored for web applications, and its
5352
* implementation is more efficient.
5453
*
@@ -135,7 +134,7 @@ public void setLazyInitHandlers(boolean lazyInitHandlers) {
135134
protected Object getHandlerInternal(HttpServletRequest request) throws Exception {
136135
String lookupPath = initLookupPath(request);
137136
Object handler;
138-
if (getPatternParser() != null) {
137+
if (usesPathPatterns()) {
139138
RequestPath path = ServletRequestPathUtils.getParsedRequestPath(request);
140139
handler = lookupHandler(path, lookupPath, request);
141140
}
@@ -167,8 +166,7 @@ protected Object getHandlerInternal(HttpServletRequest request) throws Exception
167166

168167
/**
169168
* Look up a handler instance for the given URL path. This method is used
170-
* when parsed {@code PathPattern}s are
171-
* {@link #setPatternParser(PathPatternParser) enabled}.
169+
* when parsed {@code PathPattern}s are {@link #usesPathPatterns() enabled}.
172170
* @param path the parsed RequestPath
173171
* @param lookupPath the String lookupPath for checking direct hits
174172
* @param request current HTTP request
@@ -462,8 +460,8 @@ public final Map<String, Object> getHandlerMap() {
462460
}
463461

464462
/**
465-
* Identical to {@link #getHandlerMap()} but with parsed patterns when
466-
* {@link #setPatternParser(PathPatternParser)} is set, or otherwise empty.
463+
* Identical to {@link #getHandlerMap()} but populated when parsed patterns
464+
* are {@link #usesPathPatterns() enabled}; otherwise empty.
467465
* @since 5.3
468466
*/
469467
public final Map<PathPattern, Object> getPathPatternHandlerMap() {

Diff for: spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfo.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ public String getName() {
187187
}
188188

189189
/**
190-
* Return the patterns condition in use when parsed patterns are enabled via
191-
* {@link org.springframework.web.servlet.handler.AbstractHandlerMapping#setPatternParser(PathPatternParser)}.
190+
* Return the patterns condition in use when parsed patterns are
191+
* {@link AbstractHandlerMapping#usesPathPatterns() enabled}.
192192
* <p>This is mutually exclusive with {@link #getPatternsCondition()} such
193193
* that when one returns {@code null} the other one returns an instance.
194194
* @since 5.3
@@ -750,7 +750,7 @@ public static class BuilderConfiguration {
750750

751751

752752
/**
753-
* Enable use of parsed {@link PathPattern} as described in
753+
* Enable use of parsed {@link PathPattern}s as described in
754754
* {@link AbstractHandlerMapping#setPatternParser(PathPatternParser)}.
755755
* <p><strong>Note:</strong> This property is mutually exclusive with
756756
* {@link #setPathMatcher(PathMatcher)}.

0 commit comments

Comments
 (0)