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 */
020package org.apache.mina.filter.codec.textline;
021
022import java.nio.charset.Charset;
023
024import org.apache.mina.core.buffer.BufferDataException;
025import org.apache.mina.core.session.IoSession;
026import org.apache.mina.filter.codec.ProtocolCodecFactory;
027import org.apache.mina.filter.codec.ProtocolDecoder;
028import 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 <a href="http://mina.apache.org">Apache MINA Project</a>
036 */
037public class TextLineCodecFactory implements ProtocolCodecFactory {
038
039    private final TextLineEncoder encoder;
040
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 The charset to use in the encoding and decoding
056     */
057    public TextLineCodecFactory(Charset charset) {
058        encoder = new TextLineEncoder(charset, LineDelimiter.UNIX);
059        decoder = new TextLineDecoder(charset, LineDelimiter.AUTO);
060    }
061
062    /**
063     * Creates a new instance of TextLineCodecFactory.  This constructor
064     * provides more flexibility for the developer.
065     *
066     * @param charset
067     *  The charset to use in the encoding and decoding
068     * @param encodingDelimiter
069     *  The line delimeter for the encoder
070     * @param decodingDelimiter
071     *  The line delimeter for the decoder
072     */
073    public TextLineCodecFactory(Charset charset, String encodingDelimiter, String decodingDelimiter) {
074        encoder = new TextLineEncoder(charset, encodingDelimiter);
075        decoder = new TextLineDecoder(charset, decodingDelimiter);
076    }
077
078    /**
079     * Creates a new instance of TextLineCodecFactory.  This constructor
080     * provides more flexibility for the developer.
081     *
082     * @param charset
083     *  The charset to use in the encoding and decoding
084     * @param encodingDelimiter
085     *  The line delimeter for the encoder
086     * @param decodingDelimiter
087     *  The line delimeter for the decoder
088     */
089    public TextLineCodecFactory(Charset charset, LineDelimiter encodingDelimiter, LineDelimiter decodingDelimiter) {
090        encoder = new TextLineEncoder(charset, encodingDelimiter);
091        decoder = new TextLineDecoder(charset, decodingDelimiter);
092    }
093
094    /**
095     * {@inheritDoc}
096     */
097    public ProtocolEncoder getEncoder(IoSession session) {
098        return encoder;
099    }
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}