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