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.textline;
21  
22  import java.nio.charset.Charset;
23  
24  import org.apache.mina.core.buffer.BufferDataException;
25  import org.apache.mina.core.session.IoSession;
26  import org.apache.mina.filter.codec.ProtocolCodecFactory;
27  import org.apache.mina.filter.codec.ProtocolDecoder;
28  import org.apache.mina.filter.codec.ProtocolEncoder;
29  
30  /**
31   * A {@link ProtocolCodecFactory} that performs encoding and decoding between
32   * a text line data and a Java string object.  This codec is useful especially
33   * when you work with a text-based protocols such as SMTP and IMAP.
34   *
35   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
36   */
37  public class TextLineCodecFactory implements ProtocolCodecFactory {
38  
39      private final TextLineEncoder encoder;
40  
41      private final TextLineDecoder decoder;
42  
43      /**
44       * Creates a new instance with the current default {@link Charset}.
45       */
46      public TextLineCodecFactory() {
47          this(Charset.defaultCharset());
48      }
49  
50      /**
51       * Creates a new instance with the specified {@link Charset}.  The
52       * encoder uses a UNIX {@link LineDelimiter} and the decoder uses
53       * the AUTO {@link LineDelimiter}.
54       *
55       * @param charset The charset to use in the encoding and decoding
56       */
57      public TextLineCodecFactory(Charset charset) {
58          encoder = new TextLineEncoder(charset, LineDelimiter.UNIX);
59          decoder = new TextLineDecoder(charset, LineDelimiter.AUTO);
60      }
61  
62      /**
63       * Creates a new instance of TextLineCodecFactory.  This constructor
64       * provides more flexibility for the developer.
65       *
66       * @param charset
67       *  The charset to use in the encoding and decoding
68       * @param encodingDelimiter
69       *  The line delimeter for the encoder
70       * @param decodingDelimiter
71       *  The line delimeter for the decoder
72       */
73      public TextLineCodecFactory(Charset charset, String encodingDelimiter, String decodingDelimiter) {
74          encoder = new TextLineEncoder(charset, encodingDelimiter);
75          decoder = new TextLineDecoder(charset, decodingDelimiter);
76      }
77  
78      /**
79       * Creates a new instance of TextLineCodecFactory.  This constructor
80       * provides more flexibility for the developer.
81       *
82       * @param charset
83       *  The charset to use in the encoding and decoding
84       * @param encodingDelimiter
85       *  The line delimeter for the encoder
86       * @param decodingDelimiter
87       *  The line delimeter for the decoder
88       */
89      public TextLineCodecFactory(Charset charset, LineDelimiter encodingDelimiter, LineDelimiter decodingDelimiter) {
90          encoder = new TextLineEncoder(charset, encodingDelimiter);
91          decoder = new TextLineDecoder(charset, decodingDelimiter);
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      public ProtocolEncoder getEncoder(IoSession session) {
98          return encoder;
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     public ProtocolDecoder getDecoder(IoSession session) {
105         return decoder;
106     }
107 
108     /**
109      * @return the allowed maximum size of the encoded line.
110      * If the size of the encoded line exceeds this value, the encoder
111      * will throw a {@link IllegalArgumentException}.  The default value
112      * is {@link Integer#MAX_VALUE}.
113      * <p>
114      * This method does the same job with {@link TextLineEncoder#getMaxLineLength()}.
115      */
116     public int getEncoderMaxLineLength() {
117         return encoder.getMaxLineLength();
118     }
119 
120     /**
121      * Sets the allowed maximum size of the encoded line.
122      * If the size of the encoded line exceeds this value, the encoder
123      * will throw a {@link IllegalArgumentException}.  The default value
124      * is {@link Integer#MAX_VALUE}.
125      * <p>
126      * This method does the same job with {@link TextLineEncoder#setMaxLineLength(int)}.
127      * 
128      * @param maxLineLength The maximum encoded line length
129      */
130     public void setEncoderMaxLineLength(int maxLineLength) {
131         encoder.setMaxLineLength(maxLineLength);
132     }
133 
134     /**
135      * @return the allowed maximum size of the line to be decoded.
136      * If the size of the line to be decoded exceeds this value, the
137      * decoder will throw a {@link BufferDataException}.  The default
138      * value is <tt>1024</tt> (1KB).
139      * <p>
140      * This method does the same job with {@link TextLineDecoder#getMaxLineLength()}.
141      */
142     public int getDecoderMaxLineLength() {
143         return decoder.getMaxLineLength();
144     }
145 
146     /**
147      * Sets the allowed maximum size of the line to be decoded.
148      * If the size of the line to be decoded exceeds this value, the
149      * decoder will throw a {@link BufferDataException}.  The default
150      * value is <tt>1024</tt> (1KB).
151      * <p>
152      * This method does the same job with {@link TextLineDecoder#setMaxLineLength(int)}.
153      * 
154      * @param maxLineLength the maximum decoded line length
155      */
156     public void setDecoderMaxLineLength(int maxLineLength) {
157         decoder.setMaxLineLength(maxLineLength);
158     }
159 }