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