001: package org.apache.mina.filter.codec.prefixedstring;
002:
003: import org.apache.mina.common.IoSession;
004: import org.apache.mina.common.BufferDataException;
005: import org.apache.mina.filter.codec.ProtocolCodecFactory;
006: import org.apache.mina.filter.codec.ProtocolDecoder;
007: import org.apache.mina.filter.codec.ProtocolEncoder;
008:
009: import java.nio.charset.Charset;
010:
011: /**
012: * A {@link ProtocolCodecFactory} that performs encoding and decoding
013: * of a Java String object using a fixed-length length prefix.
014: *
015: * @author The Apache MINA Project (dev@mina.apache.org)
016: * @version $Rev: 596187 $, $Date: 2007-11-19 04:41:14 +0100 (ma, 19 nov 2007) $
017: */
018: public class PrefixedStringCodecFactory implements ProtocolCodecFactory {
019:
020: private final PrefixedStringEncoder encoder;
021:
022: private final PrefixedStringDecoder decoder;
023:
024: public PrefixedStringCodecFactory(Charset charset) {
025: encoder = new PrefixedStringEncoder(charset);
026: decoder = new PrefixedStringDecoder(charset);
027: }
028:
029: public PrefixedStringCodecFactory() {
030: this (Charset.defaultCharset());
031: }
032:
033: /**
034: * Returns the allowed maximum size of an encoded string.
035: * If the size of the encoded String exceeds this value, the encoder
036: * will throw a {@link IllegalArgumentException}.
037: * The default value is {@link PrefixedStringEncoder#DEFAULT_MAX_DATA_LENGTH}.
038: * <p/>
039: * This method does the same job as {@link PrefixedStringEncoder#setMaxDataLength(int)}.
040: *
041: * @return the allowed maximum size of an encoded string.
042: */
043: public int getEncoderMaxDataLength() {
044: return encoder.getMaxDataLength();
045: }
046:
047: /**
048: * Sets the allowed maximum size of an encoded String.
049: * If the size of the encoded String exceeds this value, the encoder
050: * will throw a {@link IllegalArgumentException}.
051: * The default value is {@link PrefixedStringEncoder#DEFAULT_MAX_DATA_LENGTH}.
052: * <p/>
053: * This method does the same job as {@link PrefixedStringEncoder#getMaxDataLength()}.
054: *
055: * @param maxDataLength allowed maximum size of an encoded String.
056: */
057: public void setEncoderMaxDataLength(int maxDataLength) {
058: encoder.setMaxDataLength(maxDataLength);
059: }
060:
061: /**
062: * Returns the allowed maximum size of a decoded string.
063: * <p>
064: * This method does the same job as {@link PrefixedStringEncoder#setMaxDataLength(int)}.
065: * </p>
066: *
067: * @return the allowed maximum size of an encoded string.
068: * @see #setDecoderMaxDataLength(int)
069: */
070: public int getDecoderMaxDataLength() {
071: return decoder.getMaxDataLength();
072: }
073:
074: /**
075: * Sets the maximum allowed value specified as data length in the decoded data
076: * <p>
077: * Useful for preventing an OutOfMemory attack by the peer.
078: * The decoder will throw a {@link BufferDataException} when data length
079: * specified in the incoming data is greater than maxDataLength
080: * The default value is {@link PrefixedStringDecoder#DEFAULT_MAX_DATA_LENGTH}.
081: *
082: * This method does the same job as {@link PrefixedStringDecoder#setMaxDataLength(int)}.
083: * </p>
084: *
085: * @param maxDataLength maximum allowed value specified as data length in the incoming data
086: */
087: public void setDecoderMaxDataLength(int maxDataLength) {
088: decoder.setMaxDataLength(maxDataLength);
089: }
090:
091: /**
092: * Sets the length of the prefix used by the decoder
093: *
094: * @param prefixLength the length of the length prefix (1, 2, or 4)
095: */
096: public void setDecoderPrefixLength(int prefixLength) {
097: decoder.setPrefixLength(prefixLength);
098: }
099:
100: /**
101: * Gets the length of the length prefix (1, 2, or 4) used by the decoder
102: *
103: * @return length of the length prefix
104: */
105: public int getDecoderPrefixLength() {
106: return decoder.getPrefixLength();
107: }
108:
109: /**
110: * Sets the length of the prefix used by the encoder
111: *
112: * @param prefixLength the length of the length prefix (1, 2, or 4)
113: */
114: public void setEncoderPrefixLength(int prefixLength) {
115: encoder.setPrefixLength(prefixLength);
116: }
117:
118: /**
119: * Gets the length of the length prefix (1, 2, or 4) used by the encoder
120: *
121: * @return length of the length prefix
122: */
123: public int getEncoderPrefixLength() {
124: return encoder.getPrefixLength();
125: }
126:
127: public ProtocolEncoder getEncoder(IoSession session)
128: throws Exception {
129: return encoder;
130: }
131:
132: public ProtocolDecoder getDecoder(IoSession session)
133: throws Exception {
134: return decoder;
135: }
136: }
|