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