HandlerMappingIntrospector ensures initialized RequestPath
Closes gh-26814
This commit is contained in:
parent
0f31830ae2
commit
69bbdce826
|
|
@ -968,7 +968,9 @@ public class DispatcherServlet extends FrameworkServlet {
|
||||||
restoreAttributesAfterInclude(request, attributesSnapshot);
|
restoreAttributesAfterInclude(request, attributesSnapshot);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ServletRequestPathUtils.setParsedRequestPath(previousRequestPath, request);
|
if (this.parseRequestPath) {
|
||||||
|
ServletRequestPathUtils.setParsedRequestPath(previousRequestPath, request);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2020 the original author or authors.
|
* Copyright 2002-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -23,6 +23,7 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
@ -36,6 +37,7 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
||||||
|
import org.springframework.http.server.RequestPath;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
|
|
@ -46,6 +48,7 @@ import org.springframework.web.servlet.DispatcherServlet;
|
||||||
import org.springframework.web.servlet.HandlerExecutionChain;
|
import org.springframework.web.servlet.HandlerExecutionChain;
|
||||||
import org.springframework.web.servlet.HandlerInterceptor;
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
import org.springframework.web.servlet.HandlerMapping;
|
import org.springframework.web.servlet.HandlerMapping;
|
||||||
|
import org.springframework.web.util.ServletRequestPathUtils;
|
||||||
import org.springframework.web.util.UrlPathHelper;
|
import org.springframework.web.util.UrlPathHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -79,8 +82,7 @@ public class HandlerMappingIntrospector
|
||||||
private List<HandlerMapping> handlerMappings;
|
private List<HandlerMapping> handlerMappings;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private Map<HandlerMapping, MatchableHandlerMapping> pathPatternMatchableHandlerMappings =
|
private Map<HandlerMapping, PathPatternMatchableHandlerMapping> pathPatternHandlerMappings = new ConcurrentHashMap<>();
|
||||||
new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -119,7 +121,7 @@ public class HandlerMappingIntrospector
|
||||||
if (this.handlerMappings == null) {
|
if (this.handlerMappings == null) {
|
||||||
Assert.notNull(this.applicationContext, "No ApplicationContext");
|
Assert.notNull(this.applicationContext, "No ApplicationContext");
|
||||||
this.handlerMappings = initHandlerMappings(this.applicationContext);
|
this.handlerMappings = initHandlerMappings(this.applicationContext);
|
||||||
this.pathPatternMatchableHandlerMappings = initPathPatternMatchableHandlerMappings(this.handlerMappings);
|
this.pathPatternHandlerMappings = initPathPatternMatchableHandlerMappings(this.handlerMappings);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -136,51 +138,90 @@ public class HandlerMappingIntrospector
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public MatchableHandlerMapping getMatchableHandlerMapping(HttpServletRequest request) throws Exception {
|
public MatchableHandlerMapping getMatchableHandlerMapping(HttpServletRequest request) throws Exception {
|
||||||
Assert.notNull(this.handlerMappings, "Handler mappings not initialized");
|
HttpServletRequest wrappedRequest = new RequestAttributeChangeIgnoringWrapper(request);
|
||||||
Assert.notNull(this.pathPatternMatchableHandlerMappings, "Handler mappings with PathPatterns not initialized");
|
return doWithMatchingMapping(wrappedRequest, false, (matchedMapping, executionChain) -> {
|
||||||
HttpServletRequest wrapper = new RequestAttributeChangeIgnoringWrapper(request);
|
if (matchedMapping instanceof MatchableHandlerMapping) {
|
||||||
for (HandlerMapping handlerMapping : this.handlerMappings) {
|
PathPatternMatchableHandlerMapping mapping = this.pathPatternHandlerMappings.get(matchedMapping);
|
||||||
Object handler = handlerMapping.getHandler(wrapper);
|
if (mapping != null) {
|
||||||
if (handler == null) {
|
RequestPath requestPath = ServletRequestPathUtils.getParsedRequestPath(request);
|
||||||
continue;
|
return mapping.decorate(requestPath);
|
||||||
}
|
}
|
||||||
if (handlerMapping instanceof MatchableHandlerMapping) {
|
else {
|
||||||
return this.pathPatternMatchableHandlerMappings.getOrDefault(
|
return (MatchableHandlerMapping) matchedMapping;
|
||||||
handlerMapping, (MatchableHandlerMapping) handlerMapping);
|
}
|
||||||
}
|
}
|
||||||
throw new IllegalStateException("HandlerMapping is not a MatchableHandlerMapping");
|
throw new IllegalStateException("HandlerMapping is not a MatchableHandlerMapping");
|
||||||
}
|
});
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Nullable
|
@Nullable
|
||||||
public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
|
public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
|
||||||
Assert.notNull(this.handlerMappings, "Handler mappings not initialized");
|
RequestAttributeChangeIgnoringWrapper wrappedRequest = new RequestAttributeChangeIgnoringWrapper(request);
|
||||||
RequestAttributeChangeIgnoringWrapper wrapper = new RequestAttributeChangeIgnoringWrapper(request);
|
return doWithMatchingMappingIgnoringException(wrappedRequest, (handlerMapping, executionChain) -> {
|
||||||
for (HandlerMapping handlerMapping : this.handlerMappings) {
|
for (HandlerInterceptor interceptor : executionChain.getInterceptorList()) {
|
||||||
HandlerExecutionChain handler = null;
|
|
||||||
try {
|
|
||||||
handler = handlerMapping.getHandler(wrapper);
|
|
||||||
}
|
|
||||||
catch (Exception ex) {
|
|
||||||
// Ignore
|
|
||||||
}
|
|
||||||
if (handler == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
for (HandlerInterceptor interceptor : handler.getInterceptorList()) {
|
|
||||||
if (interceptor instanceof CorsConfigurationSource) {
|
if (interceptor instanceof CorsConfigurationSource) {
|
||||||
return ((CorsConfigurationSource) interceptor).getCorsConfiguration(wrapper);
|
return ((CorsConfigurationSource) interceptor).getCorsConfiguration(wrappedRequest);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (handler.getHandler() instanceof CorsConfigurationSource) {
|
if (executionChain.getHandler() instanceof CorsConfigurationSource) {
|
||||||
return ((CorsConfigurationSource) handler.getHandler()).getCorsConfiguration(wrapper);
|
return ((CorsConfigurationSource) executionChain.getHandler()).getCorsConfiguration(wrappedRequest);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private <T> T doWithMatchingMapping(
|
||||||
|
HttpServletRequest request, boolean ignoreException,
|
||||||
|
BiFunction<HandlerMapping, HandlerExecutionChain, T> matchHandler) throws Exception {
|
||||||
|
|
||||||
|
Assert.notNull(this.handlerMappings, "Handler mappings not initialized");
|
||||||
|
Assert.notNull(this.pathPatternHandlerMappings, "Handler mappings with PathPatterns not initialized");
|
||||||
|
|
||||||
|
boolean parseRequestPath = !this.pathPatternHandlerMappings.isEmpty();
|
||||||
|
RequestPath previousPath = null;
|
||||||
|
if (parseRequestPath) {
|
||||||
|
previousPath = (RequestPath) request.getAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE);
|
||||||
|
ServletRequestPathUtils.parseAndCache(request);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
for (HandlerMapping handlerMapping : this.handlerMappings) {
|
||||||
|
HandlerExecutionChain chain = null;
|
||||||
|
try {
|
||||||
|
chain = handlerMapping.getHandler(request);
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
if (!ignoreException) {
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (chain == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return matchHandler.apply(handlerMapping, chain);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
if (parseRequestPath) {
|
||||||
|
ServletRequestPathUtils.setParsedRequestPath(previousPath, request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private <T> T doWithMatchingMappingIgnoringException(
|
||||||
|
HttpServletRequest request, BiFunction<HandlerMapping, HandlerExecutionChain, T> matchHandler) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
return doWithMatchingMapping(request, true, matchHandler);
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
throw new IllegalStateException("HandlerMapping exception not suppressed", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private static List<HandlerMapping> initHandlerMappings(ApplicationContext applicationContext) {
|
private static List<HandlerMapping> initHandlerMappings(ApplicationContext applicationContext) {
|
||||||
Map<String, HandlerMapping> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
|
Map<String, HandlerMapping> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
|
||||||
|
|
@ -219,7 +260,7 @@ public class HandlerMappingIntrospector
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Map<HandlerMapping, MatchableHandlerMapping> initPathPatternMatchableHandlerMappings(
|
private static Map<HandlerMapping, PathPatternMatchableHandlerMapping> initPathPatternMatchableHandlerMappings(
|
||||||
List<HandlerMapping> mappings) {
|
List<HandlerMapping> mappings) {
|
||||||
|
|
||||||
return mappings.stream()
|
return mappings.stream()
|
||||||
|
|
@ -241,8 +282,7 @@ public class HandlerMappingIntrospector
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setAttribute(String name, Object value) {
|
public void setAttribute(String name, Object value) {
|
||||||
// Allow UrlPathHelper-resolved lookupPath to be saved for efficiency
|
if (name.equals(ServletRequestPathUtils.PATH_ATTRIBUTE) || name.equals(UrlPathHelper.PATH_ATTRIBUTE)) {
|
||||||
if (name.equals(UrlPathHelper.PATH_ATTRIBUTE)) {
|
|
||||||
super.setAttribute(name, value);
|
super.setAttribute(name, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2020 the original author or authors.
|
* Copyright 2002-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -21,6 +21,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
import org.springframework.http.server.PathContainer;
|
import org.springframework.http.server.PathContainer;
|
||||||
|
import org.springframework.http.server.RequestPath;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.web.servlet.HandlerExecutionChain;
|
import org.springframework.web.servlet.HandlerExecutionChain;
|
||||||
|
|
@ -70,4 +71,39 @@ class PathPatternMatchableHandlerMapping implements MatchableHandlerMapping {
|
||||||
public HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
|
public HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
|
||||||
return this.delegate.getHandler(request);
|
return this.delegate.getHandler(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MatchableHandlerMapping decorate(RequestPath requestPath) {
|
||||||
|
return new MatchableHandlerMapping() {
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public RequestMatchResult match(HttpServletRequest request, String pattern) {
|
||||||
|
RequestPath previousPath = setRequestPathAttribute(request);
|
||||||
|
try {
|
||||||
|
return PathPatternMatchableHandlerMapping.this.match(request, pattern);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
ServletRequestPathUtils.setParsedRequestPath(previousPath, request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
|
||||||
|
RequestPath previousPath = setRequestPathAttribute(request);
|
||||||
|
try {
|
||||||
|
return PathPatternMatchableHandlerMapping.this.getHandler(request);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
ServletRequestPathUtils.setParsedRequestPath(previousPath, request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private RequestPath setRequestPathAttribute(HttpServletRequest request) {
|
||||||
|
RequestPath previous = (RequestPath) request.getAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE);
|
||||||
|
ServletRequestPathUtils.setParsedRequestPath(requestPath, request);
|
||||||
|
return previous;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -127,16 +127,11 @@ public class HandlerMappingIntrospectorTests {
|
||||||
context.refresh();
|
context.refresh();
|
||||||
|
|
||||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path/123");
|
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path/123");
|
||||||
|
|
||||||
// Initialize the RequestPath. At runtime, ServletRequestPathFilter is expected to do that.
|
|
||||||
if (usePathPatterns) {
|
|
||||||
ServletRequestPathUtils.parseAndCache(request);
|
|
||||||
}
|
|
||||||
|
|
||||||
MatchableHandlerMapping mapping = initIntrospector(context).getMatchableHandlerMapping(request);
|
MatchableHandlerMapping mapping = initIntrospector(context).getMatchableHandlerMapping(request);
|
||||||
|
|
||||||
assertThat(mapping).isNotNull();
|
assertThat(mapping).isNotNull();
|
||||||
assertThat(request.getAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE)).as("Attribute changes not ignored").isNull();
|
assertThat(request.getAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE)).as("Attribute changes not ignored").isNull();
|
||||||
|
assertThat(request.getAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE)).as("Parsed path not cleaned").isNull();
|
||||||
|
|
||||||
assertThat(mapping.match(request, "/p*/*")).isNotNull();
|
assertThat(mapping.match(request, "/p*/*")).isNotNull();
|
||||||
assertThat(mapping.match(request, "/b*/*")).isNull();
|
assertThat(mapping.match(request, "/b*/*")).isNull();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue