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.extras.extended.whoAmI;
21  
22  
23  import org.apache.directory.api.ldap.model.message.ExtendedResponseImpl;
24  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
25  import org.apache.directory.api.ldap.model.name.Dn;
26  import org.apache.directory.api.util.Strings;
27  
28  
29  /**
30   * The RFC 4532 WhoAmI response :
31   * 
32   * <pre>
33   * authzid OCTET STRING OPTIONAL
34   * </pre>
35   * 
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public class WhoAmIResponseImpl extends ExtendedResponseImpl implements WhoAmIResponse
39  {
40      /** The authzid */
41      private byte[] authzId;
42      
43      /** The authzId when it's a DN */
44      private Dn dn;
45      
46      /** The authzId when it's a userId */
47      private String userId;
48  
49      
50      /**
51       * Create a new instance for the WhoAmI response
52       * @param messageId The Message ID
53       * @param rcode The result code
54       * @param diagnosticMessage The diagnostic message
55       */
56      public WhoAmIResponseImpl( int messageId, ResultCodeEnum rcode, String diagnosticMessage )
57      {
58          super( messageId, EXTENSION_OID );
59  
60          super.getLdapResult().setMatchedDn( null );
61          super.getLdapResult().setResultCode( rcode );
62          super.getLdapResult().setDiagnosticMessage( diagnosticMessage );
63      }
64  
65  
66      /**
67       * Create a new instance for the WhoAmI response
68       * @param messageId The Message ID
69       * @param rcode The result code
70       */
71      public WhoAmIResponseImpl( int messageId, ResultCodeEnum rcode )
72      {
73          super( messageId, EXTENSION_OID );
74  
75          super.getLdapResult().setMatchedDn( null );
76          super.getLdapResult().setResultCode( rcode );
77      }
78  
79  
80      /**
81       * Instantiates a new WhoAmI response.
82       *
83       * @param messageId the message id
84       */
85      public WhoAmIResponseImpl( int messageId )
86      {
87          super( messageId, EXTENSION_OID );
88          super.getLdapResult().setMatchedDn( null );
89          super.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
90      }
91  
92  
93      /**
94       * Instantiates a new WhoAmI response.
95       */
96      public WhoAmIResponseImpl()
97      {
98          super( EXTENSION_OID );
99          super.getLdapResult().setMatchedDn( null );
100         super.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
101     }
102 
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public byte[] getAuthzId()
109     {
110         return authzId;
111     }
112 
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public void setAuthzId( byte[] authzId )
119     {
120         this.authzId = authzId;
121     }
122 
123 
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     public boolean isDnAuthzId()
129     {
130         return dn != null;
131     }
132 
133 
134     /**
135      * {@inheritDoc}
136      */
137     @Override
138     public boolean isUserAuthzId()
139     {
140         return userId != null;
141     }
142 
143 
144     /**
145      * {@inheritDoc}
146      */
147     @Override
148     public String getAuthzIdString()
149     {
150         return Strings.utf8ToString( authzId );
151     }
152 
153 
154     /**
155      * {@inheritDoc}
156      */
157     @Override
158     public String getUserId()
159     {
160         return userId;
161     }
162 
163 
164     /**
165      * Set the userId
166      * 
167      * @param userId The User ID
168      */
169     public void setUserId( String userId )
170     {
171         this.userId = userId;
172     }
173 
174 
175     /**
176      * {@inheritDoc}
177      */
178     @Override
179     public Dn getDn()
180     {
181         return dn;
182     }
183 
184 
185     /**
186      * Set the DN
187      * 
188      * @param dn The DN to set
189      */
190     public void setDn( Dn dn )
191     {
192         this.dn = dn;
193     }
194 
195 
196     /**
197      * @see Object#toString()
198      */
199     @Override
200     public String toString()
201     {
202         StringBuilder sb = new StringBuilder();
203 
204         sb.append( "WhoAmI Extended Response :" );
205         sb.append( "\n    authzid : " );
206 
207         if ( authzId != null )
208         {
209             if ( isDnAuthzId() )
210             {
211                 sb.append( "DN: " ).append( getDn() );
212             }
213             else
214             {
215                 sb.append( "UserId: " ).append( getUserId() );
216             }
217         }
218         else
219         {
220             sb.append( "null" );
221         }
222 
223         return sb.toString();
224     }
225 }