001/* 002 * Copyright 2007-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2007-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) 2007-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.controls; 037 038 039 040import com.unboundid.ldap.sdk.Control; 041import com.unboundid.ldap.sdk.JSONControlDecodeHelper; 042import com.unboundid.ldap.sdk.LDAPException; 043import com.unboundid.ldap.sdk.ResultCode; 044import com.unboundid.util.NotMutable; 045import com.unboundid.util.NotNull; 046import com.unboundid.util.ThreadSafety; 047import com.unboundid.util.ThreadSafetyLevel; 048import com.unboundid.util.json.JSONField; 049import com.unboundid.util.json.JSONObject; 050 051import static com.unboundid.ldap.sdk.controls.ControlMessages.*; 052 053 054 055/** 056 * This class provides an implementation of the subtree delete request control 057 * as defined in draft-armijo-ldap-treedelete. This can be used to delete an 058 * entry and all subordinate entries in a single operation. 059 * <BR><BR> 060 * Normally, if an entry has one or more subordinates, a directory server will 061 * refuse to delete it by rejecting the request with a 062 * {@link ResultCode#NOT_ALLOWED_ON_NONLEAF} result. In such cases, it is 063 * necessary to first recursively remove all of its subordinates before the 064 * target entry can be deleted. However, this subtree delete request control 065 * can be used to request that the server remove the entry and all subordinates 066 * as a single operation. For servers that support this control, it is 067 * generally much more efficient and convenient than removing all of the 068 * subordinate entries one at a time. 069 * <BR><BR> 070 * <H2>Example</H2> 071 * The following example demonstrates the use of the subtree delete control: 072 * <PRE> 073 * // First, try to delete an entry that has children, but don't include the 074 * // subtree delete control. This delete attempt should fail, and the 075 * // "NOT_ALLOWED_ON_NONLEAF" result is most appropriate if the failure reason 076 * // is that the entry has subordinates. 077 * DeleteRequest deleteRequest = 078 * new DeleteRequest("ou=entry with children,dc=example,dc=com"); 079 * LDAPResult resultWithoutControl; 080 * try 081 * { 082 * resultWithoutControl = connection.delete(deleteRequest); 083 * // We shouldn't get here because the delete should fail. 084 * } 085 * catch (LDAPException le) 086 * { 087 * // This is expected because the entry has children. 088 * resultWithoutControl = le.toLDAPResult(); 089 * ResultCode resultCode = le.getResultCode(); 090 * String errorMessageFromServer = le.getDiagnosticMessage(); 091 * } 092 * LDAPTestUtils.assertResultCodeEquals(resultWithoutControl, 093 * ResultCode.NOT_ALLOWED_ON_NONLEAF); 094 * 095 * // Update the delete request to include the subtree delete request control 096 * // and try again. 097 * deleteRequest.addControl(new SubtreeDeleteRequestControl()); 098 * LDAPResult resultWithControl; 099 * try 100 * { 101 * resultWithControl = connection.delete(deleteRequest); 102 * // The delete should no longer be rejected just because the target entry 103 * // has children. 104 * } 105 * catch (LDAPException le) 106 * { 107 * // The delete still failed for some other reason. 108 * resultWithControl = le.toLDAPResult(); 109 * ResultCode resultCode = le.getResultCode(); 110 * String errorMessageFromServer = le.getDiagnosticMessage(); 111 * } 112 * LDAPTestUtils.assertResultCodeEquals(resultWithControl, ResultCode.SUCCESS); 113 * </PRE> 114 */ 115@NotMutable() 116@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 117public final class SubtreeDeleteRequestControl 118 extends Control 119{ 120 /** 121 * The OID (1.2.840.113556.1.4.805) for the subtree delete request control. 122 */ 123 @NotNull public static final String SUBTREE_DELETE_REQUEST_OID = 124 "1.2.840.113556.1.4.805"; 125 126 127 128 /** 129 * The serial version UID for this serializable class. 130 */ 131 private static final long serialVersionUID = 3748121547717081961L; 132 133 134 135 /** 136 * Creates a new subtree delete request control. The control will not be 137 * marked critical. 138 */ 139 public SubtreeDeleteRequestControl() 140 { 141 super(SUBTREE_DELETE_REQUEST_OID, false, null); 142 } 143 144 145 146 /** 147 * Creates a new subtree delete request control. 148 * 149 * @param isCritical Indicates whether the control should be marked 150 * critical. 151 */ 152 public SubtreeDeleteRequestControl(final boolean isCritical) 153 { 154 super(SUBTREE_DELETE_REQUEST_OID, isCritical, null); 155 } 156 157 158 159 /** 160 * Creates a new subtree delete request control which is decoded from the 161 * provided generic control. 162 * 163 * @param control The generic control to be decoded as a subtree delete 164 * request control. 165 * 166 * @throws LDAPException If the provided control cannot be decoded as a 167 * subtree delete request control. 168 */ 169 public SubtreeDeleteRequestControl(@NotNull final Control control) 170 throws LDAPException 171 { 172 super(control); 173 174 if (control.hasValue()) 175 { 176 throw new LDAPException(ResultCode.DECODING_ERROR, 177 ERR_SUBTREE_DELETE_HAS_VALUE.get()); 178 } 179 } 180 181 182 183 /** 184 * {@inheritDoc} 185 */ 186 @Override() 187 @NotNull() 188 public String getControlName() 189 { 190 return INFO_CONTROL_NAME_SUBTREE_DELETE_REQUEST.get(); 191 } 192 193 194 195 /** 196 * Retrieves a representation of this subtree delete request control as a 197 * JSON object. The JSON object uses the following fields (note that since 198 * this control does not have a value, neither the {@code value-base64} nor 199 * {@code value-json} fields may be present): 200 * <UL> 201 * <LI> 202 * {@code oid} -- A mandatory string field whose value is the object 203 * identifier for this control. For the subtree delete request control, 204 * the OID is "1.2.840.113556.1.4.805". 205 * </LI> 206 * <LI> 207 * {@code control-name} -- An optional string field whose value is a 208 * human-readable name for this control. This field is only intended for 209 * descriptive purposes, and when decoding a control, the {@code oid} 210 * field should be used to identify the type of control. 211 * </LI> 212 * <LI> 213 * {@code criticality} -- A mandatory Boolean field used to indicate 214 * whether this control is considered critical. 215 * </LI> 216 * </UL> 217 * 218 * @return A JSON object that contains a representation of this control. 219 */ 220 @Override() 221 @NotNull() 222 public JSONObject toJSONControl() 223 { 224 return new JSONObject( 225 new JSONField(JSONControlDecodeHelper.JSON_FIELD_OID, 226 SUBTREE_DELETE_REQUEST_OID), 227 new JSONField(JSONControlDecodeHelper.JSON_FIELD_CONTROL_NAME, 228 INFO_CONTROL_NAME_SUBTREE_DELETE_REQUEST.get()), 229 new JSONField(JSONControlDecodeHelper.JSON_FIELD_CRITICALITY, 230 isCritical())); 231 } 232 233 234 235 /** 236 * Attempts to decode the provided object as a JSON representation of a 237 * subtree delete request control. 238 * 239 * @param controlObject The JSON object to be decoded. It must not be 240 * {@code null}. 241 * @param strict Indicates whether to use strict mode when decoding 242 * the provided JSON object. If this is {@code true}, 243 * then this method will throw an exception if the 244 * provided JSON object contains any unrecognized 245 * fields. If this is {@code false}, then unrecognized 246 * fields will be ignored. 247 * 248 * @return The subtree delete request control that was decoded from 249 * the provided JSON object. 250 * 251 * @throws LDAPException If the provided JSON object cannot be parsed as a 252 * valid subtree delete request control. 253 */ 254 @NotNull() 255 public static SubtreeDeleteRequestControl decodeJSONControl( 256 @NotNull final JSONObject controlObject, 257 final boolean strict) 258 throws LDAPException 259 { 260 final JSONControlDecodeHelper jsonControl = new JSONControlDecodeHelper( 261 controlObject, strict, false, false); 262 263 return new SubtreeDeleteRequestControl(jsonControl.getCriticality()); 264 } 265 266 267 268 /** 269 * {@inheritDoc} 270 */ 271 @Override() 272 public void toString(@NotNull final StringBuilder buffer) 273 { 274 buffer.append("SubtreeDeleteRequestControl(isCritical="); 275 buffer.append(isCritical()); 276 buffer.append(')'); 277 } 278}