1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.filters.compression;
24  
25  import java.io.IOException;
26  import java.io.OutputStreamWriter;
27  import java.io.PrintWriter;
28  
29  import javax.servlet.ServletOutputStream;
30  import javax.servlet.http.HttpServletResponse;
31  import javax.servlet.http.HttpServletResponseWrapper;
32  
33  /**
34   * <a href="CompressionResponse.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Jayson Falkner
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class CompressionResponse extends HttpServletResponseWrapper {
41  
42      public CompressionResponse(HttpServletResponse res) {
43          super(res);
44  
45          _res = res;
46      }
47  
48      public void finishResponse() {
49          try {
50              if (_writer != null) {
51                  _writer.close();
52              }
53              else if (_stream != null) {
54                  _stream.close();
55              }
56          }
57          catch (IOException e) {
58          }
59      }
60  
61      public void flushBuffer() throws IOException {
62          if (_stream != null) {
63              _stream.flush();
64          }
65      }
66  
67      public ServletOutputStream getOutputStream() throws IOException {
68          if (_writer != null) {
69              throw new IllegalStateException();
70          }
71  
72          if (_stream == null) {
73              _stream = _createOutputStream();
74          }
75  
76          return _stream;
77      }
78  
79      public PrintWriter getWriter() throws IOException {
80          if (_writer != null) {
81              return _writer;
82          }
83  
84          if (_stream != null) {
85              throw new IllegalStateException();
86          }
87  
88          _stream = _createOutputStream();
89  
90          _writer = new PrintWriter(new OutputStreamWriter(
91              //_stream, _res.getCharacterEncoding()));
92              _stream, CompressionFilter.ENCODING));
93  
94          return _writer;
95      }
96  
97      private ServletOutputStream _createOutputStream() throws IOException {
98          return new CompressionStream(_res);
99      }
100 
101     private HttpServletResponse _res = null;
102     private ServletOutputStream _stream = null;
103     private PrintWriter _writer = null;
104 
105 }