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 The Apache MINA Project (dev@mina.apache.org) 35 * @version $Rev: 596187 $, $Date: 2007-11-19 04:41:14 +0100 (ma, 19 nov 2007) $ 36 */ 37 public class PrefixedStringCodecFactory implements ProtocolCodecFactory { 38 39 private final PrefixedStringEncoder encoder; 40 41 private final PrefixedStringDecoder decoder; 42 43 public PrefixedStringCodecFactory(Charset charset) { 44 encoder = new PrefixedStringEncoder(charset); 45 decoder = new PrefixedStringDecoder(charset); 46 } 47 48 public PrefixedStringCodecFactory() { 49 this(Charset.defaultCharset()); 50 } 51 52 /** 53 * Returns the allowed maximum size of an encoded string. 54 * If the size of the encoded String exceeds this value, the encoder 55 * will throw a {@link IllegalArgumentException}. 56 * The default value is {@link PrefixedStringEncoder#DEFAULT_MAX_DATA_LENGTH}. 57 * <p/> 58 * This method does the same job as {@link PrefixedStringEncoder#setMaxDataLength(int)}. 59 * 60 * @return the allowed maximum size of an encoded string. 61 */ 62 public int getEncoderMaxDataLength() { 63 return encoder.getMaxDataLength(); 64 } 65 66 /** 67 * Sets the allowed maximum size of an encoded String. 68 * If the size of the encoded String exceeds this value, the encoder 69 * will throw a {@link IllegalArgumentException}. 70 * The default value is {@link PrefixedStringEncoder#DEFAULT_MAX_DATA_LENGTH}. 71 * <p/> 72 * This method does the same job as {@link PrefixedStringEncoder#getMaxDataLength()}. 73 * 74 * @param maxDataLength allowed maximum size of an encoded String. 75 */ 76 public void setEncoderMaxDataLength(int maxDataLength) { 77 encoder.setMaxDataLength(maxDataLength); 78 } 79 80 /** 81 * Returns the allowed maximum size of a decoded string. 82 * <p> 83 * This method does the same job as {@link PrefixedStringEncoder#setMaxDataLength(int)}. 84 * </p> 85 * 86 * @return the allowed maximum size of an encoded string. 87 * @see #setDecoderMaxDataLength(int) 88 */ 89 public int getDecoderMaxDataLength() { 90 return decoder.getMaxDataLength(); 91 } 92 93 /** 94 * Sets the maximum allowed value specified as data length in the decoded data 95 * <p> 96 * Useful for preventing an OutOfMemory attack by the peer. 97 * The decoder will throw a {@link BufferDataException} when data length 98 * specified in the incoming data is greater than maxDataLength 99 * The default value is {@link PrefixedStringDecoder#DEFAULT_MAX_DATA_LENGTH}. 100 * 101 * This method does the same job as {@link PrefixedStringDecoder#setMaxDataLength(int)}. 102 * </p> 103 * 104 * @param maxDataLength maximum allowed value specified as data length in the incoming data 105 */ 106 public void setDecoderMaxDataLength(int maxDataLength) { 107 decoder.setMaxDataLength(maxDataLength); 108 } 109 110 /** 111 * Sets the length of the prefix used by the decoder 112 * 113 * @param prefixLength the length of the length prefix (1, 2, or 4) 114 */ 115 public void setDecoderPrefixLength(int prefixLength) { 116 decoder.setPrefixLength(prefixLength); 117 } 118 119 /** 120 * Gets the length of the length prefix (1, 2, or 4) used by the decoder 121 * 122 * @return length of the length prefix 123 */ 124 public int getDecoderPrefixLength() { 125 return decoder.getPrefixLength(); 126 } 127 128 /** 129 * Sets the length of the prefix used by the encoder 130 * 131 * @param prefixLength the length of the length prefix (1, 2, or 4) 132 */ 133 public void setEncoderPrefixLength(int prefixLength) { 134 encoder.setPrefixLength(prefixLength); 135 } 136 137 /** 138 * Gets the length of the length prefix (1, 2, or 4) used by the encoder 139 * 140 * @return length of the length prefix 141 */ 142 public int getEncoderPrefixLength() { 143 return encoder.getPrefixLength(); 144 } 145 146 public ProtocolEncoder getEncoder(IoSession session) throws Exception { 147 return encoder; 148 } 149 150 public ProtocolDecoder getDecoder(IoSession session) throws Exception { 151 return decoder; 152 } 153 }