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