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.io.unsync.UnsyncPrintWriter;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.servlet.HttpHeaders;
21  import com.liferay.portal.kernel.util.StringPool;
22  
23  import java.io.IOException;
24  import java.io.OutputStreamWriter;
25  import java.io.PrintWriter;
26  
27  import javax.servlet.ServletOutputStream;
28  import javax.servlet.http.HttpServletResponse;
29  import javax.servlet.http.HttpServletResponseWrapper;
30  
31  /**
32   * <a href="GZipResponse.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Jayson Falkner
35   * @author Brian Wing Shun Chan
36   * @author Shuyang Zhou
37   */
38  public class GZipResponse extends HttpServletResponseWrapper {
39  
40      public GZipResponse(HttpServletResponse response) {
41          super(response);
42  
43          _response = response;
44  
45          // Clear previous content length setting. GZip response does not buffer
46          // output to get final content length. The response will be chunked
47          // unless an outer filter calculates the content length.
48  
49          _response.setContentLength(-1);
50  
51          _response.addHeader(HttpHeaders.CONTENT_ENCODING, _GZIP);
52      }
53  
54      public void finishResponse() {
55          try {
56              if (_writer != null) {
57                  _writer.close();
58              }
59              else if (_stream != null) {
60                  _stream.close();
61              }
62          }
63          catch (IOException e) {
64          }
65      }
66  
67      public void flushBuffer() throws IOException {
68          if (_stream != null) {
69              _stream.flush();
70          }
71      }
72  
73      public ServletOutputStream getOutputStream() throws IOException {
74          if (_writer != null) {
75              throw new IllegalStateException();
76          }
77  
78          if (_stream == null) {
79              _stream = new GZipServletOutputStream(_response.getOutputStream());
80          }
81  
82          return _stream;
83      }
84  
85      public PrintWriter getWriter() throws IOException {
86          if (_writer != null) {
87              return _writer;
88          }
89  
90          if (_stream != null) {
91              throw new IllegalStateException();
92          }
93  
94          if (_log.isWarnEnabled()) {
95              _log.warn("Use getOutputStream for optimum performance");
96          }
97  
98          _stream = new GZipServletOutputStream(_response.getOutputStream());
99  
100         _writer = new UnsyncPrintWriter(new OutputStreamWriter(
101             //_stream, _res.getCharacterEncoding()));
102             _stream, StringPool.UTF8));
103 
104         return _writer;
105     }
106 
107     public void setContentLength(int contentLength) {
108     }
109 
110     private static final String _GZIP = "gzip";
111 
112     private static final Log _log = LogFactoryUtil.getLog(GZipResponse.class);
113 
114     private HttpServletResponse _response;
115     private ServletOutputStream _stream;
116     private PrintWriter _writer;
117 
118 }