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 */
020package org.apache.directory.api.ldap.codec.decorators;
021
022
023import java.nio.BufferOverflowException;
024import java.nio.ByteBuffer;
025
026import org.apache.directory.api.asn1.EncoderException;
027import org.apache.directory.api.asn1.ber.tlv.TLV;
028import org.apache.directory.api.i18n.I18n;
029import org.apache.directory.api.ldap.codec.api.LdapApiService;
030import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
031import org.apache.directory.api.ldap.model.message.AddResponse;
032
033
034/**
035 * A decorator for the AddResponse message
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class AddResponseDecorator extends ResponseDecorator<AddResponse> implements AddResponse
040{
041    /** The encoded addResponse length */
042    private int addResponseLength;
043
044
045    /**
046     * Makes a AddResponse a MessageDecorator.
047     *
048     * @param codec The LDAP service instance
049     * @param decoratedMessage the decorated AddResponse
050     */
051    public AddResponseDecorator( LdapApiService codec, AddResponse decoratedMessage )
052    {
053        super( codec, decoratedMessage );
054    }
055
056
057    /**
058     * @return The decorated AddResponse
059     */
060    public AddResponse getAddResponse()
061    {
062        return getDecorated();
063    }
064
065
066    //-------------------------------------------------------------------------
067    // The Decorator methods
068    //-------------------------------------------------------------------------
069    /**
070     * Compute the AddResponse length 
071     * <br>
072     * AddResponse : 
073     * <pre>
074     * 0x69 L1
075     *  |
076     *  +--&gt; LdapResult
077     * 
078     * L1 = Length(LdapResult)
079     * 
080     * Length(AddResponse) = Length(0x69) + Length(L1) + L1
081     * </pre>
082     */
083    @Override
084    public int computeLength()
085    {
086        AddResponse addResponse = getAddResponse();
087        setLdapResult( new LdapResultDecorator( getCodecService(), addResponse.getLdapResult() ) );
088        addResponseLength = ( ( LdapResultDecorator ) getLdapResult() ).computeLength();
089
090        return 1 + TLV.getNbBytes( addResponseLength ) + addResponseLength;
091    }
092
093
094    /**
095     * Encode the AddResponse message to a PDU.
096     * 
097     * @param buffer The buffer where to put the PDU
098     * @return The encoded response
099     * @throws EncoderException If teh encoding failed
100     */
101    @Override
102    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
103    {
104        try
105        {
106            // The AddResponse Tag
107            buffer.put( LdapCodecConstants.ADD_RESPONSE_TAG );
108            buffer.put( TLV.getBytes( addResponseLength ) );
109
110            // The LdapResult
111            ( ( LdapResultDecorator ) getLdapResult() ).encode( buffer );
112
113            return buffer;
114        }
115        catch ( BufferOverflowException boe )
116        {
117            throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
118        }
119    }
120}