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 |
---|---|
private java.lang.String |
allowHeader |
private boolean |
alwaysMustRevalidate |
private CacheControl |
cacheControl |
private int |
cacheSeconds |
protected static java.lang.String |
HEADER_CACHE_CONTROL |
private static java.lang.String |
HEADER_EXPIRES |
private static java.lang.String |
HEADER_PRAGMA |
static java.lang.String |
METHOD_GET
HTTP method "GET"
|
static java.lang.String |
METHOD_HEAD
HTTP method "HEAD"
|
static java.lang.String |
METHOD_POST
HTTP method "POST"
|
private boolean |
requireSession |
private java.util.Set<java.lang.String> |
supportedMethods
Set of supported HTTP methods
|
private boolean |
useCacheControlHeader
Use HTTP 1.1 cache-control header?
|
private boolean |
useCacheControlNoStore
Use HTTP 1.1 cache-control header value "no-store"?
|
private boolean |
useExpiresHeader
Use HTTP 1.0 expires header?
|
private java.lang.String[] |
varyByRequestHeaders |
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(java.lang.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.
as of 4.2, in favor of
applyCacheControl(HttpServletResponse, org.springframework.http.CacheControl) |
protected void |
cacheForSeconds(HttpServletResponse response,
int seconds)
Deprecated.
as of 4.2, in favor of
applyCacheControl(HttpServletResponse, org.springframework.http.CacheControl) |
protected void |
cacheForSeconds(HttpServletResponse response,
int seconds,
boolean mustRevalidate)
Deprecated.
as of 4.2, in favor of
applyCacheControl(HttpServletResponse, org.springframework.http.CacheControl) |
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 java.lang.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.
|
java.lang.String[] |
getSupportedMethods()
Return the HTTP methods that this content generator supports.
|
java.lang.String[] |
getVaryByRequestHeaders()
Return the configured request header names for the "Vary" response header.
|
private java.util.Collection<java.lang.String> |
getVaryRequestHeadersToAdd(HttpServletResponse response) |
private void |
initAllowHeader() |
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.
as of 4.2, in favor of
applyCacheControl(HttpServletResponse, org.springframework.http.CacheControl) |
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(java.lang.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(java.lang.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 java.lang.String METHOD_GET
public static final java.lang.String METHOD_HEAD
public static final java.lang.String METHOD_POST
private static final java.lang.String HEADER_PRAGMA
private static final java.lang.String HEADER_EXPIRES
protected static final java.lang.String HEADER_CACHE_CONTROL
private java.util.Set<java.lang.String> supportedMethods
private java.lang.String allowHeader
private boolean requireSession
private CacheControl cacheControl
private int cacheSeconds
private java.lang.String[] varyByRequestHeaders
private boolean useExpiresHeader
private boolean useCacheControlHeader
private boolean useCacheControlNoStore
private boolean alwaysMustRevalidate
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(java.lang.String... supportedMethods)
supportedMethods
- the supported HTTP methods for this content generatorpublic final void setSupportedMethods(java.lang.String... methods)
Default is GET, HEAD and POST for simple form controller types; unrestricted for general controllers and interceptors.
public final java.lang.String[] getSupportedMethods()
private void initAllowHeader()
protected java.lang.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(java.lang.String... varyByRequestHeaders)
varyByRequestHeaders
- one or more request header namespublic final java.lang.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 configuredServletException
checkRequest(HttpServletRequest)
,
applyCacheSeconds(HttpServletResponse, int)
@Deprecated protected final void applyCacheSeconds(HttpServletResponse response, int cacheSeconds, boolean mustRevalidate)
applyCacheControl(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(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(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(HttpServletResponse, org.springframework.http.CacheControl)
See http://www.mnot.net/cache_docs
.
private java.util.Collection<java.lang.String> getVaryRequestHeadersToAdd(HttpServletResponse response)