public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport implements HandlerMapping, Ordered, BeanNameAware
HandlerMapping
implementations. Supports ordering, a default handler, handler interceptors,
including handler interceptors mapped by path patterns.
Note: This base class does not support exposure of the
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
. Support for this attribute
is up to concrete subclasses, typically based on request URL mappings.
getHandlerInternal(javax.servlet.http.HttpServletRequest)
,
setDefaultHandler(java.lang.Object)
,
setAlwaysUseFullPath(boolean)
,
setUrlDecode(boolean)
,
AntPathMatcher
,
setInterceptors(java.lang.Object...)
,
HandlerInterceptor
logger
BEST_MATCHING_HANDLER_ATTRIBUTE, BEST_MATCHING_PATTERN_ATTRIBUTE, INTROSPECT_TYPE_LEVEL_MAPPING, LOOKUP_PATH, MATRIX_VARIABLES_ATTRIBUTE, PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, URI_TEMPLATE_VARIABLES_ATTRIBUTE
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor and Description |
---|
AbstractHandlerMapping() |
Modifier and Type | Method and Description |
---|---|
protected HandlerInterceptor |
adaptInterceptor(Object interceptor)
Adapt the given interceptor object to
HandlerInterceptor . |
protected void |
detectMappedInterceptors(List<HandlerInterceptor> mappedInterceptors)
Detect beans of type
MappedInterceptor and add them to the list
of mapped interceptors. |
protected void |
extendInterceptors(List<Object> interceptors)
Extension hook that subclasses can override to register additional interceptors,
given the configured interceptors (see
setInterceptors(java.lang.Object...) ). |
protected String |
formatMappingName() |
protected HandlerInterceptor[] |
getAdaptedInterceptors()
Return the adapted interceptors as
HandlerInterceptor array. |
protected CorsConfiguration |
getCorsConfiguration(Object handler,
HttpServletRequest request)
Retrieve the CORS configuration for the given handler.
|
protected HandlerExecutionChain |
getCorsHandlerExecutionChain(HttpServletRequest request,
HandlerExecutionChain chain,
CorsConfiguration config)
Update the HandlerExecutionChain for CORS-related handling.
|
CorsProcessor |
getCorsProcessor()
Return the configured
CorsProcessor . |
Object |
getDefaultHandler()
Return the default handler for this handler mapping,
or
null if none. |
HandlerExecutionChain |
getHandler(HttpServletRequest request)
Look up a handler for the given request, falling back to the default
handler if no specific one is found.
|
protected HandlerExecutionChain |
getHandlerExecutionChain(Object handler,
HttpServletRequest request)
Build a
HandlerExecutionChain for the given handler, including
applicable interceptors. |
protected abstract Object |
getHandlerInternal(HttpServletRequest request)
Look up a handler for the given request, returning
null if no
specific one is found. |
protected MappedInterceptor[] |
getMappedInterceptors()
Return all configured
MappedInterceptor s as an array. |
int |
getOrder()
Get the order value of this object.
|
PathMatcher |
getPathMatcher()
Return the PathMatcher implementation to use for matching URL paths
against registered URL patterns.
|
UrlPathHelper |
getUrlPathHelper()
Return the UrlPathHelper implementation to use for resolution of lookup paths.
|
protected boolean |
hasCorsConfigurationSource(Object handler)
Return
true if there is a CorsConfigurationSource for this handler. |
protected void |
initApplicationContext()
Initializes the interceptors.
|
protected void |
initInterceptors()
Initialize the specified interceptors adapting
WebRequestInterceptor s to HandlerInterceptor . |
void |
setAlwaysUseFullPath(boolean alwaysUseFullPath)
Shortcut to same property on underlying
UrlPathHelper . |
void |
setBeanName(String name)
Set the name of the bean in the bean factory that created this bean.
|
void |
setCorsConfigurations(Map<String,CorsConfiguration> corsConfigurations)
Set the "global" CORS configurations based on URL patterns.
|
void |
setCorsConfigurationSource(CorsConfigurationSource corsConfigurationSource)
Set the "global" CORS configuration source.
|
void |
setCorsProcessor(CorsProcessor corsProcessor)
Configure a custom
CorsProcessor to use to apply the matched
CorsConfiguration for a request. |
void |
setDefaultHandler(Object defaultHandler)
Set the default handler for this handler mapping.
|
void |
setInterceptors(Object... interceptors)
Set the interceptors to apply for all handlers mapped by this handler mapping.
|
void |
setOrder(int order)
Specify the order value for this HandlerMapping bean.
|
void |
setPathMatcher(PathMatcher pathMatcher)
Set the PathMatcher implementation to use for matching URL paths
against registered URL patterns.
|
void |
setRemoveSemicolonContent(boolean removeSemicolonContent)
Shortcut to same property on underlying
UrlPathHelper . |
void |
setUrlDecode(boolean urlDecode)
Shortcut to same property on underlying
UrlPathHelper . |
void |
setUrlPathHelper(UrlPathHelper urlPathHelper)
Set the UrlPathHelper to use for resolution of lookup paths.
|
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, obtainApplicationContext, requiredContextClass, setApplicationContext
public void setDefaultHandler(@Nullable Object defaultHandler)
Default is null
, indicating no default handler.
@Nullable public Object getDefaultHandler()
null
if none.public void setAlwaysUseFullPath(boolean alwaysUseFullPath)
UrlPathHelper
.public void setUrlDecode(boolean urlDecode)
UrlPathHelper
.UrlPathHelper.setUrlDecode(boolean)
public void setRemoveSemicolonContent(boolean removeSemicolonContent)
UrlPathHelper
.public void setUrlPathHelper(UrlPathHelper urlPathHelper)
Use this to override the default UrlPathHelper with a custom subclass, or to share common UrlPathHelper settings across multiple HandlerMappings and MethodNameResolvers.
public UrlPathHelper getUrlPathHelper()
public void setPathMatcher(PathMatcher pathMatcher)
AntPathMatcher
public PathMatcher getPathMatcher()
public void setInterceptors(Object... interceptors)
Supported interceptor types are HandlerInterceptor, WebRequestInterceptor, and MappedInterceptor. Mapped interceptors apply only to request URLs that match its path patterns. Mapped interceptor beans are also detected by type during initialization.
interceptors
- array of handler interceptorsadaptInterceptor(java.lang.Object)
,
HandlerInterceptor
,
WebRequestInterceptor
public void setCorsConfigurations(Map<String,CorsConfiguration> corsConfigurations)
setCorsConfigurationSource(CorsConfigurationSource)
public void setCorsConfigurationSource(CorsConfigurationSource corsConfigurationSource)
setCorsConfigurations(Map)
public void setCorsProcessor(CorsProcessor corsProcessor)
CorsProcessor
to use to apply the matched
CorsConfiguration
for a request.
By default DefaultCorsProcessor
is used.
public CorsProcessor getCorsProcessor()
CorsProcessor
.public void setOrder(int order)
The default value is Ordered.LOWEST_PRECEDENCE
, meaning non-ordered.
Ordered.getOrder()
public int getOrder()
Ordered
Higher values are interpreted as lower priority. As a consequence,
the object with the lowest value has the highest priority (somewhat
analogous to Servlet load-on-startup
values).
Same order values will result in arbitrary sort positions for the affected objects.
getOrder
in interface Ordered
Ordered.HIGHEST_PRECEDENCE
,
Ordered.LOWEST_PRECEDENCE
public void setBeanName(String name)
BeanNameAware
Invoked after population of normal bean properties but before an
init callback such as InitializingBean.afterPropertiesSet()
or a custom init-method.
setBeanName
in interface BeanNameAware
name
- the name of the bean in the factory.
Note that this name is the actual bean name used in the factory, which may
differ from the originally specified name: in particular for inner bean
names, the actual bean name might have been made unique through appending
"#..." suffixes. Use the BeanFactoryUtils.originalBeanName(String)
method to extract the original bean name (without suffix), if desired.protected String formatMappingName()
protected void initApplicationContext() throws BeansException
initApplicationContext
in class ApplicationObjectSupport
ApplicationContextException
- in case of initialization errorsBeansException
- if thrown by ApplicationContext methodsextendInterceptors(java.util.List)
,
initInterceptors()
protected void extendInterceptors(List<Object> interceptors)
setInterceptors(java.lang.Object...)
).
Will be invoked before initInterceptors()
adapts the specified
interceptors into HandlerInterceptor
instances.
The default implementation is empty.
interceptors
- the configured interceptor List (never null
), allowing
to add further interceptors before as well as after the existing interceptorsprotected void detectMappedInterceptors(List<HandlerInterceptor> mappedInterceptors)
MappedInterceptor
and add them to the list
of mapped interceptors.
This is called in addition to any MappedInterceptor
s that may
have been provided via setInterceptors(java.lang.Object...)
, by default adding all
beans of type MappedInterceptor
from the current context and its
ancestors. Subclasses can override and refine this policy.
mappedInterceptors
- an empty list to add toprotected void initInterceptors()
WebRequestInterceptor
s to HandlerInterceptor
.protected HandlerInterceptor adaptInterceptor(Object interceptor)
HandlerInterceptor
.
By default, the supported interceptor types are
HandlerInterceptor
and WebRequestInterceptor
. Each given
WebRequestInterceptor
is wrapped with
WebRequestHandlerInterceptorAdapter
.
interceptor
- the interceptorHandlerInterceptor
,
WebRequestInterceptor
,
WebRequestHandlerInterceptorAdapter
@Nullable protected final HandlerInterceptor[] getAdaptedInterceptors()
HandlerInterceptor
array.HandlerInterceptor
s,
or null
if none@Nullable protected final MappedInterceptor[] getMappedInterceptors()
MappedInterceptor
s as an array.MappedInterceptor
s, or null
if none@Nullable public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception
getHandler
in interface HandlerMapping
request
- current HTTP requestException
- if there is an internal errorgetHandlerInternal(javax.servlet.http.HttpServletRequest)
@Nullable protected abstract Object getHandlerInternal(HttpServletRequest request) throws Exception
null
if no
specific one is found. This method is called by getHandler(javax.servlet.http.HttpServletRequest)
;
a null
return value will lead to the default handler, if one is set.
On CORS pre-flight requests this method should return a match not for
the pre-flight request but for the expected actual request based on the URL
path, the HTTP methods from the "Access-Control-Request-Method" header, and
the headers from the "Access-Control-Request-Headers" header thus allowing
the CORS configuration to be obtained via getCorsConfiguration(Object, HttpServletRequest)
,
Note: This method may also return a pre-built HandlerExecutionChain
,
combining a handler object with dynamically determined interceptors.
Statically specified interceptors will get merged into such an existing chain.
request
- current HTTP requestnull
if none foundException
- if there is an internal errorprotected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request)
HandlerExecutionChain
for the given handler, including
applicable interceptors.
The default implementation builds a standard HandlerExecutionChain
with the given handler, the common interceptors of the handler mapping, and any
MappedInterceptors
matching to the current request URL. Interceptors
are added in the order they were registered. Subclasses may override this
in order to extend/rearrange the list of interceptors.
NOTE: The passed-in handler object may be a raw handler or a
pre-built HandlerExecutionChain
. This method should handle those
two cases explicitly, either building a new HandlerExecutionChain
or extending the existing chain.
For simply adding an interceptor in a custom subclass, consider calling
super.getHandlerExecutionChain(handler, request)
and invoking
HandlerExecutionChain.addInterceptor(org.springframework.web.servlet.HandlerInterceptor)
on the returned chain object.
handler
- the resolved handler instance (never null
)request
- current HTTP requestnull
)getAdaptedInterceptors()
protected boolean hasCorsConfigurationSource(Object handler)
true
if there is a CorsConfigurationSource
for this handler.@Nullable protected CorsConfiguration getCorsConfiguration(Object handler, HttpServletRequest request)
handler
- the handler to check (never null
).request
- the current request.null
if noneprotected HandlerExecutionChain getCorsHandlerExecutionChain(HttpServletRequest request, HandlerExecutionChain chain, @Nullable CorsConfiguration config)
For pre-flight requests, the default implementation replaces the selected
handler with a simple HttpRequestHandler that invokes the configured
setCorsProcessor(org.springframework.web.cors.CorsProcessor)
.
For actual requests, the default implementation inserts a HandlerInterceptor that makes CORS-related checks and adds CORS headers.
request
- the current requestchain
- the handler chainconfig
- the applicable CORS configuration (possibly null
)