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.asn1.ASN1OctetString;
028    import com.unboundid.ldap.sdk.Control;
029    import com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl;
030    import com.unboundid.ldap.sdk.controls.PasswordExpiredControl;
031    import com.unboundid.ldap.sdk.controls.PasswordExpiringControl;
032    import com.unboundid.util.Extensible;
033    import com.unboundid.util.NotMutable;
034    import com.unboundid.util.ThreadSafety;
035    import com.unboundid.util.ThreadSafetyLevel;
036    
037    
038    
039    /**
040     * This class provides a data structure that holds information about an LDAP
041     * control.
042     * <BR><BR>
043     * This class is primarily intended to be used in the process of updating
044     * applications which use the Netscape Directory SDK for Java to switch to or
045     * coexist with the UnboundID LDAP SDK for Java.  For applications not written
046     * using the Netscape Directory SDK for Java, the {@link Control} class should
047     * be used instead.
048     */
049    @Extensible()
050    @NotMutable()
051    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
052    public class LDAPControl
053           implements Serializable
054    {
055      /**
056       * The OID for the ManageDsaIT request control.
057       */
058      public static final String MANAGEDSAIT =
059           ManageDsaITRequestControl.MANAGE_DSA_IT_REQUEST_OID;
060    
061    
062    
063      /**
064       * The OID for the password expired control.
065       */
066      public static final String PWEXPIRED =
067           PasswordExpiredControl.PASSWORD_EXPIRED_OID;
068    
069    
070    
071      /**
072       * The OID for the password expiring control.
073       */
074      public static final String PWEXPIRING =
075           PasswordExpiringControl.PASSWORD_EXPIRING_OID;
076    
077    
078    
079      /**
080       * The serial version UID for this serializable class.
081       */
082      private static final long serialVersionUID = 7828506470553016637L;
083    
084    
085    
086      // Indicates whether this control is critical.
087      private final boolean isCritical;
088    
089      // The value for this control.
090      private final byte[] value;
091    
092      // The OID for this control.
093      private final String oid;
094    
095    
096    
097      /**
098       * Creates a new LDAP control from the provided control.
099       *
100       * @param  control  The control to use to create this control.
101       */
102      public LDAPControl(final Control control)
103      {
104        oid        = control.getOID();
105        isCritical = control.isCritical();
106    
107        if (control.hasValue())
108        {
109          value = control.getValue().getValue();
110        }
111        else
112        {
113          value = null;
114        }
115      }
116    
117    
118    
119      /**
120       * Creates a new LDAP control with the specified information.
121       *
122       * @param  id        The OID for the control.
123       * @param  critical  Indicates whether the control should be marked critical.
124       * @param  vals      The encoded value for the control.
125       */
126      public LDAPControl(final String id, final boolean critical, final byte[] vals)
127      {
128        oid        = id;
129        isCritical = critical;
130        value      = vals;
131      }
132    
133    
134    
135      /**
136       * Retrieves the OID for this control.
137       *
138       * @return  The OID for this control.
139       */
140      public String getID()
141      {
142        return oid;
143      }
144    
145    
146    
147      /**
148       * Indicates whether this control is marked critical.
149       *
150       * @return  {@code true} if this control is marked critical, or {@code false}
151       *          if not.
152       */
153      public boolean isCritical()
154      {
155        return isCritical;
156      }
157    
158    
159    
160      /**
161       * Retrieves the value for this control, if available.
162       *
163       * @return  The value for this control, or {@code null} if there is none.
164       */
165      public byte[] getValue()
166      {
167        return value;
168      }
169    
170    
171    
172      /**
173       * Converts this LDAP control to a {@link Control} object.
174       *
175       * @return  The {@code Control} object for this LDAP control.
176       */
177      public final Control toControl()
178      {
179        if (value == null)
180        {
181          return new Control(oid, isCritical, null);
182        }
183        else
184        {
185          return new Control(oid, isCritical, new ASN1OctetString(value));
186        }
187      }
188    
189    
190    
191      /**
192       * Converts the provided array of controls to an array of LDAP controls.
193       *
194       * @param  ldapControls  The LDAP controls to be converted.
195       *
196       * @return  The corresponding array of controls.
197       */
198      public static Control[] toControls(final LDAPControl[] ldapControls)
199      {
200        if (ldapControls == null)
201        {
202          return null;
203        }
204    
205        final Control[] controls = new Control[ldapControls.length];
206        for (int i=0; i < ldapControls.length; i++)
207        {
208          controls[i] = ldapControls[i].toControl();
209        }
210    
211        return controls;
212      }
213    
214    
215    
216      /**
217       * Converts the provided array of LDAP controls to an array of controls.
218       *
219       * @param  controls  The controls to be converted.
220       *
221       * @return  The corresponding array of LDAP controls.
222       */
223      public static LDAPControl[] toLDAPControls(final Control[] controls)
224      {
225        if (controls == null)
226        {
227          return null;
228        }
229    
230        final LDAPControl[] ldapControls = new LDAPControl[controls.length];
231        for (int i=0; i < controls.length; i++)
232        {
233          ldapControls[i] = new LDAPControl(controls[i]);
234        }
235    
236        return ldapControls;
237      }
238    
239    
240    
241      /**
242       * Creates a duplicate of this control.
243       *
244       * @return  A duplicate of this control.
245       */
246      public LDAPControl duplicate()
247      {
248        return new LDAPControl(oid, isCritical, value);
249      }
250    
251    
252    
253      /**
254       * Retrieves a string representation of this control.
255       *
256       * @return  A string representation of this control.
257       */
258      @Override()
259      public String toString()
260      {
261        return toControl().toString();
262      }
263    }