001: /*
002: * Copyright 2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package compressionFilters;
018:
019: import java.io.IOException;
020: import java.util.Enumeration;
021: import javax.servlet.Filter;
022: import javax.servlet.FilterChain;
023: import javax.servlet.FilterConfig;
024: import javax.servlet.ServletException;
025: import javax.servlet.ServletRequest;
026: import javax.servlet.ServletResponse;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: /**
031: * Implementation of <code>javax.servlet.Filter</code> used to compress
032: * the ServletResponse if it is bigger than a threshold.
033: *
034: * @author Amy Roh
035: * @author Dmitri Valdin
036: * @version $Revision: 1.2 $, $Date: 2004/03/18 16:40:33 $
037: */
038:
039: public class CompressionFilter implements Filter {
040:
041: /**
042: * The filter configuration object we are associated with. If this value
043: * is null, this filter instance is not currently configured.
044: */
045: private FilterConfig config = null;
046:
047: /**
048: * Minimal reasonable threshold
049: */
050: private int minThreshold = 128;
051:
052: /**
053: * The threshold number to compress
054: */
055: protected int compressionThreshold;
056:
057: /**
058: * Debug level for this filter
059: */
060: private int debug = 0;
061:
062: /**
063: * Place this filter into service.
064: *
065: * @param filterConfig The filter configuration object
066: */
067:
068: public void init(FilterConfig filterConfig) {
069:
070: config = filterConfig;
071: if (filterConfig != null) {
072: String value = filterConfig.getInitParameter("debug");
073: if (value != null) {
074: debug = Integer.parseInt(value);
075: } else {
076: debug = 0;
077: }
078: String str = filterConfig
079: .getInitParameter("compressionThreshold");
080: if (str != null) {
081: compressionThreshold = Integer.parseInt(str);
082: if (compressionThreshold != 0
083: && compressionThreshold < minThreshold) {
084: if (debug > 0) {
085: System.out
086: .println("compressionThreshold should be either 0 - no compression or >= "
087: + minThreshold);
088: System.out
089: .println("compressionThreshold set to "
090: + minThreshold);
091: }
092: compressionThreshold = minThreshold;
093: }
094: } else {
095: compressionThreshold = 0;
096: }
097:
098: } else {
099: compressionThreshold = 0;
100: }
101:
102: }
103:
104: /**
105: * Take this filter out of service.
106: */
107: public void destroy() {
108:
109: this .config = null;
110:
111: }
112:
113: /**
114: * The <code>doFilter</code> method of the Filter is called by the container
115: * each time a request/response pair is passed through the chain due
116: * to a client request for a resource at the end of the chain.
117: * The FilterChain passed into this method allows the Filter to pass on the
118: * request and response to the next entity in the chain.<p>
119: * This method first examines the request to check whether the client support
120: * compression. <br>
121: * It simply just pass the request and response if there is no support for
122: * compression.<br>
123: * If the compression support is available, it creates a
124: * CompressionServletResponseWrapper object which compresses the content and
125: * modifies the header if the content length is big enough.
126: * It then invokes the next entity in the chain using the FilterChain object
127: * (<code>chain.doFilter()</code>), <br>
128: **/
129:
130: public void doFilter(ServletRequest request,
131: ServletResponse response, FilterChain chain)
132: throws IOException, ServletException {
133:
134: if (debug > 0) {
135: System.out.println("@doFilter");
136: }
137:
138: if (compressionThreshold == 0) {
139: if (debug > 0) {
140: System.out
141: .println("doFilter gets called, but compressionTreshold is set to 0 - no compression");
142: }
143: chain.doFilter(request, response);
144: return;
145: }
146:
147: boolean supportCompression = false;
148: if (request instanceof HttpServletRequest) {
149: if (debug > 1) {
150: System.out.println("requestURI = "
151: + ((HttpServletRequest) request)
152: .getRequestURI());
153: }
154:
155: // Are we allowed to compress ?
156: String s = (String) ((HttpServletRequest) request)
157: .getParameter("gzip");
158: if ("false".equals(s)) {
159: if (debug > 0) {
160: System.out
161: .println("got parameter gzip=false --> don't compress, just chain filter");
162: }
163: chain.doFilter(request, response);
164: return;
165: }
166:
167: Enumeration e = ((HttpServletRequest) request)
168: .getHeaders("Accept-Encoding");
169: while (e.hasMoreElements()) {
170: String name = (String) e.nextElement();
171: if (name.indexOf("gzip") != -1) {
172: if (debug > 0) {
173: System.out.println("supports compression");
174: }
175: supportCompression = true;
176: } else {
177: if (debug > 0) {
178: System.out.println("no support for compresion");
179: }
180: }
181: }
182: }
183:
184: if (!supportCompression) {
185: if (debug > 0) {
186: System.out
187: .println("doFilter gets called wo compression");
188: }
189: chain.doFilter(request, response);
190: return;
191: } else {
192: if (response instanceof HttpServletResponse) {
193: CompressionServletResponseWrapper wrappedResponse = new CompressionServletResponseWrapper(
194: (HttpServletResponse) response);
195: wrappedResponse.setDebugLevel(debug);
196: wrappedResponse
197: .setCompressionThreshold(compressionThreshold);
198: if (debug > 0) {
199: System.out
200: .println("doFilter gets called with compression");
201: }
202: try {
203: chain.doFilter(request, wrappedResponse);
204: } finally {
205: wrappedResponse.finishResponse();
206: }
207: return;
208: }
209: }
210: }
211:
212: /**
213: * Set filter config
214: * This function is equivalent to init. Required by Weblogic 6.1
215: *
216: * @param filterConfig The filter configuration object
217: */
218: public void setFilterConfig(FilterConfig filterConfig) {
219: init(filterConfig);
220: }
221:
222: /**
223: * Return filter config
224: * Required by Weblogic 6.1
225: */
226: public FilterConfig getFilterConfig() {
227: return config;
228: }
229:
230: }
|