001/*
002 * Copyright 2009-2023 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-2023 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-2023 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 java.io.Serializable;
041
042import com.unboundid.util.Mutable;
043import com.unboundid.util.NotExtensible;
044import com.unboundid.util.NotNull;
045import com.unboundid.util.Nullable;
046import com.unboundid.util.ThreadSafety;
047import com.unboundid.util.ThreadSafetyLevel;
048
049
050
051/**
052 * This class provides a data structure which may be used to define a set of
053 * constraints that may be used when processing operations.
054 * <BR><BR>
055 * This class is primarily intended to be used in the process of updating
056 * applications which use the Netscape Directory SDK for Java to switch to or
057 * coexist with the UnboundID LDAP SDK for Java.  For applications not written
058 * using the Netscape Directory SDK for Java, the
059 * {@link com.unboundid.ldap.sdk.LDAPConnectionOptions} class should be used
060 * instead.
061 */
062@NotExtensible()
063@Mutable()
064@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
065public class LDAPConstraints
066       implements Serializable
067{
068  /**
069   * The serial version UID for this serializable class.
070   */
071  private static final long serialVersionUID = 6843729471197926148L;
072
073
074
075  // Indicates whether to follow referrals.
076  private boolean followReferrals;
077
078  // The referral hop limit.
079  private int hopLimit;
080
081  // The response time limit in milliseconds.
082  private int timeLimit;
083
084  // The mechanism to use to authenticate to the target server when following
085  // referrals.
086  @Nullable private LDAPBind bindProc;
087
088  // The client controls.
089  @NotNull private LDAPControl[] clientControls;
090
091  // The server controls.
092  @NotNull private LDAPControl[] serverControls;
093
094  // The mechanism to use to obtain credentials used when authenticating a
095  // referral connection.
096  @Nullable private LDAPRebind rebindProc;
097
098
099
100  /**
101   * Creates a new default set of constraints.
102   */
103  public LDAPConstraints()
104  {
105    bindProc        = null;
106    clientControls  = new LDAPControl[0];
107    followReferrals = false;
108    hopLimit        = 5;
109    rebindProc      = null;
110    serverControls  = new LDAPControl[0];
111    timeLimit       = 0;
112  }
113
114
115
116  /**
117   * Creates a set of LDAP constraints with the provided information.
118   *
119   * @param  msLimit      The maximum length of time in milliseconds to wait for
120   *                      a response from the server.
121   * @param  doReferrals  Indicates whether to attempt to follow referrals.
122   * @param  bindProc     The object to use to authenticate a connection when
123   *                      following referrals.
124   * @param  hopLimit     The maximum number of hops to take when following a
125   *                      referral.
126   */
127  public LDAPConstraints(final int msLimit, final boolean doReferrals,
128                         @Nullable final LDAPBind bindProc, final int hopLimit)
129  {
130    this();
131
132    timeLimit       = msLimit;
133    followReferrals = doReferrals;
134    this.bindProc   = bindProc;
135    this.hopLimit   = hopLimit;
136  }
137
138
139
140  /**
141   * Creates a set of LDAP constraints with the provided information.
142   *
143   * @param  msLimit      The maximum length of time in milliseconds to wait for
144   *                      a response from the server.
145   * @param  doReferrals  Indicates whether to attempt to follow referrals.
146   * @param  rebindProc   The object to use to provide the information needed to
147   *                      authenticate a connection created for following a
148   *                      referral.
149   * @param  hopLimit     The maximum number of hops to take when following a
150   *                      referral.
151   */
152  public LDAPConstraints(final int msLimit, final boolean doReferrals,
153                         @Nullable final LDAPRebind rebindProc,
154                         final int hopLimit)
155  {
156    this();
157
158    timeLimit       = msLimit;
159    followReferrals = doReferrals;
160    this.rebindProc = rebindProc;
161    this.hopLimit   = hopLimit;
162  }
163
164
165
166  /**
167   * Retrieves the maximum length of time in milliseconds to wait for a response
168   * from the server.
169   *
170   * @return  The maximum length of time in milliseconds to wait for a response
171   *          from the server.
172   */
173  public int getTimeLimit()
174  {
175    return timeLimit;
176  }
177
178
179
180  /**
181   * Specifies the maximum length of time in milliseconds to wait for a response
182   * from the server.
183   *
184   * @param  timeLimit  The maximum length of time in milliseconds to wait for a
185   *                    response from the server.
186   */
187  public void setTimeLimit(final int timeLimit)
188  {
189    if (timeLimit < 0)
190    {
191      this.timeLimit = 0;
192    }
193    else
194    {
195      this.timeLimit = timeLimit;
196    }
197  }
198
199
200
201  /**
202   * Indicates whether the client should automatically attempt to follow
203   * referrals.
204   *
205   * @return  {@code true} if the client should attempt to follow referrals, or
206   *          {@code false} if not.
207   */
208  public boolean getReferrals()
209  {
210    return followReferrals;
211  }
212
213
214
215  /**
216   * Specifies whether the client should automatically attempt to follow
217   * referrals.
218   *
219   * @param  doReferrals  Indicates whether the client should automatically
220   *                      attempt to follow referrals.
221   */
222  public void setReferrals(final boolean doReferrals)
223  {
224    followReferrals = doReferrals;
225  }
226
227
228
229  /**
230   * Retrieves the object that should be used to authenticate connections when
231   * following referrals.
232   *
233   * @return  The object that should be used to authenticate connections when
234   *          following referrals, or {@code null} if none has been defined.
235   */
236  @Nullable()
237  public LDAPBind getBindProc()
238  {
239    return bindProc;
240  }
241
242
243
244  /**
245   * Specifies the object that should be used to authenticate connections when
246   * following referrals.
247   *
248   * @param  bindProc  The object that should be used to authenticate
249   *                   connections when following referrals.
250   */
251  public void setBindProc(@Nullable final LDAPBind bindProc)
252  {
253    this.bindProc = bindProc;
254  }
255
256
257
258  /**
259   * Retrieves the object that should be used to obtain authentication
260   * information for use when following referrals.
261   *
262   * @return  The object that should be used to obtain authentication
263   *          information for use when following referrals, or {@code null} if
264   *          none has been defined.
265   */
266  @Nullable()
267  public LDAPRebind getRebindProc()
268  {
269    return rebindProc;
270  }
271
272
273
274  /**
275   * Specifies the object that should be used to obtain authentication
276   * information for use when following referrals.
277   *
278   * @param  rebindProc  The object that should be used to obtain authentication
279   *                     information for use when following referrals.
280   */
281  public void setRebindProc(@Nullable final LDAPRebind rebindProc)
282  {
283    this.rebindProc = rebindProc;
284  }
285
286
287
288  /**
289   * Retrieves the maximum number of hops to take when attempting to follow a
290   * referral.
291   *
292   * @return  The maximum number of hops to take when attempting to follow a
293   *          referral.
294   */
295  public int getHopLimit()
296  {
297    return hopLimit;
298  }
299
300
301
302  /**
303   * Retrieves the maximum number of hops to take when attempting to follow a
304   * referral.
305   *
306   * @param  hopLimit  The maximum number of hops to take when attempting to
307   *                   follow a referral.
308   */
309  public void setHopLimit(final int hopLimit)
310  {
311    if (hopLimit < 0)
312    {
313      this.hopLimit = 0;
314    }
315    else
316    {
317      this.hopLimit = hopLimit;
318    }
319  }
320
321
322
323  /**
324   * Retrieves the controls that should be applied by the clients.
325   *
326   * @return The controls that should be applied by the client.
327   */
328  @NotNull()
329  public LDAPControl[] getClientControls()
330  {
331    return clientControls;
332  }
333
334
335
336  /**
337   * Specifies the controls that should be applied by the client.
338   *
339   * @param  control  The control that should be applied by client.
340   */
341  public void setClientControls(@NotNull final LDAPControl control)
342  {
343    clientControls = new LDAPControl[] { control };
344  }
345
346
347
348  /**
349   * Specifies the controls that should be applied by the client.
350   *
351   * @param  controls  The controls that should be applied by client.
352   */
353  public void setClientControls(@Nullable final LDAPControl[] controls)
354  {
355    if (controls == null)
356    {
357      clientControls = new LDAPControl[0];
358    }
359    else
360    {
361      clientControls = controls;
362    }
363  }
364
365
366
367  /**
368   * Retrieves the controls that should be applied by the server.
369   *
370   * @return The controls that should be applied by the server.
371   */
372  @NotNull()
373  public LDAPControl[] getServerControls()
374  {
375    return serverControls;
376  }
377
378
379
380  /**
381   * Specifies the controls that should be applied by the server.
382   *
383   * @param  control  The control that should be applied by server.
384   */
385  public void setServerControls(@NotNull final LDAPControl control)
386  {
387    serverControls = new LDAPControl[] { control };
388  }
389
390
391
392  /**
393   * Specifies the controls that should be applied by the server.
394   *
395   * @param  controls  The controls that should be applied by server.
396   */
397  public void setServerControls(@Nullable final LDAPControl[] controls)
398  {
399    if (controls == null)
400    {
401      serverControls = new LDAPControl[0];
402    }
403    else
404    {
405      serverControls = controls;
406    }
407  }
408
409
410
411  /**
412   * Retrieves a duplicate of this LDAP constraints object.
413   *
414   * @return  A duplicate of this LDAP constraints object.
415   */
416  @NotNull()
417  public LDAPConstraints duplicate()
418  {
419    final LDAPConstraints c = new LDAPConstraints();
420
421    c.bindProc        = bindProc;
422    c.clientControls  = clientControls;
423    c.followReferrals = followReferrals;
424    c.hopLimit        = hopLimit;
425    c.rebindProc      = rebindProc;
426    c.serverControls  = serverControls;
427    c.timeLimit       = timeLimit;
428
429    return c;
430  }
431
432
433
434  /**
435   * Retrieves a string representation of this LDAP constraints object.
436   *
437   * @return  A string representation of this LDAP constraints object.
438   */
439  @Override()
440  @NotNull()
441  public String toString()
442  {
443    final StringBuilder buffer = new StringBuilder();
444
445    buffer.append("LDAPConstraints(followReferrals=");
446    buffer.append(followReferrals);
447    buffer.append(", bindProc=");
448    buffer.append(String.valueOf(bindProc));
449    buffer.append(", rebindProc=");
450    buffer.append(String.valueOf(rebindProc));
451    buffer.append(", hopLimit=");
452    buffer.append(hopLimit);
453    buffer.append(", timeLimit=");
454    buffer.append(timeLimit);
455    buffer.append(", clientControls={");
456
457    for (int i=0; i < clientControls.length; i++)
458    {
459      if (i > 0)
460      {
461        buffer.append(", ");
462      }
463
464      buffer.append(clientControls[i].toString());
465    }
466
467    buffer.append("}, serverControls={");
468
469    for (int i=0; i < serverControls.length; i++)
470    {
471      if (i > 0)
472      {
473        buffer.append(", ");
474      }
475
476      buffer.append(serverControls[i].toString());
477    }
478
479    buffer.append("})");
480
481    return buffer.toString();
482  }
483}