Class CachingCompressedResponseFilter
java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.web.filter.OncePerRequestFilter
org.broadleafcommerce.common.web.filter.AbstractIgnorableOncePerRequestFilter
org.broadleafcommerce.common.web.util.CachingCompressedResponseFilter
- All Implemented Interfaces:
jakarta.servlet.Filter,org.springframework.beans.factory.Aware,org.springframework.beans.factory.BeanNameAware,org.springframework.beans.factory.DisposableBean,org.springframework.beans.factory.InitializingBean,org.springframework.context.EnvironmentAware,org.springframework.core.env.EnvironmentCapable,org.springframework.core.Ordered,org.springframework.web.context.ServletContextAware
@Component("blCachingCompressedResponseFilter")
@ConditionalOnProperty("filter.compression.enabled")
public class CachingCompressedResponseFilter
extends AbstractIgnorableOncePerRequestFilter
Provides either dynamic, or cached static compression of response data. The 'filter.compression.enabled' property must
be set to true to enable this filter.
Cached static file compression generally refers to compression requests for static artifacts, such as css, js and similar files. To
allow static file caching, the 'filter.compression.allow.static.file.cache' property must be true and the file
must be recognized in the list of extension to mimetype mapping represented in the 'filter.compression.extension.mime.mappings'
property. Should a static file fail to be found in the mapping list, the system will fallback to dynamic compression for
the artifact. The 'filter.compression.extension.mime.mappings' property is a comma delimited list of request URI matching
regular expressions to mime types. For example, the following property setting would limit static compressed file caching
to only files that end in the '.css' and '.js' extensions:
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.- Author:
- Jeff Fischer
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected BooleanWhether or not to cache static file compression results.protected AtomicMoveprotected StringA comma delimited list of URI matching regular expressions for requests that should be ignored for any compression.protected CacheAwareResponseHandlerprotected BooleanIt is likely resource versioning will be disabled in the default, development environment.protected StringSpecify a filesystem directory in which to store compressed static files.protected StringComma delimited URI matching regular expression to mime type mapping.protected org.springframework.core.env.Environmentprotected Booleanprotected Booleanprotected Booleanprotected longThe minimum size of a cached,compressed file (in bytes) for which to use the OS sendfile feature. 49152 bytes by default.protected BooleanWhether or not to ever use the OS sendfile feature.protected BooleanWhether or not compression is enabled in the default Spring environment.Fields inherited from class org.springframework.web.filter.OncePerRequestFilter
ALREADY_FILTERED_SUFFIXFields inherited from class org.springframework.web.filter.GenericFilterBean
loggerFields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcacheStaticCompressedFileInFileSystem(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain chain, File targetFile) protected voiddoFilterInternalUnlessIgnored(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain chain) protected voidemitStaticFileToResponse(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, File targetFile) protected StringgetMimeType(jakarta.servlet.http.HttpServletRequest request) intgetOrder()protected voidprotected FileprepareTargetFile(jakarta.servlet.http.HttpServletRequest request) protected voidprocessDynamic(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain chain) protected booleanprocessStatic(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain chain, String mimeType) protected booleanprotected booleanuseGzipCompression(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Methods inherited from class org.broadleafcommerce.common.web.filter.AbstractIgnorableOncePerRequestFilter
doFilterInternal, isIgnoredMethods inherited from class org.springframework.web.filter.OncePerRequestFilter
doFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter, shouldNotFilterAsyncDispatch, shouldNotFilterErrorDispatchMethods inherited from class org.springframework.web.filter.GenericFilterBean
addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, setBeanName, setEnvironment, setServletContext
-
Field Details
-
useWhileInDefaultEnvironment
@Value("${filter.compression.use.default.environment:true}") protected Boolean useWhileInDefaultEnvironmentWhether or not compression is enabled in the default Spring environment. Used to turn off during development. True by default (compression enabled). -
cacheWhileInDefaultEnvironment
@Value("${filter.compression.cache.default.environment:false}") protected Boolean cacheWhileInDefaultEnvironmentIt is likely resource versioning will be disabled in the default, development environment. This setting will tell the system to not cache static resources in the local filesystem in the default environment to avoid static asset caching issues where a versioned name change is not involved. -
compressedFileTempDirectory
@Value("${filter.compression.file.temp.directory:none}") protected String compressedFileTempDirectorySpecify a filesystem directory in which to store compressed static files. 'none' by default, which means use the java temp directory. -
sendFileSize
@Value("${filter.compression.sendfile.size:49152}") protected long sendFileSizeThe minimum size of a cached,compressed file (in bytes) for which to use the OS sendfile feature. 49152 bytes by default. -
useSendFile
Whether or not to ever use the OS sendfile feature. True by default (use the feature). -
compressionExtensionToMimeMappings
@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 compressionExtensionToMimeMappingsComma delimited URI matching regular expression to mime type mapping. The default value is.*\\.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 -
allowStaticFileCache
@Value("${filter.compression.allow.static.file.cache:false}") protected Boolean allowStaticFileCacheWhether or not to cache static file compression results. The default value is false (don't use the cache). -
blackListURIs
@Value("${filter.compression.blacklist.uri.regex:.*\\.jpg,.*\\.jpeg,.*\\.gif,.*\\.png}") protected String blackListURIsA comma delimited list of URI matching regular expressions for requests that should be ignored for any compression. The default value is '.*\\.jpg,.*\\.jpeg,.*\\.gif,.*\\.png'. -
resourceVersioningEnabled
-
isDefaultEnvironment
-
initialized
-
environment
@Autowired protected org.springframework.core.env.Environment environment -
cacheAwareResponseHandler
-
atomicMove
-
extensionToMime
-
blackListPatterns
-
-
Constructor Details
-
CachingCompressedResponseFilter
public CachingCompressedResponseFilter()
-
-
Method Details
-
doFilterInternalUnlessIgnored
protected void doFilterInternalUnlessIgnored(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain chain) throws IOException, jakarta.servlet.ServletException - Specified by:
doFilterInternalUnlessIgnoredin classAbstractIgnorableOncePerRequestFilter- Throws:
IOExceptionjakarta.servlet.ServletException
-
getOrder
public int getOrder() -
initFilterBean
protected void initFilterBean() throws jakarta.servlet.ServletException- Overrides:
initFilterBeanin classorg.springframework.web.filter.GenericFilterBean- Throws:
jakarta.servlet.ServletException
-
shouldUseStaticCache
protected boolean shouldUseStaticCache() -
getMimeType
-
processDynamic
protected void processDynamic(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain chain) throws IOException, jakarta.servlet.ServletException - Throws:
IOExceptionjakarta.servlet.ServletException
-
processStatic
protected boolean processStatic(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain chain, String mimeType) throws IOException, jakarta.servlet.ServletException - Throws:
IOExceptionjakarta.servlet.ServletException
-
emitStaticFileToResponse
protected void emitStaticFileToResponse(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, File targetFile) throws IOException - Throws:
IOException
-
prepareTargetFile
-
cacheStaticCompressedFileInFileSystem
protected void cacheStaticCompressedFileInFileSystem(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain chain, File targetFile) throws IOException, jakarta.servlet.ServletException - Throws:
IOExceptionjakarta.servlet.ServletException
-
useGzipCompression
protected boolean useGzipCompression(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws MalformedURLException - Throws:
MalformedURLException
-