Top Description Fields Constructors Methods
org.springframework.web.servlet.mvc

public Class WebContentInterceptor

extends WebContentGenerator
implements HandlerInterceptor
Class Inheritance
All Implemented Interfaces
org.springframework.web.servlet.HandlerInterceptor
Imports
java.util.Enumeration, .HashMap, .Map, .Properties, jakarta.servlet.ServletException, .ServletRequest, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.springframework.http.CacheControl, org.springframework.http.server.PathContainer, org.springframework.lang.Nullable, org.springframework.util.AntPathMatcher, .Assert, .ObjectUtils, .PathMatcher, org.springframework.web.servlet.HandlerInterceptor, .HandlerMapping, .ModelAndView, org.springframework.web.servlet.support.WebContentGenerator, org.springframework.web.util.ServletRequestPathUtils, .UrlPathHelper, org.springframework.web.util.pattern.PathPattern, .PathPatternParser

Handler interceptor that checks the request for supported methods and a required session and prepares the response by applying the configured cache settings.

Cache settings may be configured for specific URLs via path pattern with addCacheMapping(CacheControl, String...) and setCacheMappings(Properties), along with a fallback on default settings for all URLs via setCacheControl(CacheControl).

Pattern matching can be done with PathMatcher or with parsed PathPatterns. The syntax is largely the same with the latter being more tailored for web usage and more efficient. The choice depends on the presence of a resolved String lookupPath or a parsed RequestPath which in turn depends on the HandlerMapping that matched the current request.

All the settings supported by this interceptor can also be set on AbstractController. This interceptor is mainly intended for applying checks and preparations to a set of controllers mapped by a HandlerMapping.

Authors
Juergen Hoeller, Brian Clozel, Rossen Stoyanchev
Since
27.11.2003
See Also
PathMatcher, AntPathMatcher

Field Summary

Modifier and TypeField and Description
private final Map<PathPattern, CacheControl>
private final Map<PathPattern, Integer>
private static PathMatcher
private PathMatcher
private final PathPatternParser
Inherited from org.springframework.web.servlet.support.WebContentGenerator:
HEADER_CACHE_CONTROLMETHOD_GETMETHOD_HEADMETHOD_POST

Constructor Summary

AccessConstructor and Description
public
public
WebContentInterceptor(PathPatternParser parser)

Constructor with a PathPatternParser to parse patterns with.

Method Summary

Modifier and TypeMethod and Description
public void
addCacheMapping(CacheControl
the CacheControl to use
cacheControl
,
String...
the URL paths that will map to the given CacheControl
paths
)

Map specific URL paths to a specific org.springframework.http.CacheControl.

public void
afterCompletion(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
,
Object
the handler (or HandlerMethod) that started asynchronous execution, for type and/or instance examination
handler
,
Exception
any exception thrown on handler execution, if any; this does not include exceptions that have been handled through an exception resolver
ex
)

Overrides default org.springframework.web.servlet.HandlerInterceptor.afterCompletion.

This implementation is empty.

protected CacheControl

Returns:

the matched CacheControl, or null if no match
lookupCacheControl
(PathContainer
the path to match to
path
)

Find a org.springframework.http.CacheControl instance for the given parsed path.

protected CacheControl

Returns:

the matched CacheControl, or null if no match
lookupCacheControl
(String
the path to match to
lookupPath
)

Find a org.springframework.http.CacheControl instance for the given String lookupPath.

protected Integer

Returns:

the matched cacheSeconds, or null if there is no match
lookupCacheSeconds
(PathContainer
the path to match to
path
)

Find a cacheSeconds value for the given parsed path.

protected Integer

Returns:

the matched cacheSeconds, or null if there is no match
lookupCacheSeconds
(String
the path to match to
lookupPath
)

Find a cacheSeconds instance for the given String lookupPath.

public void
postHandle(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
,
Object
the handler (or HandlerMethod) that started asynchronous execution, for type and/or instance examination
handler
,
ModelAndView
the ModelAndView that the handler returned (can also be null)
modelAndView
)

Overrides default org.springframework.web.servlet.HandlerInterceptor.postHandle.

This implementation is empty.

public boolean
preHandle(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
,
Object
chosen handler to execute, for type and/or instance evaluation
handler
)

Overrides default org.springframework.web.servlet.HandlerInterceptor.preHandle.

Interception point before the execution of a handler.

public void
setAlwaysUseFullPath(boolean alwaysUseFullPath)
Deprecated as of 5.3, the path is resolved externally and obtained with ServletRequestPathUtils#getCachedPathValue(ServletRequest)

Shortcut to the same property on the configured UrlPathHelper.

public void
setCacheMappings(Properties
a mapping between URL paths (as keys) and cache seconds (as values, need to be integer-parsable)
cacheMappings
)

Map settings for cache seconds to specific URL paths via patterns.

public void
setPathMatcher(PathMatcher pathMatcher)

Configure the PathMatcher to use to match URL paths against registered URL patterns to select the cache settings for a request.

public void
setUrlDecode(boolean urlDecode)
Deprecated as of 5.3, the path is resolved externally and obtained with ServletRequestPathUtils#getCachedPathValue(ServletRequest)

Shortcut to the same property on the configured UrlPathHelper.

public void
setUrlPathHelper(UrlPathHelper urlPathHelper)
Deprecated as of 5.3, the path is resolved externally and obtained with ServletRequestPathUtils#getCachedPathValue(ServletRequest)

Set the UrlPathHelper to use for resolution of lookup paths.

Inherited from org.springframework.web.servlet.support.WebContentGenerator:
applyCacheControlapplyCacheSecondsapplyCacheSecondscacheForSecondscacheForSecondscheckAndPreparecheckRequestgetAllowHeadergetCacheControlgetCacheSecondsgetSupportedMethodsgetVaryByRequestHeadersisAlwaysMustRevalidateisRequireSessionisUseCacheControlHeaderisUseCacheControlNoStoreisUseExpiresHeaderprepareResponsepreventCachingsetAlwaysMustRevalidatesetCacheControlsetCacheSecondssetRequireSessionsetSupportedMethodssetUseCacheControlHeadersetUseCacheControlNoStoresetUseExpiresHeadersetVaryByRequestHeaders

Field Detail

cacheControlMappingsback to summary
private final Map<PathPattern, CacheControl> cacheControlMappings
cacheMappingsback to summary
private final Map<PathPattern, Integer> cacheMappings
defaultPathMatcherback to summary
private static PathMatcher defaultPathMatcher
pathMatcherback to summary
private PathMatcher pathMatcher
patternParserback to summary
private final PathPatternParser patternParser

Constructor Detail

WebContentInterceptorback to summary
public WebContentInterceptor()

Default constructor with PathPatternParser#defaultInstance.

WebContentInterceptorback to summary
public WebContentInterceptor(PathPatternParser parser)

Constructor with a PathPatternParser to parse patterns with.

Since
5.3

Method Detail

addCacheMappingback to summary
public void addCacheMapping(CacheControl cacheControl, String... paths)

Map specific URL paths to a specific org.springframework.http.CacheControl.

Overrides the default cache seconds setting of this interceptor. Can specify an empty org.springframework.http.CacheControl instance to exclude a URL path from default caching.

For pattern syntax see AntPathMatcher and PathPattern as well as the class-level Javadoc for details for when each is used. The syntax is largely the same with PathPattern more tailored for web usage.

Note

Path patterns are not supposed to overlap. If a request matches several mappings, it is effectively undefined which one will apply (due to the lack of key ordering in the underlying java.util.HashMap).

Parameters
cacheControl:CacheControl

the CacheControl to use

paths:String[]

the URL paths that will map to the given CacheControl

Since
4.2
See Also
setCacheSeconds
afterCompletionback to summary
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception

Overrides default org.springframework.web.servlet.HandlerInterceptor.afterCompletion.

This implementation is empty.

Parameters
request:HttpServletRequest

Doc from org.springframework.web.servlet.HandlerInterceptor.afterCompletion.

current HTTP request

response:HttpServletResponse

Doc from org.springframework.web.servlet.HandlerInterceptor.afterCompletion.

current HTTP response

handler:Object

Doc from org.springframework.web.servlet.HandlerInterceptor.afterCompletion.

the handler (or HandlerMethod) that started asynchronous execution, for type and/or instance examination

ex:Exception

Doc from org.springframework.web.servlet.HandlerInterceptor.afterCompletion.

any exception thrown on handler execution, if any; this does not include exceptions that have been handled through an exception resolver

Annotations
@Override
Exceptions
Exception:

Doc from org.springframework.web.servlet.HandlerInterceptor.afterCompletion.

in case of errors

lookupCacheControlback to summary
protected CacheControl lookupCacheControl(PathContainer path)

Find a org.springframework.http.CacheControl instance for the given parsed path. This is used when the HandlerMapping uses parsed PathPatterns.

Parameters
path:PathContainer

the path to match to

Returns:CacheControl

the matched CacheControl, or null if no match

Annotations
@Nullable
Since
5.3
lookupCacheControlback to summary
protected CacheControl lookupCacheControl(String lookupPath)

Find a org.springframework.http.CacheControl instance for the given String lookupPath. This is used when the HandlerMapping relies on String pattern matching with PathMatcher.

Parameters
lookupPath:String

the path to match to

Returns:CacheControl

the matched CacheControl, or null if no match

Annotations
@Nullable
lookupCacheSecondsback to summary
protected Integer lookupCacheSeconds(PathContainer path)

Find a cacheSeconds value for the given parsed path. This is used when the HandlerMapping uses parsed PathPatterns.

Parameters
path:PathContainer

the path to match to

Returns:Integer

the matched cacheSeconds, or null if there is no match

Annotations
@Nullable
Since
5.3
lookupCacheSecondsback to summary
protected Integer lookupCacheSeconds(String lookupPath)

Find a cacheSeconds instance for the given String lookupPath. This is used when the HandlerMapping relies on String pattern matching with PathMatcher.

Parameters
lookupPath:String

the path to match to

Returns:Integer

the matched cacheSeconds, or null if there is no match

Annotations
@Nullable
postHandleback to summary
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception

Overrides default org.springframework.web.servlet.HandlerInterceptor.postHandle.

This implementation is empty.

Parameters
request:HttpServletRequest

Doc from org.springframework.web.servlet.HandlerInterceptor.postHandle.

current HTTP request

response:HttpServletResponse

Doc from org.springframework.web.servlet.HandlerInterceptor.postHandle.

current HTTP response

handler:Object

Doc from org.springframework.web.servlet.HandlerInterceptor.postHandle.

the handler (or HandlerMethod) that started asynchronous execution, for type and/or instance examination

modelAndView:ModelAndView

Doc from org.springframework.web.servlet.HandlerInterceptor.postHandle.

the ModelAndView that the handler returned (can also be null)

Annotations
@Override
Exceptions
Exception:

Doc from org.springframework.web.servlet.HandlerInterceptor.postHandle.

in case of errors

preHandleback to summary
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException

Overrides default org.springframework.web.servlet.HandlerInterceptor.preHandle.

Doc from org.springframework.web.servlet.HandlerInterceptor.preHandle.

Interception point before the execution of a handler. Called after HandlerMapping determined an appropriate handler object, but before HandlerAdapter invokes the handler.

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 org.springframework.web.servlet.AsyncHandlerInterceptor.

The default implementation returns true.

Parameters
request:HttpServletRequest

current HTTP request

response:HttpServletResponse

current HTTP response

handler:Object

chosen handler to execute, for type and/or instance evaluation

Returns:boolean

true 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.

Annotations
@Override
setAlwaysUseFullPathback to summary
public void setAlwaysUseFullPath(boolean alwaysUseFullPath)

Deprecated

as of 5.3, the path is resolved externally and obtained with ServletRequestPathUtils#getCachedPathValue(ServletRequest)

Shortcut to the same property on the configured UrlPathHelper.

Annotations
@Deprecated
setCacheMappingsback to summary
public void setCacheMappings(Properties cacheMappings)

Map settings for cache seconds to specific URL paths via patterns.

Overrides the default cache seconds setting of this interceptor. Can specify "-1" to exclude a URL path from default caching.

For pattern syntax see AntPathMatcher and PathPattern as well as the class-level Javadoc for details for when each is used. The syntax is largely the same with PathPattern more tailored for web usage.

Note

Path patterns are not supposed to overlap. If a request matches several mappings, it is effectively undefined which one will apply (due to the lack of key ordering in java.util.Properties).

Parameters
cacheMappings:Properties

a mapping between URL paths (as keys) and cache seconds (as values, need to be integer-parsable)

See Also
setCacheSeconds
setPathMatcherback to summary
public void setPathMatcher(PathMatcher pathMatcher)

Configure the PathMatcher to use to match URL paths against registered URL patterns to select the cache settings for a request.

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.

See Also
addCacheMapping, setCacheMappings, org.springframework.util.AntPathMatcher
setUrlDecodeback to summary
public void setUrlDecode(boolean urlDecode)

Deprecated

as of 5.3, the path is resolved externally and obtained with ServletRequestPathUtils#getCachedPathValue(ServletRequest)

Shortcut to the same property on the configured UrlPathHelper.

Annotations
@Deprecated
setUrlPathHelperback to summary
public void setUrlPathHelper(UrlPathHelper urlPathHelper)

Deprecated

as of 5.3, the path is resolved externally and obtained with ServletRequestPathUtils#getCachedPathValue(ServletRequest)

Set the UrlPathHelper to use for resolution of lookup paths.

Annotations
@Deprecated