public class ShallowEtagHeaderFilter extends OncePerRequestFilter
Filter
that generates an ETag
value based on the
content on the response. This ETag is compared to the If-None-Match
header of the request. If these headers are equal, the response content is
not sent, but rather a 304 "Not Modified"
status instead.
Since the ETag is based on the response content, the response
(e.g. a View
) is still rendered.
As such, this filter only saves bandwidth, not server performance.
NOTE: As of Spring Framework 5.0, this filter uses request/response decorators built on the Servlet 3.1 API.
ALREADY_FILTERED_SUFFIX
logger
Constructor and Description |
---|
ShallowEtagHeaderFilter() |
Modifier and Type | Method and Description |
---|---|
static void |
disableContentCaching(ServletRequest request)
This method can be used to suppress the content caching response wrapper
of the ShallowEtagHeaderFilter.
|
protected void |
doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain)
Same contract as for
doFilter , but guaranteed to be
just invoked once per request within a single request thread. |
protected String |
generateETagHeaderValue(InputStream inputStream,
boolean isWeak)
Generate the ETag header value from the given response body byte array.
|
protected boolean |
isEligibleForEtag(HttpServletRequest request,
HttpServletResponse response,
int responseStatusCode,
InputStream inputStream)
Whether an ETag should be calculated for the given request and response
exchange.
|
boolean |
isWriteWeakETag()
Return whether the ETag value written to the response should be weak, as per RFC 7232.
|
void |
setWriteWeakETag(boolean writeWeakETag)
Set whether the ETag value written to the response should be weak, as per RFC 7232.
|
protected boolean |
shouldNotFilterAsyncDispatch()
The default value is
false so that the filter may delay the generation
of an ETag until the last asynchronously dispatched thread. |
doFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter, shouldNotFilterErrorDispatch
addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext
public void setWriteWeakETag(boolean writeWeakETag)
Should be configured using an <init-param>
for parameter name
"writeWeakETag" in the filter definition in web.xml
.
public boolean isWriteWeakETag()
protected boolean shouldNotFilterAsyncDispatch()
false
so that the filter may delay the generation
of an ETag until the last asynchronously dispatched thread.shouldNotFilterAsyncDispatch
in class OncePerRequestFilter
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException
OncePerRequestFilter
doFilter
, but guaranteed to be
just invoked once per request within a single request thread.
See OncePerRequestFilter.shouldNotFilterAsyncDispatch()
for details.
Provides HttpServletRequest and HttpServletResponse arguments instead of the default ServletRequest and ServletResponse ones.
doFilterInternal
in class OncePerRequestFilter
ServletException
IOException
protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletResponse response, int responseStatusCode, InputStream inputStream)
true
if all of the following match:
2xx
series.request
- the HTTP requestresponse
- the HTTP responseresponseStatusCode
- the HTTP response status codeinputStream
- the response bodytrue
if eligible for ETag generation, false
otherwiseprotected String generateETagHeaderValue(InputStream inputStream, boolean isWeak) throws IOException
The default implementation generates an MD5 hash.
inputStream
- the response body as an InputStreamisWeak
- whether the generated ETag should be weakIOException
DigestUtils
public static void disableContentCaching(ServletRequest request)
Note: This method must be called before the response is written to in order for the entire response content to be written without caching.