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