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.add;
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.i18n.I18n;
029    import org.apache.directory.shared.ldap.codec.LdapConstants;
030    import org.apache.directory.shared.ldap.codec.LdapResponseCodec;
031    import org.apache.directory.shared.ldap.codec.MessageTypeEnum;
032    
033    
034    /**
035     * An AddResponse Message. Its syntax is : 
036     * 
037     * AddResponse ::= [APPLICATION 9] LDAPResult
038     * 
039     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040     * @version $Rev: 912399 $, $Date: 2010-02-21 21:52:31 +0100 (Sun, 21 Feb 2010) $, 
041     */
042    public class AddResponseCodec extends LdapResponseCodec
043    {
044        // ~ Constructors
045        // -------------------------------------------------------------------------------
046    
047        /**
048         * Creates a new AddResponse object.
049         */
050        public AddResponseCodec()
051        {
052            super();
053        }
054    
055    
056        // ~ Methods
057        // ------------------------------------------------------------------------------------
058    
059        /**
060         * Get the message type
061         * 
062         * @return Returns the type.
063         */
064        public MessageTypeEnum getMessageType()
065        {
066            return MessageTypeEnum.ADD_RESPONSE;
067        }
068    
069        
070        /**
071         * {@inheritDoc}
072         */
073        public String getMessageTypeName()
074        {
075            return "ADD_RESPONSE";
076        }
077    
078    
079        /**
080         * Compute the AddResponse length 
081         * 
082         * AddResponse : 
083         * 
084         * 0x69 L1
085         *  |
086         *  +--> LdapResult
087         * 
088         * L1 = Length(LdapResult)
089         * 
090         * Length(AddResponse) = Length(0x69) + Length(L1) + L1
091         */
092        protected int computeLengthProtocolOp()
093        {
094            int ldapResultLength = super.computeLdapResultLength();
095    
096            return 1 + TLV.getNbBytes( ldapResultLength ) + ldapResultLength;
097        }
098    
099    
100        /**
101         * Encode the AddResponse message to a PDU.
102         * 
103         * @param buffer The buffer where to put the PDU
104         * @return The PDU.
105         */
106        protected void encodeProtocolOp( ByteBuffer buffer ) throws EncoderException
107        {
108            try
109            {
110                // The tag
111                buffer.put( LdapConstants.ADD_RESPONSE_TAG );
112                buffer.put( TLV.getBytes( getLdapResponseLength() ) );
113            }
114            catch ( BufferOverflowException boe )
115            {
116                throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
117            }
118    
119            // The ldapResult
120            super.encode( buffer );
121        }
122    
123    
124        /**
125         * Get a String representation of an AddResponse
126         * 
127         * @return An AddResponse String
128         */
129        public String toString()
130        {
131            return toString( "    Add Response" );
132        }
133    }