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