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    @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}