001/*
002 * Copyright 2007-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2007-2024 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2007-2024 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.ldap.sdk.extensions;
037
038
039import com.unboundid.asn1.ASN1OctetString;
040import com.unboundid.ldap.sdk.Control;
041import com.unboundid.ldap.sdk.ExtendedResult;
042import com.unboundid.ldap.sdk.ResultCode;
043import com.unboundid.util.NotMutable;
044import com.unboundid.util.NotNull;
045import com.unboundid.util.Nullable;
046import com.unboundid.util.ThreadSafety;
047import com.unboundid.util.ThreadSafetyLevel;
048
049import static com.unboundid.ldap.sdk.extensions.ExtOpMessages.*;
050
051
052
053/**
054 * This class implements a data structure for storing the information from an
055 * extended result for the "Who Am I?" extended request as defined in
056 * <A HREF="http://www.ietf.org/rfc/rfc4532.txt">RFC 4532</A>.  It is able to
057 * decode a generic extended result to extract the returned authorization
058 * identify from it.
059 * <BR><BR>
060 * See the documentation for the {@link WhoAmIExtendedRequest} class for an
061 * example that demonstrates using the "Who Am I?" extended operation.
062 */
063@NotMutable()
064@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
065public final class WhoAmIExtendedResult
066       extends ExtendedResult
067{
068  /**
069   * The serial version UID for this serializable class.
070   */
071  private static final long serialVersionUID = 7466531316632846077L;
072
073
074
075  // The authorization identity string returned by the server.
076  @Nullable private final String authorizationID;
077
078
079
080  /**
081   * Creates a new "Who Am I?" extended result from the provided extended
082   * result.
083   *
084   * @param  extendedResult  The extended result to be decoded as a "Who Am I?"
085   *                         extended result.
086   */
087  public WhoAmIExtendedResult(@NotNull final ExtendedResult extendedResult)
088  {
089    super(extendedResult);
090
091    final ASN1OctetString value = extendedResult.getValue();
092    if (value == null)
093    {
094      authorizationID = null;
095    }
096    else
097    {
098      authorizationID = value.stringValue();
099    }
100  }
101
102
103
104  /**
105   * Creates a new "Who Am I?" extended result with the provided information.
106   *
107   * @param  messageID          The message ID for the LDAP message that is
108   *                            associated with this LDAP result.
109   * @param  resultCode         The result code from the response.
110   * @param  diagnosticMessage  The diagnostic message from the response, if
111   *                            available.
112   * @param  matchedDN          The matched DN from the response, if available.
113   * @param  referralURLs       The set of referral URLs from the response, if
114   *                            available.
115   * @param  authorizationID    The authorization ID for this response, if
116   *                            available.
117   * @param  responseControls   The set of controls from the response, if
118   *                            available.
119   */
120  public WhoAmIExtendedResult(final int messageID,
121                              @NotNull final ResultCode resultCode,
122                              @Nullable final String diagnosticMessage,
123                              @Nullable final String matchedDN,
124                              @Nullable final String[] referralURLs,
125                              @Nullable final String authorizationID,
126                              @Nullable final Control[] responseControls)
127  {
128    super(messageID, resultCode, diagnosticMessage, matchedDN, referralURLs,
129          null, encodeValue(authorizationID), responseControls);
130
131    this.authorizationID = authorizationID;
132  }
133
134
135
136  /**
137   * Encodes the value for this extended result using the provided information.
138   *
139   * @param  authorizationID  The authorization ID for this response.
140   *
141   * @return  An ASN.1 octet string containing the encoded value, or
142   *          {@code null} if there should not be an encoded value.
143   */
144  @Nullable()
145  private static ASN1OctetString encodeValue(
146                                      @Nullable final String authorizationID)
147  {
148    if (authorizationID == null)
149    {
150      return null;
151    }
152    else
153    {
154      return new ASN1OctetString(authorizationID);
155    }
156  }
157
158
159
160  /**
161   * Retrieves the authorization ID for this "Who Am I?" extended result, if
162   * available.
163   *
164   * @return  The authorization ID for this "Who Am I?" extended result, or
165   *          {@code null} if none was provided.
166   */
167  @Nullable()
168  public String getAuthorizationID()
169  {
170    return authorizationID;
171  }
172
173
174
175  /**
176   * {@inheritDoc}
177   */
178  @Override()
179  @NotNull()
180  public String getExtendedResultName()
181  {
182    return INFO_EXTENDED_RESULT_NAME_WHO_AM_I.get();
183  }
184
185
186
187  /**
188   * Appends a string representation of this extended result to the provided
189   * buffer.
190   *
191   * @param  buffer  The buffer to which a string representation of this
192   *                 extended result will be appended.
193   */
194  @Override()
195  public void toString(@NotNull final StringBuilder buffer)
196  {
197    buffer.append("WhoAmIExtendedResult(resultCode=");
198    buffer.append(getResultCode());
199
200    final int messageID = getMessageID();
201    if (messageID >= 0)
202    {
203      buffer.append(", messageID=");
204      buffer.append(messageID);
205    }
206
207    if (authorizationID != null)
208    {
209      buffer.append(", authorizationID='");
210      buffer.append(authorizationID);
211      buffer.append('\'');
212    }
213
214    final String diagnosticMessage = getDiagnosticMessage();
215    if (diagnosticMessage != null)
216    {
217      buffer.append(", diagnosticMessage='");
218      buffer.append(diagnosticMessage);
219      buffer.append('\'');
220    }
221
222    final String matchedDN = getMatchedDN();
223    if (matchedDN != null)
224    {
225      buffer.append(", matchedDN='");
226      buffer.append(matchedDN);
227      buffer.append('\'');
228    }
229
230    final String[] referralURLs = getReferralURLs();
231    if (referralURLs.length > 0)
232    {
233      buffer.append(", referralURLs={");
234      for (int i=0; i < referralURLs.length; i++)
235      {
236        if (i > 0)
237        {
238          buffer.append(", ");
239        }
240
241        buffer.append('\'');
242        buffer.append(referralURLs[i]);
243        buffer.append('\'');
244      }
245      buffer.append('}');
246    }
247
248    final Control[] responseControls = getResponseControls();
249    if (responseControls.length > 0)
250    {
251      buffer.append(", responseControls={");
252      for (int i=0; i < responseControls.length; i++)
253      {
254        if (i > 0)
255        {
256          buffer.append(", ");
257        }
258
259        buffer.append(responseControls[i]);
260      }
261      buffer.append('}');
262    }
263
264    buffer.append(')');
265  }
266}