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 "setCacheSeconds" or "setCacheControl" properties.
As of 4.2, its default behavior changed when using only setCacheSeconds(int)
, sending
HTTP response headers that are more in line with current browsers and proxies implementations.
Reverting to the previous behavior can be easily done by using one of the nealy deprecated methods
setUseExpiresHeader(boolean)
, setUseCacheControlHeader(boolean)
, setUseCacheControlNoStore(boolean)
or
setAlwaysMustRevalidate(boolean)
.
setCacheSeconds(int)
,
setRequireSession(boolean)
Modifier and Type | Field and Description |
---|---|
private boolean |
alwaysMustRevalidate |
private CacheControl |
cacheControl |
private int |
cacheSeconds |
private java.text.SimpleDateFormat |
dateFormat |
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?
|
private boolean |
usePreviousHttpCachingBehavior |
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 |
addHttp10CacheHeaders(HttpServletResponse response) |
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)
Check and prepare the given request and response according to the settings
of this generator.
|
protected void |
checkAndPrepare(HttpServletRequest request,
HttpServletResponse response,
boolean lastModified)
Deprecated.
|
protected void |
checkAndPrepare(HttpServletRequest request,
HttpServletResponse response,
CacheControl cacheControl)
Check and prepare the given request and response according to the settings
of this generator.
|
protected void |
checkAndPrepare(HttpServletRequest request,
HttpServletResponse response,
int cacheSeconds) |
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.
|
boolean |
isAlwaysMustRevalidate()
Deprecated.
|
boolean |
isRequireSession()
Return whether a session is required to handle requests.
|
boolean |
isUseCacheControlHeader()
Deprecated.
|
boolean |
isUseCacheControlNoStore()
Deprecated.
|
boolean |
isUseExpiresHeader()
Deprecated.
|
protected void |
preventCaching(HttpServletResponse response)
Prevent the response from being cached.
|
void |
setAlwaysMustRevalidate(boolean mustRevalidate)
Deprecated.
|
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.
|
void |
setUseCacheControlNoStore(boolean useCacheControlNoStore)
Deprecated.
|
void |
setUseExpiresHeader(boolean useExpiresHeader)
Deprecated.
|
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 int cacheSeconds
private boolean useExpiresHeader
private boolean useCacheControlHeader
private boolean useCacheControlNoStore
private boolean alwaysMustRevalidate
private boolean usePreviousHttpCachingBehavior
private final java.text.SimpleDateFormat dateFormat
private CacheControl cacheControl
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 void setCacheControl(CacheControl cacheControl)
CacheControl
instance to build
the Cache-Control HTTP response header.public CacheControl getCacheControl()
CacheControl
instance
that builds the Cache-Control HTTP response header.@Deprecated public final void setUseExpiresHeader(boolean useExpiresHeader)
setCacheSeconds(int)
or setCacheControl(org.springframework.http.CacheControl)
.Note: Cache headers will only get applied if caching is enabled (or explicitly prevented) for the current request.
@Deprecated public final boolean isUseExpiresHeader()
@Deprecated public final void setUseCacheControlHeader(boolean useCacheControlHeader)
setCacheSeconds(int)
or setCacheControl(org.springframework.http.CacheControl)
.Note: Cache headers will only get applied if caching is enabled (or explicitly prevented) for the current request.
@Deprecated public final boolean isUseCacheControlHeader()
@Deprecated public final void setUseCacheControlNoStore(boolean useCacheControlNoStore)
setCacheSeconds(int)
or setCacheControl(org.springframework.http.CacheControl)
.@Deprecated public final boolean isUseCacheControlNoStore()
@Deprecated public void setAlwaysMustRevalidate(boolean mustRevalidate)
setCacheSeconds(int)
or setCacheControl(org.springframework.http.CacheControl)
.WebRequest#checkNotModified(long)
. Default is "false".@Deprecated public boolean isAlwaysMustRevalidate()
public final void setCacheSeconds(int seconds)
For more specific needs, a custom CacheControl
should be used.
public final int getCacheSeconds()
protected final void checkAndPrepare(HttpServletRequest request, HttpServletResponse response) throws ServletException
request
- current HTTP requestresponse
- current HTTP responseServletException
- if the request cannot be handled because a check failed@Deprecated protected final void checkAndPrepare(HttpServletRequest request, HttpServletResponse response, boolean lastModified) throws ServletException
ServletException
protected final void checkAndPrepare(HttpServletRequest request, HttpServletResponse response, int cacheSeconds) throws ServletException
ServletException
protected final void checkAndPrepare(HttpServletRequest request, HttpServletResponse response, CacheControl cacheControl) throws ServletException
CacheControl
given as a parameter.request
- current HTTP requestresponse
- current HTTP responsecacheControl
- the CacheControl
to useServletException
- if the request cannot be handled because a check failedprotected void addHttp10CacheHeaders(HttpServletResponse response)
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 preventCaching(HttpServletResponse response)
http://www.mnot.net/cache_docs
.