Package org.springframework.web.servlet.support

# Class WebContentGenerator

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

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

Direct Known Subclasses:
  `AbstractController`, `AbstractHandlerMethodAdapter`, `ResourceHttpRequestHandler`, `WebContentInterceptor`

public abstract class WebContentGenerator extends org.springframework.web.context.support.WebApplicationObjectSupport
Convenient superclass for any kind of web content generator, like `AbstractController` and `WebContentInterceptor`. Can also be used for custom handlers that have their own `HandlerAdapter`.

Supports HTTP cache control options. The usage of corresponding HTTP headers can be controlled via the `"cacheSeconds"` and `"cacheControl"` properties.

Author:
  Rod Johnson, Juergen Hoeller, Brian Clozel, Rossen Stoyanchev
See Also:
  - `setCacheSeconds(int)`
  - `setCacheControl(org.springframework.http.CacheControl)`
  - `setRequireSession(boolean)`

## Field Summary

Fields
Modifier and Type
Field
Description
`protected static final String`
`HEADER_CACHE_CONTROL`
`static final String`
`METHOD_GET`
HTTP method "GET".
`static final String`
`METHOD_HEAD`
HTTP method "HEAD".
`static final String`
`METHOD_POST`
HTTP method "POST".

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

`logger`

## Constructor Summary

Constructors
Constructor
Description
`WebContentGenerator()`
Create a new WebContentGenerator which supports HTTP methods GET, HEAD and POST by default.
`WebContentGenerator(boolean restrictDefaultSupportedMethods)`
Create a new WebContentGenerator.
`WebContentGenerator(String... supportedMethods)`
Create a new WebContentGenerator.

## Method Summary

Modifier and Type
Method
Description
`protected final void`
`applyCacheControl(HttpServletResponse response, org.springframework.http.CacheControl cacheControl)`
Set the HTTP Cache-Control header according to the given settings.
`protected final void`
`applyCacheSeconds(HttpServletResponse response, int cacheSeconds)`
Apply the given cache seconds and generate corresponding HTTP headers, i.e.
`protected final void`
`checkAndPrepare(HttpServletRequest request, HttpServletResponse response, int cacheSeconds, boolean lastModified)`
Deprecated.
as of 4.2, since the `lastModified` flag is effectively ignored, with a must-revalidate header only generated if explicitly configured
`protected final void`
`checkRequest(HttpServletRequest request)`
Check the given request for supported methods and a required session, if any.
`protected String`
`getAllowHeader()`
Return the "Allow" header value to use in response to an HTTP OPTIONS request based on the configured `supported methods` also automatically adding "OPTIONS" to the list even if not present as a supported method.
`final org.springframework.http.CacheControl`
`getCacheControl()`
Get the `CacheControl` instance that builds the Cache-Control HTTP response header.
`final int`
`getCacheSeconds()`
Return the number of seconds that content is cached.
`final String[]`
`getSupportedMethods()`
Return the HTTP methods that this content generator supports.
`final String[]`
`getVaryByRequestHeaders()`
Return the configured request header names for the "Vary" response header.
`final boolean`
`isRequireSession()`
Return whether a session is required to handle requests.
`protected final void`
`prepareResponse(HttpServletResponse response)`
Prepare the given response according to the settings of this generator.
`final void`
`setCacheControl(org.springframework.http.CacheControl cacheControl)`
Set the `CacheControl` instance to build the Cache-Control HTTP response header.
`final void`
`setCacheSeconds(int seconds)`
Cache content for the given number of seconds, by writing cache-related HTTP headers to the response: seconds == -1 (default value): no generation cache-related headers seconds == 0: "Cache-Control: no-store" will prevent caching seconds > 0: "Cache-Control: max-age=seconds" will ask to cache content
`final void`
`setRequireSession(boolean requireSession)`
Set whether a session should be required to handle requests.
`final void`
`setSupportedMethods(String... methods)`
Set the HTTP methods that this content generator should support.
`final void`
`setVaryByRequestHeaders(String... varyByRequestHeaders)`
Configure one or more request header names (for example, "Accept-Language") to add to the "Vary" response header to inform clients that the response is subject to content negotiation and variances based on the value of the given request headers.

### 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`

## Field Details

### METHOD_GET

public static final String METHOD_GET
HTTP method "GET".

See Also:
  - Constant Field Values

### METHOD_HEAD

public static final String METHOD_HEAD
HTTP method "HEAD".

See Also:
  - Constant Field Values

### METHOD_POST

public static final String METHOD_POST
HTTP method "POST".

See Also:
  - Constant Field Values

### HEADER_CACHE_CONTROL

protected static final String HEADER_CACHE_CONTROL

See Also:
  - Constant Field Values

## Constructor Details

### WebContentGenerator

public WebContentGenerator()
Create a new WebContentGenerator which supports HTTP methods GET, HEAD and POST by default.

### WebContentGenerator

public WebContentGenerator(boolean restrictDefaultSupportedMethods)
Create a new WebContentGenerator.

Parameters:
  `restrictDefaultSupportedMethods` - `true` if this generator should support HTTP methods GET, HEAD and POST by default, or `false` if it should be unrestricted

### WebContentGenerator

public WebContentGenerator(String... supportedMethods)
Create a new WebContentGenerator.

Parameters:
  `supportedMethods` - the supported HTTP methods for this content generator

## Method Details

### setSupportedMethods

public final void setSupportedMethods(@Nullable String... methods)
Set the HTTP methods that this content generator should support.

Default is GET, HEAD and POST for simple form controller types; unrestricted for general controllers and interceptors.

### getSupportedMethods

@Nullable public final String[] getSupportedMethods()
Return the HTTP methods that this content generator supports.

### getAllowHeader

@Nullable protected String getAllowHeader()
Return the "Allow" header value to use in response to an HTTP OPTIONS request based on the configured `supported methods` also automatically adding "OPTIONS" to the list even if not present as a supported method. This means subclasses don't have to explicitly list "OPTIONS" as a supported method as long as HTTP OPTIONS requests are handled before making a call to `checkRequest(HttpServletRequest)`.

Since:
  4.3

### setRequireSession

public final void setRequireSession(boolean requireSession)
Set whether a session should be required to handle requests.

### isRequireSession

public final boolean isRequireSession()
Return whether a session is required to handle requests.

### setCacheControl

public final void setCacheControl(@Nullable org.springframework.http.CacheControl cacheControl)
Set the `CacheControl` instance to build the Cache-Control HTTP response header.

Since:
  4.2

### getCacheControl

@Nullable public final org.springframework.http.CacheControl getCacheControl()
Get the `CacheControl` instance that builds the Cache-Control HTTP response header.

Since:
  4.2

### setCacheSeconds

public final void setCacheSeconds(int seconds)
Cache content for the given number of seconds, by writing cache-related HTTP headers to the response:

- seconds == -1 (default value): no generation cache-related headers
- seconds == 0: "Cache-Control: no-store" will prevent caching
- seconds > 0: "Cache-Control: max-age=seconds" will ask to cache content

For more specific needs, a custom `CacheControl` should be used.

See Also:
  - `setCacheControl(org.springframework.http.CacheControl)`

### getCacheSeconds

public final int getCacheSeconds()
Return the number of seconds that content is cached.

### setVaryByRequestHeaders

public final void setVaryByRequestHeaders(@Nullable String... varyByRequestHeaders)
Configure one or more request header names (for example, "Accept-Language") to add to the "Vary" response header to inform clients that the response is subject to content negotiation and variances based on the value of the given request headers. The configured request header names are added only if not already present in the response "Vary" header.

Parameters:
  `varyByRequestHeaders` - one or more request header names
Since:
  4.3

### getVaryByRequestHeaders

@Nullable public final String[] getVaryByRequestHeaders()
Return the configured request header names for the "Vary" response header.

Since:
  4.3

### checkRequest

protected final void checkRequest(HttpServletRequest request) throws ServletException
Check the given request for supported methods and a required session, if any.

Parameters:
  `request` - current HTTP request
Throws:
  `ServletException` - if the request cannot be handled because a check failed
Since:
  4.2

### prepareResponse

protected final void prepareResponse(HttpServletResponse response)
Prepare the given response according to the settings of this generator. Applies the number of cache seconds specified for this generator.

Parameters:
  `response` - current HTTP response
Since:
  4.2

### applyCacheControl

protected final void applyCacheControl(HttpServletResponse response, org.springframework.http.CacheControl cacheControl)
Set the HTTP Cache-Control header according to the given settings.

Parameters:
  `response` - current HTTP response
  `cacheControl` - the pre-configured cache control settings
Since:
  4.2

### applyCacheSeconds

protected final void applyCacheSeconds(HttpServletResponse response, int cacheSeconds)
Apply the given cache seconds and generate corresponding HTTP headers, i.e. allow caching for the given number of seconds in case of a positive value, prevent caching if given a 0 value, do nothing else. Does not tell the browser to revalidate the resource.

Parameters:
  `response` - current HTTP response
  `cacheSeconds` - positive number of seconds into the future that the response should be cacheable for, 0 to prevent caching

### checkAndPrepare

@Deprecated protected final void checkAndPrepare(HttpServletRequest request, HttpServletResponse response, int cacheSeconds, boolean lastModified) throws ServletException
Deprecated.
as of 4.2, since the `lastModified` flag is effectively ignored, with a must-revalidate header only generated if explicitly configured
Check and prepare the given request and response according to the settings of this generator.

Throws:
  `ServletException`
See Also:
  - `checkRequest(HttpServletRequest)`
  - `applyCacheSeconds(HttpServletResponse, int)`