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.controls; 037 038 039 040import com.unboundid.ldap.sdk.Control; 041import com.unboundid.ldap.sdk.LDAPException; 042import com.unboundid.ldap.sdk.ResultCode; 043import com.unboundid.util.NotMutable; 044import com.unboundid.util.ThreadSafety; 045import com.unboundid.util.ThreadSafetyLevel; 046 047import static com.unboundid.ldap.sdk.controls.ControlMessages.*; 048 049 050 051/** 052 * This class provides an implementation of the permissive modify request 053 * control, which is supported by a number of servers and may be included in a 054 * modify request to indicate that the server should not reject a modify 055 * request which attempts to add an attribute value which already exists or 056 * remove an attribute value which does not exist. Normally, such modification 057 * attempts would be rejected. 058 * <BR><BR> 059 * The OID for this control is "1.2.840.113556.1.4.1413". It does not have a 060 * value. 061 * <BR><BR> 062 * <H2>Example</H2> 063 * The following example demonstrates the use of the permissive modify request 064 * control to process a modification that attempts to add an attribute value 065 * to an entry that already contains that value. 066 * <PRE> 067 * // Ensure that we start with a known description value in the test entry 068 * // by using a replace to overwrite any existing value(s). 069 * ModifyRequest replaceRequest = new ModifyRequest( 070 * "uid=test.user,ou=People,dc=example,dc=com", 071 * new Modification(ModificationType.REPLACE, "description", "value")); 072 * LDAPResult replaceResult = connection.modify(replaceRequest); 073 * 074 * // Create a modify request that will attempt to add the value that already 075 * // exists. If we attempt to do this without the permissive modify control, 076 * // the attempt should fail. 077 * ModifyRequest addExistingValueRequest = new ModifyRequest( 078 * "uid=test.user,ou=People,dc=example,dc=com", 079 * new Modification(ModificationType.ADD, "description", "value")); 080 * LDAPResult addExistingValueResultWithoutControl; 081 * try 082 * { 083 * addExistingValueResultWithoutControl = 084 * connection.modify(addExistingValueRequest); 085 * // We shouldn't get here because the attempt to add the existing value 086 * // should fail. 087 * } 088 * catch (LDAPException le) 089 * { 090 * // We expected this failure because the value we're trying to add already 091 * // exists in the entry. 092 * addExistingValueResultWithoutControl = le.toLDAPResult(); 093 * ResultCode resultCode = le.getResultCode(); 094 * String errorMessageFromServer = le.getDiagnosticMessage(); 095 * } 096 * 097 * // Update the modify request to include the permissive modify request 098 * // control, and re-send the request. The operation should now succeed. 099 * addExistingValueRequest.addControl(new PermissiveModifyRequestControl()); 100 * LDAPResult addExistingValueResultWithControl; 101 * try 102 * { 103 * addExistingValueResultWithControl = 104 * connection.modify(addExistingValueRequest); 105 * // If we've gotten here, then the modification was successful. 106 * } 107 * catch (LDAPException le) 108 * { 109 * // If we've gotten here, then the modification failed for some reason. 110 * addExistingValueResultWithControl = le.toLDAPResult(); 111 * ResultCode resultCode = le.getResultCode(); 112 * String errorMessageFromServer = le.getDiagnosticMessage(); 113 * } 114 * </PRE> 115 */ 116@NotMutable() 117@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 118public final class PermissiveModifyRequestControl 119 extends Control 120{ 121 /** 122 * The OID (1.2.840.113556.1.4.1413) for the permissive modify request 123 * control. 124 */ 125 public static final String PERMISSIVE_MODIFY_REQUEST_OID = 126 "1.2.840.113556.1.4.1413"; 127 128 129 130 /** 131 * The serial version UID for this serializable class. 132 */ 133 private static final long serialVersionUID = -2599039772002106760L; 134 135 136 137 /** 138 * Creates a new permissive modify request control. The control will not be 139 * marked critical. 140 */ 141 public PermissiveModifyRequestControl() 142 { 143 super(PERMISSIVE_MODIFY_REQUEST_OID, false, null); 144 } 145 146 147 148 /** 149 * Creates a new permissive modify request control. 150 * 151 * @param isCritical Indicates whether the control should be marked 152 * critical. 153 */ 154 public PermissiveModifyRequestControl(final boolean isCritical) 155 { 156 super(PERMISSIVE_MODIFY_REQUEST_OID, isCritical, null); 157 } 158 159 160 161 /** 162 * Creates a new permissive modify request control which is decoded from the 163 * provided generic control. 164 * 165 * @param control The generic control to be decoded as a permissive modify 166 * request control. 167 * 168 * @throws LDAPException If the provided control cannot be decoded as a 169 * permissive modify request control. 170 */ 171 public PermissiveModifyRequestControl(final Control control) 172 throws LDAPException 173 { 174 super(control); 175 176 if (control.hasValue()) 177 { 178 throw new LDAPException(ResultCode.DECODING_ERROR, 179 ERR_PERMISSIVE_MODIFY_HAS_VALUE.get()); 180 } 181 } 182 183 184 185 /** 186 * {@inheritDoc} 187 */ 188 @Override() 189 public String getControlName() 190 { 191 return INFO_CONTROL_NAME_PERMISSIVE_MODIFY_REQUEST.get(); 192 } 193 194 195 196 /** 197 * {@inheritDoc} 198 */ 199 @Override() 200 public void toString(final StringBuilder buffer) 201 { 202 buffer.append("PermissiveModifyRequestControl(isCritical="); 203 buffer.append(isCritical()); 204 buffer.append(')'); 205 } 206}