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.LDAPException;
048import com.unboundid.ldap.sdk.ResultCode;
049import com.unboundid.util.Debug;
050import com.unboundid.util.NotMutable;
051import com.unboundid.util.NotNull;
052import com.unboundid.util.Nullable;
053import com.unboundid.util.StaticUtils;
054import com.unboundid.util.ThreadSafety;
055import com.unboundid.util.ThreadSafetyLevel;
056
057import static com.unboundid.ldap.sdk.unboundidds.controls.ControlMessages.*;
058
059
060
061/**
062 * This class provides a request control which may be included in a search
063 * request to indicate that soft-deleted entries may be included in the results,
064 * or it may be included in a compare or modify request to indicate that the
065 * operation should operate against the target entry even if it is a
066 * soft-deleted entry.
067 * <BR>
068 * <BLOCKQUOTE>
069 *   <B>NOTE:</B>  This class, and other classes within the
070 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
071 *   supported for use against Ping Identity, UnboundID, and
072 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
073 *   for proprietary functionality or for external specifications that are not
074 *   considered stable or mature enough to be guaranteed to work in an
075 *   interoperable way with other types of LDAP servers.
076 * </BLOCKQUOTE>
077 * <BR>
078 * The criticality for this control may be either {@code TRUE} or {@code FALSE},
079 * but this will only impact how the delete request is to be handled by servers
080 * which do not support this control.  A criticality of {@code TRUE} will cause
081 * any server which does not support this control to reject the request, while
082 * a criticality of {@code FALSE} should cause the request to be processed as if
083 * the control had not been included.
084 * <BR><BR>
085 * The control may optionally have a value.  If a value is provided, then it
086 * must be the encoded representation of the following ASN.1 element:
087 * <PRE>
088 *   SoftDeleteAccessRequestValue ::= SEQUENCE {
089 *     includeNonSoftDeletedEntries     [0] BOOLEAN DEFAULT TRUE,
090 *     returnEntriesInUndeletedForm     [1] BOOLEAN DEFAULT FALSE,
091 *     ... }
092 * </PRE>
093 * See the documentation for the {@link SoftDeleteRequestControl} class for an
094 * example demonstrating the use of this control.
095 *
096 * @see  SoftDeleteResponseControl
097 */
098@NotMutable()
099@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
100public final class SoftDeletedEntryAccessRequestControl
101       extends Control
102{
103  /**
104   * The OID (1.3.6.1.4.1.30221.2.5.24) for the soft-deleted entry access
105   * request control.
106   */
107  @NotNull public static final String SOFT_DELETED_ENTRY_ACCESS_REQUEST_OID =
108       "1.3.6.1.4.1.30221.2.5.24";
109
110
111
112  /**
113   * The BER type for the include non-soft-deleted entries element.
114   */
115  private static final byte TYPE_INCLUDE_NON_SOFT_DELETED_ENTRIES = (byte) 0x80;
116
117
118
119  /**
120   * The BER type for the return entries in undeleted form element.
121   */
122  private static final byte TYPE_RETURN_ENTRIES_IN_UNDELETED_FORM = (byte) 0x81;
123
124
125
126  /**
127   * The serial version UID for this serializable class.
128   */
129  private static final long serialVersionUID = -3633807543861389512L;
130
131
132
133  // Indicates whether to include non-soft-deleted entries in search results.
134  private final boolean includeNonSoftDeletedEntries;
135
136  // Indicates whether to return soft-deleted entries in the form they appeared
137  // before they were deleted.
138  private final boolean returnEntriesInUndeletedForm;
139
140
141
142  /**
143   * Creates a new soft-deleted entry access request control with the default
144   * settings for all elements.  It will not be marked critical.
145   */
146  public SoftDeletedEntryAccessRequestControl()
147  {
148    this(false, true, false);
149  }
150
151
152
153  /**
154   * Creates a new soft delete request control with the provided information.
155   *
156   * @param  isCritical                    Indicates whether this control should
157   *                                       be marked critical.  This will only
158   *                                       have an effect on the way the
159   *                                       associated delete operation is
160   *                                       handled by servers which do NOT
161   *                                       support the soft-deleted entry access
162   *                                       request control.  For such servers, a
163   *                                       control that is critical will cause
164   *                                       associated request to be rejected,
165   *                                       while a control that is not critical
166   *                                       will be processed as if the control
167   *                                       was not included in the request.
168   * @param  includeNonSoftDeletedEntries  Indicates whether search results
169   *                                       should include non-soft-deleted
170   *                                       entries if they match the criteria
171   *                                       for the associated search request.
172   * @param  returnEntriesInUndeletedForm  Indicates whether soft-deleted
173   *                                       entries returned in search results
174   *                                       should be returned in the form in
175   *                                       which they would appear if they were
176   *                                       undeleted.  Note that if soft-deleted
177   *                                       entries should be returned in their
178   *                                       undeleted form, then it may be
179   *                                       possible for multiple entries to be
180   *                                       returned with the same DN (if
181   *                                       multiple soft-deleted entries with
182   *                                       the same original DN match the
183   *                                       criteria, or if at least one
184   *                                       soft-deleted entry and one normal
185   *                                       entry with the same DN both match the
186   *                                       search criteria).
187   */
188  public SoftDeletedEntryAccessRequestControl(final boolean isCritical,
189              final boolean includeNonSoftDeletedEntries,
190              final boolean returnEntriesInUndeletedForm)
191  {
192    super(SOFT_DELETED_ENTRY_ACCESS_REQUEST_OID, isCritical,
193         encodeValue(includeNonSoftDeletedEntries,
194              returnEntriesInUndeletedForm));
195
196    this.includeNonSoftDeletedEntries = includeNonSoftDeletedEntries;
197    this.returnEntriesInUndeletedForm = returnEntriesInUndeletedForm;
198  }
199
200
201
202  /**
203   * Creates a new soft-deleted entry access request control which is decoded
204   * from the provided generic control.
205   *
206   * @param  control  The generic control to be decoded as a soft-deleted entry
207   *                  access request control.
208   *
209   * @throws  LDAPException  If the provided control cannot be decoded as a
210   *                         soft-deleted entry access request control.
211   */
212  public SoftDeletedEntryAccessRequestControl(@NotNull final Control control)
213         throws LDAPException
214  {
215    super(control);
216
217    boolean includeNonSoftDeleted = true;
218    boolean returnAsUndeleted     = false;
219
220    if (control.hasValue())
221    {
222      try
223      {
224        final ASN1Sequence valueSequence =
225             ASN1Sequence.decodeAsSequence(control.getValue().getValue());
226        for (final ASN1Element e : valueSequence.elements())
227        {
228          switch (e.getType())
229          {
230            case TYPE_INCLUDE_NON_SOFT_DELETED_ENTRIES:
231              includeNonSoftDeleted =
232                   ASN1Boolean.decodeAsBoolean(e).booleanValue();
233              break;
234            case TYPE_RETURN_ENTRIES_IN_UNDELETED_FORM:
235              returnAsUndeleted = ASN1Boolean.decodeAsBoolean(e).booleanValue();
236              break;
237            default:
238              throw new LDAPException(ResultCode.DECODING_ERROR,
239                   ERR_SOFT_DELETED_ACCESS_REQUEST_UNSUPPORTED_ELEMENT_TYPE.get(
240                        StaticUtils.toHex(e.getType())));
241          }
242        }
243      }
244      catch (final LDAPException le)
245      {
246        Debug.debugException(le);
247        throw le;
248      }
249      catch (final Exception e)
250      {
251        Debug.debugException(e);
252        throw new LDAPException(ResultCode.DECODING_ERROR,
253             ERR_SOFT_DELETED_ACCESS_REQUEST_CANNOT_DECODE_VALUE.get(
254                  StaticUtils.getExceptionMessage(e)),
255             e);
256      }
257    }
258
259    includeNonSoftDeletedEntries = includeNonSoftDeleted;
260    returnEntriesInUndeletedForm = returnAsUndeleted;
261  }
262
263
264
265  /**
266   * Encodes the provided information into an ASN.1 octet string suitable for
267   * use as the value of a soft-deleted entry access request control.
268   *
269   * @param  includeNonSoftDeletedEntries  Indicates whether search results
270   *                                       should include non-soft-deleted
271   *                                       entries if they match the criteria
272   *                                       for the associated search request.
273   * @param  returnEntriesInUndeletedForm  Indicates whether soft-deleted
274   *                                       entries returned in search results
275   *                                       should be returned in the form in
276   *                                       which they would appear if they were
277   *                                       undeleted.  Note that if soft-deleted
278   *                                       entries should be returned in their
279   *                                       undeleted form, then it may be
280   *                                       possible for multiple entries to be
281   *                                       returned with the same DN (if
282   *                                       multiple soft-deleted entries with
283   *                                       the same original DN match the
284   *                                       criteria, or if at least one
285   *                                       soft-deleted entry and one normal
286   *                                       entry with the same DN both match the
287   *                                       search criteria).
288   *
289   * @return  An ASN.1 octet string with an encoding suitable for use as the
290   *          value of a soft-deleted entry access request control, or
291   *          {@code null} if no value is needed for the control.
292   */
293  @Nullable()
294  private static ASN1OctetString encodeValue(
295                      final boolean includeNonSoftDeletedEntries,
296                      final boolean returnEntriesInUndeletedForm)
297  {
298    if (includeNonSoftDeletedEntries && (! returnEntriesInUndeletedForm))
299    {
300      return null;
301    }
302
303    final ArrayList<ASN1Element> elements = new ArrayList<>(2);
304    if (! includeNonSoftDeletedEntries)
305    {
306      elements.add(new ASN1Boolean(TYPE_INCLUDE_NON_SOFT_DELETED_ENTRIES,
307           false));
308    }
309
310    if (returnEntriesInUndeletedForm)
311    {
312      elements.add(new ASN1Boolean(TYPE_RETURN_ENTRIES_IN_UNDELETED_FORM,
313           true));
314    }
315
316    return new ASN1OctetString(new ASN1Sequence(elements).encode());
317  }
318
319
320
321  /**
322   * Indicates whether search results should include non-soft-deleted entries
323   * if they match the criteria for the associated search request.
324   *
325   * @return  {@code true} if the server should return any "normal"
326   *          non-soft-deleted entries that match the search criteria, or
327   *          {@code false} if the server should only return soft-deleted
328   *          entries that match the search criteria.
329   */
330  public boolean includeNonSoftDeletedEntries()
331  {
332    return includeNonSoftDeletedEntries;
333  }
334
335
336
337  /**
338   * Indicates whether soft-deleted entries returned in search results should be
339   * returned in the form in which they would appear if they were undeleted.
340   * Note that if soft-deleted entries should be returned in their undeleted
341   * form, then it may be possible for multiple entries to be returned with the
342   * same DN (if multiple soft-deleted entries with the same original DN match
343   * the criteria, or if at least one soft-deleted entry and one normal entry
344   * with the same DN both match the search criteria).
345   *
346   * @return  {@code false} if soft-deleted entries should be returned in their
347   *          current form as soft-deleted entries, or {@code true} if they
348   *          should be returned in the form in which they would appear if they
349   *          were undeleted (e.g., using the original DN for the entry and
350   *          without all the additional meta-attributes added during the
351   *          soft delete process).
352   */
353  public boolean returnEntriesInUndeletedForm()
354  {
355    return returnEntriesInUndeletedForm;
356  }
357
358
359
360  /**
361   * {@inheritDoc}
362   */
363  @Override()
364  @NotNull()
365  public String getControlName()
366  {
367    return INFO_CONTROL_NAME_SOFT_DELETED_ACCESS_REQUEST.get();
368  }
369
370
371
372  /**
373   * {@inheritDoc}
374   */
375  @Override()
376  public void toString(@NotNull final StringBuilder buffer)
377  {
378    buffer.append("SoftDeletedEntryAccessRequestControl(isCritical=");
379    buffer.append(isCritical());
380    buffer.append(", includeNonSoftDeletedEntries=");
381    buffer.append(includeNonSoftDeletedEntries);
382    buffer.append(", returnEntriesInUndeletedForm=");
383    buffer.append(returnEntriesInUndeletedForm);
384    buffer.append(')');
385  }
386}