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