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 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.unboundidds.controls.ControlMessages.*;
052
053
054
055/**
056 * This class provides an implementation of the LDAP no-op control as defined in
057 * draft-zeilenga-ldap-noop.  This control may be included in an add, delete,
058 * modify, or modify DN request to indicate that the server should validate the
059 * request but not actually make any changes to the data.  It allows the client
060 * to verify that the operation would likely succeed (including schema
061 * validation, access control checks, and other processing) without making any
062 * changes to the server data.
063 * <BR>
064 * <BLOCKQUOTE>
065 *   <B>NOTE:</B>  This class, and other classes within the
066 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
067 *   supported for use against Ping Identity, UnboundID, and
068 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
069 *   for proprietary functionality or for external specifications that are not
070 *   considered stable or mature enough to be guaranteed to work in an
071 *   interoperable way with other types of LDAP servers.
072 * </BLOCKQUOTE>
073 * <BR>
074 * Note that an operation which includes the no-op control will never have a
075 * {@link ResultCode#SUCCESS} result.  Instead, if the operation would likely
076 * have completed successfully if the no-op control had not been included, then
077 * the server will include a response with the {@link ResultCode#NO_OPERATION}
078 * result.  If the operation would not have been successful, then the result
079 * code in the response will be the appropriate result code for that failure.
080 * Note that if the response from the server includes the
081 * {@link ResultCode#NO_OPERATION} result, then the LDAP SDK will not throw an
082 * exception but will instead return the response in an
083 * {@link com.unboundid.ldap.sdk.LDAPResult} object.  There is no corresponding
084 * response control.
085 * <BR><BR>
086 * Note that at the time this control was written, the latest version of the
087 * specification may be found in draft-zeilenga-ldap-noop-11.  This version of
088 * the document does not explicitly specify either the OID that should be used
089 * for the control, or the result code that should be used for the associated
090 * operation if all other processing is completed successfully but no changes
091 * are made as a result of this control.  Until such time as these are defined,
092 * this implementation uses the OID temporarily assigned for its use by the
093 * OpenLDAP Foundation, which is used by at least the OpenLDAP, OpenDS, and the
094 * Ping Identity, UnboundID, and Nokia/Alcatel-Lucent 8661 Directory Server
095 * implementations.
096 * <BR><BR>
097 * This control has an OID of 1.3.6.1.4.1.4203.1.10.2 and a criticality of true.
098 * It does not have a value.
099 * <BR><BR>
100 * <H2>Example</H2>
101 * The following example demonstrates the process for attempting to perform a
102 * modify operation including the LDAP no-op control so that the change is not
103 * actually applied:
104 * <PRE>
105 * ModifyRequest modifyRequest = new ModifyRequest("dc=example,dc=com",
106 *      new Modification(ModificationType.REPLACE, "description",
107 *           "new value"));
108 * modifyRequest.addControl(new NoOpRequestControl());
109 *
110 * try
111 * {
112 *   LDAPResult result = connection.modify(modifyRequest);
113 *   if (result.getResultCode() == ResultCode.NO_OPERATION)
114 *   {
115 *     // The modify would likely have succeeded.
116 *   }
117 *   else
118 *   {
119 *     // The modify would likely have failed.
120 *   }
121 * }
122 * catch (LDAPException le)
123 * {
124 *   // The modify attempt failed even with the no-op control.
125 * }
126 * </PRE>
127 */
128@NotMutable()
129@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
130public final class NoOpRequestControl
131       extends Control
132{
133  /**
134   * The OID (1.3.6.1.4.1.4203.1.10.2) for the LDAP no-op request control.
135   */
136  @NotNull public static final String NO_OP_REQUEST_OID =
137       "1.3.6.1.4.1.4203.1.10.2";
138
139
140
141  /**
142   * The serial version UID for this serializable class.
143   */
144  private static final long serialVersionUID = -7435407787971958294L;
145
146
147
148  /**
149   * Creates a new no-op request control.  It will be marked critical, as
150   * required by the control specification.
151   */
152  public NoOpRequestControl()
153  {
154    super(NO_OP_REQUEST_OID, true, null);
155  }
156
157
158
159  /**
160   * Creates a new no-op request control which is decoded from the provided
161   * generic control.
162   *
163   * @param  control  The generic control to be decoded as a no-op request
164   *                  control.
165   *
166   * @throws  LDAPException  If the provided control cannot be decoded as a
167   *                         no-op request control.
168   */
169  public NoOpRequestControl(@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_NOOP_REQUEST_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_NOOP_REQUEST.get();
191  }
192
193
194
195  /**
196   * Retrieves a representation of this no-op request control as a JSON object.
197   * The JSON object uses the following fields (note that since this control
198   * 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 no-op request control, the OID is
204   *     "1.3.6.1.4.1.4203.1.10.2".
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              NO_OP_REQUEST_OID),
227         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CONTROL_NAME,
228              INFO_CONTROL_NAME_NOOP_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 no-op
237   * 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 no-op request control that was decoded from the provided JSON
249   *          object.
250   *
251   * @throws  LDAPException  If the provided JSON object cannot be parsed as a
252   *                         valid no-op request control.
253   */
254  @NotNull()
255  public static NoOpRequestControl 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 NoOpRequestControl();
264  }
265
266
267
268  /**
269   * {@inheritDoc}
270   */
271  @Override()
272  public void toString(@NotNull final StringBuilder buffer)
273  {
274    buffer.append("NoOpRequestControl(isCritical=");
275    buffer.append(isCritical());
276    buffer.append(')');
277  }
278}