001/*
002 * Copyright 2009-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-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) 2009-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.persist;
037
038
039
040import com.unboundid.ldap.sdk.Control;
041import com.unboundid.ldap.sdk.LDAPException;
042import com.unboundid.ldap.sdk.ResultCode;
043import com.unboundid.ldap.sdk.Version;
044import com.unboundid.util.NotMutable;
045import com.unboundid.util.NotNull;
046import com.unboundid.util.Nullable;
047import com.unboundid.util.StaticUtils;
048import com.unboundid.util.ThreadSafety;
049import com.unboundid.util.ThreadSafetyLevel;
050
051
052
053/**
054 * This class defines an exception that may be thrown if a problem occurs while
055 * attempting to perform processing related to persisting Java objects in an
056 * LDAP directory server.
057 */
058@NotMutable()
059@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
060public final class LDAPPersistException
061       extends LDAPException
062{
063  /**
064   * The serial version UID for this serializable class.
065   */
066  private static final long serialVersionUID = 8625904586803506713L;
067
068
069
070  // The object that was in the process of being decoded, if available.  If it
071  // is non-null, then it will likely only be partially initialized.
072  @Nullable private final Object partiallyDecodedObject;
073
074
075
076  /**
077   * Creates a new LDAP persist exception that wraps the provided LDAP
078   * exception.
079   *
080   * @param  e  The LDAP exception to wrap with this LDAP persist exception.
081   */
082  public LDAPPersistException(@NotNull final LDAPException e)
083  {
084    super(e);
085
086    partiallyDecodedObject = null;
087  }
088
089
090
091  /**
092   * Creates a new LDAP persist exception with the provided message.
093   *
094   * @param  message  The message for this exception.
095   */
096  public LDAPPersistException(@NotNull final String message)
097  {
098    super(ResultCode.LOCAL_ERROR, message);
099
100    partiallyDecodedObject = null;
101  }
102
103
104
105  /**
106   * Creates a new LDAP persist exception with the provided message and cause.
107   *
108   * @param  message  The message for this exception.
109   * @param  cause    The underlying cause for this exception.
110   */
111  public LDAPPersistException(@NotNull final String message,
112                              @Nullable final Throwable cause)
113  {
114    super(ResultCode.LOCAL_ERROR, message, cause);
115
116    partiallyDecodedObject = null;
117  }
118
119
120
121  /**
122   * Creates a new LDAP persist exception with the provided message and cause.
123   *
124   * @param  message                 The message for this exception.
125   * @param  partiallyDecodedObject  The object that was in the process of being
126   *                                 decoded when this exception was thrown.  It
127   *                                 may be {@code null} if the exception was
128   *                                 thrown outside of the context of decoding
129   *                                 an object.  If an object is available, then
130   *                                 it will likely be only partially
131   *                                 initialized.
132   * @param  cause                   The underlying cause for this exception.
133   */
134  public LDAPPersistException(@NotNull final String message,
135                              @Nullable final Object partiallyDecodedObject,
136                              @Nullable final Throwable cause)
137  {
138    super(ResultCode.LOCAL_ERROR, message, cause);
139
140    this.partiallyDecodedObject = partiallyDecodedObject;
141  }
142
143
144
145  /**
146   * Retrieves the partially-decoded object in the process of being initialized
147   * when this exception was thrown.
148   *
149   * @return  The partially-decoded object in the process of being initialized
150   *          when this exception was thrown, or {@code null} if none is
151   *          available or the exception was not thrown while decoding an
152   *          object.
153   */
154  @Nullable()
155  public Object getPartiallyDecodedObject()
156  {
157    return partiallyDecodedObject;
158  }
159
160
161
162  /**
163   * {@inheritDoc}
164   */
165  @Override()
166  public void toString(@NotNull final StringBuilder buffer)
167  {
168    super.toString(buffer);
169  }
170
171
172
173  /**
174   * {@inheritDoc}
175   */
176  @Override()
177  public void toString(@NotNull final StringBuilder buffer,
178                       final boolean includeCause,
179                       final boolean includeStackTrace)
180  {
181    buffer.append("LDAPException(resultCode=");
182    buffer.append(getResultCode());
183
184    final String errorMessage = getMessage();
185    final String diagnosticMessage = getDiagnosticMessage();
186    if ((errorMessage != null) && (! errorMessage.equals(diagnosticMessage)))
187    {
188      buffer.append(", errorMessage='");
189      buffer.append(errorMessage);
190      buffer.append('\'');
191    }
192
193    if (partiallyDecodedObject != null)
194    {
195      buffer.append(", partiallyDecodedObject=");
196      buffer.append(partiallyDecodedObject);
197    }
198
199    if (diagnosticMessage != null)
200    {
201      buffer.append(", diagnosticMessage='");
202      buffer.append(diagnosticMessage);
203      buffer.append('\'');
204    }
205
206    final String matchedDN = getMatchedDN();
207    if (matchedDN != null)
208    {
209      buffer.append(", matchedDN='");
210      buffer.append(matchedDN);
211      buffer.append('\'');
212    }
213
214    final String[] referralURLs = getReferralURLs();
215    if (referralURLs.length > 0)
216    {
217      buffer.append(", referralURLs={");
218
219      for (int i=0; i < referralURLs.length; i++)
220      {
221        if (i > 0)
222        {
223          buffer.append(", ");
224        }
225
226        buffer.append('\'');
227        buffer.append(referralURLs[i]);
228        buffer.append('\'');
229      }
230
231      buffer.append('}');
232    }
233
234    final Control[] responseControls = getResponseControls();
235    if (responseControls.length > 0)
236    {
237      buffer.append(", responseControls={");
238
239      for (int i=0; i < responseControls.length; i++)
240      {
241        if (i > 0)
242        {
243          buffer.append(", ");
244        }
245
246        buffer.append(responseControls[i]);
247      }
248
249      buffer.append('}');
250    }
251
252    if (includeStackTrace)
253    {
254      buffer.append(", trace='");
255      StaticUtils.getStackTrace(getStackTrace(), buffer);
256      buffer.append('\'');
257    }
258
259    if (includeCause || includeStackTrace)
260    {
261      final Throwable cause = getCause();
262      if (cause != null)
263      {
264        buffer.append(", cause=");
265        buffer.append(StaticUtils.getExceptionMessage(cause, true,
266             includeStackTrace));
267      }
268    }
269
270    final String ldapSDKVersionString = ", ldapSDKVersion=" +
271         Version.NUMERIC_VERSION_STRING + ", revision=" + Version.REVISION_ID;
272    if (buffer.indexOf(ldapSDKVersionString) < 0)
273    {
274      buffer.append(ldapSDKVersionString);
275    }
276
277    buffer.append("')");
278  }
279}