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