1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.coyote.http11.filters;
19
20 import java.io.IOException;
21 import java.io.OutputStream;
22 import java.util.zip.GZIPOutputStream;
23
24 import org.apache.tomcat.util.buf.ByteChunk;
25
26 import org.apache.coyote.OutputBuffer;
27 import org.apache.coyote.Response;
28 import org.apache.coyote.http11.OutputFilter;
29
30 /**
31 * Gzip output filter.
32 *
33 * @author Remy Maucherat
34 */
35 public class GzipOutputFilter implements OutputFilter {
36
37
38 // -------------------------------------------------------------- Constants
39
40
41 protected static final String ENCODING_NAME = "gzip";
42 protected static final ByteChunk ENCODING = new ByteChunk();
43
44
45 // ----------------------------------------------------- Static Initializer
46
47
48 static {
49 ENCODING.setBytes(ENCODING_NAME.getBytes(), 0, ENCODING_NAME.length());
50 }
51
52
53 // ----------------------------------------------------- Instance Variables
54
55
56 /**
57 * Next buffer in the pipeline.
58 */
59 protected OutputBuffer buffer;
60
61
62 /**
63 * Compression output stream.
64 */
65 protected GZIPOutputStream compressionStream = null;
66
67
68 /**
69 * Fake internal output stream.
70 */
71 protected OutputStream fakeOutputStream = new FakeOutputStream();
72
73
74 // --------------------------------------------------- OutputBuffer Methods
75
76
77 /**
78 * Write some bytes.
79 *
80 * @return number of bytes written by the filter
81 */
82 public int doWrite(ByteChunk chunk, Response res)
83 throws IOException {
84 if (compressionStream == null) {
85 compressionStream = new GZIPOutputStream(fakeOutputStream);
86 }
87 compressionStream.write(chunk.getBytes(), chunk.getStart(),
88 chunk.getLength());
89 return chunk.getLength();
90 }
91
92
93 // --------------------------------------------------- OutputFilter Methods
94
95
96 /**
97 * Some filters need additional parameters from the response. All the
98 * necessary reading can occur in that method, as this method is called
99 * after the response header processing is complete.
100 */
101 public void setResponse(Response response) {
102 }
103
104
105 /**
106 * Set the next buffer in the filter pipeline.
107 */
108 public void setBuffer(OutputBuffer buffer) {
109 this.buffer = buffer;
110 }
111
112
113 /**
114 * End the current request. It is acceptable to write extra bytes using
115 * buffer.doWrite during the execution of this method.
116 */
117 public long end()
118 throws IOException {
119 if (compressionStream == null) {
120 compressionStream = new GZIPOutputStream(fakeOutputStream);
121 }
122 compressionStream.finish();
123 compressionStream.close();
124 return ((OutputFilter) buffer).end();
125 }
126
127
128 /**
129 * Make the filter ready to process the next request.
130 */
131 public void recycle() {
132 // Set compression stream to null
133 compressionStream = null;
134 }
135
136
137 /**
138 * Return the name of the associated encoding; Here, the value is
139 * "identity".
140 */
141 public ByteChunk getEncodingName() {
142 return ENCODING;
143 }
144
145
146 // ------------------------------------------- FakeOutputStream Inner Class
147
148
149 protected class FakeOutputStream
150 extends OutputStream {
151 protected ByteChunk outputChunk = new ByteChunk();
152 protected byte[] singleByteBuffer = new byte[1];
153 public void write(int b)
154 throws IOException {
155 // Shouldn't get used for good performance, but is needed for
156 // compatibility with Sun JDK 1.4.0
157 singleByteBuffer[0] = (byte) (b & 0xff);
158 outputChunk.setBytes(singleByteBuffer, 0, 1);
159 buffer.doWrite(outputChunk, null);
160 }
161 public void write(byte[] b, int off, int len)
162 throws IOException {
163 outputChunk.setBytes(b, off, len);
164 buffer.doWrite(outputChunk, null);
165 }
166 public void flush() throws IOException {}
167 public void close() throws IOException {}
168 }
169
170
171 }