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