public abstract class WebContentGenerator extends WebApplicationObjectSupport
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.
NOTE: As of Spring 4.2, this generator's default behavior changed when
using only setCacheSeconds(int)
, sending HTTP response headers that are in line
with current browsers and proxies implementations (i.e. no HTTP 1.0 headers anymore)
Reverting to the previous behavior can be easily done by using one of the newly
deprecated methods setUseExpiresHeader(boolean)
, setUseCacheControlHeader(boolean)
,
setUseCacheControlNoStore(boolean)
or setAlwaysMustRevalidate(boolean)
.
setCacheSeconds(int)
,
setCacheControl(org.springframework.http.CacheControl)
,
setRequireSession(boolean)
Modifier and Type | Field and Description |
---|---|
protected static String |
HEADER_CACHE_CONTROL |
static String |
METHOD_GET
HTTP method "GET"
|
static String |
METHOD_HEAD
HTTP method "HEAD"
|
static String |
METHOD_POST
HTTP method "POST"
|
logger
Constructor and 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.
|
Modifier and Type | Method and Description |
---|---|
protected void |
applyCacheControl(HttpServletResponse response,
CacheControl cacheControl)
Set the HTTP Cache-Control header according to the given settings.
|
protected void |
applyCacheSeconds(HttpServletResponse response,
int cacheSeconds)
Apply the given cache seconds and generate corresponding HTTP headers,
i.e.
|
protected void |
applyCacheSeconds(HttpServletResponse response,
int cacheSeconds,
boolean mustRevalidate)
Deprecated.
|
protected void |
cacheForSeconds(HttpServletResponse response,
int seconds)
Deprecated.
|
protected void |
cacheForSeconds(HttpServletResponse response,
int seconds,
boolean mustRevalidate)
Deprecated.
|
protected void |
checkAndPrepare(HttpServletRequest request,
HttpServletResponse response,
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 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 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. |
CacheControl |
getCacheControl()
Get the
CacheControl instance
that builds the Cache-Control HTTP response header. |
int |
getCacheSeconds()
Return the number of seconds that content is cached.
|
String[] |
getSupportedMethods()
Return the HTTP methods that this content generator supports.
|
String[] |
getVaryByRequestHeaders()
Return the configured request header names for the "Vary" response header.
|
boolean |
isAlwaysMustRevalidate()
Deprecated.
as of 4.2, in favor of
getCacheControl() |
boolean |
isRequireSession()
Return whether a session is required to handle requests.
|
boolean |
isUseCacheControlHeader()
Deprecated.
as of 4.2, in favor of
getCacheControl() |
boolean |
isUseCacheControlNoStore()
Deprecated.
as of 4.2, in favor of
getCacheControl() |
boolean |
isUseExpiresHeader()
Deprecated.
as of 4.2, in favor of
getCacheControl() |
protected void |
prepareResponse(HttpServletResponse response)
Prepare the given response according to the settings of this generator.
|
protected void |
preventCaching(HttpServletResponse response)
Deprecated.
|
void |
setAlwaysMustRevalidate(boolean mustRevalidate)
Deprecated.
as of 4.2, in favor of
setCacheControl(org.springframework.http.CacheControl) |
void |
setCacheControl(CacheControl cacheControl)
Set the
CacheControl instance to build
the Cache-Control HTTP response header. |
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
|
void |
setRequireSession(boolean requireSession)
Set whether a session should be required to handle requests.
|
void |
setSupportedMethods(String... methods)
Set the HTTP methods that this content generator should support.
|
void |
setUseCacheControlHeader(boolean useCacheControlHeader)
Deprecated.
as of 4.2, since going forward, the HTTP 1.1 cache-control
header will be required, with the HTTP 1.0 headers disappearing
|
void |
setUseCacheControlNoStore(boolean useCacheControlNoStore)
Deprecated.
as of 4.2, in favor of
setCacheControl(org.springframework.http.CacheControl) |
void |
setUseExpiresHeader(boolean useExpiresHeader)
Deprecated.
as of 4.2, since going forward, the HTTP 1.1 cache-control
header will be required, with the HTTP 1.0 headers disappearing
|
void |
setVaryByRequestHeaders(String... varyByRequestHeaders)
Configure one or more request header names (e.g.
|
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
public static final String METHOD_GET
public static final String METHOD_HEAD
public static final String METHOD_POST
protected static final String HEADER_CACHE_CONTROL
public WebContentGenerator()
public WebContentGenerator(boolean restrictDefaultSupportedMethods)
restrictDefaultSupportedMethods
- true
if this
generator should support HTTP methods GET, HEAD and POST by default,
or false
if it should be unrestrictedpublic WebContentGenerator(String... supportedMethods)
supportedMethods
- the supported HTTP methods for this content generatorpublic final void setSupportedMethods(String... methods)
Default is GET, HEAD and POST for simple form controller types; unrestricted for general controllers and interceptors.
public final String[] getSupportedMethods()
protected String getAllowHeader()
supported
methods
also automatically adding "OPTIONS" to the list even if not
present as a supported method. This means sub-classes 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)
.public final void setRequireSession(boolean requireSession)
public final boolean isRequireSession()
public final void setCacheControl(CacheControl cacheControl)
CacheControl
instance to build
the Cache-Control HTTP response header.public final CacheControl getCacheControl()
CacheControl
instance
that builds the Cache-Control HTTP response header.public final void setCacheSeconds(int seconds)
For more specific needs, a custom CacheControl
should be used.
public final int getCacheSeconds()
public final void setVaryByRequestHeaders(String... varyByRequestHeaders)
Note: This property is only supported on Servlet 3.0+ which allows checking existing response header values.
varyByRequestHeaders
- one or more request header namespublic final String[] getVaryByRequestHeaders()
@Deprecated public final void setUseExpiresHeader(boolean useExpiresHeader)
Note: Cache headers will only get applied if caching is enabled (or explicitly prevented) for the current request.
@Deprecated public final boolean isUseExpiresHeader()
getCacheControl()
@Deprecated public final void setUseCacheControlHeader(boolean useCacheControlHeader)
Note: Cache headers will only get applied if caching is enabled (or explicitly prevented) for the current request.
@Deprecated public final boolean isUseCacheControlHeader()
getCacheControl()
@Deprecated public final void setUseCacheControlNoStore(boolean useCacheControlNoStore)
setCacheControl(org.springframework.http.CacheControl)
@Deprecated public final boolean isUseCacheControlNoStore()
getCacheControl()
@Deprecated public final void setAlwaysMustRevalidate(boolean mustRevalidate)
setCacheControl(org.springframework.http.CacheControl)
WebRequest.checkNotModified(long)
.
Default is "false".
@Deprecated public final boolean isAlwaysMustRevalidate()
getCacheControl()
protected final void checkRequest(HttpServletRequest request) throws ServletException
request
- current HTTP requestServletException
- if the request cannot be handled because a check failedprotected final void prepareResponse(HttpServletResponse response)
response
- current HTTP responseprotected final void applyCacheControl(HttpServletResponse response, CacheControl cacheControl)
response
- current HTTP responsecacheControl
- the pre-configured cache control settingsprotected final void applyCacheSeconds(HttpServletResponse response, int cacheSeconds)
response
- current HTTP responsecacheSeconds
- positive number of seconds into the future that the
response should be cacheable for, 0 to prevent caching@Deprecated protected final void checkAndPrepare(HttpServletRequest request, HttpServletResponse response, boolean lastModified) throws ServletException
lastModified
flag is effectively ignored,
with a must-revalidate header only generated if explicitly configuredServletException
checkRequest(HttpServletRequest)
,
prepareResponse(HttpServletResponse)
@Deprecated protected final void checkAndPrepare(HttpServletRequest request, HttpServletResponse response, int cacheSeconds, boolean lastModified) throws ServletException
lastModified
flag is effectively ignored,
with a must-revalidate header only generated if explicitly configured@Deprecated protected final void applyCacheSeconds(HttpServletResponse response, int cacheSeconds, boolean mustRevalidate)
applyCacheControl(javax.servlet.http.HttpServletResponse, org.springframework.http.CacheControl)
That is, allow caching for the given number of seconds in the case of a positive value, prevent caching if given a 0 value, else do nothing (i.e. leave caching to the client).
response
- the current HTTP responsecacheSeconds
- the (positive) number of seconds into the future
that the response should be cacheable for; 0 to prevent caching; and
a negative value to leave caching to the client.mustRevalidate
- whether the client should revalidate the resource
(typically only necessary for controllers with last-modified support)@Deprecated protected final void cacheForSeconds(HttpServletResponse response, int seconds)
applyCacheControl(javax.servlet.http.HttpServletResponse, org.springframework.http.CacheControl)
response
- current HTTP responseseconds
- number of seconds into the future that the response
should be cacheable for@Deprecated protected final void cacheForSeconds(HttpServletResponse response, int seconds, boolean mustRevalidate)
applyCacheControl(javax.servlet.http.HttpServletResponse, org.springframework.http.CacheControl)
true
.response
- the current HTTP responseseconds
- number of seconds into the future that the response
should be cacheable formustRevalidate
- whether the client should revalidate the resource
(typically only necessary for controllers with last-modified support)@Deprecated protected final void preventCaching(HttpServletResponse response)
applyCacheControl(javax.servlet.http.HttpServletResponse, org.springframework.http.CacheControl)
See http://www.mnot.net/cache_docs
.