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     */
020    package org.apache.directory.shared.ldap.codec.bind;
021    
022    
023    import java.nio.BufferOverflowException;
024    import java.nio.ByteBuffer;
025    
026    import org.apache.directory.shared.asn1.ber.tlv.TLV;
027    import org.apache.directory.shared.asn1.codec.EncoderException;
028    import org.apache.directory.shared.i18n.I18n;
029    import org.apache.directory.shared.ldap.codec.LdapConstants;
030    import org.apache.directory.shared.ldap.codec.LdapResponseCodec;
031    import org.apache.directory.shared.ldap.codec.MessageTypeEnum;
032    import org.apache.directory.shared.ldap.util.StringTools;
033    
034    
035    /**
036     * A BindResponse Message. 
037     * 
038     * Its syntax is : 
039     * BindResponse ::= [APPLICATION 1] SEQUENCE { 
040     *   COMPONENTS OF LDAPResult, 
041     *   serverSaslCreds [7] OCTET STRING OPTIONAL } 
042     *   
043     * LdapResult ::= resultCode matchedDN errorMessage (referrals)*
044     * 
045     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
046     * @version $Rev: 912399 $, $Date: 2010-02-21 21:52:31 +0100 (Sun, 21 Feb 2010) $, 
047     */
048    public class BindResponseCodec extends LdapResponseCodec
049    {
050        // ~ Instance fields
051        // ----------------------------------------------------------------------------
052    
053        /** The server credentials */
054        private byte[] serverSaslCreds;
055    
056        /** The bind response length */
057        private int bindResponseLength;
058    
059    
060        // ~ Constructors
061        // -------------------------------------------------------------------------------
062    
063        /**
064         * Creates a new BindResponse object.
065         */
066        public BindResponseCodec()
067        {
068            super();
069        }
070    
071    
072        // ~ Methods
073        // ------------------------------------------------------------------------------------
074    
075        /**
076         * Get the message type
077         * 
078         * @return Returns the type.
079         */
080        public MessageTypeEnum getMessageType()
081        {
082            return MessageTypeEnum.BIND_RESPONSE;
083        }
084    
085        
086        /**
087         * {@inheritDoc}
088         */
089        public String getMessageTypeName()
090        {
091            return "BIND_RESPONSE";
092        }
093    
094    
095        /**
096         * @return Returns the serverSaslCreds.
097         */
098        public byte[] getServerSaslCreds()
099        {
100            if ( serverSaslCreds == null )
101            {
102                return null;
103            }
104    
105            final byte[] copy = new byte[ serverSaslCreds.length ];
106            System.arraycopy( serverSaslCreds, 0, copy, 0, serverSaslCreds.length );
107            return copy;
108        }
109    
110    
111        /**
112         * Set the server sasl credentials
113         * 
114         * @param serverSaslCreds The serverSaslCreds to set.
115         */
116        public void setServerSaslCreds( byte[] serverSaslCreds )
117        {
118            if ( serverSaslCreds != null )
119            {
120                this.serverSaslCreds = new byte[ serverSaslCreds.length ];
121                System.arraycopy( serverSaslCreds, 0, this.serverSaslCreds, 0, serverSaslCreds.length );
122            } else {
123                this.serverSaslCreds = null;
124            }
125        }
126    
127    
128        /**
129         * Compute the BindResponse length 
130         * 
131         * BindResponse : 
132         * <pre>
133         * 0x61 L1 
134         *   | 
135         *   +--> LdapResult
136         *   +--> [serverSaslCreds] 
137         *   
138         * L1 = Length(LdapResult) [ + Length(serverSaslCreds) ] 
139         * Length(BindResponse) = Length(0x61) + Length(L1) + L1
140         * </pre>
141         */
142        protected int computeLengthProtocolOp()
143        {
144            int ldapResultLength = computeLdapResultLength();
145    
146            bindResponseLength = ldapResultLength;
147    
148            if ( serverSaslCreds != null )
149            {
150                bindResponseLength += 1 + TLV.getNbBytes( serverSaslCreds.length )
151                    + serverSaslCreds.length;
152            }
153    
154            return 1 + TLV.getNbBytes( bindResponseLength ) + bindResponseLength;
155        }
156    
157    
158        /**
159         * Encode the BindResponse message to a PDU.
160         * 
161         * BindResponse :
162         * <pre>
163         * LdapResult.encode 
164         * [0x87 LL serverSaslCreds]
165         * </pre>
166         * 
167         * @param buffer The buffer where to put the PDU
168         * @return The PDU.
169         */
170        protected void encodeProtocolOp( ByteBuffer buffer ) throws EncoderException
171        {
172            try
173            {
174                // The BindResponse Tag
175                buffer.put( LdapConstants.BIND_RESPONSE_TAG );
176                buffer.put( TLV.getBytes( bindResponseLength ) );
177    
178                // The LdapResult
179                super.encode( buffer );
180    
181                // The serverSaslCredential, if any
182                if ( serverSaslCreds != null )
183                {
184                    buffer.put( ( byte ) LdapConstants.SERVER_SASL_CREDENTIAL_TAG );
185    
186                    buffer.put( TLV.getBytes( serverSaslCreds.length ) );
187    
188                    if ( serverSaslCreds.length != 0 )
189                    {
190                        buffer.put( serverSaslCreds );
191                    }
192                }
193            }
194            catch ( BufferOverflowException boe )
195            {
196                throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
197            }
198        }
199    
200    
201        /**
202         * Get a String representation of a BindResponse
203         * 
204         * @return A BindResponse String
205         */
206        public String toString()
207        {
208            StringBuilder sb = new StringBuilder();
209    
210            sb.append( "    BindResponse" );
211    
212            if ( serverSaslCreds != null )
213            {
214                sb.append( "\n        Server sasl credentials : '" ).
215                    append( StringTools.dumpBytes( serverSaslCreds ) ).
216                    append( '\'' );
217            }
218            
219            sb.append( super.toString() );
220    
221            return toString( sb.toString() );
222        }
223    }