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.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 codec The LDAP service instance
049     * @param decoratedMessage the decorated BindResponse
050     */
051    public BindResponseDecorator( LdapApiService codec, BindResponse decoratedMessage )
052    {
053        super( codec, decoratedMessage );
054    }
055
056
057    //-------------------------------------------------------------------------
058    // The BindResponse methods
059    //-------------------------------------------------------------------------
060
061    /**
062     * {@inheritDoc}
063     */
064    @Override
065    public byte[] getServerSaslCreds()
066    {
067        return getDecorated().getServerSaslCreds();
068    }
069
070
071    /**
072     * {@inheritDoc}
073     */
074    @Override
075    public void setServerSaslCreds( byte[] serverSaslCreds )
076    {
077        getDecorated().setServerSaslCreds( serverSaslCreds );
078    }
079
080
081    //-------------------------------------------------------------------------
082    // The Decorator methods
083    //-------------------------------------------------------------------------
084    /**
085     * Compute the BindResponse length 
086     * <br>
087     * BindResponse : 
088     * <pre>
089     * 0x61 L1 
090     *   | 
091     *   +--&gt; LdapResult
092     *   +--&gt; [serverSaslCreds] 
093     *   
094     * L1 = Length(LdapResult) [ + Length(serverSaslCreds) ] 
095     * Length(BindResponse) = Length(0x61) + Length(L1) + L1
096     * </pre>
097     */
098    @Override
099    public int computeLength()
100    {
101        BindResponse bindResponse = getDecorated();
102        int ldapResultLength = ( ( LdapResultDecorator ) getLdapResult() ).computeLength();
103
104        bindResponseLength = ldapResultLength;
105
106        byte[] serverSaslCreds = bindResponse.getServerSaslCreds();
107
108        if ( serverSaslCreds != null )
109        {
110            bindResponseLength += 1 + TLV.getNbBytes( serverSaslCreds.length ) + serverSaslCreds.length;
111        }
112
113        return 1 + TLV.getNbBytes( bindResponseLength ) + bindResponseLength;
114    }
115
116
117    /**
118     * Encode the BindResponse message to a PDU.
119     * <br>
120     * BindResponse :
121     * <pre>
122     * LdapResult.encode 
123     * [0x87 LL serverSaslCreds]
124     * </pre>
125     * 
126     * @param buffer The buffer where to put the PDU
127     * @return The encoded response
128     * @throws EncoderException when encoding operations fail
129     */
130    @Override
131    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
132    {
133        BindResponse bindResponse = getDecorated();
134
135        try
136        {
137            // The BindResponse Tag
138            buffer.put( LdapCodecConstants.BIND_RESPONSE_TAG );
139            buffer.put( TLV.getBytes( bindResponseLength ) );
140
141            // The LdapResult
142            ( ( LdapResultDecorator ) getLdapResult() ).encode( buffer );
143
144            // The serverSaslCredential, if any
145            byte[] serverSaslCreds = bindResponse.getServerSaslCreds();
146
147            if ( serverSaslCreds != null )
148            {
149                buffer.put( ( byte ) LdapCodecConstants.SERVER_SASL_CREDENTIAL_TAG );
150
151                buffer.put( TLV.getBytes( serverSaslCreds.length ) );
152
153                if ( serverSaslCreds.length != 0 )
154                {
155                    buffer.put( serverSaslCreds );
156                }
157            }
158        }
159        catch ( BufferOverflowException boe )
160        {
161            throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
162        }
163
164        return buffer;
165    }
166}