001/* 002 * Copyright 2008-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2008-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) 2008-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.unboundidds.controls; 037 038 039 040import java.util.ArrayList; 041import java.util.Arrays; 042import java.util.Collection; 043import java.util.Collections; 044import java.util.List; 045import java.util.Iterator; 046 047import com.unboundid.asn1.ASN1Element; 048import com.unboundid.asn1.ASN1OctetString; 049import com.unboundid.asn1.ASN1Sequence; 050import com.unboundid.ldap.sdk.Control; 051import com.unboundid.ldap.sdk.JSONControlDecodeHelper; 052import com.unboundid.ldap.sdk.LDAPException; 053import com.unboundid.ldap.sdk.ResultCode; 054import com.unboundid.util.Debug; 055import com.unboundid.util.NotMutable; 056import com.unboundid.util.NotNull; 057import com.unboundid.util.StaticUtils; 058import com.unboundid.util.ThreadSafety; 059import com.unboundid.util.ThreadSafetyLevel; 060import com.unboundid.util.Validator; 061import com.unboundid.util.json.JSONArray; 062import com.unboundid.util.json.JSONField; 063import com.unboundid.util.json.JSONObject; 064import com.unboundid.util.json.JSONString; 065import com.unboundid.util.json.JSONValue; 066 067import static com.unboundid.ldap.sdk.unboundidds.controls.ControlMessages.*; 068 069 070 071/** 072 * This class provides a request control which may be used to request that 073 * entries below one or more base DNs be excluded from the results returned to 074 * a client while processing a search operation. For example, this may be 075 * useful in cases where you want to perform a search below "dc=example,dc=com", 076 * but want to exclude all entries below "ou=private,dc=example,dc=com". 077 * <BR> 078 * <BLOCKQUOTE> 079 * <B>NOTE:</B> This class, and other classes within the 080 * {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only 081 * supported for use against Ping Identity, UnboundID, and 082 * Nokia/Alcatel-Lucent 8661 server products. These classes provide support 083 * for proprietary functionality or for external specifications that are not 084 * considered stable or mature enough to be guaranteed to work in an 085 * interoperable way with other types of LDAP servers. 086 * </BLOCKQUOTE> 087 * <BR> 088 * The criticality for this control may be either {@code true} or {@code false}. 089 * It must have a value with the following encoding: 090 * <PRE> 091 * ExcludeBranchRequest ::= SEQUENCE { 092 * baseDNs [0] SEQUENCE OF LDAPDN, 093 * ... } 094 * </PRE> 095 */ 096@NotMutable() 097@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 098public final class ExcludeBranchRequestControl 099 extends Control 100{ 101 /** 102 * The OID (1.3.6.1.4.1.30221.2.5.17) for the exclude branch request control. 103 */ 104 @NotNull public static final String EXCLUDE_BRANCH_REQUEST_OID = 105 "1.3.6.1.4.1.30221.2.5.17"; 106 107 108 109 /** 110 * The BER type for the base DNs element. 111 */ 112 private static final byte TYPE_BASE_DNS = (byte) 0xA0; 113 114 115 116 /** 117 * The name of the field used to represent the base DNs in the JSON 118 * representation of this control. 119 */ 120 @NotNull private static final String JSON_FIELD_BASE_DNS = "base-dns"; 121 122 123 124 /** 125 * The serial version UID for this serializable class. 126 */ 127 private static final long serialVersionUID = -8599554860060612417L; 128 129 130 131 // The list of base DNs to be excluded from the search results. 132 @NotNull private final List<String> baseDNs; 133 134 135 136 /** 137 * Creates a new exclude branch request control with the provided set of base 138 * DNs. It will be marked critical. 139 * 140 * @param baseDNs The base DNs for entries to be excluded from search 141 * results. It must not be {@code null} or empty. 142 */ 143 public ExcludeBranchRequestControl(@NotNull final Collection<String> baseDNs) 144 { 145 this(true, baseDNs); 146 } 147 148 149 150 /** 151 * Creates a new exclude branch request control with the provided set of base 152 * DNs. It will be marked critical. 153 * 154 * @param baseDNs The base DNs for entries to be excluded from search 155 * results. It must not be {@code null} or empty. 156 */ 157 public ExcludeBranchRequestControl(@NotNull final String... baseDNs) 158 { 159 this(true, baseDNs); 160 } 161 162 163 164 /** 165 * Creates a new exclude branch request control with the provided information. 166 * 167 * @param isCritical Indicates whether the control should be marked 168 * critical. 169 * @param baseDNs The base DNs for entries to be excluded from search 170 * results. It must not be {@code null} or empty. 171 */ 172 public ExcludeBranchRequestControl(final boolean isCritical, 173 @NotNull final String... baseDNs) 174 { 175 super(EXCLUDE_BRANCH_REQUEST_OID, isCritical, encodeValue(baseDNs)); 176 177 this.baseDNs = Collections.unmodifiableList(Arrays.asList(baseDNs)); 178 } 179 180 181 182 /** 183 * Creates a new exclude branch request control with the provided information. 184 * 185 * @param isCritical Indicates whether the control should be marked 186 * critical. 187 * @param baseDNs The base DNs for entries to be excluded from search 188 * results. It must not be {@code null} or empty. 189 */ 190 public ExcludeBranchRequestControl(final boolean isCritical, 191 @NotNull final Collection<String> baseDNs) 192 { 193 super(EXCLUDE_BRANCH_REQUEST_OID, isCritical, encodeValue(baseDNs)); 194 195 this.baseDNs = Collections.unmodifiableList(new ArrayList<>(baseDNs)); 196 } 197 198 199 200 /** 201 * Creates a new exclude branch request control which is decoded from the 202 * provided generic control. 203 * 204 * @param control The generic control to be decoded as an exclude branch 205 * request control. 206 * 207 * @throws LDAPException If the provided control cannot be decoded as an 208 * exclude branch request control. 209 */ 210 public ExcludeBranchRequestControl(@NotNull final Control control) 211 throws LDAPException 212 { 213 super(control); 214 215 final ASN1OctetString value = control.getValue(); 216 if (value == null) 217 { 218 throw new LDAPException(ResultCode.DECODING_ERROR, 219 ERR_EXCLUDE_BRANCH_MISSING_VALUE.get()); 220 } 221 222 final ASN1Sequence valueSequence; 223 try 224 { 225 valueSequence = ASN1Sequence.decodeAsSequence(value.getValue()); 226 } 227 catch (final Exception e) 228 { 229 Debug.debugException(e); 230 throw new LDAPException(ResultCode.DECODING_ERROR, 231 ERR_EXCLUDE_BRANCH_VALUE_NOT_SEQUENCE.get( 232 StaticUtils.getExceptionMessage(e)), e); 233 } 234 235 try 236 { 237 final ASN1Element[] elements = valueSequence.elements(); 238 239 final ASN1Element[] dnElements = 240 ASN1Sequence.decodeAsSequence(elements[0]).elements(); 241 final ArrayList<String> dnList = new ArrayList<>(dnElements.length); 242 for (final ASN1Element e : dnElements) 243 { 244 dnList.add(ASN1OctetString.decodeAsOctetString(e).stringValue()); 245 } 246 baseDNs = Collections.unmodifiableList(dnList); 247 248 if (baseDNs.isEmpty()) 249 { 250 throw new LDAPException(ResultCode.DECODING_ERROR, 251 ERR_EXCLUDE_BRANCH_NO_BASE_DNS.get()); 252 } 253 } 254 catch (final LDAPException le) 255 { 256 Debug.debugException(le); 257 throw le; 258 } 259 catch (final Exception e) 260 { 261 Debug.debugException(e); 262 throw new LDAPException(ResultCode.DECODING_ERROR, 263 ERR_EXCLUDE_BRANCH_ERROR_PARSING_VALUE.get( 264 StaticUtils.getExceptionMessage(e)), e); 265 } 266 } 267 268 269 270 /** 271 * Encodes the provided information into a form suitable for use as the value 272 * of this control. 273 * 274 * @param baseDNs The base DNs for entries to be excluded from search 275 * results. It must not be {@code null} or empty. 276 * 277 * @return The encoded value for this control. 278 */ 279 @NotNull() 280 private static ASN1OctetString encodeValue(@NotNull final String... baseDNs) 281 { 282 Validator.ensureNotNull(baseDNs); 283 return encodeValue(Arrays.asList(baseDNs)); 284 } 285 286 287 288 /** 289 * Encodes the provided information into a form suitable for use as the value 290 * of this control. 291 * 292 * @param baseDNs The base DNs for entries to be excluded from search 293 * results. It must not be {@code null} or empty. 294 * 295 * @return The encoded value for this control. 296 */ 297 @NotNull() 298 private static ASN1OctetString encodeValue( 299 @NotNull final Collection<String> baseDNs) 300 { 301 Validator.ensureNotNull(baseDNs); 302 Validator.ensureFalse(baseDNs.isEmpty()); 303 304 final ArrayList<ASN1Element> dnElements = new ArrayList<>(baseDNs.size()); 305 for (final String s : baseDNs) 306 { 307 dnElements.add(new ASN1OctetString(s)); 308 } 309 310 final ASN1Sequence baseDNSequence = 311 new ASN1Sequence(TYPE_BASE_DNS, dnElements); 312 final ASN1Sequence valueSequence = new ASN1Sequence(baseDNSequence); 313 return new ASN1OctetString(valueSequence.encode()); 314 } 315 316 317 318 /** 319 * Retrieves a list of the base DNs for entries to exclude from the search 320 * results. 321 * 322 * @return A list of the base DNs for entries to exclude from the search 323 * results. 324 */ 325 @NotNull() 326 public List<String> getBaseDNs() 327 { 328 return baseDNs; 329 } 330 331 332 333 /** 334 * {@inheritDoc} 335 */ 336 @Override() 337 @NotNull() 338 public String getControlName() 339 { 340 return INFO_CONTROL_NAME_EXCLUDE_BRANCH.get(); 341 } 342 343 344 345 /** 346 * Retrieves a representation of this exclude branch request control as a JSON 347 * object. The JSON object uses the following fields: 348 * <UL> 349 * <LI> 350 * {@code oid} -- A mandatory string field whose value is the object 351 * identifier for this control. For the exclude branch request control, 352 * the OID is "1.3.6.1.4.1.30221.2.5.17". 353 * </LI> 354 * <LI> 355 * {@code control-name} -- An optional string field whose value is a 356 * human-readable name for this control. This field is only intended for 357 * descriptive purposes, and when decoding a control, the {@code oid} 358 * field should be used to identify the type of control. 359 * </LI> 360 * <LI> 361 * {@code criticality} -- A mandatory Boolean field used to indicate 362 * whether this control is considered critical. 363 * </LI> 364 * <LI> 365 * {@code value-base64} -- An optional string field whose value is a 366 * base64-encoded representation of the raw value for this exclude branch 367 * request control. Exactly one of the {@code value-base64} and 368 * {@code value-json} fields must be present. 369 * </LI> 370 * <LI> 371 * {@code value-json} -- An optional JSON object field whose value is a 372 * user-friendly representation of the value for this exclude branch 373 * request control. Exactly one of the {@code value-base64} and 374 * {@code value-json} fields must be present, and if the 375 * {@code value-json} field is used, then it will use the following 376 * fields: 377 * <UL> 378 * <LI> 379 * {@code base-dns} -- A mandatory, non-empty array field whose values 380 * must be strings that represent the base DNs of branches to exclude 381 * from the search results. 382 * </LI> 383 * </UL> 384 * </LI> 385 * </UL> 386 * 387 * @return A JSON object that contains a representation of this control. 388 */ 389 @Override() 390 @NotNull() 391 public JSONObject toJSONControl() 392 { 393 final List<JSONValue> baseDNValues = new ArrayList<>(baseDNs.size()); 394 for (final String baseDN : baseDNs) 395 { 396 baseDNValues.add(new JSONString(baseDN)); 397 } 398 399 return new JSONObject( 400 new JSONField(JSONControlDecodeHelper.JSON_FIELD_OID, 401 EXCLUDE_BRANCH_REQUEST_OID), 402 new JSONField(JSONControlDecodeHelper.JSON_FIELD_CONTROL_NAME, 403 INFO_CONTROL_NAME_EXCLUDE_BRANCH.get()), 404 new JSONField(JSONControlDecodeHelper.JSON_FIELD_CRITICALITY, 405 isCritical()), 406 new JSONField(JSONControlDecodeHelper.JSON_FIELD_VALUE_JSON, 407 new JSONObject( 408 new JSONField(JSON_FIELD_BASE_DNS, 409 new JSONArray(baseDNValues))))); 410 } 411 412 413 414 /** 415 * Attempts to decode the provided object as a JSON representation of an 416 * exclude branch request control. 417 * 418 * @param controlObject The JSON object to be decoded. It must not be 419 * {@code null}. 420 * @param strict Indicates whether to use strict mode when decoding 421 * the provided JSON object. If this is {@code true}, 422 * then this method will throw an exception if the 423 * provided JSON object contains any unrecognized 424 * fields. If this is {@code false}, then unrecognized 425 * fields will be ignored. 426 * 427 * @return The exclude branch request control that was decoded from the 428 * provided JSON object. 429 * 430 * @throws LDAPException If the provided JSON object cannot be parsed as a 431 * valid exclude branch request control. 432 */ 433 @NotNull() 434 public static ExcludeBranchRequestControl decodeJSONControl( 435 @NotNull final JSONObject controlObject, 436 final boolean strict) 437 throws LDAPException 438 { 439 final JSONControlDecodeHelper jsonControl = new JSONControlDecodeHelper( 440 controlObject, strict, true, true); 441 442 final ASN1OctetString rawValue = jsonControl.getRawValue(); 443 if (rawValue != null) 444 { 445 return new ExcludeBranchRequestControl(new Control(jsonControl.getOID(), 446 jsonControl.getCriticality(), rawValue)); 447 } 448 449 450 final JSONObject valueObject = jsonControl.getValueObject(); 451 452 final List<JSONValue> baseDNValues = 453 valueObject.getFieldAsArray(JSON_FIELD_BASE_DNS); 454 if (baseDNValues == null) 455 { 456 throw new LDAPException(ResultCode.DECODING_ERROR, 457 ERR_EXCLUDE_BRANCH_JSON_MISSING_BASE_DNS.get( 458 controlObject.toSingleLineString(), JSON_FIELD_BASE_DNS)); 459 } 460 461 if (baseDNValues.isEmpty()) 462 { 463 throw new LDAPException(ResultCode.DECODING_ERROR, 464 ERR_EXCLUDE_BRANCH_JSON_EMPTY_BASE_DNS.get( 465 controlObject.toSingleLineString())); 466 } 467 468 469 final List<String> baseDNs = new ArrayList<>(baseDNValues.size()); 470 for (final JSONValue baseDNValue : baseDNValues) 471 { 472 if (baseDNValue instanceof JSONString) 473 { 474 baseDNs.add(((JSONString) baseDNValue).stringValue()); 475 } 476 else 477 { 478 throw new LDAPException(ResultCode.DECODING_ERROR, 479 ERR_EXCLUDE_BRANCH_JSON_BASE_DN_NOT_STRING.get( 480 controlObject.toSingleLineString(), JSON_FIELD_BASE_DNS)); 481 } 482 } 483 484 485 if (strict) 486 { 487 final List<String> unrecognizedFields = 488 JSONControlDecodeHelper.getControlObjectUnexpectedFields( 489 valueObject, JSON_FIELD_BASE_DNS); 490 if (! unrecognizedFields.isEmpty()) 491 { 492 throw new LDAPException(ResultCode.DECODING_ERROR, 493 ERR_EXCLUDE_BRANCH_JSON_CONTROL_UNRECOGNIZED_FIELD.get( 494 controlObject.toSingleLineString(), 495 unrecognizedFields.get(0))); 496 } 497 } 498 499 500 return new ExcludeBranchRequestControl( 501 jsonControl.getCriticality(), baseDNs); 502 } 503 504 505 506 /** 507 * {@inheritDoc} 508 */ 509 @Override() 510 public void toString(@NotNull final StringBuilder buffer) 511 { 512 buffer.append("ExcludeBranchRequestControl(isCritical="); 513 buffer.append(isCritical()); 514 buffer.append(", baseDNs={"); 515 516 final Iterator<String> iterator = baseDNs.iterator(); 517 while (iterator.hasNext()) 518 { 519 buffer.append('\''); 520 buffer.append(iterator.next()); 521 buffer.append('\''); 522 523 if (iterator.hasNext()) 524 { 525 buffer.append(", "); 526 } 527 } 528 529 buffer.append("})"); 530 } 531}