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.migrate.ldapjdk;
037
038
039
040import com.unboundid.util.Mutable;
041import com.unboundid.util.NotExtensible;
042import com.unboundid.util.NotNull;
043import com.unboundid.util.Nullable;
044import com.unboundid.util.ThreadSafety;
045import com.unboundid.util.ThreadSafetyLevel;
046
047
048
049/**
050 * This class provides a data structure which may be used to define a set of
051 * constraints that may be used when processing search operations.
052 * <BR><BR>
053 * This class is primarily intended to be used in the process of updating
054 * applications which use the Netscape Directory SDK for Java to switch to or
055 * coexist with the UnboundID LDAP SDK for Java.  For applications not written
056 * using the Netscape Directory SDK for Java, the
057 * {@link com.unboundid.ldap.sdk.LDAPConnectionOptions} class should be used
058 * instead.
059 */
060@NotExtensible()
061@Mutable()
062@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
063public class LDAPSearchConstraints
064       extends LDAPConstraints
065{
066  /**
067   * The serial version UID for this serializable class.
068   */
069  private static final long serialVersionUID = -487551577157782460L;
070
071
072
073  // The result batch size.
074  private int batchSize;
075
076  // The alias dereferencing policy.
077  private int derefPolicy;
078
079  // The maximum number of results to return for a search.
080  private int sizeLimit;
081
082  // The maximum length of time in seconds the server should spend processing a
083  // search.
084  private int timeLimit;
085
086
087
088  /**
089   * Creates a new set of search constraints with the default settings.
090   */
091  public LDAPSearchConstraints()
092  {
093    super();
094
095    batchSize   = 1;
096    derefPolicy = LDAPConnection.DEREF_NEVER;
097    sizeLimit   = 1000;
098    timeLimit   = 0;
099  }
100
101
102
103  /**
104   * Creates a new set of search constraints with the specified information.
105   *
106   * @param  msLimit      The maximum length of time in milliseconds to spend
107   *                      waiting for the response.
108   * @param  dereference  The policy to use when dereferencing aliases.
109   * @param  maxResults   The maximum number of entries to return from the
110   *                      server.
111   * @param  doReferrals  Indicates whether to follow referrals.
112   * @param  batchSize    The batch size to use when retrieving results.
113   * @param  rebindProc   The object to use to obtain information for
114   *                      authenticating the connection for use when following
115   *                      referrals.
116   * @param  hopLimit     The maximum number of hops to take when following
117   *                      referrals.
118   */
119  public LDAPSearchConstraints(final int msLimit, final int dereference,
120                               final int maxResults, final boolean doReferrals,
121                               final int batchSize,
122                               @Nullable final LDAPRebind rebindProc,
123                               final int hopLimit)
124  {
125    this();
126
127    derefPolicy    = dereference;
128    sizeLimit      = maxResults;
129    this.batchSize = batchSize;
130
131    setTimeLimit(msLimit);
132    setReferrals(doReferrals);
133    setRebindProc(rebindProc);
134    setHopLimit(hopLimit);
135  }
136
137
138
139  /**
140   * Creates a new set of search constraints with the specified information.
141   *
142   * @param  msLimit      The maximum length of time in milliseconds to spend
143   *                      waiting for the response.
144   * @param  timeLimit    The maximum length of time in seconds the server
145   *                      should spend processing the request.
146   * @param  dereference  The policy to use when dereferencing aliases.
147   * @param  maxResults   The maximum number of entries to return from the
148   *                      server.
149   * @param  doReferrals  Indicates whether to follow referrals.
150   * @param  batchSize    The batch size to use when retrieving results.
151   * @param  rebindProc   The object to use to obtain information for
152   *                      authenticating the connection for use when following
153   *                      referrals.
154   * @param  hopLimit     The maximum number of hops to take when following
155   *                      referrals.
156   */
157  public LDAPSearchConstraints(final int msLimit, final int timeLimit,
158                               final int dereference,
159                               final int maxResults, final boolean doReferrals,
160                               final int batchSize,
161                               @Nullable final LDAPRebind rebindProc,
162                               final int hopLimit)
163  {
164    this();
165
166    derefPolicy    = dereference;
167    sizeLimit      = maxResults;
168    this.timeLimit = timeLimit;
169    this.batchSize = batchSize;
170
171    setTimeLimit(msLimit);
172    setReferrals(doReferrals);
173    setRebindProc(rebindProc);
174    setHopLimit(hopLimit);
175  }
176
177
178
179  /**
180   * Creates a new set of search constraints with the specified information.
181   *
182   * @param  msLimit      The maximum length of time in milliseconds to spend
183   *                      waiting for the response.
184   * @param  timeLimit    The maximum length of time in seconds the server
185   *                      should spend processing the request.
186   * @param  dereference  The policy to use when dereferencing aliases.
187   * @param  maxResults   The maximum number of entries to return from the
188   *                      server.
189   * @param  doReferrals  Indicates whether to follow referrals.
190   * @param  batchSize    The batch size to use when retrieving results.
191   * @param  bindProc     The object to use to obtain authenticating the
192   *                      connection for use when following referrals.
193   * @param  hopLimit     The maximum number of hops to take when following
194   *                      referrals.
195   */
196  public LDAPSearchConstraints(final int msLimit, final int timeLimit,
197                               final int dereference,
198                               final int maxResults, final boolean doReferrals,
199                               final int batchSize,
200                               @Nullable final LDAPBind bindProc,
201                               final int hopLimit)
202  {
203    this();
204
205    derefPolicy    = dereference;
206    sizeLimit      = maxResults;
207    this.timeLimit = timeLimit;
208    this.batchSize = batchSize;
209
210    setTimeLimit(msLimit);
211    setReferrals(doReferrals);
212    setBindProc(bindProc);
213    setHopLimit(hopLimit);
214  }
215
216
217
218  /**
219   * Retrieves the suggested batch size to use when retrieving results.
220   *
221   * @return  The suggested batch size to use when retrieving results.
222   */
223  public int getBatchSize()
224  {
225    return batchSize;
226  }
227
228
229
230  /**
231   * Specifies the suggested batch size to use when retrieving results.
232   *
233   * @param  batchSize  The suggested batch size to use when retrieving results.
234   */
235  public void setBatchSize(final int batchSize)
236  {
237    if (batchSize < 1)
238    {
239      this.batchSize = 1;
240    }
241    else
242    {
243      this.batchSize = batchSize;
244    }
245  }
246
247
248
249  /**
250   * Retrieves the alias dereferencing policy that should be used.
251   *
252   * @return  The alias dereferencing policy that should be used.
253   */
254  public int getDereference()
255  {
256    return derefPolicy;
257  }
258
259
260
261  /**
262   * Specifies the alias dereferencing policy that should be used.
263   *
264   * @param  dereference  The alias dereferencing policy that should be used.
265   */
266  public void setDereference(final int dereference)
267  {
268    derefPolicy = dereference;
269  }
270
271
272
273  /**
274   * Retrieves the maximum number of entries that should be returned for a
275   * search.
276   *
277   * @return  The maximum number of entries that should be returned for a
278   *          search.
279   */
280  public int getMaxResults()
281  {
282    return sizeLimit;
283  }
284
285
286
287  /**
288   * Specifies the maximum number of entries that should be returned for a
289   * search.
290   *
291   * @param  maxResults  The maximum number of entries that should be returned
292   *                     for a search.
293   */
294  public void setMaxResults(final int maxResults)
295  {
296    if (maxResults < 0)
297    {
298      sizeLimit = 0;
299    }
300    else
301    {
302      sizeLimit = maxResults;
303    }
304  }
305
306
307
308  /**
309   * Retrieves the maximum length of time in seconds that the server should
310   * spend processing a search.
311   *
312   * @return  The maximum length of time in seconds that the server should spend
313   *          processing a search.
314   */
315  public int getServerTimeLimit()
316  {
317    return timeLimit;
318  }
319
320
321
322  /**
323   * Specifies the maximum length of time in seconds that the server should
324   * spend processing a search.
325   *
326   * @param  limit  The maximum length of time in seconds that the server should
327   *                spend processing a search.
328   */
329  public void setServerTimeLimit(final int limit)
330  {
331    if (limit < 0)
332    {
333      timeLimit = 0;
334    }
335    else
336    {
337      timeLimit = limit;
338    }
339  }
340
341
342
343  /**
344   * Creates a duplicate of this search constraints object.
345   *
346   * @return  A duplicate of this search constraints object.
347   */
348  @Override()
349  @NotNull()
350  public LDAPSearchConstraints duplicate()
351  {
352    final LDAPSearchConstraints c = new LDAPSearchConstraints();
353
354    c.batchSize   = batchSize;
355    c.derefPolicy = derefPolicy;
356    c.sizeLimit   = sizeLimit;
357    c.timeLimit   = timeLimit;
358
359    c.setBindProc(getBindProc());
360    c.setClientControls(getClientControls());
361    c.setReferrals(getReferrals());
362    c.setHopLimit(getHopLimit());
363    c.setRebindProc(getRebindProc());
364    c.setServerControls(getServerControls());
365    c.setTimeLimit(getTimeLimit());
366
367    return c;
368  }
369
370
371
372  /**
373   * Retrieves a string representation of this search constraints object.
374   *
375   * @return  A string representation of this search constraints object.
376   */
377  @Override()
378  @NotNull()
379  public String toString()
380  {
381    final StringBuilder buffer = new StringBuilder();
382
383    buffer.append("LDAPSearchConstraints(constraints=");
384    buffer.append(super.toString());
385    buffer.append(", batchSize=");
386    buffer.append(batchSize);
387    buffer.append(", derefPolicy=");
388    buffer.append(derefPolicy);
389    buffer.append(", maxResults=");
390    buffer.append(sizeLimit);
391    buffer.append(", serverTimeLimit=");
392    buffer.append(timeLimit);
393    buffer.append(')');
394
395    return buffer.toString();
396  }
397}