001 /*
002 * Copyright 2007-2016 UnboundID Corp.
003 * All Rights Reserved.
004 */
005 /*
006 * Copyright (C) 2008-2016 UnboundID Corp.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021 package com.unboundid.ldap.sdk;
022
023
024
025 import java.util.List;
026
027 import com.unboundid.util.StaticUtils;
028
029
030
031 /**
032 * This class defines an exception that can be thrown if a problem occurs while
033 * performing LDAP-related processing. It includes all of the elements of the
034 * {@code SearchResult} object, potentially including entries and references
035 * returned before the failure result.
036 */
037 public final class LDAPSearchException
038 extends LDAPException
039 {
040 /**
041 * The serial version UID for this serializable class.
042 */
043 private static final long serialVersionUID = 350230437196125113L;
044
045
046
047 // The search result with information from this exception.
048 private final SearchResult searchResult;
049
050
051
052 /**
053 * Creates a new LDAP search exception with the provided information.
054 *
055 * @param resultCode The result code for this LDAP search exception.
056 * @param errorMessage The error message for this LDAP search exception.
057 */
058 public LDAPSearchException(final ResultCode resultCode,
059 final String errorMessage)
060 {
061 super(resultCode, errorMessage);
062
063 searchResult = new SearchResult(-1, resultCode, errorMessage, null,
064 StaticUtils.NO_STRINGS, 0, 0, StaticUtils.NO_CONTROLS);
065 }
066
067
068
069 /**
070 * Creates a new LDAP search exception with the provided information.
071 *
072 * @param resultCode The result code for this LDAP search exception.
073 * @param errorMessage The error message for this LDAP search exception.
074 * @param cause The underlying exception that triggered this LDAP
075 * search exception.
076 */
077 public LDAPSearchException(final ResultCode resultCode,
078 final String errorMessage, final Throwable cause)
079 {
080 super(resultCode, errorMessage, cause);
081
082 searchResult = new SearchResult(-1, resultCode, errorMessage, null,
083 StaticUtils.NO_STRINGS , 0, 0, StaticUtils.NO_CONTROLS);
084 }
085
086
087
088 /**
089 * Creates a new LDAP search exception from the provided exception.
090 *
091 * @param ldapException The LDAP exception with the information to include
092 * in this LDAP search exception.
093 */
094 public LDAPSearchException(final LDAPException ldapException)
095 {
096 super(ldapException.getResultCode(), ldapException.getMessage(),
097 ldapException.getMatchedDN(), ldapException.getReferralURLs(),
098 ldapException.getResponseControls(), ldapException);
099
100 if (ldapException instanceof LDAPSearchException)
101 {
102 final LDAPSearchException lse = (LDAPSearchException) ldapException;
103 searchResult = lse.searchResult;
104 }
105 else
106 {
107 searchResult = new SearchResult(-1, ldapException.getResultCode(),
108 ldapException.getMessage(),
109 ldapException.getMatchedDN(),
110 ldapException.getReferralURLs(), 0, 0,
111 ldapException.getResponseControls());
112 }
113 }
114
115
116
117 /**
118 * Creates a new LDAP search exception with the provided result.
119 *
120 * @param searchResult The search result to use to create this LDAP search
121 * exception.
122 */
123 public LDAPSearchException(final SearchResult searchResult)
124 {
125 super(searchResult);
126
127 this.searchResult = searchResult;
128 }
129
130
131
132 /**
133 * Retrieves the search result object associated with this LDAP search
134 * exception.
135 *
136 * @return The search result object associated with this LDAP search
137 * exception.
138 */
139 public SearchResult getSearchResult()
140 {
141 return searchResult;
142 }
143
144
145
146 /**
147 * Retrieves the number of matching entries returned for the search operation
148 * before this exception was thrown.
149 *
150 * @return The number of matching entries returned for the search operation
151 * before this exception was thrown.
152 */
153 public int getEntryCount()
154 {
155 return searchResult.getEntryCount();
156 }
157
158
159
160 /**
161 * Retrieves the number of search references returned for the search
162 * operation before this exception was thrown.
163 *
164 * @return The number of search references returned for the search operation
165 * before this exception was thrown.
166 */
167 public int getReferenceCount()
168 {
169 return searchResult.getReferenceCount();
170 }
171
172
173
174 /**
175 * Retrieves a list containing the matching entries returned from the search
176 * operation before this exception was thrown. This will only be available if
177 * a {@code SearchResultListener} was not used during the search.
178 *
179 * @return A list containing the matching entries returned from the search
180 * operation before this exception was thrown, or {@code null} if a
181 * {@code SearchResultListener} was used during the search.
182 */
183 public List<SearchResultEntry> getSearchEntries()
184 {
185 return searchResult.getSearchEntries();
186 }
187
188
189
190 /**
191 * Retrieves a list containing the search references returned from the search
192 * operation before this exception was thrown. This will only be available if
193 * a {@code SearchResultListener} was not used during the search.
194 *
195 * @return A list containing the search references returned from the search
196 * operation before this exception was thrown, or {@code null} if a
197 * {@code SearchResultListener} was used during the search.
198 */
199 public List<SearchResultReference> getSearchReferences()
200 {
201 return searchResult.getSearchReferences();
202 }
203
204
205
206 /**
207 * Creates a new {@code SearchResult} object from this exception.
208 *
209 * @return The {@code SearchResult} object created from this exception.
210 */
211 @Override()
212 public SearchResult toLDAPResult()
213 {
214 return searchResult;
215 }
216
217
218
219 /**
220 * Appends a string representation of this LDAP exception to the provided
221 * buffer.
222 *
223 * @param buffer The buffer to which to append a string representation of
224 * this LDAP exception.
225 */
226 @Override()
227 public void toString(final StringBuilder buffer)
228 {
229 buffer.append("LDAPSearchException(resultCode=");
230 buffer.append(getResultCode());
231 buffer.append(", numEntries=");
232 buffer.append(searchResult.getEntryCount());
233 buffer.append(", numReferences=");
234 buffer.append(searchResult.getReferenceCount());
235
236 final String errorMessage = getMessage();
237 if (errorMessage != null)
238 {
239 buffer.append(", errorMessage='");
240 buffer.append(errorMessage);
241 buffer.append('\'');
242 }
243
244 final String matchedDN = getMatchedDN();
245 if (matchedDN != null)
246 {
247 buffer.append(", matchedDN='");
248 buffer.append(matchedDN);
249 buffer.append('\'');
250 }
251
252 final String[] referralURLs = getReferralURLs();
253 if (referralURLs.length > 0)
254 {
255 buffer.append(", referralURLs={");
256
257 for (int i=0; i < referralURLs.length; i++)
258 {
259 if (i > 0)
260 {
261 buffer.append(", ");
262 }
263
264 buffer.append('\'');
265 buffer.append(referralURLs[i]);
266 buffer.append('\'');
267 }
268
269 buffer.append('}');
270 }
271
272 final Control[] responseControls = getResponseControls();
273 if (responseControls.length > 0)
274 {
275 buffer.append(", responseControls={");
276
277 for (int i=0; i < responseControls.length; i++)
278 {
279 if (i > 0)
280 {
281 buffer.append(", ");
282 }
283
284 buffer.append(responseControls[i]);
285 }
286
287 buffer.append('}');
288 }
289
290 buffer.append(')');
291 }
292 }