View Javadoc

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;
21  
22  import java.io.IOException;
23  
24  import org.apache.mina.common.ByteBuffer;
25  import org.apache.mina.common.IoFilter;
26  import org.apache.mina.common.IoFilterAdapter;
27  import org.apache.mina.common.IoFilterChain;
28  import org.apache.mina.common.IoSession;
29  import org.apache.mina.filter.support.Zlib;
30  
31  /**
32   * An {@link IoFilter} which compresses all data using
33   * <a href="http://www.jcraft.com/jzlib/">JZlib</a>.
34   * Support for the LZW (DLCZ) algorithm is also planned.
35   * <p>
36   * This filter only supports compression using the <tt>PARTIAL FLUSH</tt> method,
37   * since that is the only method useful when doing stream level compression.
38   * <p>
39   * This filter supports compression/decompression of the input and output
40   * channels selectively.  It can also be enabled/disabled on the fly.
41   * <p>
42   * This filter does not discard the zlib objects, keeping them around for the
43   * entire life of the filter.  This is because the zlib dictionary needs to
44   * be built up over time, which is used during compression and decompression.
45   * Over time, as repetitive data is sent over the wire, the compression efficiency
46   * steadily increases.
47   * <p>
48   * Note that the zlib header is written only once. It is not necessary that
49   * the data received after processing by this filter may not be complete due
50   * to packet fragmentation.
51   * <p>
52   * It goes without saying that the other end of this stream should also have a
53   * compatible compressor/decompressor using the same algorithm.
54   *
55   * @author The Apache Directory Project (mina-dev@directory.apache.org)
56   * @version $Rev: 560324 $, $Date: 2007-07-28 02:21:08 +0900 (토, 28  7월 2007) $
57   */
58  public class CompressionFilter extends IoFilterAdapter {
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 static final String DEFLATER = CompressionFilter.class.getName()
85              + ".Deflater";
86  
87      /**
88       * A session attribute that stores the {@link Zlib} object used for decompression.
89       */
90      private static final String INFLATER = CompressionFilter.class.getName()
91              + ".Inflater";
92  
93      /**
94       * A flag that allows you to disable compression once.
95       */
96      public static final String DISABLE_COMPRESSION_ONCE = CompressionFilter.class
97              .getName()
98              + ".DisableCompressionOnce";
99  
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     public void messageReceived(NextFilter nextFilter, IoSession session,
147             Object message) throws Exception {
148         if (!compressInbound) {
149             nextFilter.messageReceived(session, message);
150             return;
151         }
152 
153         Zlib inflater = (Zlib) session.getAttribute(INFLATER);
154         if (inflater == null) {
155             throw new IllegalStateException();
156         }
157 
158         ByteBuffer inBuffer = (ByteBuffer) message;
159         ByteBuffer outBuffer = inflater.inflate(inBuffer);
160         inBuffer.release();
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     public void filterWrite(NextFilter nextFilter, IoSession session,
168             WriteRequest writeRequest) throws IOException {
169         if (!compressOutbound) {
170             nextFilter.filterWrite(session, writeRequest);
171             return;
172         }
173 
174         if (session.containsAttribute(DISABLE_COMPRESSION_ONCE)) {
175             // Remove the marker attribute because it is temporary.
176             session.removeAttribute(DISABLE_COMPRESSION_ONCE);
177             nextFilter.filterWrite(session, writeRequest);
178             return;
179         }
180 
181         Zlib deflater = (Zlib) session.getAttribute(DEFLATER);
182         if (deflater == null) {
183             throw new IllegalStateException();
184         }
185 
186         ByteBuffer inBuffer = (ByteBuffer) writeRequest.getMessage();
187         if (!inBuffer.hasRemaining()) {
188             // Ignore empty buffers
189             nextFilter.filterWrite(session, writeRequest);
190         } else {
191             ByteBuffer outBuf = deflater.deflate(inBuffer);
192             inBuffer.release();
193             nextFilter.filterWrite(session, new WriteRequest(outBuf,
194                     writeRequest.getFuture()));
195         }
196     }
197 
198     public void onPreAdd(IoFilterChain parent, String name,
199             NextFilter nextFilter) throws Exception {
200         if (parent.contains(CompressionFilter.class)) {
201             throw new IllegalStateException(
202                     "A filter chain cannot contain more than"
203                             + " one Stream Compression filter.");
204         }
205 
206         Zlib deflater = new Zlib(compressionLevel, Zlib.MODE_DEFLATER);
207         Zlib inflater = new Zlib(compressionLevel, Zlib.MODE_INFLATER);
208 
209         IoSession session = parent.getSession();
210 
211         session.setAttribute(DEFLATER, deflater);
212         session.setAttribute(INFLATER, inflater);
213     }
214 
215     /**
216      * Returns <tt>true</tt> if incoming data is being compressed.
217      */
218     public boolean isCompressInbound() {
219         return compressInbound;
220     }
221 
222     /**
223      * Sets if incoming data has to be compressed.
224      */
225     public void setCompressInbound(boolean compressInbound) {
226         this.compressInbound = compressInbound;
227     }
228 
229     /**
230      * Returns <tt>true</tt> if the filter is compressing data being written.
231      */
232     public boolean isCompressOutbound() {
233         return compressOutbound;
234     }
235 
236     /**
237      * Set if outgoing data has to be compressed.
238      */
239     public void setCompressOutbound(boolean compressOutbound) {
240         this.compressOutbound = compressOutbound;
241     }
242 
243     public void onPostRemove(IoFilterChain parent, String name,
244             NextFilter nextFilter) throws Exception {
245         super.onPostRemove(parent, name, nextFilter);
246         IoSession session = parent.getSession();
247         if (session == null) {
248             return;
249         }
250 
251         Zlib inflater = (Zlib) session.getAttribute(INFLATER);
252         Zlib deflater = (Zlib) session.getAttribute(DEFLATER);
253         if (deflater != null) {
254             deflater.cleanUp();
255         }
256 
257         if (inflater != null) {
258             inflater.cleanUp();
259         }
260     }
261 }