Rakesh API

org.rakeshv.filters
Class CompressionFilter

java.lang.Object
  extended by org.rakeshv.filters.FilterAdapter
      extended by org.rakeshv.filters.CompressionFilter
All Implemented Interfaces:
javax.servlet.Filter

public class CompressionFilter
extends FilterAdapter

A Filter class that is used to compress the response to a client request. The filter is activated only if the client specified the Accept-Encoding, or the TE HTTP headers.

The following shows ways in which you can enable the filters in your web application configuration file (WEB-INF/web.xml):

  <filter>
    <filter-name>compressionFilter</filter-name>
    <filter-class>org.rakeshv.filters.CompressionFilter</filter-class>
    <description>A servlet filter that supports Gzip, Deflate and ZIP compressed responses.</description>
  </filter>

  <filter-mapping>
    <filter-name>compressionFilter</filter-name>
    <servlet-name>myServlet</servlet-name>
  </filter-mapping>

  <filter-mapping>
    <filter-name>compressionFilter</filter-name>
    <url-pattern>/docs/*</url-pattern>
  </filter-mapping>
 

© Copyright 2003, Rakesh Vidyadharan

Version:
$Id: CompressionFilter.java,v 1.7 2005/07/19 17:36:37 rakesh Exp $
Author:
Rakesh Vidyadharan 2nd September 2003

Field Summary
static int DEFLATE_COMPRESSION
          A constant that is used to denote that deflate compression is to be used.
private  javax.servlet.FilterConfig filterConfig
          A reference to the Servlet container's Filter configuration.
static int GZIP_COMPRESSION
          A constant that is used to denote that gzip compression is to be used.
static int ZIP_COMPRESSION
          A constant that is used to denote that zip compression is to be used.
 
Constructor Summary
CompressionFilter()
           
 
Method Summary
 void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)
          Check the Accept-Encoding and accept-encoding HTTP headers, and if either of them specified the accepted compression methods (those supported by the java.util.zip classes), then the response is compressed using the desired compression algorithm.
 
Methods inherited from class org.rakeshv.filters.FilterAdapter
destroy, init, setFilterConfig
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFLATE_COMPRESSION

public static final int DEFLATE_COMPRESSION
A constant that is used to denote that deflate compression is to be used.

See Also:
Constant Field Values

ZIP_COMPRESSION

public static final int ZIP_COMPRESSION
A constant that is used to denote that zip compression is to be used.

See Also:
Constant Field Values

GZIP_COMPRESSION

public static final int GZIP_COMPRESSION
A constant that is used to denote that gzip compression is to be used.

See Also:
Constant Field Values

filterConfig

private javax.servlet.FilterConfig filterConfig
A reference to the Servlet container's Filter configuration.

Constructor Detail

CompressionFilter

public CompressionFilter()
Method Detail

doFilter

public void doFilter(javax.servlet.ServletRequest request,
                     javax.servlet.ServletResponse response,
                     javax.servlet.FilterChain chain)
              throws java.io.IOException,
                     javax.servlet.ServletException
Check the Accept-Encoding and accept-encoding HTTP headers, and if either of them specified the accepted compression methods (those supported by the java.util.zip classes), then the response is compressed using the desired compression algorithm. If multiple algorithms were specified, then one of the specified algorithms will be used. The exact algorithm that will be used is in the order specified:

  1. GZIP
  2. ZIP
  3. Deflate

If the special * value is used to specify the compression to use, Deflate compression will be used.

Note: This method does an implicit call to CompressionResponseWrapper.finish() if the response if being compressed. Other servlets down the chain do not need to worry about finishing the compression stream.

Specified by:
doFilter in interface javax.servlet.Filter
Overrides:
doFilter in class FilterAdapter
Throws:
java.io.IOException - - If exceptions are encountered while applying the filter.
javax.servlet.ServletException - - If exceptions are encountered while interacting with the request or response objects.

Rakesh API

Copyright © 2002-2005 - Rakesh Vidyadharan