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     public byte[] getAuthzId()
108     {
109         return authzId;
110     }
111 
112 
113     /**
114      * {@inheritDoc}
115      */
116     public void setAuthzId( byte[] authzId )
117     {
118         this.authzId = authzId;
119     }
120 
121 
122     /**
123      * {@inheritDoc}
124      */
125     public boolean isDnAuthzId()
126     {
127         if ( dn != null )
128         {
129             return true;
130         }
131         else
132         {
133             return false;
134         }
135     }
136 
137 
138     /**
139      * {@inheritDoc}
140      */
141     public boolean isUserAuthzId()
142     {
143         return userId != null;
144     }
145 
146 
147     /**
148      * {@inheritDoc}
149      */
150     public String getAuthzIdString()
151     {
152         return Strings.utf8ToString( authzId );
153     }
154 
155 
156     /**
157      * {@inheritDoc}
158      */
159     public String getUserId()
160     {
161         return userId;
162     }
163 
164 
165     /**
166      * Set the userId
167      */
168     public void setUserId( String userId )
169     {
170         this.userId = userId;
171     }
172 
173 
174     /**
175      * {@inheritDoc}
176      */
177     public Dn getDn()
178     {
179         return dn;
180     }
181 
182 
183     /**
184      * Set the DN
185      */
186     public void setDn( Dn dn )
187     {
188         this.dn = dn;
189     }
190 
191 
192     /**
193      * @see Object#toString()
194      */
195     public String toString()
196     {
197         StringBuilder sb = new StringBuilder();
198 
199         sb.append( "WhoAmI Extended Response :" );
200         sb.append( "\n    authzid : " );
201 
202         if ( authzId != null )
203         {
204             if ( isDnAuthzId() )
205             {
206                 sb.append( "DN: " ).append( getDn() );
207             }
208             else
209             {
210                 sb.append( "UserId: " ).append( getUserId() );
211             }
212         }
213         else
214         {
215             sb.append( "null" );
216         }
217 
218         return sb.toString();
219     }
220 }