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