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 Details

    • useWhileInDefaultEnvironment

      @Value("${filter.compression.use.default.environment:true}") protected Boolean useWhileInDefaultEnvironment
      Whether 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 cacheWhileInDefaultEnvironment
      It 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 compressedFileTempDirectory
      Specify 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 sendFileSize
      The minimum size of a cached,compressed file (in bytes) for which to use the OS sendfile feature. 49152 bytes by default.
    • useSendFile

      @Value("${filter.compression.use.sendfile:true}") protected Boolean 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 compressionExtensionToMimeMappings
      Comma 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 allowStaticFileCache
      Whether 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 blackListURIs
      A 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

      @Value("${resource.versioning.enabled:true}") protected Boolean resourceVersioningEnabled
    • isDefaultEnvironment

      protected Boolean isDefaultEnvironment
    • initialized

      protected Boolean initialized
    • environment

      @Autowired protected org.springframework.core.env.Environment environment
    • cacheAwareResponseHandler

      @Autowired protected CacheAwareResponseHandler cacheAwareResponseHandler
    • atomicMove

      protected AtomicMove atomicMove
    • extensionToMime

      protected Map<Pattern,String> extensionToMime
    • blackListPatterns

      protected List<Pattern> 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:
      doFilterInternalUnlessIgnored in class AbstractIgnorableOncePerRequestFilter
      Throws:
      IOException
      jakarta.servlet.ServletException
    • getOrder

      public int getOrder()
    • initFilterBean

      protected void initFilterBean() throws jakarta.servlet.ServletException
      Overrides:
      initFilterBean in class org.springframework.web.filter.GenericFilterBean
      Throws:
      jakarta.servlet.ServletException
    • shouldUseStaticCache

      protected boolean shouldUseStaticCache()
    • getMimeType

      protected String getMimeType(jakarta.servlet.http.HttpServletRequest request)
    • processDynamic

      protected void processDynamic(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain chain) throws IOException, jakarta.servlet.ServletException
      Throws:
      IOException
      jakarta.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:
      IOException
      jakarta.servlet.ServletException
    • emitStaticFileToResponse

      protected void emitStaticFileToResponse(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, File targetFile) throws IOException
      Throws:
      IOException
    • prepareTargetFile

      protected File prepareTargetFile(jakarta.servlet.http.HttpServletRequest request)
    • 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:
      IOException
      jakarta.servlet.ServletException
    • useGzipCompression

      protected boolean useGzipCompression(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws MalformedURLException
      Throws:
      MalformedURLException