javax.servlet.Filter
used to compress
* the ServletResponse if it is bigger than a threshold.
*
* @author Amy Roh
* @author Dmitri Valdin
*/
public class CompressionFilter implements Filter {
/**
* Minimal reasonable threshold.
*/
private static final int MIN_THRESHOLD = 128;
/**
* Minimal reasonable buffer.
*/
// 8 KiB is what tomcat would use by default anyway
private static final int MIN_BUFFER = 8192;
/**
* The threshold number to compress.
*/
protected int compressionThreshold = 0;
/**
* The compression buffer size to avoid chunking.
*/
protected int compressionBuffer = 0;
/**
* The mime types to compress.
*/
protected String[] compressionMimeTypes = {"text/html", "text/xml", "text/plain"};
/**
* Debug level for this filter.
*/
private int debug = 0;
/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
@Override
public void init(FilterConfig filterConfig) {
if (filterConfig != null) {
String value = filterConfig.getInitParameter("debug");
if (value!=null) {
debug = Integer.parseInt(value);
}
String str = filterConfig.getInitParameter("compressionThreshold");
if (str != null) {
compressionThreshold = Integer.parseInt(str);
if (compressionThreshold != 0 && compressionThreshold < MIN_THRESHOLD) {
if (debug > 0) {
System.out.println("compressionThreshold should be either 0 - no compression or >= " + MIN_THRESHOLD);
System.out.println("compressionThreshold set to " + MIN_THRESHOLD);
}
compressionThreshold = MIN_THRESHOLD;
}
}
str = filterConfig.getInitParameter("compressionBuffer");
if (str != null) {
compressionBuffer = Integer.parseInt(str);
if (compressionBuffer < MIN_BUFFER) {
if (debug > 0) {
System.out.println("compressionBuffer should be >= " + MIN_BUFFER);
System.out.println("compressionBuffer set to " + MIN_BUFFER);
}
compressionBuffer = MIN_BUFFER;
}
}
str = filterConfig.getInitParameter("compressionMimeTypes");
if (str != null) {
ListdoFilter
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()
),
**/
@Override
public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain )
throws IOException, ServletException {
if (debug > 0) {
System.out.println("@doFilter");
}
if (compressionThreshold == 0) {
if (debug > 0) {
System.out.println("doFilter got called, but compressionThreshold is set to 0 - no compression");
}
chain.doFilter(request, response);
return;
}
boolean supportCompression = false;
if (request instanceof HttpServletRequest) {
if (debug > 1) {
System.out.println("requestURI = " + ((HttpServletRequest)request).getRequestURI());
}
// Are we allowed to compress ?
String s = ((HttpServletRequest)request).getParameter("gzip");
if ("false".equals(s)) {
if (debug > 0) {
System.out.println("got parameter gzip=false --> don't compress, just chain filter");
}
chain.doFilter(request, response);
return;
}
Enumeration