1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.portal.servlet.filters.gzip;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.servlet.HttpHeaders;
28  import com.liferay.portal.kernel.util.ByteArrayMaker;
29  
30  import java.io.IOException;
31  import java.io.OutputStream;
32  
33  import java.util.zip.GZIPOutputStream;
34  
35  import javax.servlet.ServletOutputStream;
36  import javax.servlet.http.HttpServletResponse;
37  
38  /**
39   * <a href="GZipStream.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Jayson Falkner
42   * @author Brian Wing Shun Chan
43   */
44  public class GZipStream extends ServletOutputStream {
45  
46      public GZipStream(HttpServletResponse response) throws IOException {
47          super();
48  
49          _response = response;
50          _output = response.getOutputStream();
51          _bufferedOutput = new ByteArrayMaker();
52          _closed = false;
53      }
54  
55      public void close() throws IOException {
56          if (_closed) {
57              throw new IOException();
58          }
59  
60          if (_bufferedOutput instanceof ByteArrayMaker) {
61              ByteArrayMaker baos = (ByteArrayMaker)_bufferedOutput;
62  
63              ByteArrayMaker compressedContent = new ByteArrayMaker();
64  
65              GZIPOutputStream gzipOutput = new GZIPOutputStream(
66                  compressedContent);
67  
68              gzipOutput.write(baos.toByteArray());
69              gzipOutput.finish();
70  
71              byte[] compressedBytes = compressedContent.toByteArray();
72  
73              _response.setContentLength(compressedBytes.length);
74              _response.addHeader(HttpHeaders.CONTENT_ENCODING, _GZIP);
75  
76              _output.write(compressedBytes);
77              _output.flush();
78              _output.close();
79  
80              _closed = true;
81          }
82          else if (_bufferedOutput instanceof GZIPOutputStream) {
83              GZIPOutputStream gzipOutput = (GZIPOutputStream)_bufferedOutput;
84  
85              gzipOutput.finish();
86  
87              _output.flush();
88              _output.close();
89  
90              _closed = true;
91          }
92      }
93  
94      public void flush() throws IOException {
95          if (_closed) {
96              throw new IOException();
97          }
98  
99          _bufferedOutput.flush();
100     }
101 
102     public void write(int b) throws IOException {
103         if (_closed) {
104             throw new IOException();
105         }
106 
107         // LEP-649
108 
109         //_checkBufferSize(1);
110 
111         _bufferedOutput.write((byte)b);
112     }
113 
114     public void write(byte b[]) throws IOException {
115         write(b, 0, b.length);
116     }
117 
118     public void write(byte b[], int off, int len) throws IOException {
119         if (_closed) {
120             throw new IOException();
121         }
122 
123         // LEP-649
124 
125         //_checkBufferSize(len);
126 
127         try {
128             _bufferedOutput.write(b, off, len);
129         }
130         catch (IOException ioe) {
131             _log.warn(ioe.getMessage());
132         }
133     }
134 
135     public boolean closed() {
136         return _closed;
137     }
138 
139     public void reset() {
140     }
141 
142     private static final String _GZIP = "gzip";
143 
144     private static Log _log = LogFactoryUtil.getLog(GZipStream.class);
145 
146     private HttpServletResponse _response = null;
147     private ServletOutputStream _output = null;
148     private OutputStream _bufferedOutput = null;
149     private boolean _closed = false;
150 
151 }