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