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      @Override
76      public byte[] getServerSaslCreds()
77      {
78          if ( serverSaslCreds == null )
79          {
80              return null;
81          }
82  
83          final byte[] copy = new byte[serverSaslCreds.length];
84          System.arraycopy( serverSaslCreds, 0, copy, 0, serverSaslCreds.length );
85          return copy;
86      }
87  
88  
89      /**
90       * Sets the optional property holding SASL authentication response paramters
91       * that are SASL mechanism specific. Leave null if authentication mode is
92       * simple.
93       * 
94       * @param serverSaslCreds
95       *            the sasl auth. mech. specific credentials
96       */
97      @Override
98      public void setServerSaslCreds( byte[] serverSaslCreds )
99      {
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 }