001    /*
002     * Copyright 2007-2016 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2008-2016 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    import com.unboundid.asn1.ASN1OctetString;
025    import com.unboundid.ldap.sdk.Control;
026    import com.unboundid.ldap.sdk.ExtendedResult;
027    import com.unboundid.ldap.sdk.ResultCode;
028    import com.unboundid.util.NotMutable;
029    import com.unboundid.util.ThreadSafety;
030    import com.unboundid.util.ThreadSafetyLevel;
031    
032    import static com.unboundid.ldap.sdk.extensions.ExtOpMessages.*;
033    
034    
035    
036    /**
037     * This class implements a data structure for storing the information from an
038     * extended result for the "Who Am I?" extended request as defined in
039     * <A HREF="http://www.ietf.org/rfc/rfc4532.txt">RFC 4532</A>.  It is able to
040     * decode a generic extended result to extract the returned authorization
041     * identify from it.
042     * <BR><BR>
043     * See the documentation for the {@link WhoAmIExtendedRequest} class for an
044     * example that demonstrates using the "Who Am I?" extended operation.
045     */
046    @NotMutable()
047    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
048    public final class WhoAmIExtendedResult
049           extends ExtendedResult
050    {
051      /**
052       * The serial version UID for this serializable class.
053       */
054      private static final long serialVersionUID = 7466531316632846077L;
055    
056    
057    
058      // The authorization identity string returned by the server.
059      private final String authorizationID;
060    
061    
062    
063      /**
064       * Creates a new "Who Am I?" extended result from the provided extended
065       * result.
066       *
067       * @param  extendedResult  The extended result to be decoded as a "Who Am I?"
068       *                         extended result.
069       */
070      public WhoAmIExtendedResult(final ExtendedResult extendedResult)
071      {
072        super(extendedResult);
073    
074        final ASN1OctetString value = extendedResult.getValue();
075        if (value == null)
076        {
077          authorizationID = null;
078        }
079        else
080        {
081          authorizationID = value.stringValue();
082        }
083      }
084    
085    
086    
087      /**
088       * Creates a new "Who Am I?" extended result with the provided information.
089       *
090       * @param  messageID          The message ID for the LDAP message that is
091       *                            associated with this LDAP result.
092       * @param  resultCode         The result code from the response.
093       * @param  diagnosticMessage  The diagnostic message from the response, if
094       *                            available.
095       * @param  matchedDN          The matched DN from the response, if available.
096       * @param  referralURLs       The set of referral URLs from the response, if
097       *                            available.
098       * @param  authorizationID    The authorization ID for this response, if
099       *                            available.
100       * @param  responseControls   The set of controls from the response, if
101       *                            available.
102       */
103      public WhoAmIExtendedResult(final int messageID, final ResultCode resultCode,
104                                  final String diagnosticMessage,
105                                  final String matchedDN,
106                                  final String[] referralURLs,
107                                  final String authorizationID,
108                                  final Control[] responseControls)
109      {
110        super(messageID, resultCode, diagnosticMessage, matchedDN, referralURLs,
111              null, encodeValue(authorizationID), responseControls);
112    
113        this.authorizationID = authorizationID;
114      }
115    
116    
117    
118      /**
119       * Encodes the value for this extended result using the provided information.
120       *
121       * @param  authorizationID  The authorization ID for this response.
122       *
123       * @return  An ASN.1 octet string containing the encoded value, or
124       *          {@code null} if there should not be an encoded value.
125       */
126      private static ASN1OctetString encodeValue(final String authorizationID)
127      {
128        if (authorizationID == null)
129        {
130          return null;
131        }
132        else
133        {
134          return new ASN1OctetString(authorizationID);
135        }
136      }
137    
138    
139    
140      /**
141       * Retrieves the authorization ID for this "Who Am I?" extended result, if
142       * available.
143       *
144       * @return  The authorization ID for this "Who Am I?" extended result, or
145       *          {@code null} if none was provided.
146       */
147      public String getAuthorizationID()
148      {
149        return authorizationID;
150      }
151    
152    
153    
154      /**
155       * {@inheritDoc}
156       */
157      @Override()
158      public String getExtendedResultName()
159      {
160        return INFO_EXTENDED_RESULT_NAME_WHO_AM_I.get();
161      }
162    
163    
164    
165      /**
166       * Appends a string representation of this extended result to the provided
167       * buffer.
168       *
169       * @param  buffer  The buffer to which a string representation of this
170       *                 extended result will be appended.
171       */
172      @Override()
173      public void toString(final StringBuilder buffer)
174      {
175        buffer.append("WhoAmIExtendedResult(resultCode=");
176        buffer.append(getResultCode());
177    
178        final int messageID = getMessageID();
179        if (messageID >= 0)
180        {
181          buffer.append(", messageID=");
182          buffer.append(messageID);
183        }
184    
185        if (authorizationID != null)
186        {
187          buffer.append(", authorizationID='");
188          buffer.append(authorizationID);
189          buffer.append('\'');
190        }
191    
192        final String diagnosticMessage = getDiagnosticMessage();
193        if (diagnosticMessage != null)
194        {
195          buffer.append(", diagnosticMessage='");
196          buffer.append(diagnosticMessage);
197          buffer.append('\'');
198        }
199    
200        final String matchedDN = getMatchedDN();
201        if (matchedDN != null)
202        {
203          buffer.append(", matchedDN='");
204          buffer.append(matchedDN);
205          buffer.append('\'');
206        }
207    
208        final String[] referralURLs = getReferralURLs();
209        if (referralURLs.length > 0)
210        {
211          buffer.append(", referralURLs={");
212          for (int i=0; i < referralURLs.length; i++)
213          {
214            if (i > 0)
215            {
216              buffer.append(", ");
217            }
218    
219            buffer.append('\'');
220            buffer.append(referralURLs[i]);
221            buffer.append('\'');
222          }
223          buffer.append('}');
224        }
225    
226        final Control[] responseControls = getResponseControls();
227        if (responseControls.length > 0)
228        {
229          buffer.append(", responseControls={");
230          for (int i=0; i < responseControls.length; i++)
231          {
232            if (i > 0)
233            {
234              buffer.append(", ");
235            }
236    
237            buffer.append(responseControls[i]);
238          }
239          buffer.append('}');
240        }
241    
242        buffer.append(')');
243      }
244    }