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