KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > compressionFilters > CompressionFilter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  *
23  * Portions Copyright Apache Software Foundation.
24  */

25
26
27 package compressionFilters;
28
29 import java.io.IOException JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import javax.servlet.Filter JavaDoc;
32 import javax.servlet.FilterChain JavaDoc;
33 import javax.servlet.FilterConfig JavaDoc;
34 import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.ServletRequest JavaDoc;
36 import javax.servlet.ServletResponse JavaDoc;
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
39
40
41 /**
42  * Implementation of <code>javax.servlet.Filter</code> used to compress
43  * the ServletResponse if it is bigger than a threshold.
44  *
45  * @author Amy Roh
46  * @author Dmitri Valdin
47  * @version $Revision: 1.2 $, $Date: 2005/12/08 01:13:46 $
48  */

49
50 public class CompressionFilter implements Filter JavaDoc{
51
52     /**
53      * The filter configuration object we are associated with. If this value
54      * is null, this filter instance is not currently configured.
55      */

56     private FilterConfig JavaDoc config = null;
57
58     /**
59      * Minimal reasonable threshold
60      */

61     private int minThreshold = 128;
62
63
64     /**
65      * The threshold number to compress
66      */

67     protected int compressionThreshold;
68
69     /**
70      * Debug level for this filter
71      */

72     private int debug = 0;
73
74     /**
75      * Place this filter into service.
76      *
77      * @param filterConfig The filter configuration object
78      */

79
80     public void init(FilterConfig JavaDoc filterConfig) {
81
82         config = filterConfig;
83         if (filterConfig != null) {
84             String JavaDoc value = filterConfig.getInitParameter("debug");
85             if (value!=null) {
86                 debug = Integer.parseInt(value);
87             } else {
88                 debug = 0;
89             }
90             String JavaDoc str = filterConfig.getInitParameter("compressionThreshold");
91             if (str!=null) {
92                 compressionThreshold = Integer.parseInt(str);
93                 if (compressionThreshold != 0 && compressionThreshold < minThreshold) {
94                     if (debug > 0) {
95                         System.out.println("compressionThreshold should be either 0 - no compression or >= " + minThreshold);
96                         System.out.println("compressionThreshold set to " + minThreshold);
97                     }
98                     compressionThreshold = minThreshold;
99                 }
100             } else {
101                 compressionThreshold = 0;
102             }
103
104         } else {
105             compressionThreshold = 0;
106         }
107
108     }
109
110     /**
111     * Take this filter out of service.
112     */

113     public void destroy() {
114
115         this.config = null;
116
117     }
118
119     /**
120      * The <code>doFilter</code> method of the Filter is called by the container
121      * each time a request/response pair is passed through the chain due
122      * to a client request for a resource at the end of the chain.
123      * The FilterChain passed into this method allows the Filter to pass on the
124      * request and response to the next entity in the chain.<p>
125      * This method first examines the request to check whether the client support
126      * compression. <br>
127      * It simply just pass the request and response if there is no support for
128      * compression.<br>
129      * If the compression support is available, it creates a
130      * CompressionServletResponseWrapper object which compresses the content and
131      * modifies the header if the content length is big enough.
132      * It then invokes the next entity in the chain using the FilterChain object
133      * (<code>chain.doFilter()</code>), <br>
134      **/

135
136     public void doFilter ( ServletRequest JavaDoc request, ServletResponse JavaDoc response,
137                         FilterChain JavaDoc chain ) throws IOException JavaDoc, ServletException JavaDoc {
138
139         if (debug > 0) {
140             System.out.println("@doFilter");
141         }
142
143         if (compressionThreshold == 0) {
144             if (debug > 0) {
145                 System.out.println("doFilter gets called, but compressionTreshold is set to 0 - no compression");
146             }
147             chain.doFilter(request, response);
148             return;
149         }
150
151         boolean supportCompression = false;
152         if (request instanceof HttpServletRequest JavaDoc) {
153             if (debug > 1) {
154                 System.out.println("requestURI = " + ((HttpServletRequest JavaDoc)request).getRequestURI());
155             }
156
157             // Are we allowed to compress ?
158
String JavaDoc s = (String JavaDoc) ((HttpServletRequest JavaDoc)request).getParameter("gzip");
159             if ("false".equals(s)) {
160                 if (debug > 0) {
161                     System.out.println("got parameter gzip=false --> don't compress, just chain filter");
162                 }
163                 chain.doFilter(request, response);
164                 return;
165             }
166
167             Enumeration JavaDoc e =
168                 ((HttpServletRequest JavaDoc)request).getHeaders("Accept-Encoding");
169             while (e.hasMoreElements()) {
170                 String JavaDoc name = (String JavaDoc)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.println("doFilter gets called wo compression");
187             }
188             chain.doFilter(request, response);
189             return;
190         } else {
191             if (response instanceof HttpServletResponse JavaDoc) {
192                 CompressionServletResponseWrapper wrappedResponse =
193                     new CompressionServletResponseWrapper((HttpServletResponse JavaDoc)response);
194                 wrappedResponse.setDebugLevel(debug);
195                 wrappedResponse.setCompressionThreshold(compressionThreshold);
196                 if (debug > 0) {
197                     System.out.println("doFilter gets called with compression");
198                 }
199                 try {
200                     chain.doFilter(request, wrappedResponse);
201                 } finally {
202                     wrappedResponse.finishResponse();
203                 }
204                 return;
205             }
206         }
207     }
208
209     /**
210      * Set filter config
211      * This function is equivalent to init. Required by Weblogic 6.1
212      *
213      * @param filterConfig The filter configuration object
214      */

215     public void setFilterConfig(FilterConfig JavaDoc filterConfig) {
216         init(filterConfig);
217     }
218
219     /**
220      * Return filter config
221      * Required by Weblogic 6.1
222      */

223     public FilterConfig JavaDoc getFilterConfig() {
224         return config;
225     }
226
227 }
228
229
Popular Tags