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.del;
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 DelResponse Message. Its syntax is : 
034     * 
035     * DelResponse ::= [APPLICATION 11] 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 DelResponseCodec extends LdapResponseCodec
041    {
042        // ~ Constructors
043        // -------------------------------------------------------------------------------
044    
045        /**
046         * Creates a new DelResponse object.
047         */
048        public DelResponseCodec()
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.DEL_RESPONSE;
062        }
063    
064    
065        /**
066         * Compute the DelResponse length 
067         * 
068         * DelResponse :
069         * 
070         * 0x6B L1
071         *  |
072         *  +--> LdapResult
073         * 
074         * L1 = Length(LdapResult)
075         * 
076         * Length(DelResponse) = Length(0x6B) + Length(L1) + L1
077         */
078        public int computeLength()
079        {
080            int ldapResponseLength = super.computeLength();
081    
082            return 1 + TLV.getNbBytes( ldapResponseLength ) + ldapResponseLength;
083        }
084    
085    
086        /**
087         * Encode the DelResponse message to a PDU.
088         * 
089         * @param buffer The buffer where to put the PDU
090         * @return The PDU.
091         */
092        public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
093        {
094            if ( buffer == null )
095            {
096                throw new EncoderException( "Cannot put a PDU in a null buffer !" );
097            }
098    
099            try
100            {
101                // The tag
102                buffer.put( LdapConstants.DEL_RESPONSE_TAG );
103                buffer.put( TLV.getBytes( getLdapResponseLength() ) );
104            }
105            catch ( BufferOverflowException boe )
106            {
107                throw new EncoderException( "The PDU buffer size is too small !" );
108            }
109    
110            // The ldapResult
111            return super.encode( buffer );
112        }
113    
114    
115        /**
116         * Get a String representation of a DelResponse
117         * 
118         * @return A DelResponse String
119         */
120        public String toString()
121        {
122    
123            StringBuffer sb = new StringBuffer();
124    
125            sb.append( "    Del Response\n" );
126            sb.append( super.toString() );
127    
128            return sb.toString();
129        }
130    }