View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.message;
21  
22  
23  import java.util.Arrays;
24  
25  import org.apache.directory.api.util.Strings;
26  
27  
28  /**
29   * BindResponse implementation.
30   * 
31   * @author <a href="mailto:dev@directory.apache.org"> Apache Directory Project</a>
32   */
33  public class BindResponseImpl extends AbstractResultResponse implements BindResponse
34  {
35      static final long serialVersionUID = -5146809476518669755L;
36  
37      /** optional property holding SASL authentication response parameters */
38      private byte[] serverSaslCreds;
39  
40  
41      // ------------------------------------------------------------------------
42      // Constructors
43      // ------------------------------------------------------------------------
44      /**
45       * Creates a BindResponse as a reply to an BindRequest.
46       */
47      public BindResponseImpl()
48      {
49          super( -1, MessageTypeEnum.BIND_RESPONSE );
50      }
51  
52  
53      /**
54       * Creates a BindResponse as a reply to an BindRequest.
55       * 
56       * @param id the session unique message id
57       */
58      public BindResponseImpl( final int id )
59      {
60          super( id, MessageTypeEnum.BIND_RESPONSE );
61      }
62  
63  
64      // ------------------------------------------------------------------------
65      // BindResponse Interface Method Implementations
66      // ------------------------------------------------------------------------
67  
68      /**
69       * Gets the optional property holding SASL authentication response paramters
70       * that are SASL mechanism specific. Will return null if the authentication
71       * is simple.
72       * 
73       * @return the sasl mech. specific credentials or null of auth. is simple
74       */
75      public byte[] getServerSaslCreds()
76      {
77          if ( serverSaslCreds == null )
78          {
79              return null;
80          }
81  
82          final byte[] copy = new byte[serverSaslCreds.length];
83          System.arraycopy( serverSaslCreds, 0, copy, 0, serverSaslCreds.length );
84          return copy;
85      }
86  
87  
88      /**
89       * Sets the optional property holding SASL authentication response paramters
90       * that are SASL mechanism specific. Leave null if authentication mode is
91       * simple.
92       * 
93       * @param serverSaslCreds
94       *            the sasl auth. mech. specific credentials
95       */
96      public void setServerSaslCreds( byte[] serverSaslCreds )
97      {
98          if ( serverSaslCreds != null )
99          {
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 }