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 "useExpiresHeader", "useCacheControlHeader" and "useCacheControlNoStore" properties.
setCacheSeconds(int)
,
setRequireSession(boolean)
Modifier and Type | Field and Description |
---|---|
private boolean |
alwaysMustRevalidate |
private int |
cacheSeconds |
private 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?
|
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 |
applyCacheSeconds(HttpServletResponse response,
int seconds)
Apply the given cache seconds and generate corresponding HTTP headers,
i.e.
|
protected void |
applyCacheSeconds(HttpServletResponse response,
int seconds,
boolean mustRevalidate)
Apply the given cache seconds and generate respective HTTP headers.
|
protected void |
cacheForSeconds(HttpServletResponse response,
int seconds)
Set HTTP headers to allow caching for the given number of seconds.
|
protected void |
cacheForSeconds(HttpServletResponse response,
int seconds,
boolean mustRevalidate)
Set HTTP headers to allow caching for the given number of seconds.
|
protected void |
checkAndPrepare(HttpServletRequest request,
HttpServletResponse response,
boolean lastModified)
Check and prepare the given request and response according to the settings
of this generator.
|
protected void |
checkAndPrepare(HttpServletRequest request,
HttpServletResponse response,
int cacheSeconds,
boolean lastModified)
Check and prepare the given request and response according to the settings
of this generator.
|
int |
getCacheSeconds()
Return the number of seconds that content is cached.
|
java.lang.String[] |
getSupportedMethods()
Return the HTTP methods that this content generator supports.
|
boolean |
isAlwaysMustRevalidate()
Return whether 'must-revaliate' is added to every Cache-Control header.
|
boolean |
isRequireSession()
Return whether a session is required to handle requests.
|
boolean |
isUseCacheControlHeader()
Return whether the HTTP 1.1 cache-control header is used.
|
boolean |
isUseCacheControlNoStore()
Return whether the HTTP 1.1 cache-control header value "no-store" is used.
|
boolean |
isUseExpiresHeader()
Return whether the HTTP 1.0 expires header is used.
|
protected void |
preventCaching(HttpServletResponse response)
Prevent the response from being cached.
|
void |
setAlwaysMustRevalidate(boolean mustRevalidate)
An option to add 'must-revalidate' to every Cache-Control header.
|
void |
setCacheSeconds(int seconds)
Cache content for the given number of seconds.
|
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)
Set whether to use the HTTP 1.1 cache-control header.
|
void |
setUseCacheControlNoStore(boolean useCacheControlNoStore)
Set whether to use the HTTP 1.1 cache-control header value "no-store"
when preventing caching.
|
void |
setUseExpiresHeader(boolean useExpiresHeader)
Set whether to use the HTTP 1.0 expires header.
|
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
private static final java.lang.String HEADER_CACHE_CONTROL
private java.util.Set<java.lang.String> supportedMethods
private boolean requireSession
private boolean useExpiresHeader
private boolean useCacheControlHeader
private boolean useCacheControlNoStore
private int cacheSeconds
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()
public final void setRequireSession(boolean requireSession)
public final boolean isRequireSession()
public final void setUseExpiresHeader(boolean useExpiresHeader)
Note: Cache headers will only get applied if caching is enabled (or explicitly prevented) for the current request.
public final boolean isUseExpiresHeader()
public final void setUseCacheControlHeader(boolean useCacheControlHeader)
Note: Cache headers will only get applied if caching is enabled (or explicitly prevented) for the current request.
public final boolean isUseCacheControlHeader()
public final void setUseCacheControlNoStore(boolean useCacheControlNoStore)
public final boolean isUseCacheControlNoStore()
public void setAlwaysMustRevalidate(boolean mustRevalidate)
WebRequest.checkNotModified(long)
. Default is "false",
effectively relying on whether the handler implements
LastModified
or not.public boolean isAlwaysMustRevalidate()
public final void setCacheSeconds(int seconds)
Only if this is set to 0 (no cache) or a positive value (cache for this many seconds) will this class generate cache headers.
The headers can be overwritten by subclasses, before content is generated.
public final int getCacheSeconds()
protected final void checkAndPrepare(HttpServletRequest request, HttpServletResponse response, boolean lastModified) throws ServletException
request
- current HTTP requestresponse
- current HTTP responselastModified
- if the mapped handler provides Last-Modified supportServletException
- if the request cannot be handled because a check failedprotected final void checkAndPrepare(HttpServletRequest request, HttpServletResponse response, int cacheSeconds, boolean lastModified) throws ServletException
request
- current HTTP requestresponse
- current HTTP responsecacheSeconds
- positive number of seconds into the future that the
response should be cacheable for, 0 to prevent cachinglastModified
- if the mapped handler provides Last-Modified supportServletException
- if the request cannot be handled because a check failedprotected final void preventCaching(HttpServletResponse response)
http://www.mnot.net/cache_docs
.protected final void cacheForSeconds(HttpServletResponse response, int seconds)
response
- current HTTP responseseconds
- number of seconds into the future that the response
should be cacheable for#cacheForSeconds(javax.servlet.http.HttpServletResponse, int, boolean)
protected final void cacheForSeconds(HttpServletResponse response, int seconds, boolean mustRevalidate)
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)protected final void applyCacheSeconds(HttpServletResponse response, int seconds)
response
- current HTTP responseseconds
- positive number of seconds into the future that the
response should be cacheable for, 0 to prevent caching#cacheForSeconds(javax.servlet.http.HttpServletResponse, int, boolean)
protected final void applyCacheSeconds(HttpServletResponse response, int seconds, boolean mustRevalidate)
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 responseseconds
- 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)