Top Description Fields Constructors Methods
org.springframework.web.servlet.support

public abstract Class WebContentGenerator

extends WebApplicationObjectSupport
Class Inheritance
Known Direct Subclasses
org.springframework.web.servlet.mvc.AbstractController, org.springframework.web.servlet.mvc.WebContentInterceptor, org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter, org.springframework.web.servlet.resource.ResourceHttpRequestHandler
Imports
java.util.ArrayList, .Arrays, .Collection, .Collections, .LinkedHashSet, .Set, java.util.concurrent.TimeUnit, jakarta.servlet.ServletException, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.springframework.http.CacheControl, .HttpHeaders, .HttpMethod, org.springframework.lang.Nullable, org.springframework.util.ObjectUtils, .StringUtils, org.springframework.web.HttpRequestMethodNotSupportedException, .HttpSessionRequiredException, org.springframework.web.context.support.WebApplicationObjectSupport

Convenient superclass for any kind of web content generator, like org.springframework.web.servlet.mvc.AbstractController and org.springframework.web.servlet.mvc.WebContentInterceptor. Can also be used for custom handlers that have their own org.springframework.web.servlet.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, 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, setUseCacheControlHeader, setUseCacheControlNoStore or setAlwaysMustRevalidate.

Authors
Rod Johnson, Juergen Hoeller, Brian Clozel, Rossen Stoyanchev
See Also
setCacheSeconds, setCacheControl, setRequireSession

Field Summary

Modifier and TypeField and Description
private String
private boolean
private CacheControl
private int
protected static final String
private static final String
private static final String
public static final String
METHOD_GET

HTTP method "GET".

public static final String
METHOD_HEAD

HTTP method "HEAD".

public static final String
METHOD_POST

HTTP method "POST".

private boolean
private Set<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 String[]

Constructor Summary

AccessConstructor and Description
public
WebContentGenerator()

Create a new WebContentGenerator which supports HTTP methods GET, HEAD and POST by default.

public
WebContentGenerator(boolean
true if this generator should support HTTP methods GET, HEAD and POST by default, or false if it should be unrestricted
restrictDefaultSupportedMethods
)

Create a new WebContentGenerator.

public
WebContentGenerator(String...
the supported HTTP methods for this content generator
supportedMethods
)

Create a new WebContentGenerator.

Method Summary

Modifier and TypeMethod and Description
protected final void
applyCacheControl(HttpServletResponse
current HTTP response
response
,
CacheControl
the pre-configured cache control settings
cacheControl
)

Set the HTTP Cache-Control header according to the given settings.

protected final void
applyCacheSeconds(HttpServletResponse
current HTTP response
response
,
int
positive number of seconds into the future that the response should be cacheable for, 0 to prevent caching
cacheSeconds
)

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.

protected final void
applyCacheSeconds(HttpServletResponse
the current HTTP response
response
,
int
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.
cacheSeconds
,
boolean
whether the client should revalidate the resource (typically only necessary for controllers with last-modified support)
mustRevalidate
)
Deprecated as of 4.2, in favor of applyCacheControl

Apply the given cache seconds and generate respective HTTP headers.

protected final void
cacheForSeconds(HttpServletResponse
current HTTP response
response
,
int
number of seconds into the future that the response should be cacheable for
seconds
)
Deprecated as of 4.2, in favor of applyCacheControl

Set HTTP headers to allow caching for the given number of seconds.

protected final void
cacheForSeconds(HttpServletResponse
the current HTTP response
response
,
int
number of seconds into the future that the response should be cacheable for
seconds
,
boolean
whether the client should revalidate the resource (typically only necessary for controllers with last-modified support)
mustRevalidate
)
Deprecated as of 4.2, in favor of applyCacheControl

Set HTTP headers to allow caching for the given number of seconds.

protected final 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

Check and prepare the given request and response according to the settings of this generator.

protected final void
checkRequest(HttpServletRequest
current HTTP request
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.

public final CacheControl
getCacheControl()

Get the org.springframework.http.CacheControl instance that builds the Cache-Control HTTP response header.

public final int
getCacheSeconds()

Return the number of seconds that content is cached.

public final String[]
getSupportedMethods()

Return the HTTP methods that this content generator supports.

public final String[]
getVaryByRequestHeaders()

Return the configured request header names for the "Vary" response header.

private Collection<String>
getVaryRequestHeadersToAdd(HttpServletResponse response, String[] varyByRequestHeaders)

private void
public final boolean
isAlwaysMustRevalidate()
Deprecated as of 4.2, in favor of getCacheControl()

Return whether 'must-revalidate' is added to every Cache-Control header.

public final boolean
isRequireSession()

Return whether a session is required to handle requests.

public final boolean
isUseCacheControlHeader()
Deprecated as of 4.2, in favor of getCacheControl()

Return whether the HTTP 1.1 cache-control header is used.

public final boolean
isUseCacheControlNoStore()
Deprecated as of 4.2, in favor of getCacheControl()

Return whether the HTTP 1.1 cache-control header value "no-store" is used.

public final boolean
isUseExpiresHeader()
Deprecated as of 4.2, in favor of getCacheControl()

Return whether the HTTP 1.0 expires header is used.

protected final void
prepareResponse(HttpServletResponse
current HTTP response
response
)

Prepare the given response according to the settings of this generator.

protected final void
preventCaching(HttpServletResponse response)
Deprecated as of 4.2, in favor of applyCacheControl

Prevent the response from being cached.

public final void
setAlwaysMustRevalidate(boolean mustRevalidate)
Deprecated as of 4.2, in favor of setCacheControl

An option to add 'must-revalidate' to every Cache-Control header.

public final void
setCacheControl(CacheControl cacheControl)

Set the org.springframework.http.CacheControl instance to build the Cache-Control HTTP response header.

public final 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
public final void
setRequireSession(boolean requireSession)

Set whether a session should be required to handle requests.

public final void
setSupportedMethods(String... methods)

Set the HTTP methods that this content generator should support.

public final 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

Set whether to use the HTTP 1.1 cache-control header.

public final void
setUseCacheControlNoStore(boolean useCacheControlNoStore)
Deprecated as of 4.2, in favor of setCacheControl

Set whether to use the HTTP 1.1 cache-control header value "no-store" when preventing caching.

public final 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

Set whether to use the HTTP 1.0 expires header.

public final void
setVaryByRequestHeaders(String...
one or more request header names
varyByRequestHeaders
)

Configure one or more request header names (e.g. "Accept-Language") to add to the "Vary" response header to inform clients that the response is subject to content negotiation and variances based on the value of the given request headers.

Inherited from org.springframework.web.context.support.WebApplicationObjectSupport:
getServletContextgetTempDirgetWebApplicationContextinitApplicationContextinitServletContextisContextRequiredsetServletContext

Field Detail

allowHeaderback to summary
private String allowHeader
Annotations
@Nullable
alwaysMustRevalidateback to summary
private boolean alwaysMustRevalidate
cacheControlback to summary
private CacheControl cacheControl
Annotations
@Nullable
cacheSecondsback to summary
private int cacheSeconds
HEADER_CACHE_CONTROLback to summary
protected static final String HEADER_CACHE_CONTROL
HEADER_EXPIRESback to summary
private static final String HEADER_EXPIRES
HEADER_PRAGMAback to summary
private static final String HEADER_PRAGMA
METHOD_GETback to summary
public static final String METHOD_GET

HTTP method "GET".

METHOD_HEADback to summary
public static final String METHOD_HEAD

HTTP method "HEAD".

METHOD_POSTback to summary
public static final String METHOD_POST

HTTP method "POST".

requireSessionback to summary
private boolean requireSession
supportedMethodsback to summary
private Set<String> supportedMethods

Set of supported HTTP methods.

Annotations
@Nullable
useCacheControlHeaderback to summary
private boolean useCacheControlHeader

Use HTTP 1.1 cache-control header?

useCacheControlNoStoreback to summary
private boolean useCacheControlNoStore

Use HTTP 1.1 cache-control header value "no-store"?

useExpiresHeaderback to summary
private boolean useExpiresHeader

Use HTTP 1.0 expires header?

varyByRequestHeadersback to summary
private String[] varyByRequestHeaders
Annotations
@Nullable

Constructor Detail

WebContentGeneratorback to summary
public WebContentGenerator()

Create a new WebContentGenerator which supports HTTP methods GET, HEAD and POST by default.

WebContentGeneratorback to summary
public WebContentGenerator(boolean restrictDefaultSupportedMethods)

Create a new WebContentGenerator.

Parameters
restrictDefaultSupportedMethods:boolean

true if this generator should support HTTP methods GET, HEAD and POST by default, or false if it should be unrestricted

WebContentGeneratorback to summary
public WebContentGenerator(String... supportedMethods)

Create a new WebContentGenerator.

Parameters
supportedMethods:String[]

the supported HTTP methods for this content generator

Method Detail

applyCacheControlback to summary
protected final void applyCacheControl(HttpServletResponse response, CacheControl cacheControl)

Set the HTTP Cache-Control header according to the given settings.

Parameters
response:HttpServletResponse

current HTTP response

cacheControl:CacheControl

the pre-configured cache control settings

Since
4.2
applyCacheSecondsback to summary
protected final void applyCacheSeconds(HttpServletResponse response, int cacheSeconds)

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:HttpServletResponse

current HTTP response

cacheSeconds:int

positive number of seconds into the future that the response should be cacheable for, 0 to prevent caching

Annotations
@SuppressWarnings:deprecation
applyCacheSecondsback to summary
protected final void applyCacheSeconds(HttpServletResponse response, int cacheSeconds, boolean mustRevalidate)

Deprecated

as of 4.2, in favor of applyCacheControl

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:HttpServletResponse

the current HTTP response

cacheSeconds:int

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:boolean

whether the client should revalidate the resource (typically only necessary for controllers with last-modified support)

Annotations
@Deprecated
cacheForSecondsback to summary
protected final void cacheForSeconds(HttpServletResponse response, int seconds)

Deprecated

as of 4.2, in favor of applyCacheControl

Set HTTP headers to allow caching for the given number of seconds. Does not tell the browser to revalidate the resource.

Parameters
response:HttpServletResponse

current HTTP response

seconds:int

number of seconds into the future that the response should be cacheable for

Annotations
@Deprecated
cacheForSecondsback to summary
protected final void cacheForSeconds(HttpServletResponse response, int seconds, boolean mustRevalidate)

Deprecated

as of 4.2, in favor of applyCacheControl

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:HttpServletResponse

the current HTTP response

seconds:int

number of seconds into the future that the response should be cacheable for

mustRevalidate:boolean

whether the client should revalidate the resource (typically only necessary for controllers with last-modified support)

Annotations
@Deprecated
checkAndPrepareback to summary
protected final void checkAndPrepare(HttpServletRequest request, HttpServletResponse response, int cacheSeconds, boolean lastModified) throws ServletException

Deprecated

as of 4.2, since the lastModified flag is effectively ignored, with a must-revalidate header only generated if explicitly configured

Check and prepare the given request and response according to the settings of this generator.

Annotations
@Deprecated
See Also
checkRequest(HttpServletRequest), applyCacheSeconds(HttpServletResponse, int)
checkRequestback to summary
protected final void checkRequest(HttpServletRequest request) throws ServletException

Check the given request for supported methods and a required session, if any.

Parameters
request:HttpServletRequest

current HTTP request

Exceptions
ServletException:
if the request cannot be handled because a check failed
Since
4.2
getAllowHeaderback to summary
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. This means subclasses 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).

Annotations
@Nullable
Since
4.3
getCacheControlback to summary
public final CacheControl getCacheControl()

Get the org.springframework.http.CacheControl instance that builds the Cache-Control HTTP response header.

Annotations
@Nullable
Since
4.2
getCacheSecondsback to summary
public final int getCacheSeconds()

Return the number of seconds that content is cached.

getSupportedMethodsback to summary
public final String[] getSupportedMethods()

Return the HTTP methods that this content generator supports.

Annotations
@Nullable
getVaryByRequestHeadersback to summary
public final String[] getVaryByRequestHeaders()

Return the configured request header names for the "Vary" response header.

Annotations
@Nullable
Since
4.3
getVaryRequestHeadersToAddback to summary
private Collection<String> getVaryRequestHeadersToAdd(HttpServletResponse response, String[] varyByRequestHeaders)
initAllowHeaderback to summary
private void initAllowHeader()
isAlwaysMustRevalidateback to summary
public final boolean isAlwaysMustRevalidate()

Deprecated

as of 4.2, in favor of getCacheControl()

Return whether 'must-revalidate' is added to every Cache-Control header.

Annotations
@Deprecated
isRequireSessionback to summary
public final boolean isRequireSession()

Return whether a session is required to handle requests.

isUseCacheControlHeaderback to summary
public final boolean isUseCacheControlHeader()

Deprecated

as of 4.2, in favor of getCacheControl()

Return whether the HTTP 1.1 cache-control header is used.

Annotations
@Deprecated
isUseCacheControlNoStoreback to summary
public final boolean isUseCacheControlNoStore()

Deprecated

as of 4.2, in favor of getCacheControl()

Return whether the HTTP 1.1 cache-control header value "no-store" is used.

Annotations
@Deprecated
isUseExpiresHeaderback to summary
public final boolean isUseExpiresHeader()

Deprecated

as of 4.2, in favor of getCacheControl()

Return whether the HTTP 1.0 expires header is used.

Annotations
@Deprecated
prepareResponseback to summary
protected final void prepareResponse(HttpServletResponse response)

Prepare the given response according to the settings of this generator. Applies the number of cache seconds specified for this generator.

Parameters
response:HttpServletResponse

current HTTP response

Since
4.2
preventCachingback to summary
protected final void preventCaching(HttpServletResponse response)

Deprecated

as of 4.2, in favor of applyCacheControl

Prevent the response from being cached. Only called in HTTP 1.0 compatibility mode.

See https://www.mnot.net/cache_docs.

Annotations
@Deprecated
setAlwaysMustRevalidateback to summary
public final void setAlwaysMustRevalidate(boolean mustRevalidate)

Deprecated

as of 4.2, in favor of setCacheControl

An option to add 'must-revalidate' to every Cache-Control header. This may be useful with annotated controller methods, which can programmatically do a last-modified calculation as described in org.springframework.web.context.request.WebRequest#checkNotModified(long).

Default is "false".

Annotations
@Deprecated
setCacheControlback to summary
public final void setCacheControl(CacheControl cacheControl)

Set the org.springframework.http.CacheControl instance to build the Cache-Control HTTP response header.

Since
4.2
setCacheSecondsback to summary
public final 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

For more specific needs, a custom org.springframework.http.CacheControl should be used.

See Also
setCacheControl
setRequireSessionback to summary
public final void setRequireSession(boolean requireSession)

Set whether a session should be required to handle requests.

setSupportedMethodsback to summary
public final void setSupportedMethods(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.

setUseCacheControlHeaderback to summary
public final 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

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.

Annotations
@Deprecated
setUseCacheControlNoStoreback to summary
public final void setUseCacheControlNoStore(boolean useCacheControlNoStore)

Deprecated

as of 4.2, in favor of setCacheControl

Set whether to use the HTTP 1.1 cache-control header value "no-store" when preventing caching. Default is "true".

Annotations
@Deprecated
setUseExpiresHeaderback to summary
public final 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

Set whether to use the HTTP 1.0 expires header. Default is "false", as of 4.2.

Note

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

Annotations
@Deprecated
setVaryByRequestHeadersback to summary
public final void setVaryByRequestHeaders(String... varyByRequestHeaders)

Configure one or more request header names (e.g. "Accept-Language") to add to the "Vary" response header to inform clients that the response is subject to content negotiation and variances based on the value of the given request headers. The configured request header names are added only if not already present in the response "Vary" header.

Parameters
varyByRequestHeaders:String[]

one or more request header names

Since
4.3