|
Tomcat example source code file (CompressionFilter.java)
The Tomcat CompressionFilter.java source code/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package compressionFilters; import java.io.IOException; import java.util.Enumeration; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Implementation of <code>javax.servlet.Filter used to compress * the ServletResponse if it is bigger than a threshold. * * @author Amy Roh * @author Dmitri Valdin * @version $Revision: 467217 $, $Date: 2006-10-24 05:14:34 +0200 (mar., 24 oct. 2006) $ */ public class CompressionFilter implements Filter{ /** * The filter configuration object we are associated with. If this value * is null, this filter instance is not currently configured. */ private FilterConfig config = null; /** * Minimal reasonable threshold */ private int minThreshold = 128; /** * The threshold number to compress */ protected int compressionThreshold; /** * Debug level for this filter */ private int debug = 0; /** * Place this filter into service. * * @param filterConfig The filter configuration object */ public void init(FilterConfig filterConfig) { config = filterConfig; if (filterConfig != null) { String value = filterConfig.getInitParameter("debug"); if (value!=null) { debug = Integer.parseInt(value); } else { debug = 0; } String str = filterConfig.getInitParameter("compressionThreshold"); if (str!=null) { compressionThreshold = Integer.parseInt(str); if (compressionThreshold != 0 && compressionThreshold < minThreshold) { if (debug > 0) { System.out.println("compressionThreshold should be either 0 - no compression or >= " + minThreshold); System.out.println("compressionThreshold set to " + minThreshold); } compressionThreshold = minThreshold; } } else { compressionThreshold = 0; } } else { compressionThreshold = 0; } } /** * Take this filter out of service. */ public void destroy() { this.config = null; } /** * The <code>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.<p> * This method first examines the request to check whether the client support * compression. <br> * It simply just pass the request and response if there is no support for * compression.<br> * 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 * (<code>chain.doFilter()), Other Tomcat examples (source code examples)Here is a short list of links related to this Tomcat CompressionFilter.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.