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