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.LdapConstants;
031import org.apache.directory.api.ldap.model.message.ModifyResponse;
032
033
034/**
035 * A decorator for the ModifyResponse message
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class ModifyResponseDecorator extends ResponseDecorator<ModifyResponse>
040    implements ModifyResponse
041{
042    /** The encoded modifyResponse length */
043    private int modifyResponseLength;
044
045
046    /**
047     * Makes a ModifyResponse encodable.
048     *
049     * @param decoratedMessage the decorated ModifyResponse
050     */
051    public ModifyResponseDecorator( LdapApiService codec, ModifyResponse decoratedMessage )
052    {
053        super( codec, decoratedMessage );
054    }
055
056
057    /**
058     * Stores the encoded length for the ModifyResponse
059     * @param modifyResponseLength The encoded length
060     */
061    public void setModifyResponseLength( int modifyResponseLength )
062    {
063        this.modifyResponseLength = modifyResponseLength;
064    }
065
066
067    /**
068     * @return The encoded ModifyResponse's length
069     */
070    public int getModifyResponseLength()
071    {
072        return modifyResponseLength;
073    }
074
075
076    //-------------------------------------------------------------------------
077    // The Decorator methods
078    //-------------------------------------------------------------------------
079
080    /**
081     * Compute the ModifyResponse length 
082     * 
083     * ModifyResponse : 
084     * <pre>
085     * 0x67 L1 
086     *   | 
087     *   +--> LdapResult 
088     *   
089     * L1 = Length(LdapResult) 
090     * Length(ModifyResponse) = Length(0x67) + Length(L1) + L1
091     * </pre>
092     */
093    public int computeLength()
094    {
095        int modifyResponseLength = ( ( LdapResultDecorator ) getLdapResult() ).computeLength();
096
097        setModifyResponseLength( modifyResponseLength );
098
099        return 1 + TLV.getNbBytes( modifyResponseLength ) + modifyResponseLength;
100    }
101
102
103    /**
104     * Encode the ModifyResponse message to a PDU.
105     * 
106     * @param buffer The buffer where to put the PDU
107     */
108    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
109    {
110        try
111        {
112            // The ModifyResponse Tag
113            buffer.put( LdapConstants.MODIFY_RESPONSE_TAG );
114            buffer.put( TLV.getBytes( getModifyResponseLength() ) );
115
116            // The LdapResult
117            ( ( LdapResultDecorator ) getLdapResult() ).encode( buffer );
118        }
119        catch ( BufferOverflowException boe )
120        {
121            throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
122        }
123
124        return buffer;
125    }
126}