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