Top Description Inners Fields Constructors Methods
org.springframework.web.servlet.handler

public Class HandlerMappingIntrospector

extends Object
implements CorsConfigurationSource, ApplicationContextAware, InitializingBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.Aware, org.springframework.web.cors.CorsConfigurationSource
Imports
java.io.IOException, java.util.ArrayList, .Collections, .Enumeration, .HashMap, .List, .Map, .Properties, java.util.concurrent.atomic.AtomicInteger, java.util.function.BiFunction, java.util.stream.Collectors, jakarta.servlet.DispatcherType, .Filter, .ServletRequest, jakarta.servlet.http.HttpServletRequest, .HttpServletRequestWrapper, org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.factory.BeanFactoryUtils, .InitializingBean, org.springframework.context.ApplicationContext, .ApplicationContextAware, org.springframework.core.annotation.AnnotationAwareOrderComparator, org.springframework.core.io.ClassPathResource, .Resource, org.springframework.core.io.support.PropertiesLoaderUtils, org.springframework.http.server.RequestPath, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, .StringUtils, org.springframework.web.cors.CorsConfiguration, .CorsConfigurationSource, org.springframework.web.servlet.DispatcherServlet, .HandlerExecutionChain, .HandlerInterceptor, .HandlerMapping, org.springframework.web.util.ServletRequestPathUtils, .UrlPathHelper, org.springframework.web.util.pattern.PathPatternParser

Helper class to get information from the HandlerMapping that would serve a specific request.

Provides the following methods:

Note that this is primarily an SPI to allow Spring Security to align its pattern matching with the same pattern matching that would be used in Spring MVC for a given request, in order to avoid security issues.

Use of this component incurs the performance overhead of mapping the request, and should not be repeated multiple times per request. createCacheFilter() exposes a Filter to cache the results. Applications that rely on Spring Security don't need to deploy this Filter since Spring Security doe that. However, other custom security layers, used in place of Spring Security that use this component should deploy the cache Filter with requirements described in the Javadoc for the method.

Author
Rossen Stoyanchev
Since
4.3.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
HandlerMappingIntrospector.AttributesPreservingRequest

Request wrapper that buffers request attributes in order protect the underlying request from attribute changes.

public static class
HandlerMappingIntrospector.CachedResult

Container for a MatchableHandlerMapping and CorsConfiguration for a given request matched by dispatcher type and requestURI.

private static class
private static class

Field Summary

Modifier and TypeField and Description
private ApplicationContext
private static final String
private final HandlerMappingIntrospector.CacheResultLogHelper
private List<HandlerMapping>
private static final Log
private Map<HandlerMapping, PathPatternMatchableHandlerMapping>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

public Filter

Returns:

the Filter instance to use
createCacheFilter
()

Filter that looks up the MatchableHandlerMapping and CorsConfiguration for the request proactively before delegating to the rest of the chain, caching the result in a request attribute, and restoring it after the chain returns.

private MatchableHandlerMapping
private <T> T
public CorsConfiguration
private static CorsConfiguration
public List<HandlerMapping>
getHandlerMappings()

Return the configured or detected HandlerMappings.

public MatchableHandlerMapping

Returns:

the resolved MatchableHandlerMapping, or null
getMatchableHandlerMapping
(HttpServletRequest
the current request
request
)

Find the HandlerMapping that would handle the given request and return a MatchableHandlerMapping to use for path matching.

private static List<HandlerMapping>
initFallback(ApplicationContext applicationContext)

private static List<HandlerMapping>
public void
public void
setApplicationContext(ApplicationContext
the ApplicationContext object to be used by this object
applicationContext
)

Implements org.springframework.context.ApplicationContextAware.setApplicationContext.

Set the ApplicationContext that this object runs in. Normally this call will be used to initialize the object.

public HandlerMappingIntrospector.CachedResult

Returns:

the previous CachedResult, if there is one from a parent dispatch
setCache
(HttpServletRequest
the current request
request
)

Perform a lookup and save the CachedResult as a request attribute.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

applicationContextback to summary
private ApplicationContext applicationContext
Annotations
@Nullable
CACHED_RESULT_ATTRIBUTEback to summary
private static final String CACHED_RESULT_ATTRIBUTE
cacheLogHelperback to summary
private final HandlerMappingIntrospector.CacheResultLogHelper cacheLogHelper
handlerMappingsback to summary
private List<HandlerMapping> handlerMappings
Annotations
@Nullable
loggerback to summary
private static final Log logger
pathPatternMappingsback to summary
private Map<HandlerMapping, PathPatternMatchableHandlerMapping> pathPatternMappings

Constructor Detail

HandlerMappingIntrospectorback to summary
public HandlerMappingIntrospector()

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Doc from org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

Annotations
@Override
createCacheFilterback to summary
public Filter createCacheFilter()

Filter that looks up the MatchableHandlerMapping and CorsConfiguration for the request proactively before delegating to the rest of the chain, caching the result in a request attribute, and restoring it after the chain returns.

Note

Applications that rely on Spring Security do not use this component directly and should not deploy the filter instead allowing Spring Security to do it. Other custom security layers used in place of Spring Security that also rely on HandlerMappingIntrospector should deploy this filter ahead of other filters where lookups are performed, and should also make sure the filter is configured to handle all dispatcher types.

Returns:Filter

the Filter instance to use

Since
6.0.14
createMatchableHandlerMappingback to summary
private MatchableHandlerMapping createMatchableHandlerMapping(HandlerMapping mapping, HttpServletRequest request)
doWithHandlerMappingback to summary
private <T> T doWithHandlerMapping(HttpServletRequest request, boolean ignoreException, BiFunction<HandlerMapping, HandlerExecutionChain, T> extractor) throws Exception
Annotations
@Nullable
getCorsConfigurationback to summary
public CorsConfiguration getCorsConfiguration(HttpServletRequest request)

Implements org.springframework.web.cors.CorsConfigurationSource.getCorsConfiguration.

Doc from org.springframework.web.cors.CorsConfigurationSource.getCorsConfiguration.

Return a CorsConfiguration based on the incoming request.

Returns:CorsConfiguration

the associated CorsConfiguration, or null if none

Annotations
@Override
@Nullable
getCorsConfigurationback to summary
private static CorsConfiguration getCorsConfiguration(HandlerExecutionChain chain, HttpServletRequest request)
Annotations
@Nullable
getHandlerMappingsback to summary
public List<HandlerMapping> getHandlerMappings()

Return the configured or detected HandlerMappings.

getMatchableHandlerMappingback to summary
public MatchableHandlerMapping getMatchableHandlerMapping(HttpServletRequest request) throws Exception

Find the HandlerMapping that would handle the given request and return a MatchableHandlerMapping to use for path matching.

Parameters
request:HttpServletRequest

the current request

Returns:MatchableHandlerMapping

the resolved MatchableHandlerMapping, or null

Annotations
@Nullable
Exceptions
Exception:
if any of the HandlerMapping's raise an exception
IllegalStateException:
if the matching HandlerMapping is not an instance of MatchableHandlerMapping
initFallbackback to summary
private static List<HandlerMapping> initFallback(ApplicationContext applicationContext)
initHandlerMappingsback to summary
private static List<HandlerMapping> initHandlerMappings(ApplicationContext context)
resetCacheback to summary
public void resetCache(ServletRequest request, HandlerMappingIntrospector.CachedResult cachedResult)

Restore a previous CachedResult. This method can be invoked from a filter after delegating to the rest of the chain.

Since
6.0.14
setApplicationContextback to summary
public void setApplicationContext(ApplicationContext applicationContext)

Implements org.springframework.context.ApplicationContextAware.setApplicationContext.

Doc from org.springframework.context.ApplicationContextAware.setApplicationContext.

Set the ApplicationContext that this object runs in. Normally this call will be used to initialize the object.

Invoked after population of normal bean properties but before an init callback such as org.springframework.beans.factory.InitializingBean#afterPropertiesSet() or a custom init-method. Invoked after ResourceLoaderAware#setResourceLoader, ApplicationEventPublisherAware#setApplicationEventPublisher and MessageSourceAware, if applicable.

Parameters
applicationContext:ApplicationContext

the ApplicationContext object to be used by this object

Annotations
@Override
setCacheback to summary
public HandlerMappingIntrospector.CachedResult setCache(HttpServletRequest request)

Perform a lookup and save the CachedResult as a request attribute. This method can be invoked from a filter before subsequent calls to getMatchableHandlerMapping(HttpServletRequest) and getCorsConfiguration(HttpServletRequest) to avoid repeated lookups.

Parameters
request:HttpServletRequest

the current request

Returns:HandlerMappingIntrospector.CachedResult

the previous CachedResult, if there is one from a parent dispatch

Annotations
@Nullable
Since
6.0.14
org.springframework.web.servlet.handler back to summary

private Class HandlerMappingIntrospector.AttributesPreservingRequest

extends HttpServletRequestWrapper
Class Inheritance

Request wrapper that buffers request attributes in order protect the underlying request from attribute changes.

Field Summary

Modifier and TypeField and Description
private final Map<String, Object>

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public Object
getAttribute(String
a String specifying the name of the attribute
name
)

Overrides jakarta.servlet.ServletRequestWrapper.getAttribute.

Implements jakarta.servlet.ServletRequest.getAttribute.

Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.

public Enumeration<String>
getAttributeNames()

Overrides jakarta.servlet.ServletRequestWrapper.getAttributeNames.

Implements jakarta.servlet.ServletRequest.getAttributeNames.

Returns an Enumeration containing the names of the attributes available to this request.

private Map<String, Object>
public void
removeAttribute(String
a String specifying the name of the attribute to remove
name
)

Overrides jakarta.servlet.ServletRequestWrapper.removeAttribute.

Implements jakarta.servlet.ServletRequest.removeAttribute.

Removes an attribute from this request.

public void
setAttribute(String
a String specifying the name of the attribute
name
,
Object
the Object to be stored
value
)

Overrides jakarta.servlet.ServletRequestWrapper.setAttribute.

Implements jakarta.servlet.ServletRequest.setAttribute.

Stores an attribute in this request.

Inherited from jakarta.servlet.http.HttpServletRequestWrapper:
authenticatechangeSessionIdgetAuthTypegetContextPathgetCookiesgetDateHeadergetHeadergetHeaderNamesgetHeadersgetHttpServletMappinggetIntHeadergetMethodgetPartgetPartsgetPathInfogetPathTranslatedgetQueryStringgetRemoteUsergetRequestedSessionIdgetRequestURIgetRequestURLgetServletPathgetSessiongetSessiongetTrailerFieldsgetUserPrincipalisRequestedSessionIdFromCookieisRequestedSessionIdFromURLisRequestedSessionIdValidisTrailerFieldsReadyisUserInRoleloginlogoutnewPushBuilderupgrade

Field Detail

attributesback to summary
private final Map<String, Object> attributes

Constructor Detail

AttributesPreservingRequestback to summary
pack-priv AttributesPreservingRequest(HttpServletRequest request)

Method Detail

getAttributeback to summary
public Object getAttribute(String name)

Overrides jakarta.servlet.ServletRequestWrapper.getAttribute.

Implements jakarta.servlet.ServletRequest.getAttribute.

Doc from jakarta.servlet.ServletRequest.getAttribute.

Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.

Attributes can be set two ways. The servlet container may set attributes to make available custom information about a request. For example, for requests made using HTTPS, the attribute jakarta.servlet.request.X509Certificate can be used to retrieve information on the certificate of the client. Attributes can also be set programmatically using ServletRequest#setAttribute. This allows information to be embedded into a request before a RequestDispatcher call.

Attribute names should follow the same conventions as package names. Names beginning with jakarta.* are reserved for use by the Jakarta EE platform.

Parameters
name:String

a String specifying the name of the attribute

Returns:Object

an Object containing the value of the attribute, or null if the attribute does not exist

Annotations
@Override
getAttributeNamesback to summary
public Enumeration<String> getAttributeNames()

Overrides jakarta.servlet.ServletRequestWrapper.getAttributeNames.

Implements jakarta.servlet.ServletRequest.getAttributeNames.

Doc from jakarta.servlet.ServletRequest.getAttributeNames.

Returns an Enumeration containing the names of the attributes available to this request. This method returns an empty Enumeration if the request has no attributes available to it.

Returns:Enumeration<String>

an Enumeration of strings containing the names of the request's attributes

Annotations
@Override
initAttributesback to summary
private Map<String, Object> initAttributes(HttpServletRequest request)
removeAttributeback to summary
public void removeAttribute(String name)

Overrides jakarta.servlet.ServletRequestWrapper.removeAttribute.

Implements jakarta.servlet.ServletRequest.removeAttribute.

Doc from jakarta.servlet.ServletRequest.removeAttribute.

Removes an attribute from this request. This method is not generally needed as attributes only persist as long as the request is being handled.

Attribute names should follow the same conventions as package names. Names beginning with jakarta.* are reserved for use by the Jakarta EE platform.

Parameters
name:String

a String specifying the name of the attribute to remove

Annotations
@Override
setAttributeback to summary
public void setAttribute(String name, Object value)

Overrides jakarta.servlet.ServletRequestWrapper.setAttribute.

Implements jakarta.servlet.ServletRequest.setAttribute.

Doc from jakarta.servlet.ServletRequest.setAttribute.

Stores an attribute in this request. Attributes are reset between requests. This method is most often used in conjunction with RequestDispatcher.

Attribute names should follow the same conventions as package names. Names beginning with jakarta.* are reserved for use by the Jakarta EE platform.

If the object passed in is null, the effect is the same as calling removeAttribute.
It is warned that when the request is dispatched from the servlet resides in a different web application by RequestDispatcher, the object set by this method may not be correctly retrieved in the caller servlet.

Parameters
name:String

a String specifying the name of the attribute

value:Object

the Object to be stored

Annotations
@Override
org.springframework.web.servlet.handler back to summary

public final Class HandlerMappingIntrospector.CachedResult

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.web.servlet.handler.HandlerMappingIntrospector.CachedResult
Annotations
@SuppressWarnings:serial

Container for a MatchableHandlerMapping and CorsConfiguration for a given request matched by dispatcher type and requestURI.
Since
6.0.14

Field Summary

Modifier and TypeField and Description
private final CorsConfiguration
private final IllegalStateException
private final DispatcherType
private final Exception
private final MatchableHandlerMapping
private final String

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public CorsConfiguration
public MatchableHandlerMapping
public static HandlerMappingIntrospector.CachedResult
getResultFor(HttpServletRequest request)

Return a CachedResult that matches the given request.

public boolean
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

corsConfigback to summary
private final CorsConfiguration corsConfig
Annotations
@Nullable
corsConfigFailureback to summary
private final IllegalStateException corsConfigFailure
Annotations
@Nullable
dispatcherTypeback to summary
private final DispatcherType dispatcherType
failureback to summary
private final Exception failure
Annotations
@Nullable
handlerMappingback to summary
private final MatchableHandlerMapping handlerMapping
Annotations
@Nullable
requestURIback to summary
private final String requestURI

Constructor Detail

CachedResultback to summary
private CachedResult(HttpServletRequest request, MatchableHandlerMapping mapping, CorsConfiguration config, Exception failure, IllegalStateException corsConfigFailure)

Method Detail

getCorsConfigback to summary
public CorsConfiguration getCorsConfig()
Annotations
@Nullable
getHandlerMappingback to summary
public MatchableHandlerMapping getHandlerMapping() throws Exception
Annotations
@Nullable
getResultForback to summary
public static HandlerMappingIntrospector.CachedResult getResultFor(HttpServletRequest request)

Return a CachedResult that matches the given request.

Annotations
@Nullable
matchesback to summary
public boolean matches(HttpServletRequest request)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
org.springframework.web.servlet.handler back to summary

private Class HandlerMappingIntrospector.CacheResultLogHelper

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.web.servlet.handler.HandlerMappingIntrospector.CacheResultLogHelper

Field Summary

Modifier and TypeField and Description
private final Map<String, AtomicInteger>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static String
private void
public void
public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

countersback to summary
private final Map<String, AtomicInteger> counters

Constructor Detail

CacheResultLogHelperback to summary
private CacheResultLogHelper()

Method Detail

getLogMessageback to summary
private static String getLogMessage(String label, HttpServletRequest request)
logCacheMissback to summary
private void logCacheMiss(String label, HttpServletRequest request)
logCorsConfigCacheMissback to summary
public void logCorsConfigCacheMiss(HttpServletRequest request)
logHandlerMappingCacheMissback to summary
public void logHandlerMappingCacheMiss(HttpServletRequest request)
org.springframework.web.servlet.handler back to summary

private Class HandlerMappingIntrospector.LookupPathMatchableHandlerMapping

extends Object
implements MatchableHandlerMapping
Class Inheritance
  • java.lang.Object
  • org.springframework.web.servlet.handler.HandlerMappingIntrospector.LookupPathMatchableHandlerMapping
All Implemented Interfaces
org.springframework.web.servlet.handler.MatchableHandlerMapping, org.springframework.web.servlet.HandlerMapping

Field Summary

Modifier and TypeField and Description
private final MatchableHandlerMapping
private final Object
private final String

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public HandlerExecutionChain
getHandler(HttpServletRequest
current HTTP request
request
)

Implements org.springframework.web.servlet.HandlerMapping.getHandler.

Return a handler and any interceptors for this request.

public PathPatternParser
getPatternParser()

Overrides default org.springframework.web.servlet.handler.MatchableHandlerMapping.getPatternParser.

Return the parser of this HandlerMapping, if configured in which case pre-parsed patterns are used.

private String
public RequestMatchResult
match(HttpServletRequest
the current request
request
,
String
the pattern to match
pattern
)

Implements org.springframework.web.servlet.handler.MatchableHandlerMapping.match.

Determine whether the request matches the given pattern.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

delegateback to summary
private final MatchableHandlerMapping delegate
lookupPathback to summary
private final Object lookupPath
pathAttributeNameback to summary
private final String pathAttributeName

Constructor Detail

LookupPathMatchableHandlerMappingback to summary
pack-priv LookupPathMatchableHandlerMapping(MatchableHandlerMapping delegate, Object lookupPath)

Method Detail

getHandlerback to summary
public HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception

Implements org.springframework.web.servlet.HandlerMapping.getHandler.

Doc from org.springframework.web.servlet.HandlerMapping.getHandler.

Return a handler and any interceptors for this request. The choice may be made on request URL, session state, or any factor the implementing class chooses.

The returned HandlerExecutionChain contains a handler Object, rather than even a tag interface, so that handlers are not constrained in any way. For example, a HandlerAdapter could be written to allow another framework's handler objects to be used.

Returns null if no match was found. This is not an error. The DispatcherServlet will query all registered HandlerMapping beans to find a match, and only decide there is an error if none can find a handler.

Parameters
request:HttpServletRequest

current HTTP request

Returns:HandlerExecutionChain

a HandlerExecutionChain instance containing handler object and any interceptors, or null if no mapping found

Annotations
@Nullable
@Override
Exceptions
Exception:
if there is an internal error
getPatternParserback to summary
public PathPatternParser getPatternParser()

Overrides default org.springframework.web.servlet.handler.MatchableHandlerMapping.getPatternParser.

Doc from org.springframework.web.servlet.handler.MatchableHandlerMapping.getPatternParser.

Return the parser of this HandlerMapping, if configured in which case pre-parsed patterns are used.

Annotations
@Override
initFullPathPatternback to summary
private String initFullPathPattern(String pattern)
matchback to summary
public RequestMatchResult match(HttpServletRequest request, String pattern)

Implements org.springframework.web.servlet.handler.MatchableHandlerMapping.match.

Doc from org.springframework.web.servlet.handler.MatchableHandlerMapping.match.

Determine whether the request matches the given pattern. Use this method when getPatternParser() returns null which means that the HandlerMapping is using String pattern matching.

Parameters
request:HttpServletRequest

the current request

pattern:String

the pattern to match

Returns:RequestMatchResult

the result from request matching, or null if none

Annotations
@Nullable
@Override