001    /*
002     * Copyright 2007-2013 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2008-2013 UnboundID Corp.
007     *
008     * This program is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU General Public License (GPLv2 only)
010     * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011     * as published by the Free Software Foundation.
012     *
013     * This program is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program; if not, see <http://www.gnu.org/licenses>.
020     */
021    package com.unboundid.ldap.sdk.extensions;
022    
023    
024    
025    import com.unboundid.ldap.sdk.Control;
026    import com.unboundid.ldap.sdk.ExtendedRequest;
027    import com.unboundid.ldap.sdk.ExtendedResult;
028    import com.unboundid.ldap.sdk.LDAPConnection;
029    import com.unboundid.ldap.sdk.LDAPException;
030    import com.unboundid.ldap.sdk.ResultCode;
031    import com.unboundid.util.NotMutable;
032    import com.unboundid.util.ThreadSafety;
033    import com.unboundid.util.ThreadSafetyLevel;
034    
035    import static com.unboundid.ldap.sdk.extensions.ExtOpMessages.*;
036    
037    
038    
039    /**
040     * This class provides an implementation of the LDAP "Who Am I?" extended
041     * request as defined in
042     * <A HREF="http://www.ietf.org/rfc/rfc4532.txt">RFC 4532</A>.  It may be used
043     * to request the current authorization identity associated with the client
044     * connection.
045     * <BR><BR>
046     * The "Who Am I?" extended operation is similar to the
047     * {@link com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl}
048     * in that it can be used to request the authorization identity for the
049     * connection.  The primary difference between them is that the authorization
050     * identity request control can only be included in a bind request (and the
051     * corresponding response control will be included in the bind result), while
052     * the "Who Am I?" extended operation can be used at any time through a separate
053     * operation.
054     * <BR><BR>
055     * <H2>Example</H2>
056     * The following example demonstrates the use of the "Who Am I?" extended
057     * operation.
058     * <PRE>
059     *   WhoAmIExtendedResult whoAmIResult =
060     *        (WhoAmIExtendedResult)
061     *        connection.processExtendedOperation(new WhoAmIExtendedRequest());
062     *
063     *   // NOTE:  The processExtendedOperation method will only throw an exception
064     *   // if a problem occurs while trying to send the request or read the
065     *   // response.  It will not throw an exception because of a non-success
066     *   // response.
067     *
068     *   if (whoAmIResult.getResultCode() == ResultCode.SUCCESS)
069     *   {
070     *     String authzID = whoAmIResult.getAuthorizationID();
071     *     if (authzID.length() == 0)
072     *     {
073     *       System.out.println("Your current authorization ID is that of the " +
074     *                          "anonymous user.");
075     *     }
076     *     else
077     *     {
078     *       System.out.println("Your current authorization ID is " +
079     *                          whoAmIResult.getAuthorizationID());
080     *     }
081     *   }
082     *   else
083     *   {
084     *     System.err.println("An error occurred while processing the " +
085     *                        "Who Am I? extended operation.");
086     *   }
087     * </PRE>
088     */
089    @NotMutable()
090    @ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
091    public final class WhoAmIExtendedRequest
092           extends ExtendedRequest
093    {
094      /**
095       * The OID (1.3.6.1.4.1.4203.1.11.3) for the "Who Am I?" extended request.
096       */
097      public static final String WHO_AM_I_REQUEST_OID = "1.3.6.1.4.1.4203.1.11.3";
098    
099    
100    
101      /**
102       * The serial version UID for this serializable class.
103       */
104      private static final long serialVersionUID = -2936513698220673318L;
105    
106    
107    
108      /**
109       * Creates a new "Who Am I?" extended request.
110       */
111      public WhoAmIExtendedRequest()
112      {
113        super(WHO_AM_I_REQUEST_OID);
114      }
115    
116    
117    
118      /**
119       * Creates a new "Who Am I?" extended request.
120       *
121       * @param  controls  The set of controls to include in the request.
122       */
123      public WhoAmIExtendedRequest(final Control[] controls)
124      {
125        super(WHO_AM_I_REQUEST_OID, controls);
126      }
127    
128    
129    
130      /**
131       * Creates a new "Who Am I?" extended request from the provided generic
132       * extended request.
133       *
134       * @param  extendedRequest  The generic extended request to use to create this
135       *                          "Who Am I?" extended request.
136       *
137       * @throws  LDAPException  If a problem occurs while decoding the request.
138       */
139      public WhoAmIExtendedRequest(final ExtendedRequest extendedRequest)
140             throws LDAPException
141      {
142        super(extendedRequest);
143    
144        if (extendedRequest.hasValue())
145        {
146          throw new LDAPException(ResultCode.DECODING_ERROR,
147                                  ERR_WHO_AM_I_REQUEST_HAS_VALUE.get());
148        }
149      }
150    
151    
152    
153      /**
154       * {@inheritDoc}
155       */
156      @Override()
157      public WhoAmIExtendedResult process(final LDAPConnection connection,
158                                          final int depth)
159             throws LDAPException
160      {
161        final ExtendedResult extendedResponse = super.process(connection, depth);
162        return new WhoAmIExtendedResult(extendedResponse);
163      }
164    
165    
166    
167      /**
168       * {@inheritDoc}
169       */
170      @Override()
171      public WhoAmIExtendedRequest duplicate()
172      {
173        return duplicate(getControls());
174      }
175    
176    
177    
178      /**
179       * {@inheritDoc}
180       */
181      @Override()
182      public WhoAmIExtendedRequest duplicate(final Control[] controls)
183      {
184        final WhoAmIExtendedRequest r = new WhoAmIExtendedRequest(controls);
185        r.setResponseTimeoutMillis(getResponseTimeoutMillis(null));
186        return r;
187      }
188    
189    
190    
191      /**
192       * {@inheritDoc}
193       */
194      @Override()
195      public String getExtendedRequestName()
196      {
197        return INFO_EXTENDED_REQUEST_NAME_WHO_AM_I.get();
198      }
199    
200    
201    
202      /**
203       * {@inheritDoc}
204       */
205      @Override()
206      public void toString(final StringBuilder buffer)
207      {
208        buffer.append("WhoAmIExtendedRequest(");
209    
210        final Control[] controls = getControls();
211        if (controls.length > 0)
212        {
213          buffer.append("controls={");
214          for (int i=0; i < controls.length; i++)
215          {
216            if (i > 0)
217            {
218              buffer.append(", ");
219            }
220    
221            buffer.append(controls[i]);
222          }
223          buffer.append('}');
224        }
225    
226        buffer.append(')');
227      }
228    }