1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }