001/*
002 * Copyright 2008-2021 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2008-2021 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-2021 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
039
040import com.unboundid.ldap.sdk.Control;
041import com.unboundid.ldap.sdk.ExtendedResult;
042import com.unboundid.ldap.sdk.LDAPException;
043import com.unboundid.ldap.sdk.ResultCode;
044import com.unboundid.util.NotMutable;
045import com.unboundid.util.NotNull;
046import com.unboundid.util.Nullable;
047import com.unboundid.util.ThreadSafety;
048import com.unboundid.util.ThreadSafetyLevel;
049
050import static com.unboundid.ldap.sdk.extensions.ExtOpMessages.*;
051
052
053
054/**
055 * This class provides an implementation of the notice of disconnection extended
056 * result as defined in
057 * <A HREF="http://www.ietf.org/rfc/rfc4511.txt">RFC 4511</A>.  It may be used
058 * as an unsolicited notification to indicate that the directory server is
059 * closing the client connection.
060 * <BR><BR>
061 * See the {@link com.unboundid.ldap.sdk.UnsolicitedNotificationHandler}
062 * interface for a mechanism that can be used to receive and handle unsolicited
063 * notifications.
064 */
065@NotMutable()
066@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
067public final class NoticeOfDisconnectionExtendedResult
068       extends ExtendedResult
069{
070  /**
071   * The OID (1.3.6.1.4.1.1466.20036) for the notice of disconnection extended
072   * result.
073   */
074  @NotNull public static final String NOTICE_OF_DISCONNECTION_RESULT_OID =
075       "1.3.6.1.4.1.1466.20036";
076
077
078
079  /**
080   * The serial version UID for this serializable class.
081   */
082  private static final long serialVersionUID = -4706102471360689558L;
083
084
085
086  /**
087   * Creates a new instance of this notice of disconnection extended result from
088   * the provided generic extended result.
089   *
090   * @param  resultCode         The result code for the notice of disconnection.
091   * @param  diagnosticMessage  The diagnostic message to include in the
092   *                            notice of disconnection.  It may be {@code null}
093   *                            if no diagnostic message should be included.
094   * @param  responseControls   The set of controls to include in the notice of
095   *                            disconnection.  It may be {@code null} or empty
096   *                            if no response controls are needed.
097   */
098  public NoticeOfDisconnectionExtendedResult(
099              @NotNull final ResultCode resultCode,
100              @Nullable final String diagnosticMessage,
101              @Nullable final Control... responseControls)
102  {
103    this(0, resultCode, diagnosticMessage, null, null, responseControls);
104  }
105
106
107
108  /**
109   * Creates a new instance of this notice of disconnection extended result from
110   * the provided generic extended result.
111   *
112   * @param  extendedResult  The extended result to use to create this notice of
113   *                         disconnection extended result.
114   */
115  public NoticeOfDisconnectionExtendedResult(
116              @NotNull final ExtendedResult extendedResult)
117  {
118    super(extendedResult);
119  }
120
121
122
123  /**
124   * Creates a new instance of this notice of disconnection extended result from
125   * the provided LDAP exception.
126   *
127   * @param  ldapException  The LDAP exception to use to create this notice of
128   *                        disconnection extended result.
129   */
130  public NoticeOfDisconnectionExtendedResult(
131              @NotNull final LDAPException ldapException)
132  {
133    this(0, ldapException.getResultCode(), ldapException.getDiagnosticMessage(),
134         ldapException.getMatchedDN(), ldapException.getReferralURLs(),
135         ldapException.getResponseControls());
136  }
137
138
139
140  /**
141   * Creates a new instance of this notice of disconnection extended result from
142   * the provided information.
143   *
144   * @param  messageID          The message ID for the LDAP message that is
145   *                            associated with this LDAP result.
146   * @param  resultCode         The result code from the response.
147   * @param  diagnosticMessage  The diagnostic message from the response, if
148   *                            available.
149   * @param  matchedDN          The matched DN from the response, if available.
150   * @param  referralURLs       The set of referral URLs from the response, if
151   *                            available.
152   * @param  responseControls   The set of controls from the response, if
153   *                            available.
154   */
155  public NoticeOfDisconnectionExtendedResult(
156              final int messageID, @NotNull final ResultCode resultCode,
157              @Nullable final String diagnosticMessage,
158              @Nullable final String matchedDN,
159              @Nullable final String[] referralURLs,
160              @Nullable final Control[] responseControls)
161  {
162    super(messageID, resultCode, diagnosticMessage, matchedDN, referralURLs,
163          NOTICE_OF_DISCONNECTION_RESULT_OID, null, responseControls);
164  }
165
166
167
168  /**
169   * {@inheritDoc}
170   */
171  @Override()
172  @NotNull()
173  public String getExtendedResultName()
174  {
175    return INFO_EXTENDED_RESULT_NAME_NOTICE_OF_DISCONNECT.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(@NotNull final StringBuilder buffer)
189  {
190    buffer.append("NoticeOfDisconnectionExtendedResult(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    final String diagnosticMessage = getDiagnosticMessage();
201    if (diagnosticMessage != null)
202    {
203      buffer.append(", diagnosticMessage='");
204      buffer.append(diagnosticMessage);
205      buffer.append('\'');
206    }
207
208    final String matchedDN = getMatchedDN();
209    if (matchedDN != null)
210    {
211      buffer.append(", matchedDN='");
212      buffer.append(matchedDN);
213      buffer.append('\'');
214    }
215
216    final String[] referralURLs = getReferralURLs();
217    if (referralURLs.length > 0)
218    {
219      buffer.append(", referralURLs={");
220      for (int i=0; i < referralURLs.length; i++)
221      {
222        if (i > 0)
223        {
224          buffer.append(", ");
225        }
226
227        buffer.append('\'');
228        buffer.append(referralURLs[i]);
229        buffer.append('\'');
230      }
231      buffer.append('}');
232    }
233
234    buffer.append(", oid=");
235    buffer.append(NOTICE_OF_DISCONNECTION_RESULT_OID);
236
237    final Control[] responseControls = getResponseControls();
238    if (responseControls.length > 0)
239    {
240      buffer.append(", responseControls={");
241      for (int i=0; i < responseControls.length; i++)
242      {
243        if (i > 0)
244        {
245          buffer.append(", ");
246        }
247
248        buffer.append(responseControls[i]);
249      }
250      buffer.append('}');
251    }
252
253    buffer.append(')');
254  }
255}