Package org.springframework.web.servlet.mvc

# Class WebContentInterceptor

java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.support.WebContentGenerator
org.springframework.web.servlet.mvc.WebContentInterceptor

All Implemented Interfaces:
  `org.springframework.beans.factory.Aware`, `org.springframework.context.ApplicationContextAware`, `org.springframework.web.context.ServletContextAware`, `HandlerInterceptor`

public class WebContentInterceptor extends WebContentGenerator implements HandlerInterceptor
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 `WebContentGenerator.setCacheControl(CacheControl)`.

Pattern matching can be done with `PathMatcher` or with parsed `PathPattern`s. 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.

Since:
  27.11.2003
Author:
  Juergen Hoeller, Brian Clozel, Rossen Stoyanchev
See Also:
  - `PathMatcher`
  - `AntPathMatcher`

## Field Summary

### Fields inherited from class org.springframework.web.servlet.support.WebContentGenerator

`HEADER_CACHE_CONTROL, METHOD_GET, METHOD_HEAD, METHOD_POST`

### Fields inherited from class org.springframework.context.support.ApplicationObjectSupport

`logger`

## Constructor Summary

Constructors
Constructor
Description
`WebContentInterceptor()`
Default constructor with `PathPatternParser.defaultInstance`.
`WebContentInterceptor(org.springframework.web.util.pattern.PathPatternParser parser)`
Constructor with a `PathPatternParser` to parse patterns with.

## Method Summary

Modifier and Type
Method
Description
`void`
`addCacheMapping(org.springframework.http.CacheControl cacheControl, String... paths)`
Map specific URL paths to a specific `CacheControl`.
`void`
`afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)`
This implementation is empty.
`protected org.springframework.http.CacheControl`
`lookupCacheControl(String lookupPath)`
Find a `CacheControl` instance for the given String lookupPath.
`protected org.springframework.http.CacheControl`
`lookupCacheControl(org.springframework.http.server.PathContainer path)`
Find a `CacheControl` instance for the given parsed `path`.
`protected Integer`
`lookupCacheSeconds(String lookupPath)`
Find a cacheSeconds instance for the given String lookupPath.
`protected Integer`
`lookupCacheSeconds(org.springframework.http.server.PathContainer path)`
Find a cacheSeconds value for the given parsed `path`.
`void`
`postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)`
This implementation is empty.
`boolean`
`preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)`
Interception point before the execution of a handler.
`void`
`setAlwaysUseFullPath(boolean alwaysUseFullPath)`
Deprecated.
as of 5.3, the path is resolved externally and obtained with `ServletRequestPathUtils.getCachedPathValue(ServletRequest)`
`void`
`setCacheMappings(Properties cacheMappings)`
Map settings for cache seconds to specific URL paths via patterns.
`void`
`setPathMatcher(org.springframework.util.PathMatcher pathMatcher)`
Configure the PathMatcher to use to match URL paths against registered URL patterns to select the cache settings for a request.
`void`
`setUrlDecode(boolean urlDecode)`
Deprecated.
as of 5.3, the path is resolved externally and obtained with `ServletRequestPathUtils.getCachedPathValue(ServletRequest)`
`void`
`setUrlPathHelper(org.springframework.web.util.UrlPathHelper urlPathHelper)`
Deprecated.
as of 5.3, the path is resolved externally and obtained with `ServletRequestPathUtils.getCachedPathValue(ServletRequest)`

### Methods inherited from class org.springframework.web.servlet.support.WebContentGenerator

`applyCacheControl, applyCacheSeconds, checkAndPrepare, checkRequest, getAllowHeader, getCacheControl, getCacheSeconds, getSupportedMethods, getVaryByRequestHeaders, isRequireSession, prepareResponse, setCacheControl, setCacheSeconds, setRequireSession, setSupportedMethods, setVaryByRequestHeaders`

### Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport

`getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext`

### Methods inherited from class org.springframework.context.support.ApplicationObjectSupport

`getApplicationContext, getMessageSourceAccessor, initApplicationContext, obtainApplicationContext, requiredContextClass, setApplicationContext`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### WebContentInterceptor

public WebContentInterceptor()
Default constructor with `PathPatternParser.defaultInstance`.

### WebContentInterceptor

public WebContentInterceptor(org.springframework.web.util.pattern.PathPatternParser parser)
Constructor with a `PathPatternParser` to parse patterns with.

Since:
  5.3

## Method Details

### setAlwaysUseFullPath

@Deprecated 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`.

### setUrlDecode

@Deprecated 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`.

### setUrlPathHelper

@Deprecated public void setUrlPathHelper(org.springframework.web.util.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.

### setPathMatcher

public void setPathMatcher(org.springframework.util.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(org.springframework.http.CacheControl, java.lang.String...)`
  - `setCacheMappings(java.util.Properties)`
  - `AntPathMatcher`

### setCacheMappings

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` - a mapping between URL paths (as keys) and cache seconds (as values, need to be integer-parsable)
See Also:
  - `WebContentGenerator.setCacheSeconds(int)`

### addCacheMapping

public void addCacheMapping(org.springframework.http.CacheControl cacheControl, String... paths)
Map specific URL paths to a specific `CacheControl`.

Overrides the default cache seconds setting of this interceptor. Can specify an empty `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` - the `CacheControl` to use
  `paths` - the URL paths that will map to the given `CacheControl`
Since:
  4.2
See Also:
  - `WebContentGenerator.setCacheSeconds(int)`

### preHandle

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException
Description copied from interface: `HandlerInterceptor`
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 `AsyncHandlerInterceptor`.

The default implementation returns `true`.

Specified by:
  `preHandle` in interface `HandlerInterceptor`
Parameters:
  `request` - current HTTP request
  `response` - current HTTP response
  `handler` - chosen handler to execute, for type and/or instance evaluation
Returns:
  `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.
Throws:
  `ServletException`

### lookupCacheControl

@Nullable protected org.springframework.http.CacheControl lookupCacheControl(org.springframework.http.server.PathContainer path)
Find a `CacheControl` instance for the given parsed `path`. This is used when the `HandlerMapping` uses parsed `PathPatterns`.

Parameters:
  `path` - the path to match to
Returns:
  the matched `CacheControl`, or `null` if no match
Since:
  5.3

### lookupCacheControl

@Nullable protected org.springframework.http.CacheControl lookupCacheControl(String lookupPath)
Find a `CacheControl` instance for the given String lookupPath. This is used when the `HandlerMapping` relies on String pattern matching with `PathMatcher`.

Parameters:
  `lookupPath` - the path to match to
Returns:
  the matched `CacheControl`, or `null` if no match

### lookupCacheSeconds

@Nullable protected Integer lookupCacheSeconds(org.springframework.http.server.PathContainer path)
Find a cacheSeconds value for the given parsed `path`. This is used when the `HandlerMapping` uses parsed `PathPatterns`.

Parameters:
  `path` - the path to match to
Returns:
  the matched cacheSeconds, or `null` if there is no match
Since:
  5.3

### lookupCacheSeconds

@Nullable 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` - the path to match to
Returns:
  the matched cacheSeconds, or `null` if there is no match

### postHandle

public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception
This implementation is empty.

Specified by:
  `postHandle` in interface `HandlerInterceptor`
Parameters:
  `request` - current HTTP request
  `response` - current HTTP response
  `handler` - the handler (or `HandlerMethod`) that started asynchronous execution, for type and/or instance examination
  `modelAndView` - the `ModelAndView` that the handler returned (can also be `null`)
Throws:
  `Exception` - in case of errors

### afterCompletion

public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception
This implementation is empty.

Specified by:
  `afterCompletion` in interface `HandlerInterceptor`
Parameters:
  `request` - current HTTP request
  `response` - current HTTP response
  `handler` - the handler (or `HandlerMethod`) that started asynchronous execution, for type and/or instance examination
  `ex` - any exception thrown on handler execution, if any; this does not include exceptions that have been handled through an exception resolver
Throws:
  `Exception` - in case of errors