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.codec.textline;
021:
022: import java.nio.charset.Charset;
023:
024: import org.apache.mina.common.BufferDataException;
025: import org.apache.mina.common.IoSession;
026: import org.apache.mina.filter.codec.ProtocolCodecFactory;
027: import org.apache.mina.filter.codec.ProtocolDecoder;
028: import org.apache.mina.filter.codec.ProtocolEncoder;
029:
030: /**
031: * A {@link ProtocolCodecFactory} that performs encoding and decoding between
032: * a text line data and a Java string object. This codec is useful especially
033: * when you work with a text-based protocols such as SMTP and IMAP.
034: *
035: * @author The Apache MINA Project (dev@mina.apache.org)
036: * @version $Rev: 596187 $, $Date: 2007-11-18 20:41:14 -0700 (Sun, 18 Nov 2007) $
037: */
038: public class TextLineCodecFactory implements ProtocolCodecFactory {
039:
040: private final TextLineEncoder encoder;
041: private final TextLineDecoder decoder;
042:
043: /**
044: * Creates a new instance with the current default {@link Charset}.
045: */
046: public TextLineCodecFactory() {
047: this (Charset.defaultCharset());
048: }
049:
050: /**
051: * Creates a new instance with the specified {@link Charset}. The
052: * encoder uses a UNIX {@link LineDelimiter} and the decoder uses
053: * the AUTO {@link LineDelimiter}.
054: *
055: * @param charset
056: * The charset to use in the encoding and decoding
057: */
058: public TextLineCodecFactory(Charset charset) {
059: encoder = new TextLineEncoder(charset, LineDelimiter.UNIX);
060: decoder = new TextLineDecoder(charset, LineDelimiter.AUTO);
061: }
062:
063: /**
064: * Creates a new instance of TextLineCodecFactory. This constructor
065: * provides more flexibility for the developer.
066: *
067: * @param charset
068: * The charset to use in the encoding and decoding
069: * @param encodingDelimiter
070: * The line delimeter for the encoder
071: * @param decodingDelimiter
072: * The line delimeter for the decoder
073: */
074: public TextLineCodecFactory(Charset charset,
075: String encodingDelimiter, String decodingDelimiter) {
076: encoder = new TextLineEncoder(charset, encodingDelimiter);
077: decoder = new TextLineDecoder(charset, decodingDelimiter);
078: }
079:
080: /**
081: * Creates a new instance of TextLineCodecFactory. This constructor
082: * provides more flexibility for the developer.
083: *
084: * @param charset
085: * The charset to use in the encoding and decoding
086: * @param encodingDelimiter
087: * The line delimeter for the encoder
088: * @param decodingDelimiter
089: * The line delimeter for the decoder
090: */
091: public TextLineCodecFactory(Charset charset,
092: LineDelimiter encodingDelimiter,
093: LineDelimiter decodingDelimiter) {
094: encoder = new TextLineEncoder(charset, encodingDelimiter);
095: decoder = new TextLineDecoder(charset, decodingDelimiter);
096: }
097:
098: public ProtocolEncoder getEncoder(IoSession session) {
099: return encoder;
100: }
101:
102: public ProtocolDecoder getDecoder(IoSession session) {
103: return decoder;
104: }
105:
106: /**
107: * Returns the allowed maximum size of the encoded line.
108: * If the size of the encoded line exceeds this value, the encoder
109: * will throw a {@link IllegalArgumentException}. The default value
110: * is {@link Integer#MAX_VALUE}.
111: * <p>
112: * This method does the same job with {@link TextLineEncoder#getMaxLineLength()}.
113: */
114: public int getEncoderMaxLineLength() {
115: return encoder.getMaxLineLength();
116: }
117:
118: /**
119: * Sets the allowed maximum size of the encoded line.
120: * If the size of the encoded line exceeds this value, the encoder
121: * will throw a {@link IllegalArgumentException}. The default value
122: * is {@link Integer#MAX_VALUE}.
123: * <p>
124: * This method does the same job with {@link TextLineEncoder#setMaxLineLength(int)}.
125: */
126: public void setEncoderMaxLineLength(int maxLineLength) {
127: encoder.setMaxLineLength(maxLineLength);
128: }
129:
130: /**
131: * Returns the allowed maximum size of the line to be decoded.
132: * If the size of the line to be decoded exceeds this value, the
133: * decoder will throw a {@link BufferDataException}. The default
134: * value is <tt>1024</tt> (1KB).
135: * <p>
136: * This method does the same job with {@link TextLineDecoder#getMaxLineLength()}.
137: */
138: public int getDecoderMaxLineLength() {
139: return decoder.getMaxLineLength();
140: }
141:
142: /**
143: * Sets the allowed maximum size of the line to be decoded.
144: * If the size of the line to be decoded exceeds this value, the
145: * decoder will throw a {@link BufferDataException}. The default
146: * value is <tt>1024</tt> (1KB).
147: * <p>
148: * This method does the same job with {@link TextLineDecoder#setMaxLineLength(int)}.
149: */
150: public void setDecoderMaxLineLength(int maxLineLength) {
151: decoder.setMaxLineLength(maxLineLength);
152: }
153: }
|