com.planetj.servlet.filter.compression
Class CompressingFilter

java.lang.Object
  extended by com.planetj.servlet.filter.compression.CompressingFilter
All Implemented Interfaces:
javax.servlet.Filter

public final class CompressingFilter
extends java.lang.Object
implements javax.servlet.Filter

This filter can, based on HTTP headers in a HttpServletRequest, compress data written to the HttpServletResponse, or decompress data read from the request. When supported by the client browser, this can potentially greatly reduce the number of bytes written across the network from and to the client. As a Filter, this class can also be easily added to any J2EE 1.3+ web application.

Installation

  1. Add the .jar file containing CompressingFilter to your web application's WEB-INF/lib directory.
  2. Add the following entries to your web.xml deployment descriptor:
     <filter>
      <filter-name>CompressingFilter</filter-name>
      <filter-class>com.planetj.servlet.filter.compression.CompressingFilter</filter-class>
     </filter>
     ...
     <filter-mapping>
      <filter-name>CompressingFilter</filter-name>
      <url-pattern>/*</url-pattern>
     </filter-mapping>
     

Configuration

CompressingFilter supports the following parameters:

These values are configured in web.xml as well with init-param elements:

 <filter>
  <filter-name>CompressingFilter</filter-name>
  <filter-class>com.planetj.servlet.filter.compression.CompressingFilter</filter-class>
  <init-param>
   <param-name>debug</param-name>
   <param-value>true</param-value>
  </init-param>
 </filter>
 

Supported compression algorithms

Response

This filter supports the following compression algorithms when compressing data to the repsonse, as specified in the "Accept-Encoding" HTTP request header:

Request

This filter supports the following compression algorithms when decompressing data from the request body, as specified in the "Content-Encoding" HTTP request header:

Controlling runtime behavior

An application may force the encoding / compression used by setting an "Accept-Encoding" value into the request as an attribute under the key FORCE_ENCODING_KEY. Obviously this has to be set upstream from the filter, not downstream.

Caveats

Note that if this filter decides that it should try to compress the response, it will close the response (whether or not it ends up compressing the response). No more can be written to the response after this filter has been applied; this should never be necessary anyway. Put this filter ahead of any filters that might try to write to the repsonse, since presumably you want this content compressed too anyway.

Since:
1.0
Author:
Sean Owen

Field Summary
private static java.lang.String ALREADY_APPLIED_KEY
           
static java.lang.String COMPRESSED_KEY
          A request attribute is set under this key with a non-null value if this filter has applied compression to the response.
private  CompressingFilterContext context
           
static java.lang.String FORCE_ENCODING_KEY
          One may force the filter to use a particular encoding by setting its value as an attribute of the ServletRequest passed to this filter, under this key.
private  CompressingFilterLoggerImpl logger
           
(package private) static java.lang.String VERSION
           
(package private) static java.lang.String VERSION_STRING
           
 
Constructor Summary
CompressingFilter()
           
 
Method Summary
 void destroy()
           
 void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)
           
private  javax.servlet.ServletRequest getRequest(javax.servlet.ServletRequest request)
           
private  javax.servlet.ServletResponse getResponse(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response)
           
 void init(javax.servlet.FilterConfig config)
           
private  boolean isCompressablePath(java.lang.String path)
          Checks to see if the given path should be compressed.
private  boolean isCompressableUserAgent(java.lang.String userAgent)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ALREADY_APPLIED_KEY

private static final java.lang.String ALREADY_APPLIED_KEY
See Also:
Constant Field Values

FORCE_ENCODING_KEY

public static final java.lang.String FORCE_ENCODING_KEY
One may force the filter to use a particular encoding by setting its value as an attribute of the ServletRequest passed to this filter, under this key. The value should be a valid "Accept-Encoding" header value, like "gzip". Specify "identity" to force no compression.

Since:
1.2
See Also:
Constant Field Values

COMPRESSED_KEY

public static final java.lang.String COMPRESSED_KEY
A request attribute is set under this key with a non-null value if this filter has applied compression to the response. Upstream filters may check for this flag. Note that if the response has been compressed, then it will be closed by the time this filter finishes as well.

Since:
1.2
See Also:
Constant Field Values

VERSION

static final java.lang.String VERSION
See Also:
Constant Field Values

VERSION_STRING

static final java.lang.String VERSION_STRING

context

private CompressingFilterContext context

logger

private CompressingFilterLoggerImpl logger
Constructor Detail

CompressingFilter

public CompressingFilter()
Method Detail

init

public void init(javax.servlet.FilterConfig config)
          throws javax.servlet.ServletException
Specified by:
init in interface javax.servlet.Filter
Throws:
javax.servlet.ServletException

doFilter

public void doFilter(javax.servlet.ServletRequest request,
                     javax.servlet.ServletResponse response,
                     javax.servlet.FilterChain chain)
              throws java.io.IOException,
                     javax.servlet.ServletException
Specified by:
doFilter in interface javax.servlet.Filter
Throws:
java.io.IOException
javax.servlet.ServletException

getRequest

private javax.servlet.ServletRequest getRequest(javax.servlet.ServletRequest request)

getResponse

private javax.servlet.ServletResponse getResponse(javax.servlet.ServletRequest request,
                                                  javax.servlet.ServletResponse response)

destroy

public void destroy()
Specified by:
destroy in interface javax.servlet.Filter

isCompressablePath

private boolean isCompressablePath(java.lang.String path)
Checks to see if the given path should be compressed. This checks against the includePathPatterns and excludePathPatterns filter init parameters; if the former is set and the given path matches a regular expression in that parameter's list, or if the latter is set and the path does not match, then this method returns true.

Parameters:
path - request path
Returns:
true if and only if the path should be compressed

isCompressableUserAgent

private boolean isCompressableUserAgent(java.lang.String userAgent)

toString

@NotNull
public java.lang.String toString()
Overrides:
toString in class java.lang.Object