001/*
002 * Copyright 2010-2021 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2010-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) 2010-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.asn1.ASN1OctetString;
041import com.unboundid.ldap.sdk.Control;
042import com.unboundid.ldap.sdk.ExtendedResult;
043import com.unboundid.ldap.sdk.LDAPException;
044import com.unboundid.ldap.sdk.ResultCode;
045import com.unboundid.util.NotMutable;
046import com.unboundid.util.NotNull;
047import com.unboundid.util.Nullable;
048import com.unboundid.util.ThreadSafety;
049import com.unboundid.util.ThreadSafetyLevel;
050import com.unboundid.util.Validator;
051
052import static com.unboundid.ldap.sdk.extensions.ExtOpMessages.*;
053
054
055
056/**
057 * This class provides an implementation of the aborted transaction extended
058 * result as defined in
059 * <A HREF="http://www.ietf.org/rfc/rfc5805.txt">RFC 5805</A>, which is used as
060 * an unsolicited notification to indicate that the server has aborted an LDAP
061 * transaction without the client's explicit request.
062 */
063@NotMutable()
064@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
065public final class AbortedTransactionExtendedResult
066       extends ExtendedResult
067{
068  /**
069   * The OID (1.3.6.1.1.21.4) for the aborted transaction extended result.
070   */
071  @NotNull public static final String ABORTED_TRANSACTION_RESULT_OID =
072       "1.3.6.1.1.21.4";
073
074
075
076  /**
077   * The serial version UID for this serializable class.
078   */
079  private static final long serialVersionUID = 7521522597566232465L;
080
081
082
083  // The transaction ID for the transaction that has been aborted.
084  @NotNull private final ASN1OctetString transactionID;
085
086
087
088  /**
089   * Creates a new instance of this aborted transaction extended result with the
090   * provided information.
091   *
092   * @param  transactionID      The transaction ID of the transaction that has
093   *                            been aborted.  It must not be {@code null}.
094   * @param  resultCode         The result code for this aborted transaction
095   *                            result.  It must not be {@code null}.
096   * @param  diagnosticMessage  The diagnostic message for this aborted
097   *                            transaction result.  It may be {@code null} if
098   *                            there is no diagnostic message.
099   * @param  matchedDN          The matched DN for this aborted transaction
100   *                            result.  It may be {@code null} if there is no
101   *                            matched DN.
102   * @param  referralURLs       The referral URLs for this aborted transaction
103   *                            result.  It may be {@code null} or empty if
104   *                            there are no referral URLs.
105   * @param  controls           The controls for this aborted transaction
106   *                            result.  It may be {@code null} or empty if
107   *                            there are no controls.
108   */
109  public AbortedTransactionExtendedResult(
110              @NotNull final ASN1OctetString transactionID,
111              @NotNull final ResultCode resultCode,
112              @Nullable final String diagnosticMessage,
113              @Nullable final String matchedDN,
114              @Nullable final String[] referralURLs,
115              @Nullable final Control[] controls)
116  {
117    super(0, resultCode, diagnosticMessage, matchedDN, referralURLs,
118         ABORTED_TRANSACTION_RESULT_OID, transactionID, controls);
119
120    Validator.ensureNotNull(transactionID, resultCode);
121
122    this.transactionID = transactionID;
123  }
124
125
126
127  /**
128   * Creates a new instance of this aborted transaction extended result from the
129   * provided generic extended result.
130   *
131   * @param  extendedResult  The extended result to use to create this aborted
132   *                         transaction extended result.
133   *
134   * @throws  LDAPException  If the provided extended result cannot be decoded
135   *                         as an aborted transaction extended result.
136   */
137  public AbortedTransactionExtendedResult(
138              @NotNull final ExtendedResult extendedResult)
139         throws LDAPException
140  {
141    super(extendedResult);
142
143    transactionID = extendedResult.getValue();
144    if (transactionID == null)
145    {
146      throw new LDAPException(ResultCode.DECODING_ERROR,
147           ERR_ABORTED_TXN_NO_VALUE.get());
148    }
149  }
150
151
152
153  /**
154   * Retrieves the transaction ID of the transaction that has been aborted.
155   *
156   * @return  The transaction ID of the transaction that has been aborted.
157   */
158  @NotNull()
159  public ASN1OctetString getTransactionID()
160  {
161    return transactionID;
162  }
163
164
165
166  /**
167   * {@inheritDoc}
168   */
169  @Override()
170  @NotNull()
171  public String getExtendedResultName()
172  {
173    return INFO_EXTENDED_RESULT_NAME_ABORTED_TXN.get();
174  }
175
176
177
178  /**
179   * Appends a string representation of this extended result to the provided
180   * buffer.
181   *
182   * @param  buffer  The buffer to which a string representation of this
183   *                 extended result will be appended.
184   */
185  @Override()
186  public void toString(@NotNull final StringBuilder buffer)
187  {
188    buffer.append("AbortedTransactionExtendedResult(transactionID='");
189    buffer.append(transactionID.stringValue());
190    buffer.append("', 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(ABORTED_TRANSACTION_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}