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;
037
038
039
040import java.util.ArrayList;
041
042import com.unboundid.asn1.ASN1OctetString;
043import com.unboundid.asn1.ASN1StreamReader;
044import com.unboundid.asn1.ASN1StreamReaderSequence;
045import com.unboundid.util.Debug;
046import com.unboundid.util.Extensible;
047import com.unboundid.util.NotMutable;
048import com.unboundid.util.NotNull;
049import com.unboundid.util.Nullable;
050import com.unboundid.util.StaticUtils;
051import com.unboundid.util.ThreadSafety;
052import com.unboundid.util.ThreadSafetyLevel;
053
054import static com.unboundid.ldap.sdk.LDAPMessages.*;
055
056
057
058/**
059 * This class provides a data structure for holding information about the result
060 * of processing a bind operation.  It provides generic bind response elements
061 * as described in the {@link LDAPResult} class, but may be overridden to
062 * provide more detailed information for specific types of bind requests.
063 */
064@Extensible()
065@NotMutable()
066@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
067public class BindResult
068       extends LDAPResult
069{
070  /**
071   * The BER type for the server SASL credentials element in the bind result.
072   */
073  private static final byte TYPE_SERVER_SASL_CREDENTIALS = (byte) 0x87;
074
075
076
077  /**
078   * The serial version UID for this serializable class.
079   */
080  private static final long serialVersionUID = 2211625049303605730L;
081
082
083
084  // The server SASL credentials from the response, if available.
085  @Nullable private final ASN1OctetString serverSASLCredentials;
086
087
088
089  /**
090   * Creates a new bind result with the provided information.
091   *
092   * @param  messageID          The message ID for the LDAP message that is
093   *                            associated with this bind result.
094   * @param  resultCode         The result code from the response.
095   * @param  diagnosticMessage  The diagnostic message from the response, if
096   *                            available.
097   * @param  matchedDN          The matched DN from the response, if available.
098   * @param  referralURLs       The set of referral URLs from the response, if
099   *                            available.
100   * @param  responseControls   The set of controls from the response, if
101   *                            available.
102   */
103  public BindResult(final int messageID, @NotNull final ResultCode resultCode,
104                    @Nullable final String diagnosticMessage,
105                    @Nullable final String matchedDN,
106                    @Nullable final String[] referralURLs,
107                    @Nullable final Control[] responseControls)
108  {
109    this(messageID, resultCode, diagnosticMessage, matchedDN, referralURLs,
110         responseControls, null);
111  }
112
113
114
115  /**
116   * Creates a new bind result with the provided information.
117   *
118   * @param  messageID              The message ID for the LDAP message that is
119   *                                associated with this bind result.
120   * @param  resultCode             The result code from the response.
121   * @param  diagnosticMessage      The diagnostic message from the response, if
122   *                                available.
123   * @param  matchedDN              The matched DN from the response, if
124   *                                available.
125   * @param  referralURLs           The set of referral URLs from the response,
126   *                                if available.
127   * @param  responseControls       The set of controls from the response, if
128   *                                available.
129   * @param  serverSASLCredentials  The server SASL credentials from the
130   *                                response, if available.
131   */
132  public BindResult(final int messageID, @NotNull final ResultCode resultCode,
133                    @Nullable final String diagnosticMessage,
134                    @Nullable final String matchedDN,
135                    @Nullable final String[] referralURLs,
136                    @Nullable final Control[] responseControls,
137                    @Nullable final ASN1OctetString serverSASLCredentials)
138  {
139    super(messageID, resultCode, diagnosticMessage, matchedDN, referralURLs,
140          responseControls);
141
142    this.serverSASLCredentials = serverSASLCredentials;
143  }
144
145
146
147  /**
148   * Creates a new bind result from the provided generic LDAP result.
149   *
150   * @param  ldapResult  The LDAP result to use to create this bind result.
151   */
152  public BindResult(@NotNull final LDAPResult ldapResult)
153  {
154    super(ldapResult);
155
156    serverSASLCredentials = null;
157  }
158
159
160
161  /**
162   * Creates a new bind result from the provided {@code LDAPException}.
163   *
164   * @param  exception  The {@code LDAPException} to use to create this bind
165   *                    result.
166   */
167  public BindResult(@NotNull final LDAPException exception)
168  {
169    super(exception.toLDAPResult());
170
171    if (exception instanceof LDAPBindException)
172    {
173      serverSASLCredentials =
174           ((LDAPBindException) exception).getServerSASLCredentials();
175    }
176    else
177    {
178      serverSASLCredentials = null;
179    }
180  }
181
182
183
184  /**
185   * Creates a new bind result from the provided bind result.  This constructor
186   * may be used in creating custom subclasses.
187   *
188   * @param  bindResult  The bind result to use to create this bind result.
189   */
190  protected BindResult(@NotNull final BindResult bindResult)
191  {
192    super(bindResult);
193
194    serverSASLCredentials = bindResult.serverSASLCredentials;
195  }
196
197
198
199  /**
200   * Creates a new bind result object with the provided message ID and with the
201   * protocol op and controls read from the given ASN.1 stream reader.
202   *
203   * @param  messageID        The LDAP message ID for the LDAP message that is
204   *                          associated with this bind result.
205   * @param  messageSequence  The ASN.1 stream reader sequence used in the
206   *                          course of reading the LDAP message elements.
207   * @param  reader           The ASN.1 stream reader from which to read the
208   *                          protocol op and controls.
209   *
210   * @return  The decoded bind result.
211   *
212   * @throws  LDAPException  If a problem occurs while reading or decoding data
213   *                         from the ASN.1 stream reader.
214   */
215  @NotNull()
216  static BindResult readBindResultFrom(final int messageID,
217              @NotNull final ASN1StreamReaderSequence messageSequence,
218              @NotNull final ASN1StreamReader reader)
219         throws LDAPException
220  {
221    try
222    {
223      final ASN1StreamReaderSequence protocolOpSequence =
224           reader.beginSequence();
225      final ResultCode resultCode = ResultCode.valueOf(reader.readEnumerated());
226
227      String matchedDN = reader.readString();
228      if (matchedDN.isEmpty())
229      {
230        matchedDN = null;
231      }
232
233      String diagnosticMessage = reader.readString();
234      if (diagnosticMessage.isEmpty())
235      {
236        diagnosticMessage = null;
237      }
238
239      String[] referralURLs = null;
240      ASN1OctetString serverSASLCredentials = null;
241      while (protocolOpSequence.hasMoreElements())
242      {
243        final byte type = (byte) reader.peek();
244        switch (type)
245        {
246          case TYPE_REFERRAL_URLS:
247            final ArrayList<String> refList = new ArrayList<>(1);
248            final ASN1StreamReaderSequence refSequence = reader.beginSequence();
249            while (refSequence.hasMoreElements())
250            {
251              refList.add(reader.readString());
252            }
253            referralURLs = new String[refList.size()];
254            refList.toArray(referralURLs);
255            break;
256
257          case TYPE_SERVER_SASL_CREDENTIALS:
258            serverSASLCredentials =
259                 new ASN1OctetString(type, reader.readBytes());
260            break;
261
262          default:
263            throw new LDAPException(ResultCode.DECODING_ERROR,
264                 ERR_BIND_RESULT_INVALID_ELEMENT.get(StaticUtils.toHex(type)));
265        }
266      }
267
268      Control[] controls = NO_CONTROLS;
269      if (messageSequence.hasMoreElements())
270      {
271        final ArrayList<Control> controlList = new ArrayList<>(1);
272        final ASN1StreamReaderSequence controlSequence = reader.beginSequence();
273        while (controlSequence.hasMoreElements())
274        {
275          controlList.add(Control.readFrom(reader));
276        }
277
278        controls = new Control[controlList.size()];
279        controlList.toArray(controls);
280      }
281
282      return new BindResult(messageID, resultCode, diagnosticMessage, matchedDN,
283                            referralURLs, controls, serverSASLCredentials);
284    }
285    catch (final LDAPException le)
286    {
287      Debug.debugException(le);
288      throw le;
289    }
290    catch (final Exception e)
291    {
292      Debug.debugException(e);
293      throw new LDAPException(ResultCode.DECODING_ERROR,
294           ERR_BIND_RESULT_CANNOT_DECODE.get(
295                StaticUtils.getExceptionMessage(e)),
296           e);
297    }
298  }
299
300
301
302  /**
303   * Retrieves the server SASL credentials from the bind result, if available.
304   *
305   * @return  The server SASL credentials from the bind response, or
306   *          {@code null} if none were provided.
307   */
308  @Nullable()
309  public ASN1OctetString getServerSASLCredentials()
310  {
311    return serverSASLCredentials;
312  }
313
314
315
316  /**
317   * {@inheritDoc}
318   */
319  @Override()
320  public void toString(@NotNull final StringBuilder buffer)
321  {
322    buffer.append("BindResult(resultCode=");
323    buffer.append(getResultCode());
324
325    final int messageID = getMessageID();
326    if (messageID >= 0)
327    {
328      buffer.append(", messageID=");
329      buffer.append(messageID);
330    }
331
332    final String diagnosticMessage = getDiagnosticMessage();
333    if (diagnosticMessage != null)
334    {
335      buffer.append(", diagnosticMessage='");
336      buffer.append(diagnosticMessage);
337      buffer.append('\'');
338    }
339
340    final String matchedDN = getMatchedDN();
341    if (matchedDN != null)
342    {
343      buffer.append(", matchedDN='");
344      buffer.append(matchedDN);
345      buffer.append('\'');
346    }
347
348    final String[] referralURLs = getReferralURLs();
349    if (referralURLs.length > 0)
350    {
351      buffer.append(", referralURLs={");
352      for (int i=0; i < referralURLs.length; i++)
353      {
354        if (i > 0)
355        {
356          buffer.append(", ");
357        }
358
359        buffer.append('\'');
360        buffer.append(referralURLs[i]);
361        buffer.append('\'');
362      }
363      buffer.append('}');
364    }
365
366    buffer.append(", hasServerSASLCredentials=");
367    buffer.append(serverSASLCredentials != null);
368
369    final Control[] responseControls = getResponseControls();
370    if (responseControls.length > 0)
371    {
372      buffer.append(", responseControls={");
373      for (int i=0; i < responseControls.length; i++)
374      {
375        if (i > 0)
376        {
377          buffer.append(", ");
378        }
379
380        buffer.append(responseControls[i]);
381      }
382      buffer.append('}');
383    }
384
385    buffer.append(')');
386  }
387}