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.ModifyDnResponse;
032
033
034/**
035 * A decorator for the ModifyDnResponse message
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class ModifyDnResponseDecorator extends ResponseDecorator<ModifyDnResponse>
040    implements ModifyDnResponse
041{
042    /** The encoded modifyDnResponse length */
043    private int modifyDnResponseLength;
044
045
046    /**
047     * Makes a ModifyDnResponse encodable.
048     *
049     * @param codec The LDAP service instance
050     * @param decoratedMessage the decorated ModifyDnResponse
051     */
052    public ModifyDnResponseDecorator( LdapApiService codec, ModifyDnResponse decoratedMessage )
053    {
054        super( codec, decoratedMessage );
055    }
056
057
058    //-------------------------------------------------------------------------
059    // The Decorator methods
060    //-------------------------------------------------------------------------
061    /**
062     * Compute the ModifyDNResponse length 
063     * <br>
064     * ModifyDNResponse : 
065     * <pre>
066     * 0x6D L1 
067     *   | 
068     *   +--&gt; LdapResult 
069     *   
070     * L1 = Length(LdapResult) 
071     * Length(ModifyDNResponse) = Length(0x6D) + Length(L1) + L1
072     * </pre>
073     */
074    @Override
075    public int computeLength()
076    {
077        modifyDnResponseLength = ( ( LdapResultDecorator ) getLdapResult() ).computeLength();
078
079        return 1 + TLV.getNbBytes( modifyDnResponseLength ) + modifyDnResponseLength;
080    }
081
082
083    /**
084     * Encode the ModifyDnResponse message to a PDU.
085     * 
086     * @param buffer The buffer where to put the PDU
087     */
088    @Override
089    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
090    {
091        try
092        {
093            // The ModifyResponse Tag
094            buffer.put( LdapCodecConstants.MODIFY_DN_RESPONSE_TAG );
095            buffer.put( TLV.getBytes( modifyDnResponseLength ) );
096
097            // The LdapResult
098            ( ( LdapResultDecorator ) getLdapResult() ).encode( buffer );
099        }
100        catch ( BufferOverflowException boe )
101        {
102            throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
103        }
104
105        return buffer;
106    }
107}