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.io.unsync.UnsyncPrintWriter;
18  import com.liferay.portal.kernel.util.StringPool;
19  
20  import java.io.IOException;
21  import java.io.OutputStreamWriter;
22  import java.io.PrintWriter;
23  
24  import javax.servlet.ServletOutputStream;
25  import javax.servlet.http.HttpServletResponse;
26  import javax.servlet.http.HttpServletResponseWrapper;
27  
28  /**
29   * <a href="GZipResponse.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Jayson Falkner
32   * @author Brian Wing Shun Chan
33   */
34  public class GZipResponse extends HttpServletResponseWrapper {
35  
36      public GZipResponse(HttpServletResponse response) {
37          super(response);
38  
39          _response = response;
40      }
41  
42      public void finishResponse() {
43          try {
44              if (_writer != null) {
45                  _writer.close();
46              }
47              else if (_stream != null) {
48                  _stream.close();
49              }
50          }
51          catch (IOException e) {
52          }
53      }
54  
55      public void flushBuffer() throws IOException {
56          if (_stream != null) {
57              _stream.flush();
58          }
59      }
60  
61      public ServletOutputStream getOutputStream() throws IOException {
62          if (_writer != null) {
63              throw new IllegalStateException();
64          }
65  
66          if (_stream == null) {
67              _stream = _createOutputStream();
68          }
69  
70          return _stream;
71      }
72  
73      public PrintWriter getWriter() throws IOException {
74          if (_writer != null) {
75              return _writer;
76          }
77  
78          if (_stream != null) {
79              throw new IllegalStateException();
80          }
81  
82          _stream = _createOutputStream();
83  
84          _writer = new UnsyncPrintWriter(new OutputStreamWriter(
85              //_stream, _res.getCharacterEncoding()));
86              _stream, StringPool.UTF8));
87  
88          return _writer;
89      }
90  
91      private ServletOutputStream _createOutputStream() throws IOException {
92          return new GZipStream(_response);
93      }
94  
95      private HttpServletResponse _response;
96      private ServletOutputStream _stream;
97      private PrintWriter _writer;
98  
99  }