001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.filter.compression;
021:
022: import java.io.IOException;
023:
024: import org.apache.mina.common.IoBuffer;
025: import org.apache.mina.common.AttributeKey;
026: import org.apache.mina.common.IoFilter;
027: import org.apache.mina.common.IoFilterChain;
028: import org.apache.mina.common.IoSession;
029: import org.apache.mina.common.WriteRequest;
030: import org.apache.mina.filter.util.WriteRequestFilter;
031:
032: /**
033: * An {@link IoFilter} which compresses all data using
034: * <a href="http://www.jcraft.com/jzlib/">JZlib</a>.
035: * Support for the LZW (DLCZ) algorithm is also planned.
036: * <p>
037: * This filter only supports compression using the <tt>PARTIAL FLUSH</tt> method,
038: * since that is the only method useful when doing stream level compression.
039: * <p>
040: * This filter supports compression/decompression of the input and output
041: * channels selectively. It can also be enabled/disabled on the fly.
042: * <p>
043: * This filter does not discard the zlib objects, keeping them around for the
044: * entire life of the filter. This is because the zlib dictionary needs to
045: * be built up over time, which is used during compression and decompression.
046: * Over time, as repetitive data is sent over the wire, the compression efficiency
047: * steadily increases.
048: * <p>
049: * Note that the zlib header is written only once. It is not necessary that
050: * the data received after processing by this filter may not be complete due
051: * to packet fragmentation.
052: * <p>
053: * It goes without saying that the other end of this stream should also have a
054: * compatible compressor/decompressor using the same algorithm.
055: *
056: * @author The Apache MINA Project (dev@mina.apache.org)
057: * @version $Rev: 581234 $, $Date: 2007-10-02 07:39:48 -0600 (Tue, 02 Oct 2007) $
058: */
059: public class CompressionFilter extends WriteRequestFilter {
060: /**
061: * Max compression level. Will give the highest compression ratio, but
062: * will also take more cpu time and is the slowest.
063: */
064: public static final int COMPRESSION_MAX = Zlib.COMPRESSION_MAX;
065:
066: /**
067: * Provides the best speed at the price of a low compression ratio.
068: */
069: public static final int COMPRESSION_MIN = Zlib.COMPRESSION_MIN;
070:
071: /**
072: * No compression done on the data.
073: */
074: public static final int COMPRESSION_NONE = Zlib.COMPRESSION_NONE;
075:
076: /**
077: * The default compression level used. Provides the best balance
078: * between speed and compression
079: */
080: public static final int COMPRESSION_DEFAULT = Zlib.COMPRESSION_DEFAULT;
081:
082: /**
083: * A session attribute that stores the {@link Zlib} object used for compression.
084: */
085: private final AttributeKey DEFLATER = new AttributeKey(getClass(),
086: "deflater");
087:
088: /**
089: * A session attribute that stores the {@link Zlib} object used for decompression.
090: */
091: private final AttributeKey INFLATER = new AttributeKey(getClass(),
092: "inflater");
093:
094: /**
095: * A flag that allows you to disable compression once.
096: */
097: public static final AttributeKey DISABLE_COMPRESSION_ONCE = new AttributeKey(
098: CompressionFilter.class, "disableOnce");
099:
100: private boolean compressInbound = true;
101:
102: private boolean compressOutbound = true;
103:
104: private int compressionLevel;
105:
106: /**
107: * Creates a new instance which compresses outboud data and decompresses
108: * inbound data with default compression level.
109: */
110: public CompressionFilter() {
111: this (true, true, COMPRESSION_DEFAULT);
112: }
113:
114: /**
115: * Creates a new instance which compresses outboud data and decompresses
116: * inbound data with the specified <tt>compressionLevel</tt>.
117: *
118: * @param compressionLevel the level of compression to be used. Must
119: * be one of {@link #COMPRESSION_DEFAULT},
120: * {@link #COMPRESSION_MAX},
121: * {@link #COMPRESSION_MIN}, and
122: * {@link #COMPRESSION_NONE}.
123: */
124: public CompressionFilter(final int compressionLevel) {
125: this (true, true, compressionLevel);
126: }
127:
128: /**
129: * Creates a new instance.
130: *
131: * @param compressInbound <tt>true</tt> if data read is to be decompressed
132: * @param compressOutbound <tt>true</tt> if data written is to be compressed
133: * @param compressionLevel the level of compression to be used. Must
134: * be one of {@link #COMPRESSION_DEFAULT},
135: * {@link #COMPRESSION_MAX},
136: * {@link #COMPRESSION_MIN}, and
137: * {@link #COMPRESSION_NONE}.
138: */
139: public CompressionFilter(final boolean compressInbound,
140: final boolean compressOutbound, final int compressionLevel) {
141: this .compressionLevel = compressionLevel;
142: this .compressInbound = compressInbound;
143: this .compressOutbound = compressOutbound;
144: }
145:
146: @Override
147: public void messageReceived(NextFilter nextFilter,
148: IoSession session, Object message) throws Exception {
149: if (!compressInbound) {
150: nextFilter.messageReceived(session, message);
151: return;
152: }
153:
154: Zlib inflater = (Zlib) session.getAttribute(INFLATER);
155: if (inflater == null) {
156: throw new IllegalStateException();
157: }
158:
159: IoBuffer inBuffer = (IoBuffer) message;
160: IoBuffer outBuffer = inflater.inflate(inBuffer);
161: nextFilter.messageReceived(session, outBuffer);
162: }
163:
164: /*
165: * @see org.apache.mina.common.IoFilter#filterWrite(org.apache.mina.common.IoFilter.NextFilter, org.apache.mina.common.IoSession, org.apache.mina.common.IoFilter.WriteRequest)
166: */
167: @Override
168: protected Object doFilterWrite(NextFilter nextFilter,
169: IoSession session, WriteRequest writeRequest)
170: throws IOException {
171: if (!compressOutbound) {
172: return null;
173: }
174:
175: if (session.containsAttribute(DISABLE_COMPRESSION_ONCE)) {
176: // Remove the marker attribute because it is temporary.
177: session.removeAttribute(DISABLE_COMPRESSION_ONCE);
178: return null;
179: }
180:
181: Zlib deflater = (Zlib) session.getAttribute(DEFLATER);
182: if (deflater == null) {
183: throw new IllegalStateException();
184: }
185:
186: IoBuffer inBuffer = (IoBuffer) writeRequest.getMessage();
187: if (!inBuffer.hasRemaining()) {
188: // Ignore empty buffers
189: return null;
190: } else {
191: return deflater.deflate(inBuffer);
192: }
193: }
194:
195: @Override
196: public void onPreAdd(IoFilterChain parent, String name,
197: NextFilter nextFilter) throws Exception {
198: if (parent.contains(CompressionFilter.class)) {
199: throw new IllegalStateException("Only one "
200: + CompressionFilter.class + " is permitted.");
201: }
202:
203: Zlib deflater = new Zlib(compressionLevel, Zlib.MODE_DEFLATER);
204: Zlib inflater = new Zlib(compressionLevel, Zlib.MODE_INFLATER);
205:
206: IoSession session = parent.getSession();
207:
208: session.setAttribute(DEFLATER, deflater);
209: session.setAttribute(INFLATER, inflater);
210: }
211:
212: /**
213: * Returns <tt>true</tt> if incoming data is being compressed.
214: */
215: public boolean isCompressInbound() {
216: return compressInbound;
217: }
218:
219: /**
220: * Sets if incoming data has to be compressed.
221: */
222: public void setCompressInbound(boolean compressInbound) {
223: this .compressInbound = compressInbound;
224: }
225:
226: /**
227: * Returns <tt>true</tt> if the filter is compressing data being written.
228: */
229: public boolean isCompressOutbound() {
230: return compressOutbound;
231: }
232:
233: /**
234: * Set if outgoing data has to be compressed.
235: */
236: public void setCompressOutbound(boolean compressOutbound) {
237: this .compressOutbound = compressOutbound;
238: }
239:
240: @Override
241: public void onPostRemove(IoFilterChain parent, String name,
242: NextFilter nextFilter) throws Exception {
243: super .onPostRemove(parent, name, nextFilter);
244: IoSession session = parent.getSession();
245: if (session == null) {
246: return;
247: }
248:
249: Zlib inflater = (Zlib) session.getAttribute(INFLATER);
250: Zlib deflater = (Zlib) session.getAttribute(DEFLATER);
251: if (deflater != null) {
252: deflater.cleanUp();
253: }
254:
255: if (inflater != null) {
256: inflater.cleanUp();
257: }
258: }
259: }
|