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.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 permissive modify request 057 * control, which is supported by a number of servers and may be included in a 058 * modify request to indicate that the server should not reject a modify 059 * request which attempts to add an attribute value which already exists or 060 * remove an attribute value which does not exist. Normally, such modification 061 * attempts would be rejected. 062 * <BR><BR> 063 * The OID for this control is "1.2.840.113556.1.4.1413". It does not have a 064 * value. 065 * <BR><BR> 066 * <H2>Example</H2> 067 * The following example demonstrates the use of the permissive modify request 068 * control to process a modification that attempts to add an attribute value 069 * to an entry that already contains that value. 070 * <PRE> 071 * // Ensure that we start with a known description value in the test entry 072 * // by using a replace to overwrite any existing value(s). 073 * ModifyRequest replaceRequest = new ModifyRequest( 074 * "uid=test.user,ou=People,dc=example,dc=com", 075 * new Modification(ModificationType.REPLACE, "description", "value")); 076 * LDAPResult replaceResult = connection.modify(replaceRequest); 077 * 078 * // Create a modify request that will attempt to add the value that already 079 * // exists. If we attempt to do this without the permissive modify control, 080 * // the attempt should fail. 081 * ModifyRequest addExistingValueRequest = new ModifyRequest( 082 * "uid=test.user,ou=People,dc=example,dc=com", 083 * new Modification(ModificationType.ADD, "description", "value")); 084 * LDAPResult addExistingValueResultWithoutControl; 085 * try 086 * { 087 * addExistingValueResultWithoutControl = 088 * connection.modify(addExistingValueRequest); 089 * // We shouldn't get here because the attempt to add the existing value 090 * // should fail. 091 * } 092 * catch (LDAPException le) 093 * { 094 * // We expected this failure because the value we're trying to add already 095 * // exists in the entry. 096 * addExistingValueResultWithoutControl = le.toLDAPResult(); 097 * ResultCode resultCode = le.getResultCode(); 098 * String errorMessageFromServer = le.getDiagnosticMessage(); 099 * } 100 * 101 * // Update the modify request to include the permissive modify request 102 * // control, and re-send the request. The operation should now succeed. 103 * addExistingValueRequest.addControl(new PermissiveModifyRequestControl()); 104 * LDAPResult addExistingValueResultWithControl; 105 * try 106 * { 107 * addExistingValueResultWithControl = 108 * connection.modify(addExistingValueRequest); 109 * // If we've gotten here, then the modification was successful. 110 * } 111 * catch (LDAPException le) 112 * { 113 * // If we've gotten here, then the modification failed for some reason. 114 * addExistingValueResultWithControl = le.toLDAPResult(); 115 * ResultCode resultCode = le.getResultCode(); 116 * String errorMessageFromServer = le.getDiagnosticMessage(); 117 * } 118 * </PRE> 119 */ 120@NotMutable() 121@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 122public final class PermissiveModifyRequestControl 123 extends Control 124{ 125 /** 126 * The OID (1.2.840.113556.1.4.1413) for the permissive modify request 127 * control. 128 */ 129 @NotNull public static final String PERMISSIVE_MODIFY_REQUEST_OID = 130 "1.2.840.113556.1.4.1413"; 131 132 133 134 /** 135 * The serial version UID for this serializable class. 136 */ 137 private static final long serialVersionUID = -2599039772002106760L; 138 139 140 141 /** 142 * Creates a new permissive modify request control. The control will not be 143 * marked critical. 144 */ 145 public PermissiveModifyRequestControl() 146 { 147 super(PERMISSIVE_MODIFY_REQUEST_OID, false, null); 148 } 149 150 151 152 /** 153 * Creates a new permissive modify request control. 154 * 155 * @param isCritical Indicates whether the control should be marked 156 * critical. 157 */ 158 public PermissiveModifyRequestControl(final boolean isCritical) 159 { 160 super(PERMISSIVE_MODIFY_REQUEST_OID, isCritical, null); 161 } 162 163 164 165 /** 166 * Creates a new permissive modify request control which is decoded from the 167 * provided generic control. 168 * 169 * @param control The generic control to be decoded as a permissive modify 170 * request control. 171 * 172 * @throws LDAPException If the provided control cannot be decoded as a 173 * permissive modify request control. 174 */ 175 public PermissiveModifyRequestControl(@NotNull final Control control) 176 throws LDAPException 177 { 178 super(control); 179 180 if (control.hasValue()) 181 { 182 throw new LDAPException(ResultCode.DECODING_ERROR, 183 ERR_PERMISSIVE_MODIFY_HAS_VALUE.get()); 184 } 185 } 186 187 188 189 /** 190 * {@inheritDoc} 191 */ 192 @Override() 193 @NotNull() 194 public String getControlName() 195 { 196 return INFO_CONTROL_NAME_PERMISSIVE_MODIFY_REQUEST.get(); 197 } 198 199 200 201 /** 202 * Retrieves a representation of this permissive modify request control as a 203 * JSON object. The JSON object uses the following fields (note that since 204 * this control does not have a value, neither the {@code value-base64} nor 205 * {@code value-json} fields may be present): 206 * <UL> 207 * <LI> 208 * {@code oid} -- A mandatory string field whose value is the object 209 * identifier for this control. For the permissive modify request 210 * control, the OID is "1.2.840.113556.1.4.1413". 211 * </LI> 212 * <LI> 213 * {@code control-name} -- An optional string field whose value is a 214 * human-readable name for this control. This field is only intended for 215 * descriptive purposes, and when decoding a control, the {@code oid} 216 * field should be used to identify the type of control. 217 * </LI> 218 * <LI> 219 * {@code criticality} -- A mandatory Boolean field used to indicate 220 * whether this control is considered critical. 221 * </LI> 222 * </UL> 223 * 224 * @return A JSON object that contains a representation of this control. 225 */ 226 @Override() 227 @NotNull() 228 public JSONObject toJSONControl() 229 { 230 return new JSONObject( 231 new JSONField(JSONControlDecodeHelper.JSON_FIELD_OID, 232 PERMISSIVE_MODIFY_REQUEST_OID), 233 new JSONField(JSONControlDecodeHelper.JSON_FIELD_CONTROL_NAME, 234 INFO_CONTROL_NAME_PERMISSIVE_MODIFY_REQUEST.get()), 235 new JSONField(JSONControlDecodeHelper.JSON_FIELD_CRITICALITY, 236 isCritical())); 237 } 238 239 240 241 /** 242 * Attempts to decode the provided object as a JSON representation of a 243 * permissive modify request control. 244 * 245 * @param controlObject The JSON object to be decoded. It must not be 246 * {@code null}. 247 * @param strict Indicates whether to use strict mode when decoding 248 * the provided JSON object. If this is {@code true}, 249 * then this method will throw an exception if the 250 * provided JSON object contains any unrecognized 251 * fields. If this is {@code false}, then unrecognized 252 * fields will be ignored. 253 * 254 * @return The permissive modify request control that was decoded from 255 * the provided JSON object. 256 * 257 * @throws LDAPException If the provided JSON object cannot be parsed as a 258 * valid permissive modify request control. 259 */ 260 @NotNull() 261 public static PermissiveModifyRequestControl decodeJSONControl( 262 @NotNull final JSONObject controlObject, 263 final boolean strict) 264 throws LDAPException 265 { 266 final JSONControlDecodeHelper jsonControl = new JSONControlDecodeHelper( 267 controlObject, strict, false, false); 268 269 return new PermissiveModifyRequestControl(jsonControl.getCriticality()); 270 } 271 272 273 274 /** 275 * {@inheritDoc} 276 */ 277 @Override() 278 public void toString(@NotNull final StringBuilder buffer) 279 { 280 buffer.append("PermissiveModifyRequestControl(isCritical="); 281 buffer.append(isCritical()); 282 buffer.append(')'); 283 } 284}