KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > filters > CompressionFilter


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18
19 package org.apache.roller.ui.core.filters;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.zip.GZIPOutputStream JavaDoc;
24 import javax.servlet.Filter JavaDoc;
25 import javax.servlet.FilterChain JavaDoc;
26 import javax.servlet.FilterConfig JavaDoc;
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.ServletRequest JavaDoc;
29 import javax.servlet.ServletResponse JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.roller.config.RollerConfig;
35 import org.apache.roller.ui.core.util.ByteArrayOutputStreamWrapper;
36 import org.apache.roller.ui.core.util.ByteArrayResponseWrapper;
37
38
39 /**
40  * Filter that compresses output with gzip (assuming that browser supports gzip).
41  * <P>
42  * Taken from More Servlets and JavaServer Pages from Prentice Hall and
43  * Sun Microsystems Press, http://www.moreservlets.com/.
44  * &copy; 2002 Marty Hall; may be freely used or adapted.
45  *
46  * @web.filter name="CompressionFilter"
47  */

48
49 public class CompressionFilter implements Filter JavaDoc {
50     
51     private static Log mLogger = LogFactory.getLog(CompressionFilter.class);
52     
53     private boolean enabled = true;
54     
55     
56     /**
57      * If browser does not support gzip, invoke resource normally. If browser
58      * does support gzip, set the Content-Encoding response header and invoke
59      * resource with a wrapped response that collects all the output. Extract
60      * the output and write it into a gzipped byte array. Finally, write that
61      * array to the client's output stream.
62      */

63     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain)
64             throws ServletException JavaDoc, IOException JavaDoc {
65         
66         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) request;
67         HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc) response;
68         
69         if (!this.enabled || !isGzipSupported(req)) {
70             // Invoke resource normally.
71
chain.doFilter(req, res);
72         } else {
73             // Tell browser we are sending it gzipped data.
74
res.setHeader("Content-Encoding", "gzip");
75             
76             // Invoke resource, accumulating output in the wrapper.
77
ByteArrayResponseWrapper responseWrapper =
78                     new ByteArrayResponseWrapper(response);
79             
80             chain.doFilter(req, responseWrapper);
81             
82             ByteArrayOutputStream JavaDoc outputStream = responseWrapper.getByteArrayOutputStream();
83             
84             // Get character array representing output.
85
mLogger.debug("Pre-zip size:" + outputStream.size());
86             
87             // Make a writer that compresses data and puts
88
// it into a byte array.
89
ByteArrayOutputStream JavaDoc byteStream = new ByteArrayOutputStream JavaDoc();
90             GZIPOutputStream JavaDoc zipOut = new GZIPOutputStream JavaDoc(byteStream);
91             
92             // Compress original output and put it into byte array.
93
zipOut.write(responseWrapper.getByteArrayOutputStream().toByteArray());
94             
95             // Gzip streams must be explicitly closed.
96
zipOut.close();
97             
98             mLogger.debug("Gzip size:" + byteStream.size());
99             
100             // Update the Content-Length header.
101
res.setContentLength(byteStream.size());
102             
103             ByteArrayOutputStreamWrapper newOut =
104                     (ByteArrayOutputStreamWrapper) responseWrapper.getOutputStream();
105             newOut.clear();
106             newOut.setFinallized();
107             
108             /* now force close of OutputStream */
109             newOut.write(byteStream.toByteArray());
110             newOut.close();
111         }
112         
113     }
114     
115
116     public void init(FilterConfig JavaDoc config) throws ServletException JavaDoc {
117         
118         // is compression enabled?
119
if(RollerConfig.getBooleanProperty("compression.gzipResponse.enabled")) {
120             this.enabled = true;
121             mLogger.info("Compressed Output ENABLED");
122         } else {
123             this.enabled = false;
124             mLogger.info("Compressed Output DISABLED");
125         }
126     }
127     
128     
129     public void destroy() {}
130     
131     
132     private boolean isGzipSupported(HttpServletRequest JavaDoc req) {
133         String JavaDoc browserEncodings = req.getHeader("Accept-Encoding");
134         return ((browserEncodings != null)
135                     && (browserEncodings.indexOf("gzip") != -1));
136     }
137     
138 }
139
Popular Tags