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