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.message;
021    
022    
023    import java.util.Arrays;
024    
025    import org.apache.directory.shared.ldap.message.internal.InternalAbstractResultResponse;
026    import org.apache.directory.shared.ldap.message.internal.InternalBindResponse;
027    import org.apache.directory.shared.ldap.util.StringTools;
028    
029    
030    /**
031     * BindResponse implementation.
032     * 
033     * @author <a href="mailto:dev@directory.apache.org"> Apache Directory Project</a>
034     * @version $Rev: 905344 $
035     */
036    public class BindResponseImpl extends InternalAbstractResultResponse implements InternalBindResponse
037    {
038        static final long serialVersionUID = -5146809476518669755L;
039    
040        /** optional property holding SASL authentication response parameters */
041        private byte[] serverSaslCreds;
042    
043    
044        // ------------------------------------------------------------------------
045        // Constructors
046        // ------------------------------------------------------------------------
047    
048        /**
049         * Creates a Lockable AddResponse as a reply to an AddRequest.
050         * 
051         * @param id
052         *            the session unique message id
053         */
054        public BindResponseImpl(final int id)
055        {
056            super( id, TYPE );
057        }
058    
059    
060        // ------------------------------------------------------------------------
061        // BindResponse Interface Method Implementations
062        // ------------------------------------------------------------------------
063    
064        /**
065         * Gets the optional property holding SASL authentication response paramters
066         * that are SASL mechanism specific. Will return null if the authentication
067         * is simple.
068         * 
069         * @return the sasl mech. specific credentials or null of auth. is simple
070         */
071        public byte[] getServerSaslCreds()
072        {
073            if ( serverSaslCreds == null )
074            {
075                return null;
076            }
077    
078            final byte[] copy = new byte[ serverSaslCreds.length ];
079            System.arraycopy( serverSaslCreds, 0, copy, 0, serverSaslCreds.length );
080            return copy;
081        }
082    
083    
084        /**
085         * Sets the optional property holding SASL authentication response paramters
086         * that are SASL mechanism specific. Leave null if authentication mode is
087         * simple.
088         * 
089         * @param serverSaslCreds
090         *            the sasl auth. mech. specific credentials
091         */
092        public void setServerSaslCreds( byte[] serverSaslCreds )
093        {
094            if ( serverSaslCreds != null )
095            {
096                this.serverSaslCreds = new byte[ serverSaslCreds.length ];
097                System.arraycopy( serverSaslCreds, 0, this.serverSaslCreds, 0, serverSaslCreds.length );
098            } else {
099                this.serverSaslCreds = null;
100            }
101        }
102    
103    
104        /**
105         * Checks to see if this BindResponse is equal to another BindResponse. The
106         * implementation and lockable properties are not factored into the
107         * evaluation of equality. Only the messageId, saslCredentials and the
108         * LdapResults of this BindResponse PDU and the compared object are taken
109         * into account if that object also implements the BindResponse interface.
110         * 
111         * @param obj
112         *            the object to test for equality with this BindResponse
113         * @return true if obj equals this BindResponse false otherwise
114         */
115        public boolean equals( Object obj )
116        {
117            // quickly return true if obj is this one
118            if ( obj == this )
119            {
120                return true;
121            }
122    
123            if ( ( obj == null ) || !( obj instanceof InternalBindResponse ) )
124            {
125                return false;
126            }
127    
128            if ( !super.equals( obj ) )
129            {
130                return false;
131            }
132    
133            InternalBindResponse response = ( InternalBindResponse ) obj;
134            byte[] creds = response.getServerSaslCreds();
135            
136            if ( serverSaslCreds == null )
137            {
138                if ( creds != null )
139                {
140                    return false;
141                }
142            }
143            else if ( creds == null )
144            {
145                return false;
146            }
147            
148            return Arrays.equals( serverSaslCreds, creds );
149        }
150    
151    
152        /**
153         * Get a String representation of a BindResponse
154         * 
155         * @return A BindResponse String
156         */
157        public String toString()
158        {
159            StringBuffer sb = new StringBuffer();
160            sb.append( "    BindResponse\n" );
161            sb.append( super.toString() );
162    
163            if ( serverSaslCreds != null )
164            {
165                sb.append( "        Server sasl credentials : '" ).append( StringTools.dumpBytes( serverSaslCreds ) ).append( "'\n" );
166            }
167    
168            return sb.toString();
169        }
170    
171    }