View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   //CabaWeb
3   //Copyright (C) 2004  Thomas Vogt <Thomas.Vogt@TVC-Software.com>
4   //
5   //This library is free software; you can redistribute it and/or
6   //modify it under the terms of the GNU Lesser General Public
7   //License as published by the Free Software Foundation; either
8   //version 2.1 of the License, or (at your option) any later version.
9   //
10  //This library is distributed in the hope that it will be useful,
11  //but WITHOUT ANY WARRANTY; without even the implied warranty of
12  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  //Lesser General Public License for more details.
14  //
15  //You should have received a copy of the GNU Lesser General Public
16  //License along with this library; if not, write to the Free Software
17  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  package org.fhw.cabaweb.webfrontend.filters;
20  
21  import java.io.IOException;
22  import java.io.OutputStreamWriter;
23  import java.io.PrintWriter;
24  
25  import javax.servlet.ServletOutputStream;
26  import javax.servlet.http.HttpServletResponse;
27  import javax.servlet.http.HttpServletResponseWrapper;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /***
33   * <strong>Filter</strong>-Klasse die im Falle das der Client das unterst&uuml;tzt,
34   * den Stream GZIP komprimiert. Antwort Wrapper Klasse.
35   *
36   * ORIGINAL aus dem Buch Servlets and JavaServer Pages
37   * von Jayson Falkner, Kevin Jones (http://www.jspbook.com/)
38   *
39   * @author Jayson Falkner, Kevin Jones
40   *
41   * @version Version 1.0 24.07.2004
42   */
43  public class GZIPResponseWrapper extends HttpServletResponseWrapper
44  {
45      /***
46       * The <code>Log</code> instance for this application.
47       */
48      private Log log = LogFactory.getLog("org.fhw.cabaweb.webfrontend.filters");
49  
50      protected HttpServletResponse origResponse = null;
51      protected ServletOutputStream stream = null;
52      protected PrintWriter writer = null;
53  
54      public GZIPResponseWrapper(HttpServletResponse response)
55      {
56          super(response);
57          origResponse = response;
58      }
59  
60      public ServletOutputStream createOutputStream() throws IOException
61      {
62          return (new GZIPResponseStream(origResponse));
63      }
64  
65      public void finishResponse()
66      {
67          try
68          {
69              if (writer != null)
70              {
71                  writer.close();
72              }
73              else
74              {
75                  if (stream != null)
76                  {
77                      stream.close();
78                  }
79              }
80          }
81          catch (IOException e)
82          {
83              log.error(" GZIP IOException!");
84              log.error(e.getStackTrace());
85          }
86      }
87  
88      public void flushBuffer() throws IOException
89      {
90          stream.flush();
91      }
92  
93      public ServletOutputStream getOutputStream() throws IOException
94      {
95          if (writer != null)
96          {
97              log.error(" GZIP getWriter() has already been called!");
98              throw new IllegalStateException("getWriter() has already been called!");
99          }
100 
101         if (stream == null)
102             stream = createOutputStream();
103 
104         return (stream);
105     }
106 
107     public PrintWriter getWriter() throws IOException
108     {
109         if (writer != null)
110         {
111             return (writer);
112         }
113 
114         if (stream != null)
115         {
116             log.error(" GZIP getOutputStream() has already been called!");
117             throw new IllegalStateException("getOutputStream() has already been called!");
118         }
119 
120         stream = createOutputStream();
121         // BUG FIX 2003-12-01 Reuse content's encoding, don't assume UTF-8
122         writer = new PrintWriter(new OutputStreamWriter(stream, origResponse.getCharacterEncoding()));
123 
124         return (writer);
125     }
126 
127     public void setContentLength(int length)
128     {
129     }
130 }