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