View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jumpmind.symmetric.web.compression;
18  
19  import java.io.IOException;
20  import java.io.OutputStreamWriter;
21  import java.io.PrintWriter;
22  import javax.servlet.ServletOutputStream;
23  import javax.servlet.http.HttpServletResponse;
24  import javax.servlet.http.HttpServletResponseWrapper;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /***
30   * Implementation of <b>HttpServletResponseWrapper</b> that works with the
31   * CompressionServletResponseStream implementation..
32   * 
33   * This package is derived from the Jakarta <a
34   * href="http://jakarta.apache.org/tomcat">Tomcat</a> examples compression
35   * filter and is distributed in SymmetricDS for convenience.
36   * 
37   * @author Amy Roh
38   * @author Dmitri Valdin
39   * @version $Revision: 496190 $, $Date: 2007-01-14 16:21:45 -0700 (Sun, 14 Jan
40   *          2007) $
41   */
42  
43  public class CompressionServletResponseWrapper extends HttpServletResponseWrapper {
44  
45      static final Log logger = LogFactory.getLog(CompressionServletResponseWrapper.class);
46  
47      /***
48       * Calls the parent constructor which creates a ServletResponse adaptor
49       * wrapping the given response object.
50       */
51  
52      public CompressionServletResponseWrapper(HttpServletResponse response) {
53          super(response);
54          origResponse = response;
55          if (logger.isDebugEnabled()) {
56              logger.debug("CompressionServletResponseWrapper constructor gets called");
57          }
58      }
59  
60      /***
61       * Original response
62       */
63  
64      protected HttpServletResponse origResponse = null;
65  
66      /***
67       * Descriptive information about this Response implementation.
68       */
69  
70      protected static final String info = "CompressionServletResponseWrapper";
71  
72      /***
73       * The ServletOutputStream that has been returned by
74       * <code>getOutputStream()</code>, if any.
75       */
76  
77      protected ServletOutputStream stream = null;
78  
79      /***
80       * The PrintWriter that has been returned by <code>getWriter()</code>, if
81       * any.
82       */
83  
84      protected PrintWriter writer = null;
85  
86      /***
87       * Content type
88       */
89      protected String contentType = null;
90  
91      // --------------------------------------------------------- Public Methods
92  
93      /***
94       * Set content type
95       */
96      public void setContentType(String contentType) {
97          if (logger.isDebugEnabled()) {
98              logger.debug("setContentType to " + contentType);
99          }
100         this.contentType = contentType;
101         origResponse.setContentType(contentType);
102     }
103 
104     /***
105      * Create and return a ServletOutputStream to write the content associated
106      * with this Response.
107      * 
108      * @exception IOException
109      *                    if an input/output error occurs
110      */
111     public ServletOutputStream createOutputStream() throws IOException {
112         if (logger.isDebugEnabled()) {
113             logger.debug("createOutputStream gets called");
114         }
115 
116         CompressionResponseStream stream = new CompressionResponseStream(origResponse);
117         return stream;
118 
119     }
120 
121     /***
122      * Finish a response.
123      */
124     public void finishResponse() {
125         try {
126             if (writer != null) {
127                 writer.close();
128             } else {
129                 if (stream != null)
130                     stream.close();
131             }
132         } catch (IOException e) {
133         }
134     }
135 
136     // ------------------------------------------------ ServletResponse Methods
137 
138     /***
139      * Flush the buffer and commit this response.
140      * 
141      * @exception IOException
142      *                    if an input/output error occurs
143      */
144     public void flushBuffer() throws IOException {
145         if (logger.isDebugEnabled()) {
146             logger.debug("flush buffer @ CompressionServletResponseWrapper");
147         }
148         ((CompressionResponseStream) stream).flush();
149 
150     }
151 
152     /***
153      * Return the servlet output stream associated with this Response.
154      * 
155      * @exception IllegalStateException
156      *                    if <code>getWriter</code> has already been called
157      *                    for this response
158      * @exception IOException
159      *                    if an input/output error occurs
160      */
161     public ServletOutputStream getOutputStream() throws IOException {
162 
163         if (writer != null)
164             throw new IllegalStateException("getWriter() has already been called for this response");
165 
166         if (stream == null)
167             stream = createOutputStream();
168         if (logger.isDebugEnabled()) {
169             logger.debug("stream is set to " + stream + " in getOutputStream");
170         }
171 
172         return (stream);
173 
174     }
175 
176     /***
177      * Return the writer associated with this Response.
178      * 
179      * @exception IllegalStateException
180      *                    if <code>getOutputStream</code> has already been
181      *                    called for this response
182      * @exception IOException
183      *                    if an input/output error occurs
184      */
185     public PrintWriter getWriter() throws IOException {
186 
187         if (writer != null)
188             return (writer);
189 
190         if (stream != null)
191             throw new IllegalStateException("getOutputStream() has already been called for this response");
192 
193         stream = createOutputStream();
194         if (logger.isDebugEnabled()) {
195             logger.debug("stream is set to " + stream + " in getWriter");
196         }
197         // String charset = getCharsetFromContentType(contentType);
198         String charEnc = origResponse.getCharacterEncoding();
199         if (logger.isDebugEnabled()) {
200             logger.debug("character encoding is " + charEnc);
201         }
202         // HttpServletResponse.getCharacterEncoding() shouldn't return null
203         // according the spec, so feel free to remove that "if"
204         if (charEnc != null) {
205             writer = new PrintWriter(new OutputStreamWriter(stream, charEnc));
206         } else {
207             writer = new PrintWriter(stream);
208         }
209 
210         return (writer);
211 
212     }
213 
214     public void setContentLength(int length) {
215     }
216 
217 }