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