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    @Override
076    public byte[] getServerSaslCreds()
077    {
078        if ( serverSaslCreds == null )
079        {
080            return null;
081        }
082
083        final byte[] copy = new byte[serverSaslCreds.length];
084        System.arraycopy( serverSaslCreds, 0, copy, 0, serverSaslCreds.length );
085        return copy;
086    }
087
088
089    /**
090     * Sets the optional property holding SASL authentication response paramters
091     * that are SASL mechanism specific. Leave null if authentication mode is
092     * simple.
093     * 
094     * @param serverSaslCreds
095     *            the sasl auth. mech. specific credentials
096     */
097    @Override
098    public void setServerSaslCreds( byte[] serverSaslCreds )
099    {
100        if ( serverSaslCreds != null )
101        {
102            this.serverSaslCreds = new byte[serverSaslCreds.length];
103            System.arraycopy( serverSaslCreds, 0, this.serverSaslCreds, 0, serverSaslCreds.length );
104        }
105        else
106        {
107            this.serverSaslCreds = null;
108        }
109    }
110
111
112    /**
113     * {@inheritDoc}
114     */
115    @Override
116    public int hashCode()
117    {
118        int hash = 37;
119        hash = hash * 17 + Arrays.hashCode( serverSaslCreds );
120        hash = hash * 17 + super.hashCode();
121
122        return hash;
123    }
124
125
126    /**
127     * Checks to see if this BindResponse is equal to another BindResponse. The
128     * implementation and lockable properties are not factored into the
129     * evaluation of equality. Only the messageId, saslCredentials and the
130     * LdapResults of this BindResponse PDU and the compared object are taken
131     * into account if that object also implements the BindResponse interface.
132     * 
133     * @param obj
134     *            the object to test for equality with this BindResponse
135     * @return true if obj equals this BindResponse false otherwise
136     */
137    @Override
138    public boolean equals( Object obj )
139    {
140        // quickly return true if obj is this one
141        if ( obj == this )
142        {
143            return true;
144        }
145
146        if ( ( obj == null ) || !( obj instanceof BindResponse ) )
147        {
148            return false;
149        }
150
151        if ( !super.equals( obj ) )
152        {
153            return false;
154        }
155
156        BindResponse response = ( BindResponse ) obj;
157        byte[] creds = response.getServerSaslCreds();
158
159        if ( serverSaslCreds == null )
160        {
161            if ( creds != null )
162            {
163                return false;
164            }
165        }
166        else if ( creds == null )
167        {
168            return false;
169        }
170
171        return Arrays.equals( serverSaslCreds, creds );
172    }
173
174
175    /**
176     * Get a String representation of a BindResponse
177     * 
178     * @return A BindResponse String
179     */
180    @Override
181    public String toString()
182    {
183        StringBuilder sb = new StringBuilder();
184
185        sb.append( "    BindResponse\n" );
186        sb.append( super.toString() );
187
188        if ( serverSaslCreds != null )
189        {
190            sb.append( "        Server sasl credentials : '" ).append( Strings.dumpBytes( serverSaslCreds ) )
191                .append( "'\n" );
192        }
193
194        return super.toString( sb.toString() );
195    }
196}