1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.mina.filter.compression;
21
22 import java.io.IOException;
23
24 import org.apache.mina.core.buffer.IoBuffer;
25 import org.apache.mina.core.filterchain.IoFilter;
26 import org.apache.mina.core.filterchain.IoFilterChain;
27 import org.apache.mina.core.session.AttributeKey;
28 import org.apache.mina.core.session.IoSession;
29 import org.apache.mina.core.write.WriteRequest;
30 import org.apache.mina.filter.util.WriteRequestFilter;
31
32 /**
33 * An {@link IoFilter} which compresses all data using
34 * <a href="http://www.jcraft.com/jzlib/">JZlib</a>.
35 * Support for the LZW (DLCZ) algorithm is also planned.
36 * <p>
37 * This filter only supports compression using the <tt>PARTIAL FLUSH</tt> method,
38 * since that is the only method useful when doing stream level compression.
39 * <p>
40 * This filter supports compression/decompression of the input and output
41 * channels selectively. It can also be enabled/disabled on the fly.
42 * <p>
43 * This filter does not discard the zlib objects, keeping them around for the
44 * entire life of the filter. This is because the zlib dictionary needs to
45 * be built up over time, which is used during compression and decompression.
46 * Over time, as repetitive data is sent over the wire, the compression efficiency
47 * steadily increases.
48 * <p>
49 * Note that the zlib header is written only once. It is not necessary that
50 * the data received after processing by this filter may not be complete due
51 * to packet fragmentation.
52 * <p>
53 * It goes without saying that the other end of this stream should also have a
54 * compatible compressor/decompressor using the same algorithm.
55 *
56 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
57 */
58 public class CompressionFilter extends WriteRequestFilter {
59 /**
60 * Max compression level. Will give the highest compression ratio, but
61 * will also take more cpu time and is the slowest.
62 */
63 public static final int COMPRESSION_MAX = Zlib.COMPRESSION_MAX;
64
65 /**
66 * Provides the best speed at the price of a low compression ratio.
67 */
68 public static final int COMPRESSION_MIN = Zlib.COMPRESSION_MIN;
69
70 /**
71 * No compression done on the data.
72 */
73 public static final int COMPRESSION_NONE = Zlib.COMPRESSION_NONE;
74
75 /**
76 * The default compression level used. Provides the best balance
77 * between speed and compression
78 */
79 public static final int COMPRESSION_DEFAULT = Zlib.COMPRESSION_DEFAULT;
80
81 /**
82 * A session attribute that stores the {@link Zlib} object used for compression.
83 */
84 private final AttributeKey DEFLATER = new AttributeKey(getClass(), "deflater");
85
86 /**
87 * A session attribute that stores the {@link Zlib} object used for decompression.
88 */
89 private final AttributeKey INFLATER = new AttributeKey(getClass(), "inflater");
90
91 /**
92 * A flag that allows you to disable compression once.
93 */
94 public static final AttributeKey DISABLE_COMPRESSION_ONCE = new AttributeKey(CompressionFilter.class, "disableOnce");
95
96 private boolean compressInbound = true;
97
98 private boolean compressOutbound = true;
99
100 private int compressionLevel;
101
102 /**
103 * Creates a new instance which compresses outboud data and decompresses
104 * inbound data with default compression level.
105 */
106 public CompressionFilter() {
107 this(true, true, COMPRESSION_DEFAULT);
108 }
109
110 /**
111 * Creates a new instance which compresses outboud data and decompresses
112 * inbound data with the specified <tt>compressionLevel</tt>.
113 *
114 * @param compressionLevel the level of compression to be used. Must
115 * be one of {@link #COMPRESSION_DEFAULT},
116 * {@link #COMPRESSION_MAX},
117 * {@link #COMPRESSION_MIN}, and
118 * {@link #COMPRESSION_NONE}.
119 */
120 public CompressionFilter(final int compressionLevel) {
121 this(true, true, compressionLevel);
122 }
123
124 /**
125 * Creates a new instance.
126 *
127 * @param compressInbound <tt>true</tt> if data read is to be decompressed
128 * @param compressOutbound <tt>true</tt> if data written is to be compressed
129 * @param compressionLevel the level of compression to be used. Must
130 * be one of {@link #COMPRESSION_DEFAULT},
131 * {@link #COMPRESSION_MAX},
132 * {@link #COMPRESSION_MIN}, and
133 * {@link #COMPRESSION_NONE}.
134 */
135 public CompressionFilter(final boolean compressInbound,
136 final boolean compressOutbound, final int compressionLevel) {
137 this.compressionLevel = compressionLevel;
138 this.compressInbound = compressInbound;
139 this.compressOutbound = compressOutbound;
140 }
141
142 @Override
143 public void messageReceived(NextFilter nextFilter, IoSession session,
144 Object message) throws Exception {
145 if (!compressInbound || !(message instanceof IoBuffer)) {
146 nextFilter.messageReceived(session, message);
147 return;
148 }
149
150 Zlib inflater = (Zlib) session.getAttribute(INFLATER);
151 if (inflater == null) {
152 throw new IllegalStateException();
153 }
154
155 IoBuffer inBuffer = (IoBuffer) message;
156 IoBuffer outBuffer = inflater.inflate(inBuffer);
157 nextFilter.messageReceived(session, outBuffer);
158 }
159
160 /*
161 * @see org.apache.mina.core.IoFilter#filterWrite(org.apache.mina.core.IoFilter.NextFilter, org.apache.mina.core.IoSession, org.apache.mina.core.IoFilter.WriteRequest)
162 */
163 @Override
164 protected Object doFilterWrite(
165 NextFilter nextFilter, IoSession session,
166 WriteRequest writeRequest) throws IOException {
167 if (!compressOutbound) {
168 return null;
169 }
170
171 if (session.containsAttribute(DISABLE_COMPRESSION_ONCE)) {
172 // Remove the marker attribute because it is temporary.
173 session.removeAttribute(DISABLE_COMPRESSION_ONCE);
174 return null;
175 }
176
177 Zlib deflater = (Zlib) session.getAttribute(DEFLATER);
178 if (deflater == null) {
179 throw new IllegalStateException();
180 }
181
182 IoBuffer inBuffer = (IoBuffer) writeRequest.getMessage();
183 if (!inBuffer.hasRemaining()) {
184 // Ignore empty buffers
185 return null;
186 } else {
187 return deflater.deflate(inBuffer);
188 }
189 }
190
191 @Override
192 public void onPreAdd(IoFilterChain parent, String name,
193 NextFilter nextFilter) throws Exception {
194 if (parent.contains(CompressionFilter.class)) {
195 throw new IllegalStateException(
196 "Only one " + CompressionFilter.class + " is permitted.");
197 }
198
199 Zlib deflater = new Zlib(compressionLevel, Zlib.MODE_DEFLATER);
200 Zlib inflater = new Zlib(compressionLevel, Zlib.MODE_INFLATER);
201
202 IoSession session = parent.getSession();
203
204 session.setAttribute(DEFLATER, deflater);
205 session.setAttribute(INFLATER, inflater);
206 }
207
208 /**
209 * Returns <tt>true</tt> if incoming data is being compressed.
210 */
211 public boolean isCompressInbound() {
212 return compressInbound;
213 }
214
215 /**
216 * Sets if incoming data has to be compressed.
217 */
218 public void setCompressInbound(boolean compressInbound) {
219 this.compressInbound = compressInbound;
220 }
221
222 /**
223 * Returns <tt>true</tt> if the filter is compressing data being written.
224 */
225 public boolean isCompressOutbound() {
226 return compressOutbound;
227 }
228
229 /**
230 * Set if outgoing data has to be compressed.
231 */
232 public void setCompressOutbound(boolean compressOutbound) {
233 this.compressOutbound = compressOutbound;
234 }
235
236 @Override
237 public void onPostRemove(IoFilterChain parent, String name,
238 NextFilter nextFilter) throws Exception {
239 super.onPostRemove(parent, name, nextFilter);
240 IoSession session = parent.getSession();
241 if (session == null) {
242 return;
243 }
244
245 Zlib inflater = (Zlib) session.getAttribute(INFLATER);
246 Zlib deflater = (Zlib) session.getAttribute(DEFLATER);
247 if (deflater != null) {
248 deflater.cleanUp();
249 }
250
251 if (inflater != null) {
252 inflater.cleanUp();
253 }
254 }
255 }