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.shared.ldap.codec.decorators;
021
022
023import java.nio.BufferOverflowException;
024import java.nio.ByteBuffer;
025
026import org.apache.directory.shared.asn1.EncoderException;
027import org.apache.directory.shared.asn1.ber.tlv.TLV;
028import org.apache.directory.shared.i18n.I18n;
029import org.apache.directory.shared.ldap.codec.api.LdapApiService;
030import org.apache.directory.shared.ldap.codec.api.LdapConstants;
031import org.apache.directory.shared.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    /**
081     * {@inheritDoc}
082     */
083    public byte[] getServerSaslCreds()
084    {
085        return getDecorated().getServerSaslCreds();
086    }
087
088
089    /**
090     * {@inheritDoc}
091     */
092    public void setServerSaslCreds( byte[] serverSaslCreds )
093    {
094        getDecorated().setServerSaslCreds( serverSaslCreds );
095    }
096    
097    
098    //-------------------------------------------------------------------------
099    // The Decorator methods
100    //-------------------------------------------------------------------------
101    /**
102     * Compute the BindResponse length 
103     * 
104     * BindResponse : 
105     * <pre>
106     * 0x61 L1 
107     *   | 
108     *   +--> LdapResult
109     *   +--> [serverSaslCreds] 
110     *   
111     * L1 = Length(LdapResult) [ + Length(serverSaslCreds) ] 
112     * Length(BindResponse) = Length(0x61) + Length(L1) + L1
113     * </pre>
114     */
115    public int computeLength()
116    {
117        BindResponse bindResponse = getDecorated();
118        int ldapResultLength = ((LdapResultDecorator)getLdapResult()).computeLength();
119
120        int bindResponseLength = ldapResultLength;
121
122        byte[] serverSaslCreds = bindResponse.getServerSaslCreds();
123
124        if ( serverSaslCreds != null )
125        {
126            bindResponseLength += 1 + TLV.getNbBytes( serverSaslCreds.length ) + serverSaslCreds.length;
127        }
128
129        setBindResponseLength( bindResponseLength );
130
131        return 1 + TLV.getNbBytes( bindResponseLength ) + bindResponseLength;
132    }
133
134
135    /**
136     * Encode the BindResponse message to a PDU.
137     * 
138     * BindResponse :
139     * <pre>
140     * LdapResult.encode 
141     * [0x87 LL serverSaslCreds]
142     * </pre>
143     * 
144     * @param bb The buffer where to put the PDU
145     * @param bindResponseDecorator The decorated BindResponse to encode
146     * @throws EncoderException when encoding operations fail
147     */
148    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
149    {
150        BindResponse bindResponse = getDecorated();
151
152        try
153        {
154            // The BindResponse Tag
155            buffer.put( LdapConstants.BIND_RESPONSE_TAG );
156            buffer.put( TLV.getBytes( getBindResponseLength() ) );
157
158            // The LdapResult
159            ((LdapResultDecorator)getLdapResult()).encode( buffer );
160
161            // The serverSaslCredential, if any
162            byte[] serverSaslCreds = bindResponse.getServerSaslCreds();
163
164            if ( serverSaslCreds != null )
165            {
166                buffer.put( ( byte ) LdapConstants.SERVER_SASL_CREDENTIAL_TAG );
167
168                buffer.put( TLV.getBytes( serverSaslCreds.length ) );
169
170                if ( serverSaslCreds.length != 0 )
171                {
172                    buffer.put( serverSaslCreds );
173                }
174            }
175        }
176        catch ( BufferOverflowException boe )
177        {
178            throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
179        }
180        
181        return buffer;
182    }
183}