public final class MappedInterceptor extends Object implements HandlerInterceptor
HandlerInterceptor
and uses URL patterns to determine whether
it applies to a given request.
Pattern matching can be done with PathMatcher
or with parsed
PathPattern
. The syntax is largely the same with the latter being more
tailored for web usage and more efficient. The choice is driven by the
presence of a resolved
String
lookupPath or a parsed
RequestPath
which in turn depends on the
HandlerMapping
that matched the current request.
MappedInterceptor
is supported by sub-classes of
AbstractHandlerMethodMapping
which detect beans of type
MappedInterceptor
and also check if interceptors directly registered
with it are of this type.
Modifier and Type | Method and Description |
---|---|
void |
afterCompletion(HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex)
Callback after completion of request processing, that is, after rendering
the view.
|
HandlerInterceptor |
getInterceptor()
The target
HandlerInterceptor to invoke in case of a match. |
PathMatcher |
getPathMatcher()
The
configured PathMatcher. |
String[] |
getPathPatterns()
Return the patterns this interceptor is mapped to.
|
boolean |
matches(HttpServletRequest request)
Check whether this interceptor is mapped to the request.
|
boolean |
matches(String lookupPath,
PathMatcher pathMatcher)
Deprecated.
as of 5.3 in favor of
matches(HttpServletRequest) |
void |
postHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler,
ModelAndView modelAndView)
Intercept the execution of a handler.
|
boolean |
preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler)
Intercept the execution of a handler.
|
void |
setPathMatcher(PathMatcher pathMatcher)
Configure the PathMatcher to use to match URL paths with against include
and exclude patterns.
|
public MappedInterceptor(@Nullable String[] includePatterns, @Nullable String[] excludePatterns, HandlerInterceptor interceptor, @Nullable PathPatternParser parser)
includePatterns
- patterns to which requests must match, or null to
match all pathsexcludePatterns
- patterns to which requests must not matchinterceptor
- the target interceptorparser
- a parser to use to pre-parse patterns into PathPattern
;
when not provided, PathPatternParser.defaultInstance
is used.public MappedInterceptor(@Nullable String[] includePatterns, HandlerInterceptor interceptor)
MappedInterceptor(String[], String[], HandlerInterceptor, PathPatternParser)
with include patterns only.public MappedInterceptor(@Nullable String[] includePatterns, @Nullable String[] excludePatterns, HandlerInterceptor interceptor)
MappedInterceptor(String[], String[], HandlerInterceptor, PathPatternParser)
without a provided parser.public MappedInterceptor(@Nullable String[] includePatterns, WebRequestInterceptor interceptor)
MappedInterceptor(String[], String[], HandlerInterceptor, PathPatternParser)
with a WebRequestInterceptor
as the target.public MappedInterceptor(@Nullable String[] includePatterns, @Nullable String[] excludePatterns, WebRequestInterceptor interceptor)
MappedInterceptor(String[], String[], HandlerInterceptor, PathPatternParser)
with a WebRequestInterceptor
as the target.@Nullable public String[] getPathPatterns()
public HandlerInterceptor getInterceptor()
HandlerInterceptor
to invoke in case of a match.public void setPathMatcher(PathMatcher pathMatcher)
This is an advanced property that should be used only when a
customized AntPathMatcher
or a custom PathMatcher is required.
By default this is AntPathMatcher
.
Note: Setting PathMatcher
enforces use of
String pattern matching even when a
parsed
RequestPath
is available.
public PathMatcher getPathMatcher()
configured
PathMatcher.public boolean matches(HttpServletRequest request)
The request mapping path is expected to have been resolved externally. See also class-level Javadoc.
request
- the request to match totrue
if the interceptor should be applied to the request@Deprecated public boolean matches(String lookupPath, PathMatcher pathMatcher)
matches(HttpServletRequest)
lookupPath
- the current request pathpathMatcher
- a path matcher for path pattern matchingtrue
if the interceptor applies to the given request pathpublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
HandlerInterceptor
DispatcherServlet processes a handler in an execution chain, consisting of any number of interceptors, with the handler itself at the end. With this method, each interceptor can decide to abort the execution chain, typically sending an HTTP error or writing a custom response.
Note: special considerations apply for asynchronous
request processing. For more details see
AsyncHandlerInterceptor
.
The default implementation returns true
.
preHandle
in interface HandlerInterceptor
request
- current HTTP requestresponse
- current HTTP responsehandler
- chosen handler to execute, for type and/or instance evaluationtrue
if the execution chain should proceed with the
next interceptor or the handler itself. Else, DispatcherServlet assumes
that this interceptor has already dealt with the response itself.Exception
- in case of errorspublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception
HandlerInterceptor
DispatcherServlet processes a handler in an execution chain, consisting of any number of interceptors, with the handler itself at the end. With this method, each interceptor can post-process an execution, getting applied in inverse order of the execution chain.
Note: special considerations apply for asynchronous
request processing. For more details see
AsyncHandlerInterceptor
.
The default implementation is empty.
postHandle
in interface HandlerInterceptor
request
- current HTTP requestresponse
- current HTTP responsehandler
- the handler (or HandlerMethod
) that started asynchronous
execution, for type and/or instance examinationmodelAndView
- the ModelAndView
that the handler returned
(can also be null
)Exception
- in case of errorspublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception
HandlerInterceptor
Note: Will only be called if this interceptor's preHandle
method has successfully completed and returned true
!
As with the postHandle
method, the method will be invoked on each
interceptor in the chain in reverse order, so the first interceptor will be
the last to be invoked.
Note: special considerations apply for asynchronous
request processing. For more details see
AsyncHandlerInterceptor
.
The default implementation is empty.
afterCompletion
in interface HandlerInterceptor
request
- current HTTP requestresponse
- current HTTP responsehandler
- the handler (or HandlerMethod
) that started asynchronous
execution, for type and/or instance examinationex
- any exception thrown on handler execution, if any; this does not
include exceptions that have been handled through an exception resolverException
- in case of errors