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       * Returns the allowed maximum size of a decoded string.
    81       * <p>
    82       * This method does the same job as {@link PrefixedStringEncoder#setMaxDataLength(int)}.
    83       * </p>
    84       *
    85       * @return the allowed maximum size of an encoded string.
    86       * @see #setDecoderMaxDataLength(int)
    87       */
    88      public int getDecoderMaxDataLength() {
    89          return decoder.getMaxDataLength();
    90      }
    91
    92      /**
    93       * Sets the maximum allowed value specified as data length in the decoded data
    94       * <p>
    95       * Useful for preventing an OutOfMemory attack by the peer.
    96       * The decoder will throw a {@link BufferDataException} when data length
    97       * specified in the incoming data is greater than maxDataLength
    98       * The default value is {@link PrefixedStringDecoder#DEFAULT_MAX_DATA_LENGTH}.
    99       *
    100      * This method does the same job as {@link PrefixedStringDecoder#setMaxDataLength(int)}.
    101      * </p>
    102      *
    103      * @param maxDataLength maximum allowed value specified as data length in the incoming data
    104      */
    105     public void setDecoderMaxDataLength(int maxDataLength) {
    106         decoder.setMaxDataLength(maxDataLength);
    107     }
    108
    109     /**
    110      * Sets the length of the prefix used by the decoder
    111      *
    112      * @param prefixLength the length of the length prefix (1, 2, or 4)
    113      */
    114     public void setDecoderPrefixLength(int prefixLength) {
    115         decoder.setPrefixLength(prefixLength);
    116     }
    117
    118     /**
    119      * Gets the length of the length prefix (1, 2, or 4) used by the decoder
    120      *
    121      * @return length of the length prefix
    122      */
    123     public int getDecoderPrefixLength() {
    124         return decoder.getPrefixLength();
    125     }
    126
    127     /**
    128      * Sets the length of the prefix used by the encoder
    129      *
    130      * @param prefixLength the length of the length prefix (1, 2, or 4)
    131      */
    132     public void setEncoderPrefixLength(int prefixLength) {
    133         encoder.setPrefixLength(prefixLength);
    134     }
    135
    136     /**
    137      * Gets the length of the length prefix (1, 2, or 4) used by the encoder
    138      *
    139      * @return length of the length prefix
    140      */
    141     public int getEncoderPrefixLength() {
    142         return encoder.getPrefixLength();
    143     }
    144
    145     public ProtocolEncoder getEncoder(IoSession session) throws Exception {
    146         return encoder;
    147     }
    148
    149     public ProtocolDecoder getDecoder(IoSession session) throws Exception {
    150         return decoder;
    151     }
    152 }