Class GzipResponseFilter

java.lang.Object
jakarta.servlet.GenericFilter
jakarta.servlet.http.HttpFilter
com.netcracker.profiler.filter.GzipResponseFilter
All Implemented Interfaces:
jakarta.servlet.Filter, jakarta.servlet.FilterConfig, Serializable

@Singleton public class GzipResponseFilter extends jakarta.servlet.http.HttpFilter

The GzipResponseFilter will apply GZIP compression on responses whenever applicable. GZIP will greatly reduce the response size when applied on character based responses like HTML, CSS and JS, on average it can save up to ~70% of bandwidth.

While GZIP is normally to be configured in the servlet container (e.g. <Context compression="on"> in Tomcat, or <property name="compression" value="on"> in Glassfish), this filter allows a servlet container independent way of configuring GZIP compression and also allows enabling GZIP compression anyway on 3rd party hosts where you have no control over servlet container configuration.

Installation

To get it to run, map this filter on the desired <url-pattern> or maybe even on the <servlet-name> of the FacesServlet. A Filter is by default dispatched on REQUEST only, you might want to explicitly add the ERROR dispatcher to get it to run on error pages as well.

 <filter>
     <filter-name>gzipResponseFilter</filter-name>
     <filter-class>org.omnifaces.filter.GzipResponseFilter</filter-class>
 </filter>
 <filter-mapping>
     <filter-name>gzipResponseFilter</filter-name>
     <url-pattern>/*</url-pattern>
     <dispatcher>REQUEST</dispatcher>
     <dispatcher>ERROR</dispatcher>
 </filter-mapping>
 

Mapping on /* may be too global as some types of requests (comet, long polling, etc) cannot be gzipped.

 <filter>
     <filter-name>gzipResponseFilter</filter-name>
     <filter-class>org.omnifaces.filter.GzipResponseFilter</filter-class>
 </filter>
 <filter-mapping>
     <filter-name>gzipResponseFilter</filter-name>
     <servlet-name>facesServlet</servlet-name>
     <dispatcher>REQUEST</dispatcher>
     <dispatcher>ERROR</dispatcher>
 </filter-mapping>
 

Configuration (optional)

This filter supports two initialization parameters which needs to be placed in <filter> element as follows:

 <init-param>
     <description>The threshold size in bytes. Must be a number between 0 and 9999. Defaults to 150.</description>
     <param-name>threshold</param-name>
     <param-value>150</param-value>
 </init-param>
 <init-param>
     <description>The mimetypes which needs to be compressed. Must be a commaseparated string. Defaults to the below values.</description>
     <param-name>mimetypes</param-name>
     <param-value>
         text/plain, text/html, text/xml, text/css, text/javascript, text/csv, text/rtf,
         application/xml, application/xhtml+xml, application/javascript, application/x-javascript, application/json,
         image/svg+xml
     </param-value>
 </init-param>
 

The default threshold is thus 150 bytes. This means that when the response is not larger than 150 bytes, then it will not be compressed with GZIP. Only when it's larger than 150 bytes, then it will be compressed. A threshold of between 150 and 1000 bytes is recommended due to overhead and latency of compression/decompression. The value must be a number between 0 and 9999. A value larger than 2000 is not recommended.

The mimetypes represents a comma separated string of mime types which needs to be compressed. It's exactly that value which appears in the Content-Type header of the response. The in the above example mentioned mime types are already the default values. Note that GZIP does not have any benefit when applied on binary mimetypes like images, office documents, PDF files, etcetera. So setting it for them is not recommended.

Since 3.11, application/x-javascript has been added to default mimetypes.

Since:
1.1
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    doFilter(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain chain)
    Perform the filtering job.
    void
    Initializes the filter parameters.

    Methods inherited from class jakarta.servlet.http.HttpFilter

    doFilter

    Methods inherited from class jakarta.servlet.GenericFilter

    getFilterConfig, getFilterName, getInitParameter, getInitParameterNames, getServletContext, init

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface jakarta.servlet.Filter

    destroy
  • Constructor Details

    • GzipResponseFilter

      public GzipResponseFilter()
  • Method Details

    • init

      public void init() throws jakarta.servlet.ServletException
      Initializes the filter parameters.
      Overrides:
      init in class jakarta.servlet.GenericFilter
      Throws:
      jakarta.servlet.ServletException
    • doFilter

      public void doFilter(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain chain) throws jakarta.servlet.ServletException, IOException
      Perform the filtering job. Only if the client accepts GZIP based on the request headers, then wrap the response in a GzipHttpServletResponse and pass it through the filter chain.
      Overrides:
      doFilter in class jakarta.servlet.http.HttpFilter
      Throws:
      jakarta.servlet.ServletException
      IOException