KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > web > util > compression > CompressionFilter


1 package com.genimen.djeneric.web.util.compression;
2
3 /*
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20 import java.util.Enumeration JavaDoc;
21
22 import javax.servlet.Filter JavaDoc;
23 import javax.servlet.FilterChain JavaDoc;
24 import javax.servlet.FilterConfig JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.ServletRequest JavaDoc;
27 import javax.servlet.ServletResponse JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30
31 /**
32  * Implementation of <code>javax.servlet.Filter</code> used to compress
33  * the ServletResponse if it is bigger than a threshold.
34  *
35  * @author Amy Roh
36  * @author Dmitri Valdin
37  * @version $Revision: 1.11 $, $Date: 2005/08/03 22:12:43 $
38  */

39
40 public class CompressionFilter implements Filter JavaDoc
41 {
42
43   /**
44    * The filter configuration object we are associated with. If this value
45    * is null, this filter instance is not currently configured.
46    */

47   private FilterConfig JavaDoc config = null;
48
49   /**
50    * Minimal reasonable threshold
51    */

52   private int minThreshold = 128;
53
54   /**
55    * The threshold number to compress
56    */

57   protected int compressionThreshold;
58
59   /**
60    * Debug level for this filter
61    */

62   private int debug = 0;
63
64   /**
65    * Place this filter into service.
66    *
67    * @param filterConfig The filter configuration object
68    */

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

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

138
139   public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain) throws IOException JavaDoc,
140       ServletException JavaDoc
141   {
142
143     if (debug > 0)
144     {
145       System.out.println("@doFilter");
146     }
147
148     if (compressionThreshold == 0)
149     {
150       if (debug > 0)
151       {
152         System.out.println("doFilter gets called, but compressionTreshold is set to 0 - no compression");
153       }
154       chain.doFilter(request, response);
155       return;
156     }
157
158     boolean supportCompression = false;
159     if (request instanceof HttpServletRequest JavaDoc)
160     {
161       if (debug > 1)
162       {
163         System.out.println("requestURI = " + ((HttpServletRequest JavaDoc) request).getRequestURI());
164       }
165
166       // Are we allowed to compress ?
167
String JavaDoc s = (String JavaDoc) ((HttpServletRequest JavaDoc) request).getParameter("gzip");
168       if ("false".equals(s))
169       {
170         if (debug > 0)
171         {
172           System.out.println("got parameter gzip=false --> don't compress, just chain filter");
173         }
174         chain.doFilter(request, response);
175         return;
176       }
177
178       Enumeration JavaDoc e = ((HttpServletRequest JavaDoc) request).getHeaders("Accept-Encoding");
179       while (e.hasMoreElements())
180       {
181         String JavaDoc name = (String JavaDoc) e.nextElement();
182         if (name.indexOf("gzip") != -1)
183         {
184           if (debug > 0)
185           {
186             System.out.println("supports compression");
187           }
188           supportCompression = true;
189         }
190         else
191         {
192           if (debug > 0)
193           {
194             System.out.println("no support for compresion");
195           }
196         }
197       }
198     }
199
200     if (!supportCompression)
201     {
202       if (debug > 0)
203       {
204         System.out.println("doFilter gets called wo compression");
205       }
206       chain.doFilter(request, response);
207       return;
208     }
209     else
210     {
211       if (response instanceof HttpServletResponse JavaDoc)
212       {
213         CompressionServletResponseWrapper wrappedResponse = new CompressionServletResponseWrapper(
214             (HttpServletResponse JavaDoc) response);
215         wrappedResponse.setDebugLevel(debug);
216         wrappedResponse.setCompressionThreshold(compressionThreshold);
217         if (debug > 0)
218         {
219           System.out.println("doFilter gets called with compression");
220         }
221         try
222         {
223           chain.doFilter(request, wrappedResponse);
224         }
225         finally
226         {
227           wrappedResponse.finishResponse();
228         }
229         return;
230       }
231     }
232   }
233
234   /**
235    * Set filter config
236    * This function is equivalent to init. Required by Weblogic 6.1
237    *
238    * @param filterConfig The filter configuration object
239    */

240   public void setFilterConfig(FilterConfig JavaDoc filterConfig)
241   {
242     init(filterConfig);
243   }
244
245   /**
246    * Return filter config
247    * Required by Weblogic 6.1
248    */

249   public FilterConfig JavaDoc getFilterConfig()
250   {
251     return config;
252   }
253
254 }
255
Popular Tags