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.BindResponse;
032
033
034/**
035 * A decorator for the BindResponse message
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class BindResponseDecorator extends ResponseDecorator<BindResponse> implements BindResponse
040{
041    /** The encoded bindResponse length */
042    private int bindResponseLength;
043
044
045    /**
046     * Makes a BindResponse a MessageDecorator.
047     *
048     * @param decoratedMessage the decorated BindResponse
049     */
050    public BindResponseDecorator( LdapApiService codec, BindResponse decoratedMessage )
051    {
052        super( codec, decoratedMessage );
053    }
054
055
056    /**
057     * Stores the encoded length for the BindResponse
058     * @param bindResponseLength The encoded length
059     */
060    public void setBindResponseLength( int bindResponseLength )
061    {
062        this.bindResponseLength = bindResponseLength;
063    }
064
065
066    /**
067     * @return The encoded BindResponse's length
068     */
069    public int getBindResponseLength()
070    {
071        return bindResponseLength;
072    }
073
074
075    //-------------------------------------------------------------------------
076    // The BindResponse methods
077    //-------------------------------------------------------------------------
078
079    /**
080     * {@inheritDoc}
081     */
082    public byte[] getServerSaslCreds()
083    {
084        return getDecorated().getServerSaslCreds();
085    }
086
087
088    /**
089     * {@inheritDoc}
090     */
091    public void setServerSaslCreds( byte[] serverSaslCreds )
092    {
093        getDecorated().setServerSaslCreds( serverSaslCreds );
094    }
095
096
097    //-------------------------------------------------------------------------
098    // The Decorator methods
099    //-------------------------------------------------------------------------
100    /**
101     * Compute the BindResponse length 
102     * 
103     * BindResponse : 
104     * <pre>
105     * 0x61 L1 
106     *   | 
107     *   +--> LdapResult
108     *   +--> [serverSaslCreds] 
109     *   
110     * L1 = Length(LdapResult) [ + Length(serverSaslCreds) ] 
111     * Length(BindResponse) = Length(0x61) + Length(L1) + L1
112     * </pre>
113     */
114    public int computeLength()
115    {
116        BindResponse bindResponse = getDecorated();
117        int ldapResultLength = ( ( LdapResultDecorator ) getLdapResult() ).computeLength();
118
119        int bindResponseLength = ldapResultLength;
120
121        byte[] serverSaslCreds = bindResponse.getServerSaslCreds();
122
123        if ( serverSaslCreds != null )
124        {
125            bindResponseLength += 1 + TLV.getNbBytes( serverSaslCreds.length ) + serverSaslCreds.length;
126        }
127
128        setBindResponseLength( bindResponseLength );
129
130        return 1 + TLV.getNbBytes( bindResponseLength ) + bindResponseLength;
131    }
132
133
134    /**
135     * Encode the BindResponse message to a PDU.
136     * 
137     * BindResponse :
138     * <pre>
139     * LdapResult.encode 
140     * [0x87 LL serverSaslCreds]
141     * </pre>
142     * 
143     * @param bb The buffer where to put the PDU
144     * @param bindResponseDecorator The decorated BindResponse to encode
145     * @throws EncoderException when encoding operations fail
146     */
147    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
148    {
149        BindResponse bindResponse = getDecorated();
150
151        try
152        {
153            // The BindResponse Tag
154            buffer.put( LdapConstants.BIND_RESPONSE_TAG );
155            buffer.put( TLV.getBytes( getBindResponseLength() ) );
156
157            // The LdapResult
158            ( ( LdapResultDecorator ) getLdapResult() ).encode( buffer );
159
160            // The serverSaslCredential, if any
161            byte[] serverSaslCreds = bindResponse.getServerSaslCreds();
162
163            if ( serverSaslCreds != null )
164            {
165                buffer.put( ( byte ) LdapConstants.SERVER_SASL_CREDENTIAL_TAG );
166
167                buffer.put( TLV.getBytes( serverSaslCreds.length ) );
168
169                if ( serverSaslCreds.length != 0 )
170                {
171                    buffer.put( serverSaslCreds );
172                }
173            }
174        }
175        catch ( BufferOverflowException boe )
176        {
177            throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
178        }
179
180        return buffer;
181    }
182}