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.shared.ldap.codec.modifyDn;
021    
022    
023    import java.nio.BufferOverflowException;
024    import java.nio.ByteBuffer;
025    
026    import org.apache.directory.shared.asn1.ber.tlv.TLV;
027    import org.apache.directory.shared.asn1.codec.EncoderException;
028    import org.apache.directory.shared.ldap.codec.LdapConstants;
029    import org.apache.directory.shared.ldap.codec.LdapResponseCodec;
030    
031    
032    /**
033     * An ModifyDNResponse Message. Its syntax is : 
034     * 
035     * ModifyDNResponse ::= [APPLICATION 13] LDAPResult
036     * 
037     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038     * @version $Rev: 764131 $, $Date: 2009-04-11 03:03:00 +0200 (Sam, 11 avr 2009) $, 
039     */
040    public class ModifyDNResponseCodec extends LdapResponseCodec
041    {
042        // ~ Constructors
043        // -------------------------------------------------------------------------------
044    
045        /**
046         * Creates a new ModifyDNResponse object.
047         */
048        public ModifyDNResponseCodec()
049        {
050            super();
051        }
052    
053    
054        /**
055         * Get the message type
056         * 
057         * @return Returns the type.
058         */
059        public int getMessageType()
060        {
061            return LdapConstants.MODIFYDN_RESPONSE;
062        }
063    
064    
065        /**
066         * Compute the ModifyDNResponse length 
067         * 
068         * ModifyDNResponse : 
069         * 0x6D L1 
070         *   | 
071         *   +--> LdapResult 
072         *   
073         * L1 = Length(LdapResult) 
074         * Length(ModifyDNResponse) = Length(0x6D) + Length(L1) + L1
075         */
076        public int computeLength()
077        {
078            int ldapResponseLength = super.computeLength();
079    
080            return 1 + TLV.getNbBytes( ldapResponseLength ) + ldapResponseLength;
081        }
082    
083    
084        /**
085         * Encode the ModifyDNResponse message to a PDU.
086         * 
087         * @param buffer The buffer where to put the PDU
088         * @return The PDU.
089         */
090        public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
091        {
092            if ( buffer == null )
093            {
094                throw new EncoderException( "Cannot put a PDU in a null buffer !" );
095            }
096    
097            try
098            {
099                // The tag
100                buffer.put( LdapConstants.MODIFY_DN_RESPONSE_TAG );
101                buffer.put( TLV.getBytes( getLdapResponseLength() ) );
102            }
103            catch ( BufferOverflowException boe )
104            {
105                throw new EncoderException( "The PDU buffer size is too small !" );
106            }
107    
108            // The ldapResult
109            return super.encode( buffer );
110        }
111    
112    
113        /**
114         * Get a String representation of a ModifyDNResponse
115         * 
116         * @return A ModifyDNResponse String
117         */
118        public String toString()
119        {
120    
121            StringBuffer sb = new StringBuffer();
122    
123            sb.append( "    Modify DN Response\n" );
124            sb.append( super.toString() );
125    
126            return sb.toString();
127        }
128    }