@Component(value="blCachingCompressedResponseFilter") @ConditionalOnProperty(value="filter.compression.enabled") public class CachingCompressedResponseFilter extends AbstractIgnorableOncePerRequestFilter
filter.compression.extension.mime.mappings=.\*\.css:text/css,.\*\.js.
See the javadoc for compressionExtensionToMimeMappings for the default values. 'filter.compression.allow.static.file.cache'
is false by default, which makes the most sense when a CDN is in place, since the CDN will be the primary source of
asset caching.
The directory in which cached static files should be stored is denoted via the 'filter.compression.file.temp.directory'
property. By default, this property is set to 'none', which signifies that the standard java temp directory should be used.
If using static file compression caching, make sure you have enough free hard disk space to accommodate compressed versions
of all the interesting static files.
Dynamic compression generally refers to compression requests for dynamic web pages, such a standard HTML page requests,
or JSON responses for RESTful endpoints. There is no caching of the compressed response and the response is compressed
on every request.
There is an additional sendfile optimization for static/cached files. For larger files, the OS can use the 'sendfile'
optimization to increase network throughput and reduce CPU overhead. By default, this is enabled for files over 49152 bytes.
This threshold can be changed via the 'filter.compression.sendfile.size' property. While the sendfile feature is enabled
by default, it can be turned off altogether via the 'filter.compression.use.sendfile' property (set to false).
Certain request URI can be blacklisted for compression altogether via the 'filter.compression.blacklist.uri.regex'
property. This is a comma delimited list of regular expression that, when matched against a request URI, will cause
that URI to not be compressed.| Modifier and Type | Field and Description |
|---|---|
protected Boolean |
allowStaticFileCache
Whether or not to cache static file compression results.
|
protected AtomicMove |
atomicMove |
protected List<Pattern> |
blackListPatterns |
protected String |
blackListURIs
A comma delimited list of URI matching regular expressions for requests that should be ignored for any compression.
|
protected CacheAwareResponseHandler |
cacheAwareResponseHandler |
protected Boolean |
cacheWhileInDefaultEnvironment
It is likely resource versioning will be disabled in the default, development environment.
|
protected String |
compressedFileTempDirectory
Specify a filesystem directory in which to store compressed static files.
|
protected String |
compressionExtensionToMimeMappings
Comma delimited URI matching regular expression to mime type mapping.
|
protected org.springframework.core.env.Environment |
environment |
protected Map<Pattern,String> |
extensionToMime |
protected Boolean |
initialized |
protected Boolean |
isDefaultEnvironment |
protected Boolean |
resourceVersioningEnabled |
protected long |
sendFileSize
The minimum size of a cached,compressed file (in bytes) for which to use the OS sendfile feature.
|
protected Boolean |
useSendFile
Whether or not to ever use the OS sendfile feature.
|
protected Boolean |
useWhileInDefaultEnvironment
Whether or not compression is enabled in the default Spring environment.
|
| Constructor and Description |
|---|
CachingCompressedResponseFilter() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
cacheStaticCompressedFileInFileSystem(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
javax.servlet.FilterChain chain,
File targetFile) |
protected void |
doFilterInternalUnlessIgnored(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
javax.servlet.FilterChain chain) |
protected void |
emitStaticFileToResponse(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
File targetFile) |
protected String |
getMimeType(javax.servlet.http.HttpServletRequest request) |
int |
getOrder() |
protected void |
initFilterBean() |
protected File |
prepareTargetFile(javax.servlet.http.HttpServletRequest request) |
protected void |
processDynamic(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
javax.servlet.FilterChain chain) |
protected boolean |
processStatic(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
javax.servlet.FilterChain chain,
String mimeType) |
protected boolean |
shouldUseStaticCache() |
protected boolean |
useGzipCompression(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) |
doFilterInternal, isIgnoreddoFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter, shouldNotFilterAsyncDispatch, shouldNotFilterErrorDispatch@Value(value="${filter.compression.use.default.environment:true}")
protected Boolean useWhileInDefaultEnvironment
@Value(value="${filter.compression.cache.default.environment:false}")
protected Boolean cacheWhileInDefaultEnvironment
@Value(value="${filter.compression.file.temp.directory:none}")
protected String compressedFileTempDirectory
@Value(value="${filter.compression.sendfile.size:49152}")
protected long sendFileSize
@Value(value="${filter.compression.use.sendfile:true}")
protected Boolean useSendFile
@Value(value="${filter.compression.extension.mime.mappings:.*\\.svg:image/svg+xml,.*\\.png:image/png,.*\\.xml:text/xml,.*\\.css:text/css,.*\\.js:application/javascript,.*\\.otf:application/x-font-opentype,.*\\.json:application/json,.*\\.css\\.map:application/json,.*\\.js\\.map:application/json}")
protected String compressionExtensionToMimeMappings
.\*\.svg:image/svg+xml,.\*\.png:image/png,.\*\.xml:text/xml,.\*\.css:text/css,.\*\.js:application/javascript,.\*\.otf:application/x-font-opentype,.\*\.json:application/json,.\*\.css\.map:application/json,.\*\.js\.map:application/json@Value(value="${filter.compression.allow.static.file.cache:false}")
protected Boolean allowStaticFileCache
@Value(value="${filter.compression.blacklist.uri.regex:.*\\.jpg,.*\\.jpeg,.*\\.gif,.*\\.png}")
protected String blackListURIs
@Value(value="${resource.versioning.enabled:true}")
protected Boolean resourceVersioningEnabled
protected Boolean isDefaultEnvironment
protected Boolean initialized
@Autowired protected org.springframework.core.env.Environment environment
@Autowired protected CacheAwareResponseHandler cacheAwareResponseHandler
protected AtomicMove atomicMove
protected void doFilterInternalUnlessIgnored(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
javax.servlet.FilterChain chain)
throws IOException,
javax.servlet.ServletException
doFilterInternalUnlessIgnored in class AbstractIgnorableOncePerRequestFilterIOExceptionjavax.servlet.ServletExceptionpublic int getOrder()
protected void initFilterBean()
throws javax.servlet.ServletException
initFilterBean in class org.springframework.web.filter.GenericFilterBeanjavax.servlet.ServletExceptionprotected boolean shouldUseStaticCache()
protected String getMimeType(javax.servlet.http.HttpServletRequest request)
protected void processDynamic(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
javax.servlet.FilterChain chain)
throws IOException,
javax.servlet.ServletException
IOExceptionjavax.servlet.ServletExceptionprotected boolean processStatic(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
javax.servlet.FilterChain chain,
String mimeType)
throws IOException,
javax.servlet.ServletException
IOExceptionjavax.servlet.ServletExceptionprotected void emitStaticFileToResponse(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
File targetFile)
throws IOException
IOExceptionprotected File prepareTargetFile(javax.servlet.http.HttpServletRequest request)
protected void cacheStaticCompressedFileInFileSystem(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
javax.servlet.FilterChain chain,
File targetFile)
throws IOException,
javax.servlet.ServletException
IOExceptionjavax.servlet.ServletExceptionprotected boolean useGzipCompression(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws MalformedURLException
MalformedURLExceptionCopyright © 2020. All rights reserved.