001/*
002 * Copyright 2012-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2012-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) 2012-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.asn1.ASN1Element;
041import com.unboundid.asn1.ASN1Sequence;
042import com.unboundid.ldap.sdk.Control;
043import com.unboundid.ldap.sdk.DeleteRequest;
044import com.unboundid.ldap.sdk.JSONControlDecodeHelper;
045import com.unboundid.ldap.sdk.LDAPException;
046import com.unboundid.ldap.sdk.ResultCode;
047import com.unboundid.util.Debug;
048import com.unboundid.util.NotMutable;
049import com.unboundid.util.NotNull;
050import com.unboundid.util.StaticUtils;
051import com.unboundid.util.ThreadSafety;
052import com.unboundid.util.ThreadSafetyLevel;
053import com.unboundid.util.json.JSONField;
054import com.unboundid.util.json.JSONObject;
055
056import static com.unboundid.ldap.sdk.unboundidds.controls.ControlMessages.*;
057
058
059
060/**
061 * This class provides a request control which may be included in a delete
062 * request to indicate that the server should completely remove the target
063 * entry, even if it would otherwise process the operation as a soft delete and
064 * merely hide the entry from most clients.
065 * <BR>
066 * <BLOCKQUOTE>
067 *   <B>NOTE:</B>  This class, and other classes within the
068 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
069 *   supported for use against Ping Identity, UnboundID, and
070 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
071 *   for proprietary functionality or for external specifications that are not
072 *   considered stable or mature enough to be guaranteed to work in an
073 *   interoperable way with other types of LDAP servers.
074 * </BLOCKQUOTE>
075 * <BR>
076 * The criticality for this control may be either {@code TRUE} or {@code FALSE},
077 * but this will only impact how the delete request is to be handled by servers
078 * which do not support this control.  A criticality of {@code TRUE} will cause
079 * any server which does not support this control to reject the request, while
080 * a criticality of {@code FALSE} should cause the delete request to be
081 * processed as if the control had not been included (i.e., as a regular "hard"
082 * delete).
083 * <BR><BR>
084 * The control may optionally have a value.  If a value is provided, then it
085 * must be the encoded representation of an empty ASN.1 sequence, like:
086 * <PRE>
087 *   HardDeleteRequestValue ::= SEQUENCE {
088 *     ... }
089 * </PRE>
090 * In the future, the value sequence may allow one or more elements to customize
091 * the behavior of the hard delete operation, but at present no such elements
092 * are defined.
093 * See the documentation for the {@link SoftDeleteRequestControl} class for an
094 * example demonstrating the use of this control.
095 *
096 * @see  SoftDeleteRequestControl
097 */
098@NotMutable()
099@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
100public final class HardDeleteRequestControl
101       extends Control
102{
103  /**
104   * The OID (1.3.6.1.4.1.30221.2.5.22) for the hard delete request control.
105   */
106  @NotNull public static final String HARD_DELETE_REQUEST_OID =
107       "1.3.6.1.4.1.30221.2.5.22";
108
109
110
111  /**
112   * The serial version UID for this serializable class.
113   */
114  private static final long serialVersionUID = 1169625153021056712L;
115
116
117
118  /**
119   * Creates a new hard delete request control.  It will not be marked critical.
120   */
121  public HardDeleteRequestControl()
122  {
123    this(false);
124  }
125
126
127
128  /**
129   * Creates a new hard delete request control with the provided information.
130   *
131   * @param  isCritical  Indicates whether this control should be marked
132   *                     critical.  This will only have an effect on the way the
133   *                     associated delete operation is handled by servers which
134   *                     do NOT support the hard delete request control.  For
135   *                     such servers, a control that is critical will cause the
136   *                     hard delete attempt to fail, while a control that is
137   *                     not critical will be processed as if the control was
138   *                     not included in the request (i.e., as a normal "hard"
139   *                     delete).
140   */
141  public HardDeleteRequestControl(final boolean isCritical)
142  {
143    super(HARD_DELETE_REQUEST_OID, isCritical, null);
144  }
145
146
147
148  /**
149   * Creates a new hard delete request control which is decoded from the
150   * provided generic control.
151   *
152   * @param  control  The generic control to be decoded as a hard delete request
153   *                  control.
154   *
155   * @throws  LDAPException  If the provided control cannot be decoded as a hard
156   *                         delete request control.
157   */
158  public HardDeleteRequestControl(@NotNull final Control control)
159         throws LDAPException
160  {
161    super(control);
162
163    if (control.hasValue())
164    {
165      try
166      {
167        final ASN1Sequence valueSequence =
168             ASN1Sequence.decodeAsSequence(control.getValue().getValue());
169        final ASN1Element[] elements = valueSequence.elements();
170        if (elements.length > 0)
171        {
172          throw new LDAPException(ResultCode.DECODING_ERROR,
173               ERR_HARD_DELETE_REQUEST_UNSUPPORTED_VALUE_ELEMENT_TYPE.get(
174                    StaticUtils.toHex(elements[0].getType())));
175        }
176      }
177      catch (final LDAPException le)
178      {
179        Debug.debugException(le);
180        throw le;
181      }
182      catch (final Exception e)
183      {
184        Debug.debugException(e);
185        throw new LDAPException(ResultCode.DECODING_ERROR,
186             ERR_HARD_DELETE_REQUEST_CANNOT_DECODE_VALUE.get(
187                  StaticUtils.getExceptionMessage(e)),
188             e);
189      }
190    }
191  }
192
193
194
195  /**
196   * Creates a new delete request that may be used to hard delete the specified
197   * target entry.
198   *
199   * @param  targetDN    The DN of the entry to be hard deleted.
200   * @param  isCritical  Indicates whether this control should be marked
201   *                     critical.  This will only have an effect on the way the
202   *                     associated delete operation is handled by servers which
203   *                     do NOT support the hard delete request control.  For
204   *                     such servers, a control that is critical will cause the
205   *                     hard delete attempt to fail, while a control that is
206   *                     not critical will be processed as if the control was
207   *                     not included in the request (i.e., as a normal "hard"
208   *                     delete).
209   *
210   * @return  A delete request with the specified target DN and an appropriate
211   *          hard delete request control.
212   */
213  @NotNull()
214  public static DeleteRequest createHardDeleteRequest(
215                     @NotNull final String targetDN,
216                     final boolean isCritical)
217  {
218    final Control[] controls =
219    {
220      new HardDeleteRequestControl(isCritical)
221    };
222
223    return new DeleteRequest(targetDN, controls);
224  }
225
226
227
228  /**
229   * {@inheritDoc}
230   */
231  @Override()
232  @NotNull()
233  public String getControlName()
234  {
235    return INFO_CONTROL_NAME_HARD_DELETE_REQUEST.get();
236  }
237
238
239
240  /**
241   * Retrieves a representation of this hard delete request control as a JSON
242   * object.  The JSON object uses the following fields (note that since this
243   * control does not have a value, neither the {@code value-base64} nor
244   * {@code value-json} fields may be present):
245   * <UL>
246   *   <LI>
247   *     {@code oid} -- A mandatory string field whose value is the object
248   *     identifier for this control.  For the hard delete request control, the
249   *     OID is "1.3.6.1.4.1.30221.2.5.22".
250   *   </LI>
251   *   <LI>
252   *     {@code control-name} -- An optional string field whose value is a
253   *     human-readable name for this control.  This field is only intended for
254   *     descriptive purposes, and when decoding a control, the {@code oid}
255   *     field should be used to identify the type of control.
256   *   </LI>
257   *   <LI>
258   *     {@code criticality} -- A mandatory Boolean field used to indicate
259   *     whether this control is considered critical.
260   *   </LI>
261   * </UL>
262   *
263   * @return  A JSON object that contains a representation of this control.
264   */
265  @Override()
266  @NotNull()
267  public JSONObject toJSONControl()
268  {
269    return new JSONObject(
270         new JSONField(JSONControlDecodeHelper.JSON_FIELD_OID,
271              HARD_DELETE_REQUEST_OID),
272         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CONTROL_NAME,
273              INFO_CONTROL_NAME_HARD_DELETE_REQUEST.get()),
274         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CRITICALITY,
275              isCritical()));
276  }
277
278
279
280  /**
281   * Attempts to decode the provided object as a JSON representation of a hard
282   * delete request control.
283   *
284   * @param  controlObject  The JSON object to be decoded.  It must not be
285   *                        {@code null}.
286   * @param  strict         Indicates whether to use strict mode when decoding
287   *                        the provided JSON object.  If this is {@code true},
288   *                        then this method will throw an exception if the
289   *                        provided JSON object contains any unrecognized
290   *                        fields.  If this is {@code false}, then unrecognized
291   *                        fields will be ignored.
292   *
293   * @return  The hard delete request control that was decoded from the provided
294   *          JSON object.
295   *
296   * @throws  LDAPException  If the provided JSON object cannot be parsed as a
297   *                         valid hard delete request control.
298   */
299  @NotNull()
300  public static HardDeleteRequestControl decodeJSONControl(
301              @NotNull final JSONObject controlObject,
302              final boolean strict)
303         throws LDAPException
304  {
305    final JSONControlDecodeHelper jsonControl = new JSONControlDecodeHelper(
306         controlObject, strict, false, false);
307
308    return new HardDeleteRequestControl(jsonControl.getCriticality());
309  }
310
311
312
313  /**
314   * {@inheritDoc}
315   */
316  @Override()
317  public void toString(@NotNull final StringBuilder buffer)
318  {
319    buffer.append("HardDeleteRequestControl(isCritical=");
320    buffer.append(isCritical());
321    buffer.append(')');
322  }
323}