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.codec.prefixedstring;
21  
22  import org.apache.mina.core.buffer.BufferDataException;
23  import org.apache.mina.core.session.IoSession;
24  import org.apache.mina.filter.codec.ProtocolCodecFactory;
25  import org.apache.mina.filter.codec.ProtocolDecoder;
26  import org.apache.mina.filter.codec.ProtocolEncoder;
27  
28  import java.nio.charset.Charset;
29  
30  /**
31   * A {@link ProtocolCodecFactory} that performs encoding and decoding
32   * of a Java String object using a fixed-length length prefix.
33   *
34   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
35   */
36  public class PrefixedStringCodecFactory implements ProtocolCodecFactory {
37  
38      private final PrefixedStringEncoder encoder;
39  
40      private final PrefixedStringDecoder decoder;
41  
42      public PrefixedStringCodecFactory(Charset charset) {
43          encoder = new PrefixedStringEncoder(charset);
44          decoder = new PrefixedStringDecoder(charset);
45      }
46  
47      public PrefixedStringCodecFactory() {
48          this(Charset.defaultCharset());
49      }
50  
51      /**
52       * Returns the allowed maximum size of an encoded string.
53       * If the size of the encoded String exceeds this value, the encoder
54       * will throw a {@link IllegalArgumentException}.
55       * The default value is {@link PrefixedStringEncoder#DEFAULT_MAX_DATA_LENGTH}.
56       * <p>
57       * This method does the same job as {@link PrefixedStringEncoder#setMaxDataLength(int)}.
58       *
59       * @return the allowed maximum size of an encoded string.
60       */
61      public int getEncoderMaxDataLength() {
62          return encoder.getMaxDataLength();
63      }
64  
65      /**
66       * Sets the allowed maximum size of an encoded String.
67       * If the size of the encoded String exceeds this value, the encoder
68       * will throw a {@link IllegalArgumentException}.
69       * The default value is {@link PrefixedStringEncoder#DEFAULT_MAX_DATA_LENGTH}.
70       * <p>
71       * This method does the same job as {@link PrefixedStringEncoder#getMaxDataLength()}.
72       *
73       * @param maxDataLength allowed maximum size of an encoded String.
74       */
75      public void setEncoderMaxDataLength(int maxDataLength) {
76          encoder.setMaxDataLength(maxDataLength);
77      }
78  
79      /**
80       * @return the allowed maximum size of a decoded string.
81       * <p>
82       * This method does the same job as {@link PrefixedStringEncoder#setMaxDataLength(int)}.
83       * @see #setDecoderMaxDataLength(int)
84       */
85      public int getDecoderMaxDataLength() {
86          return decoder.getMaxDataLength();
87      }
88  
89      /**
90       * Sets the maximum allowed value specified as data length in the decoded data
91       * <p>
92       * Useful for preventing an OutOfMemory attack by the peer.
93       * The decoder will throw a {@link BufferDataException} when data length
94       * specified in the incoming data is greater than maxDataLength
95       * The default value is {@link PrefixedStringDecoder#DEFAULT_MAX_DATA_LENGTH}.
96       * <p>
97       * This method does the same job as {@link PrefixedStringDecoder#setMaxDataLength(int)}.
98       *
99       * @param maxDataLength maximum allowed value specified as data length in the incoming data
100      */
101     public void setDecoderMaxDataLength(int maxDataLength) {
102         decoder.setMaxDataLength(maxDataLength);
103     }
104 
105     /**
106      * Sets the length of the prefix used by the decoder
107      *
108      * @param prefixLength the length of the length prefix (1, 2, or 4)
109      */
110     public void setDecoderPrefixLength(int prefixLength) {
111         decoder.setPrefixLength(prefixLength);
112     }
113 
114     /**
115      * Gets the length of the length prefix (1, 2, or 4) used by the decoder
116      *
117      * @return length of the length prefix
118      */
119     public int getDecoderPrefixLength() {
120         return decoder.getPrefixLength();
121     }
122 
123     /**
124      * Sets the length of the prefix used by the encoder
125      *
126      * @param prefixLength the length of the length prefix (1, 2, or 4)
127      */
128     public void setEncoderPrefixLength(int prefixLength) {
129         encoder.setPrefixLength(prefixLength);
130     }
131 
132     /**
133      * Gets the length of the length prefix (1, 2, or 4) used by the encoder
134      *
135      * @return length of the length prefix
136      */
137     public int getEncoderPrefixLength() {
138         return encoder.getPrefixLength();
139     }
140 
141     public ProtocolEncoder getEncoder(IoSession session) throws Exception {
142         return encoder;
143     }
144 
145     public ProtocolDecoder getDecoder(IoSession session) throws Exception {
146         return decoder;
147     }
148 }