Spring Framework

org.springframework.web.servlet.support
Class WebContentGenerator

java.lang.Object
  extended by org.springframework.context.support.ApplicationObjectSupport
      extended by org.springframework.web.context.support.WebApplicationObjectSupport
          extended by org.springframework.web.servlet.support.WebContentGenerator
All Implemented Interfaces:
Aware, ApplicationContextAware, ServletContextAware
Direct Known Subclasses:
AbstractController, AbstractHandlerMethodAdapter, AnnotationMethodHandlerAdapter, ResourceHttpRequestHandler, WebContentInterceptor

public abstract class WebContentGenerator
extends 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 "useExpiresHeader", "useCacheControlHeader" and "useCacheControlNoStore" properties.

Author:
Rod Johnson, Juergen Hoeller
See Also:
setCacheSeconds(int), setRequireSession(boolean)

Field Summary
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"
 
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
 
Constructor Summary
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.
 
Method Summary
protected  void applyCacheSeconds(javax.servlet.http.HttpServletResponse response, int seconds)
          Apply the given cache seconds and generate corresponding HTTP headers, i.e.
protected  void applyCacheSeconds(javax.servlet.http.HttpServletResponse response, int seconds, boolean mustRevalidate)
          Apply the given cache seconds and generate respective HTTP headers.
protected  void cacheForSeconds(javax.servlet.http.HttpServletResponse response, int seconds)
          Set HTTP headers to allow caching for the given number of seconds.
protected  void cacheForSeconds(javax.servlet.http.HttpServletResponse response, int seconds, boolean mustRevalidate)
          Set HTTP headers to allow caching for the given number of seconds.
protected  void checkAndPrepare(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, boolean lastModified)
          Check and prepare the given request and response according to the settings of this generator.
protected  void checkAndPrepare(javax.servlet.http.HttpServletRequest request, javax.servlet.http.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 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(javax.servlet.http.HttpServletResponse response)
          Prevent the response from being cached.
 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.
 
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, requiredContextClass, setApplicationContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

METHOD_GET

public static final java.lang.String METHOD_GET
HTTP method "GET"

See Also:
Constant Field Values

METHOD_HEAD

public static final java.lang.String METHOD_HEAD
HTTP method "HEAD"

See Also:
Constant Field Values

METHOD_POST

public static final java.lang.String METHOD_POST
HTTP method "POST"

See Also:
Constant Field Values
Constructor Detail

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(java.lang.String... supportedMethods)
Create a new WebContentGenerator.

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

setSupportedMethods

public final void setSupportedMethods(java.lang.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

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


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.


setUseExpiresHeader

public final void setUseExpiresHeader(boolean useExpiresHeader)
Set whether to use the HTTP 1.0 expires header. Default is "true".

Note: Cache headers will only get applied if caching is enabled (or explicitly prevented) for the current request.


isUseExpiresHeader

public final boolean isUseExpiresHeader()
Return whether the HTTP 1.0 expires header is used.


setUseCacheControlHeader

public final void setUseCacheControlHeader(boolean useCacheControlHeader)
Set whether to use the HTTP 1.1 cache-control header. Default is "true".

Note: Cache headers will only get applied if caching is enabled (or explicitly prevented) for the current request.


isUseCacheControlHeader

public final boolean isUseCacheControlHeader()
Return whether the HTTP 1.1 cache-control header is used.


setUseCacheControlNoStore

public final void setUseCacheControlNoStore(boolean useCacheControlNoStore)
Set whether to use the HTTP 1.1 cache-control header value "no-store" when preventing caching. Default is "true".


isUseCacheControlNoStore

public final boolean isUseCacheControlNoStore()
Return whether the HTTP 1.1 cache-control header value "no-store" is used.


setCacheSeconds

public final void setCacheSeconds(int seconds)
Cache content for the given number of seconds. Default is -1, indicating no generation of cache-related headers.

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.


getCacheSeconds

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


checkAndPrepare

protected final void checkAndPrepare(javax.servlet.http.HttpServletRequest request,
                                     javax.servlet.http.HttpServletResponse response,
                                     boolean lastModified)
                              throws javax.servlet.ServletException
Check and prepare the given request and response according to the settings of this generator. Checks for supported methods and a required session, and applies the number of cache seconds specified for this generator.

Parameters:
request - current HTTP request
response - current HTTP response
lastModified - if the mapped handler provides Last-Modified support
Throws:
javax.servlet.ServletException - if the request cannot be handled because a check failed

checkAndPrepare

protected final void checkAndPrepare(javax.servlet.http.HttpServletRequest request,
                                     javax.servlet.http.HttpServletResponse response,
                                     int cacheSeconds,
                                     boolean lastModified)
                              throws javax.servlet.ServletException
Check and prepare the given request and response according to the settings of this generator. Checks for supported methods and a required session, and applies the given number of cache seconds.

Parameters:
request - current HTTP request
response - current HTTP response
cacheSeconds - positive number of seconds into the future that the response should be cacheable for, 0 to prevent caching
lastModified - if the mapped handler provides Last-Modified support
Throws:
javax.servlet.ServletException - if the request cannot be handled because a check failed

preventCaching

protected final void preventCaching(javax.servlet.http.HttpServletResponse response)
Prevent the response from being cached. See http://www.mnot.net/cache_docs.


cacheForSeconds

protected final void cacheForSeconds(javax.servlet.http.HttpServletResponse response,
                                     int seconds)
Set HTTP headers to allow caching for the given number of seconds. Does not tell the browser to revalidate the resource.

Parameters:
response - current HTTP response
seconds - number of seconds into the future that the response should be cacheable for
See Also:
cacheForSeconds(javax.servlet.http.HttpServletResponse, int, boolean)

cacheForSeconds

protected final void cacheForSeconds(javax.servlet.http.HttpServletResponse response,
                                     int seconds,
                                     boolean mustRevalidate)
Set HTTP headers to allow caching for the given number of seconds. Tells the browser to revalidate the resource if mustRevalidate is true.

Parameters:
response - the current HTTP response
seconds - number of seconds into the future that the response should be cacheable for
mustRevalidate - whether the client should revalidate the resource (typically only necessary for controllers with last-modified support)

applyCacheSeconds

protected final void applyCacheSeconds(javax.servlet.http.HttpServletResponse response,
                                       int seconds)
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
seconds - positive number of seconds into the future that the response should be cacheable for, 0 to prevent caching
See Also:
cacheForSeconds(javax.servlet.http.HttpServletResponse, int, boolean)

applyCacheSeconds

protected final void applyCacheSeconds(javax.servlet.http.HttpServletResponse response,
                                       int seconds,
                                       boolean mustRevalidate)
Apply the given cache seconds and generate respective HTTP headers.

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).

Parameters:
response - the current HTTP response
seconds - 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)

Spring Framework