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
21 package org.apache.directory.server.changepw.protocol;
22
23
24 import org.apache.directory.server.changepw.io.ChangePasswordRequestDecoder;
25 import org.apache.mina.common.BufferDataException;
26 import org.apache.mina.common.ByteBuffer;
27 import org.apache.mina.common.IoSession;
28 import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
29 import org.apache.mina.filter.codec.ProtocolDecoderOutput;
30
31
32 /**
33 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34 * @version $Rev: 549315 $, $Date: 2007-06-20 18:13:53 -0700 (Wed, 20 Jun 2007) $
35 */
36 public class ChangePasswordTcpDecoder extends CumulativeProtocolDecoder
37 {
38 private ChangePasswordRequestDecoder decoder = new ChangePasswordRequestDecoder();
39
40 private int maxObjectSize = 16384; // 16KB
41
42
43 /**
44 * Returns the allowed maximum size of the object to be decoded.
45 * If the size of the object to be decoded exceeds this value, this
46 * decoder will throw a {@link BufferDataException}. The default
47 * value is <tt>16384</tt> (16KB).
48 *
49 * @return The max object size.
50 */
51 public int getMaxObjectSize()
52 {
53 return maxObjectSize;
54 }
55
56
57 /**
58 * Sets the allowed maximum size of the object to be decoded.
59 * If the size of the object to be decoded exceeds this value, this
60 * decoder will throw a {@link BufferDataException}. The default
61 * value is <tt>16384</tt> (16KB).
62 *
63 * @param maxObjectSize
64 */
65 public void setMaxObjectSize( int maxObjectSize )
66 {
67 if ( maxObjectSize <= 0 )
68 {
69 throw new IllegalArgumentException( "maxObjectSize: " + maxObjectSize );
70 }
71
72 this.maxObjectSize = maxObjectSize;
73 }
74
75
76 @Override
77 protected boolean doDecode( IoSession session, ByteBuffer in, ProtocolDecoderOutput out ) throws Exception
78 {
79 if ( !in.prefixedDataAvailable( 4, maxObjectSize ) )
80 {
81 return false;
82 }
83
84 in.getInt();
85
86 out.write( decoder.decode( in.buf() ) );
87
88 return true;
89 }
90 }