001/*
002 * Copyright 2015-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2015-2022 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) 2015-2022 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 com.unboundid.asn1.ASN1OctetString;
041import com.unboundid.util.NotExtensible;
042import com.unboundid.util.NotMutable;
043import com.unboundid.util.NotNull;
044import com.unboundid.util.Nullable;
045import com.unboundid.util.StaticUtils;
046import com.unboundid.util.ThreadSafety;
047import com.unboundid.util.ThreadSafetyLevel;
048
049
050
051/**
052 * This class defines an exception that can be thrown if the server returns an
053 * extended response that indicates that the operation did not complete
054 * successfully.  This may be used to obtain access to any response OID and/or
055 * value that may have been included in the extended result.
056 */
057@NotExtensible()
058@NotMutable()
059@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
060public class LDAPExtendedOperationException
061       extends LDAPException
062{
063  /**
064   * The serial version UID for this serializable class.
065   */
066  private static final long serialVersionUID = -5674215690199642408L;
067
068
069
070  // The extended result for this exception.
071  @NotNull private final ExtendedResult extendedResult;
072
073
074
075  /**
076   * Creates a new LDAP extended operation exception from the provided extended
077   * result.
078   *
079   * @param  extendedResult  The extended result to use to create this
080   *                         exception.
081   */
082  public LDAPExtendedOperationException(
083              @NotNull final ExtendedResult extendedResult)
084  {
085    super(extendedResult);
086
087    this.extendedResult = extendedResult;
088  }
089
090
091
092  /**
093   * {@inheritDoc}
094   */
095  @Override()
096  @NotNull()
097  public LDAPResult toLDAPResult()
098  {
099    return extendedResult;
100  }
101
102
103
104  /**
105   * Retrieves the extended result that was returned by the server.
106   *
107   * @return  The extended result that was returned by the server.
108   */
109  @NotNull()
110  public ExtendedResult getExtendedResult()
111  {
112    return extendedResult;
113  }
114
115
116
117  /**
118   * Retrieves the response OID from the extended result, if any.
119   *
120   * @return  The response OID from the extended result, or {@code null} if the
121   *          result did not include an OID.
122   */
123  @Nullable()
124  public String getResponseOID()
125  {
126    return extendedResult.getOID();
127  }
128
129
130
131  /**
132   * Retrieves the response value from the extended result, if any.
133   *
134   * @return  The response value from the extended result, or {@code null} if
135   *          the result did not include a value.
136   */
137  @Nullable()
138  public ASN1OctetString getResponseValue()
139  {
140    return extendedResult.getValue();
141  }
142
143
144
145  /**
146   * {@inheritDoc}
147   */
148  @Override()
149  public void toString(@NotNull final StringBuilder buffer)
150  {
151    super.toString(buffer);
152  }
153
154
155
156  /**
157   * {@inheritDoc}
158   */
159  @Override()
160  public void toString(@NotNull final StringBuilder buffer,
161                       final boolean includeCause,
162                       final boolean includeStackTrace)
163  {
164    buffer.append("LDAPException(resultCode=");
165    buffer.append(getResultCode());
166
167    final String errorMessage = getMessage();
168    final String diagnosticMessage = getDiagnosticMessage();
169    if ((errorMessage != null) && (! errorMessage.equals(diagnosticMessage)))
170    {
171      buffer.append(", errorMessage='");
172      buffer.append(errorMessage);
173      buffer.append('\'');
174    }
175
176    final String responseOID = getResponseOID();
177    if (responseOID != null)
178    {
179      buffer.append(", responseOID='");
180      buffer.append(responseOID);
181      buffer.append('\'');
182    }
183
184    final String responseName = extendedResult.getExtendedResultName();
185    if ((responseName != null) && (! responseName.equals(responseOID)))
186    {
187      buffer.append(", responseName='");
188      buffer.append(responseName);
189      buffer.append('\'');
190    }
191
192    if (diagnosticMessage != null)
193    {
194      buffer.append(", diagnosticMessage='");
195      buffer.append(diagnosticMessage);
196      buffer.append('\'');
197    }
198
199    final String matchedDN = getMatchedDN();
200    if (matchedDN != null)
201    {
202      buffer.append(", matchedDN='");
203      buffer.append(matchedDN);
204      buffer.append('\'');
205    }
206
207    final String[] referralURLs = getReferralURLs();
208    if (referralURLs.length > 0)
209    {
210      buffer.append(", referralURLs={");
211
212      for (int i=0; i < referralURLs.length; i++)
213      {
214        if (i > 0)
215        {
216          buffer.append(", ");
217        }
218
219        buffer.append('\'');
220        buffer.append(referralURLs[i]);
221        buffer.append('\'');
222      }
223
224      buffer.append('}');
225    }
226
227    final Control[] responseControls = getResponseControls();
228    if (responseControls.length > 0)
229    {
230      buffer.append(", responseControls={");
231
232      for (int i=0; i < responseControls.length; i++)
233      {
234        if (i > 0)
235        {
236          buffer.append(", ");
237        }
238
239        buffer.append(responseControls[i]);
240      }
241
242      buffer.append('}');
243    }
244
245    if (includeStackTrace)
246    {
247      buffer.append(", trace='");
248      StaticUtils.getStackTrace(getStackTrace(), buffer);
249      buffer.append('\'');
250    }
251
252    final String ldapSDKVersionString = ", ldapSDKVersion=" +
253         Version.NUMERIC_VERSION_STRING + ", revision=" + Version.REVISION_ID;
254    if (buffer.indexOf(ldapSDKVersionString) < 0)
255    {
256      buffer.append(ldapSDKVersionString);
257    }
258
259    buffer.append(')');
260  }
261}