001/*
002 * Copyright 2012-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2012-2022 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-2022 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 java.util.ArrayList;
041
042import com.unboundid.asn1.ASN1Boolean;
043import com.unboundid.asn1.ASN1Element;
044import com.unboundid.asn1.ASN1OctetString;
045import com.unboundid.asn1.ASN1Sequence;
046import com.unboundid.ldap.sdk.Control;
047import com.unboundid.ldap.sdk.DeleteRequest;
048import com.unboundid.ldap.sdk.LDAPException;
049import com.unboundid.ldap.sdk.ResultCode;
050import com.unboundid.util.Debug;
051import com.unboundid.util.NotMutable;
052import com.unboundid.util.NotNull;
053import com.unboundid.util.Nullable;
054import com.unboundid.util.StaticUtils;
055import com.unboundid.util.ThreadSafety;
056import com.unboundid.util.ThreadSafetyLevel;
057
058import static com.unboundid.ldap.sdk.unboundidds.controls.ControlMessages.*;
059
060
061
062/**
063 * This class provides a request control which may be included in a delete
064 * request to indicate that the server should perform a soft delete rather than
065 * a hard delete.  A soft delete will leave the entry in the server, but will
066 * mark it hidden so that it can only be retrieved with a special request
067 * (e.g., one which includes the {@link SoftDeletedEntryAccessRequestControl} or
068 * a filter which includes an "(objectClass=ds-soft-deleted-entry)" component).
069 * A soft-deleted entry may later be undeleted (using an add request containing
070 * the {@link UndeleteRequestControl}) in order to restore them with the same or
071 * a different DN.
072 * <BR>
073 * <BLOCKQUOTE>
074 *   <B>NOTE:</B>  This class, and other classes within the
075 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
076 *   supported for use against Ping Identity, UnboundID, and
077 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
078 *   for proprietary functionality or for external specifications that are not
079 *   considered stable or mature enough to be guaranteed to work in an
080 *   interoperable way with other types of LDAP servers.
081 * </BLOCKQUOTE>
082 * <BR>
083 * The criticality for this control may be either {@code TRUE} or {@code FALSE},
084 * but this will only impact how the delete request is to be handled by servers
085 * which do not support this control.  A criticality of {@code TRUE} will cause
086 * any server which does not support this control to reject the request, while
087 * a criticality of {@code FALSE} should cause the delete request to be
088 * processed as if the control had not been included (i.e., as a regular "hard"
089 * delete).
090 * <BR><BR>
091 * The control may optionally have a value.  If a value is provided, then it
092 * must be the encoded representation of the following ASN.1 element:
093 * <PRE>
094 *   SoftDeleteRequestValue ::= SEQUENCE {
095 *     returnSoftDeleteResponse     [0] BOOLEAN DEFAULT TRUE,
096 *     ... }
097 * </PRE>
098 * <BR><BR>
099 * <H2>Example</H2>
100 * The following example demonstrates the use of the soft delete request control
101 * to remove the "uid=test,dc=example,dc=com" user with a soft delete operation,
102 * and then to recover it with an undelete operation:
103 * <PRE>
104 * // Perform a search to verify that the test entry exists.
105 * SearchRequest searchRequest = new SearchRequest("dc=example,dc=com",
106 *      SearchScope.SUB, Filter.createEqualityFilter("uid", "test"));
107 * SearchResult searchResult = connection.search(searchRequest);
108 * LDAPTestUtils.assertEntriesReturnedEquals(searchResult, 1);
109 * String originalDN = searchResult.getSearchEntries().get(0).getDN();
110 *
111 * // Perform a soft delete against the entry.
112 * DeleteRequest softDeleteRequest = new DeleteRequest(originalDN);
113 * softDeleteRequest.addControl(new SoftDeleteRequestControl());
114 * LDAPResult softDeleteResult = connection.delete(softDeleteRequest);
115 *
116 * // Verify that a soft delete response control was included in the result.
117 * SoftDeleteResponseControl softDeleteResponseControl =
118 *      SoftDeleteResponseControl.get(softDeleteResult);
119 * String softDeletedDN = softDeleteResponseControl.getSoftDeletedEntryDN();
120 *
121 * // Verify that the original entry no longer exists.
122 * LDAPTestUtils.assertEntryMissing(connection, originalDN);
123 *
124 * // Verify that the original search no longer returns any entries.
125 * searchResult = connection.search(searchRequest);
126 * LDAPTestUtils.assertNoEntriesReturned(searchResult);
127 *
128 * // Verify that the search will return an entry if we include the
129 * // soft-deleted entry access control in the request.
130 * searchRequest.addControl(new SoftDeletedEntryAccessRequestControl());
131 * searchResult = connection.search(searchRequest);
132 * LDAPTestUtils.assertEntriesReturnedEquals(searchResult, 1);
133 *
134 * // Perform an undelete operation to restore the entry.
135 * AddRequest undeleteRequest = UndeleteRequestControl.createUndeleteRequest(
136 *      originalDN, softDeletedDN);
137 * LDAPResult undeleteResult = connection.add(undeleteRequest);
138 *
139 * // Verify that the original entry is back.
140 * LDAPTestUtils.assertEntryExists(connection, originalDN);
141 *
142 * // Permanently remove the original entry with a hard delete.
143 * DeleteRequest hardDeleteRequest = new DeleteRequest(originalDN);
144 * hardDeleteRequest.addControl(new HardDeleteRequestControl());
145 * LDAPResult hardDeleteResult = connection.delete(hardDeleteRequest);
146 * </PRE>
147 * Note that this class provides convenience methods that can be used to easily
148 * create a delete request containing an appropriate soft delete request
149 * control.  Similar methods can be found in the
150 * {@link HardDeleteRequestControl} and {@link UndeleteRequestControl} classes
151 * for creating appropriate hard delete and undelete requests, respectively.
152 *
153 * @see  HardDeleteRequestControl
154 * @see  SoftDeleteResponseControl
155 * @see  SoftDeletedEntryAccessRequestControl
156 * @see  UndeleteRequestControl
157 */
158@NotMutable()
159@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
160public final class SoftDeleteRequestControl
161       extends Control
162{
163  /**
164   * The OID (1.3.6.1.4.1.30221.2.5.20) for the soft delete request control.
165   */
166  @NotNull public static final String SOFT_DELETE_REQUEST_OID =
167       "1.3.6.1.4.1.30221.2.5.20";
168
169
170
171  /**
172   * The BER type for the return soft delete response element.
173   */
174  private static final byte TYPE_RETURN_SOFT_DELETE_RESPONSE = (byte) 0x80;
175
176
177
178  /**
179   * The serial version UID for this serializable class.
180   */
181  private static final long serialVersionUID = 4068029406430690545L;
182
183
184
185  // Indicates whether to the response should include a soft delete response
186  // control.
187  private final boolean returnSoftDeleteResponse;
188
189
190
191  /**
192   * Creates a new soft delete request control with the default settings for
193   * all elements.  It will be marked critical.
194   */
195  public SoftDeleteRequestControl()
196  {
197    this(true, true);
198  }
199
200
201
202  /**
203   * Creates a new soft delete request control with the provided information.
204   *
205   * @param  isCritical                Indicates whether this control should be
206   *                                   marked critical.  This will only have an
207   *                                   effect on the way the associated delete
208   *                                   operation is handled by servers which do
209   *                                   NOT support the soft delete request
210   *                                   control.  For such servers, a control
211   *                                   that is critical will cause the soft
212   *                                   delete attempt to fail, while a control
213   *                                   that is not critical will be processed as
214   *                                   if the control was not included in the
215   *                                   request (i.e., as a normal "hard"
216   *                                   delete).
217   * @param  returnSoftDeleteResponse  Indicates whether to return a soft delete
218   *                                   response control in the delete response
219   *                                   to the client.
220   */
221  public SoftDeleteRequestControl(final boolean isCritical,
222                                  final boolean returnSoftDeleteResponse)
223  {
224    super(SOFT_DELETE_REQUEST_OID, isCritical,
225         encodeValue(returnSoftDeleteResponse));
226
227    this.returnSoftDeleteResponse = returnSoftDeleteResponse;
228  }
229
230
231
232  /**
233   * Creates a new soft delete request control which is decoded from the
234   * provided generic control.
235   *
236   * @param  control  The generic control to be decoded as a soft delete request
237   *                  control.
238   *
239   * @throws  LDAPException  If the provided control cannot be decoded as a soft
240   *                         delete request control.
241   */
242  public SoftDeleteRequestControl(@NotNull final Control control)
243         throws LDAPException
244  {
245    super(control);
246
247    boolean returnResponse = true;
248    if (control.hasValue())
249    {
250      try
251      {
252        final ASN1Sequence valueSequence =
253             ASN1Sequence.decodeAsSequence(control.getValue().getValue());
254        for (final ASN1Element e : valueSequence.elements())
255        {
256          switch (e.getType())
257          {
258            case TYPE_RETURN_SOFT_DELETE_RESPONSE:
259              returnResponse = ASN1Boolean.decodeAsBoolean(e).booleanValue();
260              break;
261            default:
262              throw new LDAPException(ResultCode.DECODING_ERROR,
263                   ERR_SOFT_DELETE_REQUEST_UNSUPPORTED_VALUE_ELEMENT_TYPE.get(
264                        StaticUtils.toHex(e.getType())));
265          }
266        }
267      }
268      catch (final LDAPException le)
269      {
270        Debug.debugException(le);
271        throw le;
272      }
273      catch (final Exception e)
274      {
275        Debug.debugException(e);
276        throw new LDAPException(ResultCode.DECODING_ERROR,
277             ERR_SOFT_DELETE_REQUEST_CANNOT_DECODE_VALUE.get(
278                  StaticUtils.getExceptionMessage(e)),
279             e);
280      }
281    }
282
283    returnSoftDeleteResponse = returnResponse;
284  }
285
286
287
288  /**
289   * Encodes the provided information into an ASN.1 octet string suitable for
290   * use as the value of a soft delete request control.
291   *
292   * @param  returnSoftDeleteResponse  Indicates whether to return a soft delete
293   *                                   response control in the delete response
294   *                                   to the client.
295   *
296   * @return  An ASN.1 octet string with an encoding suitable for use as the
297   *          value of a soft delete request control, or {@code null} if no
298   *          value is needed for the control.
299   */
300  @Nullable()
301  private static ASN1OctetString encodeValue(
302                                      final boolean returnSoftDeleteResponse)
303  {
304    if (returnSoftDeleteResponse)
305    {
306      return null;
307    }
308
309    final ArrayList<ASN1Element> elements = new ArrayList<>(1);
310    elements.add(new ASN1Boolean(TYPE_RETURN_SOFT_DELETE_RESPONSE, false));
311    return new ASN1OctetString(new ASN1Sequence(elements).encode());
312  }
313
314
315
316  /**
317   * Indicates whether the delete response should include a
318   * {@link SoftDeleteResponseControl}.
319   *
320   * @return  {@code true} if the delete response should include a soft delete
321   *          response control, or {@code false} if not.
322   */
323  public boolean returnSoftDeleteResponse()
324  {
325    return returnSoftDeleteResponse;
326  }
327
328
329
330  /**
331   * Creates a new delete request that may be used to soft delete the specified
332   * target entry.
333   *
334   * @param  targetDN                  The DN of the entry to be soft deleted.
335   * @param  isCritical                Indicates whether this control should be
336   *                                   marked critical.  This will only have an
337   *                                   effect on the way the associated delete
338   *                                   operation is handled by servers which do
339   *                                   NOT support the soft delete request
340   *                                   control.  For such servers, a control
341   *                                   that is critical will cause the soft
342   *                                   delete attempt to fail, while a control
343   *                                   that is not critical will be processed as
344   *                                   if the control was not included in the
345   *                                   request (i.e., as a normal "hard"
346   *                                   delete).
347   * @param  returnSoftDeleteResponse  Indicates whether to return a soft delete
348   *                                   response control in the delete response
349   *                                   to the client.
350   *
351   * @return  A delete request with the specified target DN and an appropriate
352   *          soft delete request control.
353   */
354  @NotNull()
355  public static DeleteRequest createSoftDeleteRequest(
356              @NotNull final String targetDN,
357              final boolean isCritical,
358              final boolean returnSoftDeleteResponse)
359  {
360    final Control[] controls =
361    {
362      new SoftDeleteRequestControl(isCritical, returnSoftDeleteResponse)
363    };
364
365    return new DeleteRequest(targetDN, controls);
366  }
367
368
369
370  /**
371   * {@inheritDoc}
372   */
373  @Override()
374  @NotNull()
375  public String getControlName()
376  {
377    return INFO_CONTROL_NAME_SOFT_DELETE_REQUEST.get();
378  }
379
380
381
382  /**
383   * {@inheritDoc}
384   */
385  @Override()
386  public void toString(@NotNull final StringBuilder buffer)
387  {
388    buffer.append("SoftDeleteRequestControl(isCritical=");
389    buffer.append(isCritical());
390    buffer.append(", returnSoftDeleteResponse=");
391    buffer.append(returnSoftDeleteResponse);
392    buffer.append(')');
393  }
394}