1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portal.servlet.filters.gzip;
16  
17  import com.liferay.portal.kernel.util.StringPool;
18  
19  import java.io.IOException;
20  import java.io.OutputStreamWriter;
21  import java.io.PrintWriter;
22  
23  import javax.servlet.ServletOutputStream;
24  import javax.servlet.http.HttpServletResponse;
25  import javax.servlet.http.HttpServletResponseWrapper;
26  
27  /**
28   * <a href="GZipResponse.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Jayson Falkner
31   * @author Brian Wing Shun Chan
32   */
33  public class GZipResponse extends HttpServletResponseWrapper {
34  
35      public GZipResponse(HttpServletResponse response) {
36          super(response);
37  
38          _response = response;
39      }
40  
41      public void finishResponse() {
42          try {
43              if (_writer != null) {
44                  _writer.close();
45              }
46              else if (_stream != null) {
47                  _stream.close();
48              }
49          }
50          catch (IOException e) {
51          }
52      }
53  
54      public void flushBuffer() throws IOException {
55          if (_stream != null) {
56              _stream.flush();
57          }
58      }
59  
60      public ServletOutputStream getOutputStream() throws IOException {
61          if (_writer != null) {
62              throw new IllegalStateException();
63          }
64  
65          if (_stream == null) {
66              _stream = _createOutputStream();
67          }
68  
69          return _stream;
70      }
71  
72      public PrintWriter getWriter() throws IOException {
73          if (_writer != null) {
74              return _writer;
75          }
76  
77          if (_stream != null) {
78              throw new IllegalStateException();
79          }
80  
81          _stream = _createOutputStream();
82  
83          _writer = new PrintWriter(new OutputStreamWriter(
84              //_stream, _res.getCharacterEncoding()));
85              _stream, StringPool.UTF8));
86  
87          return _writer;
88      }
89  
90      private ServletOutputStream _createOutputStream() throws IOException {
91          return new GZipStream(_response);
92      }
93  
94      private HttpServletResponse _response;
95      private ServletOutputStream _stream;
96      private PrintWriter _writer;
97  
98  }