001/*
002 * Copyright 2012-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2012-2020 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) 2015-2020 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.ASN1OctetString;
041import com.unboundid.ldap.sdk.Control;
042import com.unboundid.ldap.sdk.DecodeableControl;
043import com.unboundid.ldap.sdk.DN;
044import com.unboundid.ldap.sdk.LDAPException;
045import com.unboundid.ldap.sdk.LDAPResult;
046import com.unboundid.ldap.sdk.ResultCode;
047import com.unboundid.util.NotMutable;
048import com.unboundid.util.ThreadSafety;
049import com.unboundid.util.ThreadSafetyLevel;
050import com.unboundid.util.Validator;
051
052import static com.unboundid.ldap.sdk.unboundidds.controls.ControlMessages.*;
053
054
055
056/**
057 * This class provides a response control that holds information about the
058 * soft-deleted entry that results from a soft delete request, and may also be
059 * included in a search result entry which represents a soft-deleted entry.  The
060 * value of this control will be the DN of the soft-deleted entry.
061 * <BR>
062 * <BLOCKQUOTE>
063 *   <B>NOTE:</B>  This class, and other classes within the
064 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
065 *   supported for use against Ping Identity, UnboundID, and
066 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
067 *   for proprietary functionality or for external specifications that are not
068 *   considered stable or mature enough to be guaranteed to work in an
069 *   interoperable way with other types of LDAP servers.
070 * </BLOCKQUOTE>
071 * <BR>
072 * This control has an OID of 1.3.6.1.4.1.30221.2.5.21, a criticality of false,
073 * and a value that is simply the string representation of the new DN for the
074 * soft-deleted entry.
075 * <BR><BR>
076 * See the documentation for the {@link SoftDeleteRequestControl} class for an
077 * example demonstrating the use of this control.
078 *
079 * @see  SoftDeleteRequestControl
080 */
081@NotMutable()
082@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
083public final class SoftDeleteResponseControl
084       extends Control
085       implements DecodeableControl
086{
087  /**
088   * The OID (1.3.6.1.4.1.30221.2.5.21) for the soft delete response control.
089   */
090  public static final String SOFT_DELETE_RESPONSE_OID =
091       "1.3.6.1.4.1.30221.2.5.21";
092
093
094
095  /**
096   * The serial version UID for this serializable class.
097   */
098  private static final long serialVersionUID = 3163679387266190228L;
099
100
101
102  // The DN of the soft-deleted representation of the target entry.
103  private final String softDeletedEntryDN;
104
105
106
107  /**
108   * Creates a new empty control instance that is intended to be used only for
109   * decoding controls via the {@code DecodeableControl} interface.
110   */
111  SoftDeleteResponseControl()
112  {
113    softDeletedEntryDN = null;
114  }
115
116
117
118  /**
119   * Creates a new soft delete response control with the provided information.
120   *
121   * @param  softDeletedEntryDN  The DN of the soft-deleted representation of
122   *                             the target entry.
123   */
124  public SoftDeleteResponseControl(final String softDeletedEntryDN)
125  {
126    super(SOFT_DELETE_RESPONSE_OID, false,
127         new ASN1OctetString(softDeletedEntryDN));
128
129    Validator.ensureNotNull(softDeletedEntryDN);
130
131    this.softDeletedEntryDN = softDeletedEntryDN;
132  }
133
134
135
136  /**
137   * Creates a new soft delete response control with the provided information.
138   *
139   * @param  oid         The OID for the control.
140   * @param  isCritical  Indicates whether the control should be considered
141   *                     critical.
142   * @param  value       The value for the control.
143   *
144   * @throws  LDAPException  If the provided information cannot be used to
145   *                         create a valid soft delete response control.
146   */
147  public SoftDeleteResponseControl(final String oid, final boolean isCritical,
148                                   final ASN1OctetString value)
149         throws LDAPException
150  {
151    super(oid, isCritical, value);
152
153    if (value == null)
154    {
155      throw new LDAPException(ResultCode.DECODING_ERROR,
156           ERR_SOFT_DELETE_RESPONSE_NO_VALUE.get());
157    }
158
159    softDeletedEntryDN = value.stringValue();
160    if (! DN.isValidDN(softDeletedEntryDN))
161    {
162      throw new LDAPException(ResultCode.DECODING_ERROR,
163           ERR_SOFT_DELETE_RESPONSE_VALUE_NOT_DN.get());
164    }
165  }
166
167
168
169  /**
170   * {@inheritDoc}
171   */
172  @Override()
173  public SoftDeleteResponseControl decodeControl(final String oid,
174                                                 final boolean isCritical,
175                                                 final ASN1OctetString value)
176         throws LDAPException
177  {
178    return new SoftDeleteResponseControl(oid, isCritical, value);
179  }
180
181
182
183  /**
184   * Retrieves the DN of the entry containing the soft-deleted representation of
185   * the target entry.
186   *
187   * @return  The DN of the entry containing the soft-deleted representation of
188   *          the target entry.
189   */
190  public String getSoftDeletedEntryDN()
191  {
192    return softDeletedEntryDN;
193  }
194
195
196
197  /**
198   * Extracts a soft delete response control from the provided delete result.
199   *
200   * @param  deleteResult  The delete result from which to retrieve the soft
201   *                       delete response control.
202   *
203   * @return  The soft delete response control contained in the provided delete
204   *          result, or {@code null} if the result did not contain a soft
205   *          delete response control.
206   *
207   * @throws  LDAPException  If a problem is encountered while attempting to
208   *                         decode the soft delete response control contained
209   *                         in the provided result.
210   */
211  public static SoftDeleteResponseControl get(final LDAPResult deleteResult)
212         throws LDAPException
213  {
214    final Control c = deleteResult.getResponseControl(SOFT_DELETE_RESPONSE_OID);
215    if (c == null)
216    {
217      return null;
218    }
219
220    if (c instanceof SoftDeleteResponseControl)
221    {
222      return (SoftDeleteResponseControl) c;
223    }
224    else
225    {
226      return new SoftDeleteResponseControl(c.getOID(), c.isCritical(),
227           c.getValue());
228    }
229  }
230
231
232
233  /**
234   * {@inheritDoc}
235   */
236  @Override()
237  public String getControlName()
238  {
239    return INFO_CONTROL_NAME_SOFT_DELETE_RESPONSE.get();
240  }
241
242
243
244  /**
245   * {@inheritDoc}
246   */
247  @Override()
248  public void toString(final StringBuilder buffer)
249  {
250    buffer.append("SoftDeleteResponseControl(softDeletedEntryDN='");
251    buffer.append(softDeletedEntryDN);
252    buffer.append("')");
253  }
254}