public class ShallowEtagHeaderFilter extends OncePerRequestFilter
javax.servlet.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 (or View
)
is still rendered. As such, this filter only saves bandwidth, not server performance.
Modifier and Type | Class and Description |
---|---|
private static class |
ShallowEtagHeaderFilter.ShallowEtagResponseWrapper
HttpServletRequest wrapper that buffers all content written to the
output stream and writer,
and allows this content to be retrieved via a byte array . |
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
HEADER_ETAG |
private static java.lang.String |
HEADER_IF_NONE_MATCH |
ALREADY_FILTERED_SUFFIX
logger
Constructor and Description |
---|
ShallowEtagHeaderFilter() |
Modifier and Type | Method and Description |
---|---|
private void |
copyBodyToResponse(byte[] body,
HttpServletResponse response) |
protected void |
doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain)
Same contract as for
doFilter , but guaranteed to be
just invoked once per request. |
protected java.lang.String |
generateETagHeaderValue(byte[] bytes)
Generate the ETag header value from the given response body byte array.
|
protected boolean |
isEligibleForEtag(HttpServletRequest request,
HttpServletResponse response,
int responseStatusCode,
byte[] responseBody)
Indicates whether the given request and response are eligible for ETag generation.
|
doFilter, getAlreadyFilteredAttributeName, shouldNotFilter
addRequiredProperty, afterPropertiesSet, destroy, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext
private static java.lang.String HEADER_ETAG
private static java.lang.String HEADER_IF_NONE_MATCH
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, java.io.IOException
OncePerRequestFilter
doFilter
, but guaranteed to be
just invoked once per request. Provides HttpServletRequest and
HttpServletResponse arguments instead of the default ServletRequest
and ServletResponse ones.doFilterInternal
in class OncePerRequestFilter
ServletException
java.io.IOException
private void copyBodyToResponse(byte[] body, HttpServletResponse response) throws java.io.IOException
java.io.IOException
protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletResponse response, int responseStatusCode, byte[] responseBody)
The default implementation returns true
for response status codes in the 2xx
series.
request
- the HTTP requestresponse
- the HTTP responseresponseStatusCode
- the HTTP response status coderesponseBody
- the response bodytrue
if eligible for ETag generation; false
otherwiseprotected java.lang.String generateETagHeaderValue(byte[] bytes)
The default implementation generates an MD5 hash.
bytes
- the response body as byte arrayDigestUtils