类 ShallowEtagHeaderFilter
- java.lang.Object
- org.springframework.web.filter.GenericFilterBean
- org.springframework.web.filter.OncePerRequestFilter
- org.springframework.web.filter.ShallowEtagHeaderFilter
- 所有已实现的接口:
Filter
,Aware
,BeanNameAware
,DisposableBean
,InitializingBean
,EnvironmentAware
,EnvironmentCapable
,ServletContextAware
public class ShallowEtagHeaderFilter extends OncePerRequestFilter
Filter
that generates anETag
value based on the content on the response. This ETag is compared to theIf-None-Match
header of the request. If these headers are equal, the response content is not sent, but rather a304 "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.- 从以下版本开始:
- 3.0
- 作者:
- Arjen Poutsma, Rossen Stoyanchev, Brian Clozel, Juergen Hoeller
字段概要
从类继承的字段 org.springframework.web.filter.OncePerRequestFilter
ALREADY_FILTERED_SUFFIX
从类继承的字段 org.springframework.web.filter.GenericFilterBean
logger
构造器概要
构造器 构造器 说明 ShallowEtagHeaderFilter()
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 static void
disableContentCaching(ServletRequest request)
This method can be used to disable the content caching response wrapper of the ShallowEtagHeaderFilter.protected void
doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
Same contract as fordoFilter
, 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)
Indicates whether the given request and response are eligible for ETag generation.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 isfalse
so that the filter may delay the generation of an ETag until the last asynchronously dispatched thread.从类继承的方法 org.springframework.web.filter.OncePerRequestFilter
doFilter, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter, shouldNotFilterErrorDispatch
从类继承的方法 org.springframework.web.filter.GenericFilterBean
addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext
构造器详细资料
ShallowEtagHeaderFilter
public ShallowEtagHeaderFilter()
方法详细资料
setWriteWeakETag
public void setWriteWeakETag(boolean writeWeakETag)
Set whether the ETag value written to the response should be weak, as per RFC 7232.Should be configured using an
<init-param>
for parameter name "writeWeakETag" in the filter definition inweb.xml
.- 从以下版本开始:
- 4.3
- 另请参阅:
- RFC 7232 section 2.3
isWriteWeakETag
public boolean isWriteWeakETag()
Return whether the ETag value written to the response should be weak, as per RFC 7232.- 从以下版本开始:
- 4.3
shouldNotFilterAsyncDispatch
protected boolean shouldNotFilterAsyncDispatch()
The default value isfalse
so that the filter may delay the generation of an ETag until the last asynchronously dispatched thread.
doFilterInternal
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException
从类复制的说明:OncePerRequestFilter
Same contract as fordoFilter
, but guaranteed to be just invoked once per request within a single request thread. SeeOncePerRequestFilter.shouldNotFilterAsyncDispatch()
for details.Provides HttpServletRequest and HttpServletResponse arguments instead of the default ServletRequest and ServletResponse ones.
isEligibleForEtag
protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletResponse response, int responseStatusCode, InputStream inputStream)
Indicates whether the given request and response are eligible for ETag generation.The default implementation returns
true
if all conditions match:- response status codes in the
2xx
series - request method is a GET
- response Cache-Control header is not set or does not contain a "no-store" directive
- 参数:
request
- the HTTP requestresponse
- the HTTP responseresponseStatusCode
- the HTTP response status codeinputStream
- the response body- 返回:
true
if eligible for ETag generation,false
otherwise
- response status codes in the
generateETagHeaderValue
protected String generateETagHeaderValue(InputStream inputStream, boolean isWeak) throws IOException
Generate the ETag header value from the given response body byte array.The default implementation generates an MD5 hash.
- 参数:
inputStream
- the response body as an InputStreamisWeak
- whether the generated ETag should be weak- 返回:
- the ETag header value
- 抛出:
IOException
- 另请参阅:
DigestUtils
disableContentCaching
public static void disableContentCaching(ServletRequest request)
This method can be used to disable the content caching response wrapper of the ShallowEtagHeaderFilter. This can be done before the start of HTTP streaming for example where the response will be written to asynchronously and not in the context of a Servlet container thread.- 从以下版本开始:
- 4.2