net.sf.click.extras.filter
Class CompressionFilter

java.lang.Object
  extended bynet.sf.click.extras.filter.CompressionFilter
All Implemented Interfaces:
Filter

public class CompressionFilter
extends Object
implements Filter

Provides a GZIP compression Filter to compress HTML ServletResponse content. The content will only be compressed if it is bigger than a configurable threshold. The default threshold is 2048 bytes.

To configure your application to GZIP compress HTML content include the click-extras.jar in you application and add the following filter elements to your /WEB-INF/web.xml file:

 <filter>
  <filter-name>compression-filter</filter-name>
  <filter-class>net.sf.click.extras.filter.CompressionFilter</filter-class>
 </filter>

 <filter-mapping>
  <filter-name>compression-filter</filter-name>
  <servlet-name>click-servlet</servlet-name>
 </filter-mapping>

 <servlet>
  <servlet-name>click-servlet</servlet-name>
 .. 
This filter will automaitically set the configured click.xml charset as the requests character encoding.

This package is derived from the Jakarta Tomcat examples compression filter and is distributed in Click Extras for convenience.

Author:
Amy Roh, Dmitri Valdin, Malcolm Edgar

Field Summary
protected  int compressionThreshold
          The threshold number to compress, default value is 2048 bytes.
protected  ConfigService configService
          The application configuration service.
protected  boolean configured
          The fitler has been configured flag.
protected  int minThreshold
          Minimal reasonable threshold, 2048 bytes.
 
Constructor Summary
CompressionFilter()
           
 
Method Summary
 void destroy()
          Take this filter out of service.
 void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
          The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain.
protected  ConfigService getConfigService()
          Return the application configuration service.
 FilterConfig getFilterConfig()
          Return filter config.
 void init(FilterConfig filterConfig)
          Place this filter into service.
protected  void loadConfiguration()
          Load the filters configuration and set the configured flat to true.
 void setFilterConfig(FilterConfig filterConfig)
          Set filter configuration.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

minThreshold

protected int minThreshold
Minimal reasonable threshold, 2048 bytes.


compressionThreshold

protected int compressionThreshold
The threshold number to compress, default value is 2048 bytes.


configured

protected boolean configured
The fitler has been configured flag.


configService

protected ConfigService configService
The application configuration service.

Constructor Detail

CompressionFilter

public CompressionFilter()
Method Detail

init

public void init(FilterConfig filterConfig)
Place this filter into service.

Specified by:
init in interface Filter
Parameters:
filterConfig - The filter configuration object

destroy

public void destroy()
Take this filter out of service.

Specified by:
destroy in interface Filter

doFilter

public void doFilter(ServletRequest request,
                     ServletResponse response,
                     FilterChain chain)
              throws IOException,
                     ServletException
The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. The FilterChain passed into this method allows the Filter to pass on the request and response to the next entity in the chain.

This method first examines the request to check whether the client support compression.
It simply just pass the request and response if there is no support for compression.
If the compression support is available, it creates a CompressionServletResponseWrapper object which compresses the content and modifies the header if the content length is big enough. It then invokes the next entity in the chain using the FilterChain object (chain.doFilter())

Specified by:
doFilter in interface Filter
Parameters:
request - the servlet request
response - the servlet response
chain - the filter chain
Throws:
IOException - if an I/O error occurs
ServletException - if a servlet error occurs

setFilterConfig

public void setFilterConfig(FilterConfig filterConfig)
Set filter configuration. This function is equivalent to init and is required by Weblogic 6.1.

Parameters:
filterConfig - the filter configuration object

getFilterConfig

public FilterConfig getFilterConfig()
Return filter config. This is required by Weblogic 6.1

Returns:
the filter configuration

getConfigService

protected ConfigService getConfigService()
Return the application configuration service.

Returns:
the application configuration service

loadConfiguration

protected void loadConfiguration()
Load the filters configuration and set the configured flat to true.