KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > filters > gzip > CompressionFilter


1 /*
2  * $$Id: CompressionFilter.java,v 1.3 2005/06/07 12:31:56 bel70 Exp $$
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitry Belov <bel@jresearch.org>,
23  * Jayson Falkner <jayson@jspinsider.com>
24  *
25  * ***** END LICENSE BLOCK ***** */

26 package org.jresearch.gossip.filters.gzip;
27
28 import java.io.IOException JavaDoc;
29
30 import javax.servlet.Filter JavaDoc;
31 import javax.servlet.FilterChain JavaDoc;
32 import javax.servlet.FilterConfig JavaDoc;
33 import javax.servlet.ServletException JavaDoc;
34 import javax.servlet.ServletRequest JavaDoc;
35 import javax.servlet.ServletResponse JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38
39 import org.apache.log.Logger;
40 import org.jresearch.gossip.IConst;
41 import org.jresearch.gossip.configuration.Configurator;
42 import org.jresearch.gossip.exception.ConfiguratorException;
43 import org.jresearch.gossip.exception.SystemException;
44 import org.jresearch.gossip.log.avalon.JGossipLog;
45
46 /**
47  * Filter that compresses output with gzip (assuming that browser supports
48  * gzip).
49  */

50 public class CompressionFilter implements Filter JavaDoc {
51
52     public static final String JavaDoc GZIP_NOT_ALLOWED = "GZIP_NOT_ALLOWED";
53
54     private FilterConfig JavaDoc config;
55
56     /**
57      * If browser does not support gzip, invoke resource normally. If browser
58      * <I>does </I> support gzip, set the Content-Encoding response header and
59      * invoke resource with a wrapped response that collects all the output.
60      * Extract the output and write it into a gzipped byte array. Finally, write
61      * that array to the client's output stream.
62      *
63      * @param request
64      * DOCUMENT ME!
65      * @param response
66      * DOCUMENT ME!
67      * @param chain
68      * DOCUMENT ME!
69      *
70      * @throws ServletException
71      * DOCUMENT ME!
72      * @throws IOException
73      * DOCUMENT ME!
74      */

75     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response,
76             FilterChain JavaDoc chain) throws ServletException JavaDoc, IOException JavaDoc {
77         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) request;
78         HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc) response;
79
80         try {
81             if ((!isGzipSupported(req))
82                     || (IConst.VALUES.FALSE.equals(Configurator.getInstance()
83                             .get(IConst.CONFIG.GZIP_COMPRESS)))
84                     || (req.getParameter(GZIP_NOT_ALLOWED) != null)) {
85                 try {
86                     Logger log = JGossipLog.getInstance().getAppLogger();
87                     if (log.isDebugEnabled()) {
88                         log.debug("Invoke resource normally.");
89                     }
90                 } catch (SystemException e1) { /* Ignore this exception. */
91                 }
92                 chain.doFilter(req, res);
93             } else {
94                 GZIPResponseWrapper wrappedResponse = new GZIPResponseWrapper(
95                         res);
96                 chain.doFilter(req, wrappedResponse);
97                 wrappedResponse.finishResponse();
98             }
99         } catch (ConfiguratorException e) {
100             throw new ServletException JavaDoc(e);
101         }
102     }
103
104     /**
105      * Store the FilterConfig object in case subclasses want it.
106      *
107      * @param config
108      * DOCUMENT ME!
109      *
110      * @throws ServletException
111      * DOCUMENT ME!
112      */

113     public void init(FilterConfig JavaDoc config) throws ServletException JavaDoc {
114         this.config = config;
115     }
116
117     /**
118      * DOCUMENT ME!
119      *
120      * @return DOCUMENT ME!
121      */

122     protected FilterConfig JavaDoc getFilterConfig() {
123         return (config);
124     }
125
126     /**
127      * DOCUMENT ME!
128      */

129     public void destroy() {
130     }
131
132     private boolean isGzipSupported(HttpServletRequest JavaDoc req) {
133         String JavaDoc browserEncodings = req.getHeader("Accept-Encoding");
134
135         return ((browserEncodings != null) && (browserEncodings.indexOf("gzip") != -1));
136     }
137 }
Popular Tags