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