001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.extras.extended.whoAmI;
021
022
023import org.apache.directory.api.ldap.model.message.ExtendedResponseImpl;
024import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
025import org.apache.directory.api.ldap.model.name.Dn;
026import org.apache.directory.api.util.Strings;
027
028
029/**
030 * The RFC 4532 WhoAmI response :
031 * 
032 * <pre>
033 * authzid OCTET STRING OPTIONAL
034 * </pre>
035 * 
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038public class WhoAmIResponseImpl extends ExtendedResponseImpl implements WhoAmIResponse
039{
040    /** The authzid */
041    private byte[] authzId;
042    
043    /** The authzId when it's a DN */
044    private Dn dn;
045    
046    /** The authzId when it's a userId */
047    private String userId;
048
049    
050    /**
051     * Create a new instance for the WhoAmI response
052     * @param messageId The Message ID
053     * @param rcode The result code
054     * @param diagnosticMessage The diagnostic message
055     */
056    public WhoAmIResponseImpl( int messageId, ResultCodeEnum rcode, String diagnosticMessage )
057    {
058        super( messageId, EXTENSION_OID );
059
060        super.getLdapResult().setMatchedDn( null );
061        super.getLdapResult().setResultCode( rcode );
062        super.getLdapResult().setDiagnosticMessage( diagnosticMessage );
063    }
064
065
066    /**
067     * Create a new instance for the WhoAmI response
068     * @param messageId The Message ID
069     * @param rcode The result code
070     */
071    public WhoAmIResponseImpl( int messageId, ResultCodeEnum rcode )
072    {
073        super( messageId, EXTENSION_OID );
074
075        super.getLdapResult().setMatchedDn( null );
076        super.getLdapResult().setResultCode( rcode );
077    }
078
079
080    /**
081     * Instantiates a new WhoAmI response.
082     *
083     * @param messageId the message id
084     */
085    public WhoAmIResponseImpl( int messageId )
086    {
087        super( messageId, EXTENSION_OID );
088        super.getLdapResult().setMatchedDn( null );
089        super.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
090    }
091
092
093    /**
094     * Instantiates a new WhoAmI response.
095     */
096    public WhoAmIResponseImpl()
097    {
098        super( EXTENSION_OID );
099        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}