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.api;
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.decorators.LdapResultDecorator;
030import org.apache.directory.api.ldap.codec.decorators.ResponseDecorator;
031import org.apache.directory.api.ldap.model.message.ExtendedResponse;
032import org.apache.directory.api.util.Strings;
033
034
035/**
036 * A decorator for the ExtendedResponse message
037 *
038 * @param <R> The extended response to decorate
039 * 
040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041 */
042public class ExtendedResponseDecorator<R extends ExtendedResponse> extends ResponseDecorator<R>
043    implements ExtendedResponse
044{
045    /** The response name (OID) as a byte[] */
046    private byte[] responseNameBytes;
047
048    /** The encoded extendedResponse length */
049    private int extendedResponseLength;
050
051    /** The response value */
052    protected byte[] responseValue;
053
054
055    /**
056     * Makes a ExtendedResponse encodable.
057     *
058     * @param codec The LDAP service instance
059     * @param decoratedMessage the decorated ExtendedResponse
060     */
061    public ExtendedResponseDecorator( LdapApiService codec, R decoratedMessage )
062    {
063        super( codec, decoratedMessage );
064    }
065
066
067    //-------------------------------------------------------------------------
068    // The ExtendedResponse methods
069    //-------------------------------------------------------------------------
070
071    /**
072     * {@inheritDoc}
073     */
074    @Override
075    public String getResponseName()
076    {
077        return getDecorated().getResponseName();
078    }
079
080
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public void setResponseName( String oid )
086    {
087        getDecorated().setResponseName( oid );
088    }
089
090
091    /**
092     * Gets the Extended response payload 
093     * 
094     * @return The extended payload
095     */
096    public byte[] getResponseValue()
097    {
098        return responseValue;
099    }
100
101
102    /**
103     * sets the Extended response payload 
104     * 
105     * @param responseValue The extended payload
106     */
107    public void setResponseValue( byte[] responseValue )
108    {
109        this.responseValue = responseValue;
110    }
111
112
113    //-------------------------------------------------------------------------
114    // The Decorator methods
115    //-------------------------------------------------------------------------
116    /**
117     * Compute the ExtendedResponse length
118     * <br>
119     * ExtendedResponse :
120     * <pre>
121     * 0x78 L1
122     *  |
123     *  +--&gt; LdapResult
124     * [+--&gt; 0x8A L2 name
125     * [+--&gt; 0x8B L3 response]]
126     * 
127     * L1 = Length(LdapResult)
128     *      [ + Length(0x8A) + Length(L2) + L2
129     *       [ + Length(0x8B) + Length(L3) + L3]]
130     * 
131     * Length(ExtendedResponse) = Length(0x78) + Length(L1) + L1
132     * </pre>
133     * 
134     * @return The ExtendedResponse length
135     */
136    @Override
137    public int computeLength()
138    {
139        int ldapResultLength = ( ( LdapResultDecorator ) getLdapResult() ).computeLength();
140
141        extendedResponseLength = ldapResultLength;
142
143        String id = getResponseName();
144
145        if ( !Strings.isEmpty( id ) )
146        {
147            responseNameBytes = Strings.getBytesUtf8( id );
148            int idLength = responseNameBytes.length;
149            extendedResponseLength += 1 + TLV.getNbBytes( idLength ) + idLength;
150        }
151
152        byte[] encodedValue = getResponseValue();
153
154        if ( encodedValue != null )
155        {
156            extendedResponseLength += 1 + TLV.getNbBytes( encodedValue.length ) + encodedValue.length;
157        }
158
159        return 1 + TLV.getNbBytes( extendedResponseLength ) + extendedResponseLength;
160    }
161
162
163    /**
164     * Encode the ExtendedResponse message to a PDU. 
165     * <br>
166     * ExtendedResponse :
167     * <pre>
168     * LdapResult.encode()
169     * [0x8A LL response name]
170     * [0x8B LL response]
171     * </pre>
172     * 
173     * @param buffer The buffer where to put the PDU
174     * @return The PDU.
175     */
176    @Override
177    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
178    {
179        try
180        {
181            // The ExtendedResponse Tag
182            buffer.put( LdapCodecConstants.EXTENDED_RESPONSE_TAG );
183            buffer.put( TLV.getBytes( extendedResponseLength ) );
184
185            // The LdapResult
186            ( ( LdapResultDecorator ) getLdapResult() ).encode( buffer );
187
188            // The ID, if any
189            if ( responseNameBytes != null )
190            {
191                buffer.put( ( byte ) LdapCodecConstants.EXTENDED_RESPONSE_RESPONSE_NAME_TAG );
192                buffer.put( TLV.getBytes( responseNameBytes.length ) );
193
194                if ( responseNameBytes.length != 0 )
195                {
196                    buffer.put( responseNameBytes );
197                }
198            }
199
200            // The encodedValue, if any
201            byte[] encodedValue = getResponseValue();
202
203            if ( encodedValue != null )
204            {
205                buffer.put( ( byte ) LdapCodecConstants.EXTENDED_RESPONSE_RESPONSE_TAG );
206
207                buffer.put( TLV.getBytes( encodedValue.length ) );
208
209                if ( encodedValue.length != 0 )
210                {
211                    buffer.put( encodedValue );
212                }
213            }
214        }
215        catch ( BufferOverflowException boe )
216        {
217            throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
218        }
219
220        return buffer;
221    }
222}