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 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.LDAPException;
045import com.unboundid.ldap.sdk.ResultCode;
046import com.unboundid.util.Debug;
047import com.unboundid.util.NotMutable;
048import com.unboundid.util.NotNull;
049import com.unboundid.util.StaticUtils;
050import com.unboundid.util.ThreadSafety;
051import com.unboundid.util.ThreadSafetyLevel;
052
053import static com.unboundid.ldap.sdk.unboundidds.controls.ControlMessages.*;
054
055
056
057/**
058 * This class provides a request control which may be included in a delete
059 * request to indicate that the server should completely remove the target
060 * entry, even if it would otherwise process the operation as a soft delete and
061 * merely hide the entry from most clients.
062 * <BR>
063 * <BLOCKQUOTE>
064 *   <B>NOTE:</B>  This class, and other classes within the
065 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
066 *   supported for use against Ping Identity, UnboundID, and
067 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
068 *   for proprietary functionality or for external specifications that are not
069 *   considered stable or mature enough to be guaranteed to work in an
070 *   interoperable way with other types of LDAP servers.
071 * </BLOCKQUOTE>
072 * <BR>
073 * The criticality for this control may be either {@code TRUE} or {@code FALSE},
074 * but this will only impact how the delete request is to be handled by servers
075 * which do not support this control.  A criticality of {@code TRUE} will cause
076 * any server which does not support this control to reject the request, while
077 * a criticality of {@code FALSE} should cause the delete request to be
078 * processed as if the control had not been included (i.e., as a regular "hard"
079 * delete).
080 * <BR><BR>
081 * The control may optionally have a value.  If a value is provided, then it
082 * must be the encoded representation of an empty ASN.1 sequence, like:
083 * <PRE>
084 *   HardDeleteRequestValue ::= SEQUENCE {
085 *     ... }
086 * </PRE>
087 * In the future, the value sequence may allow one or more elements to customize
088 * the behavior of the hard delete operation, but at present no such elements
089 * are defined.
090 * See the documentation for the {@link SoftDeleteRequestControl} class for an
091 * example demonstrating the use of this control.
092 *
093 * @see  SoftDeleteRequestControl
094 */
095@NotMutable()
096@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
097public final class HardDeleteRequestControl
098       extends Control
099{
100  /**
101   * The OID (1.3.6.1.4.1.30221.2.5.22) for the hard delete request control.
102   */
103  @NotNull public static final String HARD_DELETE_REQUEST_OID =
104       "1.3.6.1.4.1.30221.2.5.22";
105
106
107
108  /**
109   * The serial version UID for this serializable class.
110   */
111  private static final long serialVersionUID = 1169625153021056712L;
112
113
114
115  /**
116   * Creates a new hard delete request control.  It will not be marked critical.
117   */
118  public HardDeleteRequestControl()
119  {
120    this(false);
121  }
122
123
124
125  /**
126   * Creates a new hard delete request control with the provided information.
127   *
128   * @param  isCritical  Indicates whether this control should be marked
129   *                     critical.  This will only have an effect on the way the
130   *                     associated delete operation is handled by servers which
131   *                     do NOT support the hard delete request control.  For
132   *                     such servers, a control that is critical will cause the
133   *                     hard delete attempt to fail, while a control that is
134   *                     not critical will be processed as if the control was
135   *                     not included in the request (i.e., as a normal "hard"
136   *                     delete).
137   */
138  public HardDeleteRequestControl(final boolean isCritical)
139  {
140    super(HARD_DELETE_REQUEST_OID, isCritical, null);
141  }
142
143
144
145  /**
146   * Creates a new hard delete request control which is decoded from the
147   * provided generic control.
148   *
149   * @param  control  The generic control to be decoded as a hard delete request
150   *                  control.
151   *
152   * @throws  LDAPException  If the provided control cannot be decoded as a hard
153   *                         delete request control.
154   */
155  public HardDeleteRequestControl(@NotNull final Control control)
156         throws LDAPException
157  {
158    super(control);
159
160    if (control.hasValue())
161    {
162      try
163      {
164        final ASN1Sequence valueSequence =
165             ASN1Sequence.decodeAsSequence(control.getValue().getValue());
166        final ASN1Element[] elements = valueSequence.elements();
167        if (elements.length > 0)
168        {
169          throw new LDAPException(ResultCode.DECODING_ERROR,
170               ERR_HARD_DELETE_REQUEST_UNSUPPORTED_VALUE_ELEMENT_TYPE.get(
171                    StaticUtils.toHex(elements[0].getType())));
172        }
173      }
174      catch (final LDAPException le)
175      {
176        Debug.debugException(le);
177        throw le;
178      }
179      catch (final Exception e)
180      {
181        Debug.debugException(e);
182        throw new LDAPException(ResultCode.DECODING_ERROR,
183             ERR_HARD_DELETE_REQUEST_CANNOT_DECODE_VALUE.get(
184                  StaticUtils.getExceptionMessage(e)),
185             e);
186      }
187    }
188  }
189
190
191
192  /**
193   * Creates a new delete request that may be used to hard delete the specified
194   * target entry.
195   *
196   * @param  targetDN    The DN of the entry to be hard deleted.
197   * @param  isCritical  Indicates whether this control should be marked
198   *                     critical.  This will only have an effect on the way the
199   *                     associated delete operation is handled by servers which
200   *                     do NOT support the hard delete request control.  For
201   *                     such servers, a control that is critical will cause the
202   *                     hard delete attempt to fail, while a control that is
203   *                     not critical will be processed as if the control was
204   *                     not included in the request (i.e., as a normal "hard"
205   *                     delete).
206   *
207   * @return  A delete request with the specified target DN and an appropriate
208   *          hard delete request control.
209   */
210  @NotNull()
211  public static DeleteRequest createHardDeleteRequest(
212                     @NotNull final String targetDN,
213                     final boolean isCritical)
214  {
215    final Control[] controls =
216    {
217      new HardDeleteRequestControl(isCritical)
218    };
219
220    return new DeleteRequest(targetDN, controls);
221  }
222
223
224
225  /**
226   * {@inheritDoc}
227   */
228  @Override()
229  @NotNull()
230  public String getControlName()
231  {
232    return INFO_CONTROL_NAME_HARD_DELETE_REQUEST.get();
233  }
234
235
236
237  /**
238   * {@inheritDoc}
239   */
240  @Override()
241  public void toString(@NotNull final StringBuilder buffer)
242  {
243    buffer.append("HardDeleteRequestControl(isCritical=");
244    buffer.append(isCritical());
245    buffer.append(')');
246  }
247}