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.shared.ldap.codec.decorators;
021
022
023import java.nio.BufferOverflowException;
024import java.nio.ByteBuffer;
025
026import org.apache.directory.shared.asn1.EncoderException;
027import org.apache.directory.shared.asn1.ber.tlv.TLV;
028import org.apache.directory.shared.i18n.I18n;
029import org.apache.directory.shared.ldap.codec.api.LdapApiService;
030import org.apache.directory.shared.ldap.codec.api.LdapConstants;
031import org.apache.directory.shared.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 decoratedMessage the decorated ModifyDnResponse
050     */
051    public ModifyDnResponseDecorator( LdapApiService codec, ModifyDnResponse decoratedMessage )
052    {
053        super( codec, decoratedMessage );
054    }
055
056
057    /**
058     * @param modifyDnResponseLength The encoded ModifyDnResponse's length
059     */
060    public void setModifyDnResponseLength( int modifyDnResponseLength )
061    {
062        this.modifyDnResponseLength = modifyDnResponseLength;
063    }
064
065
066    /**
067     * Stores the encoded length for the ModifyDnResponse
068     * @return The encoded length
069     */
070    public int getModifyDnResponseLength()
071    {
072        return modifyDnResponseLength;
073    }
074
075
076    //-------------------------------------------------------------------------
077    // The Decorator methods
078    //-------------------------------------------------------------------------
079    /**
080     * Compute the ModifyDNResponse length 
081     * 
082     * ModifyDNResponse : 
083     * <pre>
084     * 0x6D L1 
085     *   | 
086     *   +--> LdapResult 
087     *   
088     * L1 = Length(LdapResult) 
089     * Length(ModifyDNResponse) = Length(0x6D) + Length(L1) + L1
090     * </pre>
091     */
092    public int computeLength()
093    {
094        int modifyDnResponseLength = ((LdapResultDecorator)getLdapResult()).computeLength();
095
096        setModifyDnResponseLength( modifyDnResponseLength );
097
098        return 1 + TLV.getNbBytes( modifyDnResponseLength ) + modifyDnResponseLength;
099    }
100    
101    
102    /**
103     * Encode the ModifyDnResponse message to a PDU.
104     * 
105     * @param buffer The buffer where to put the PDU
106     */
107    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
108    {
109        try
110        {
111            // The ModifyResponse Tag
112            buffer.put( LdapConstants.MODIFY_DN_RESPONSE_TAG );
113            buffer.put( TLV.getBytes( getModifyDnResponseLength() ) );
114
115            // The LdapResult
116            ((LdapResultDecorator)getLdapResult()).encode( buffer );
117        }
118        catch ( BufferOverflowException boe )
119        {
120            throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
121        }
122        
123        return buffer;
124    }
125}