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     */
020    package org.apache.directory.server.kerberos.protocol;
021    
022    
023    import org.apache.directory.server.i18n.I18n;
024    import org.apache.directory.server.kerberos.shared.io.decoder.KdcRequestDecoder;
025    import org.apache.mina.core.buffer.IoBuffer;
026    import org.apache.mina.core.session.IoSession;
027    import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
028    import org.apache.mina.filter.codec.ProtocolDecoderOutput;
029    
030    
031    /**
032     * A {@link CumulativeProtocolDecoder} which supports Kerberos operation over TCP,
033     * by reassembling split packets prior to ASN.1 DER decoding.
034     * 
035     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036     * @version $Rev$, $Date$
037     */
038    public class KerberosTcpDecoder extends CumulativeProtocolDecoder
039    {
040        private KdcRequestDecoder decoder = new KdcRequestDecoder();
041    
042        private int maxObjectSize = 16384; // 16KB
043    
044    
045        /**
046         * Returns the allowed maximum size of the object to be decoded.
047         * If the size of the object to be decoded exceeds this value, this
048         * decoder will throw a {@link BufferDataException}.  The default
049         * value is <tt>16384</tt> (16KB).
050         * 
051         * @return The max object size.
052         */
053        public int getMaxObjectSize()
054        {
055            return maxObjectSize;
056        }
057    
058    
059        /**
060         * Sets the allowed maximum size of the object to be decoded.
061         * If the size of the object to be decoded exceeds this value, this
062         * decoder will throw a {@link BufferDataException}.  The default
063         * value is <tt>16384</tt> (16KB).
064         * 
065         * @param maxObjectSize 
066         */
067        public void setMaxObjectSize( int maxObjectSize )
068        {
069            if ( maxObjectSize <= 0 )
070            {
071                throw new IllegalArgumentException( I18n.err( I18n.ERR_634, maxObjectSize ) );
072            }
073    
074            this.maxObjectSize = maxObjectSize;
075        }
076    
077    
078        @Override
079        protected boolean doDecode( IoSession session, IoBuffer in, ProtocolDecoderOutput out ) throws Exception
080        {
081            if ( !in.prefixedDataAvailable( 4, maxObjectSize ) )
082            {
083                return false;
084            }
085    
086            in.getInt();
087    
088            out.write( decoder.decode( in.buf() ) );
089    
090            return true;
091        }
092    }