001/*
002 * Copyright 2009-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-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) 2009-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.protocol;
037
038
039
040import com.unboundid.asn1.ASN1Buffer;
041import com.unboundid.asn1.ASN1Element;
042import com.unboundid.asn1.ASN1OctetString;
043import com.unboundid.asn1.ASN1StreamReader;
044import com.unboundid.ldap.sdk.Control;
045import com.unboundid.ldap.sdk.DeleteRequest;
046import com.unboundid.ldap.sdk.LDAPException;
047import com.unboundid.ldap.sdk.ResultCode;
048import com.unboundid.util.Debug;
049import com.unboundid.util.InternalUseOnly;
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;
056import com.unboundid.util.Validator;
057
058import static com.unboundid.ldap.protocol.ProtocolMessages.*;
059
060
061
062/**
063 * This class provides an implementation of an LDAP delete request protocol op.
064 */
065@InternalUseOnly()
066@NotMutable()
067@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
068public final class DeleteRequestProtocolOp
069       implements ProtocolOp
070{
071  /**
072   * The serial version UID for this serializable class.
073   */
074  private static final long serialVersionUID = 1577020640104649789L;
075
076
077
078  // The entry DN for this delete request.
079  @NotNull private final String dn;
080
081
082
083  /**
084   * Creates a new delete request protocol op with the provided information.
085   *
086   * @param  dn  The entry DN for this delete request.
087   */
088  public DeleteRequestProtocolOp(@NotNull final String dn)
089  {
090    this.dn = dn;
091  }
092
093
094
095  /**
096   * Creates a new delete request protocol op from the provided delete request
097   * object.
098   *
099   * @param  request  The delete request object to use to create this protocol
100   *                  op.
101   */
102  public DeleteRequestProtocolOp(@NotNull final DeleteRequest request)
103  {
104    dn = request.getDN();
105  }
106
107
108
109  /**
110   * Creates a new delete request protocol op read from the provided ASN.1
111   * stream reader.
112   *
113   * @param  reader  The ASN.1 stream reader from which to read the delete
114   *                 request protocol op.
115   *
116   * @throws  LDAPException  If a problem occurs while reading or parsing the
117   *                         delete request.
118   */
119  DeleteRequestProtocolOp(@NotNull final ASN1StreamReader reader)
120       throws LDAPException
121  {
122    try
123    {
124      dn = reader.readString();
125      Validator.ensureNotNull(dn);
126    }
127    catch (final Exception e)
128    {
129      Debug.debugException(e);
130
131      throw new LDAPException(ResultCode.DECODING_ERROR,
132           ERR_DELETE_REQUEST_CANNOT_DECODE.get(
133                StaticUtils.getExceptionMessage(e)),
134           e);
135    }
136  }
137
138
139
140  /**
141   * Retrieves the target entry DN for this delete request.
142   *
143   * @return  The target entry DN for this delete request.
144   */
145  @NotNull()
146  public String getDN()
147  {
148    return dn;
149  }
150
151
152
153  /**
154   * {@inheritDoc}
155   */
156  @Override()
157  public byte getProtocolOpType()
158  {
159    return LDAPMessage.PROTOCOL_OP_TYPE_DELETE_REQUEST;
160  }
161
162
163
164  /**
165   * {@inheritDoc}
166   */
167  @Override()
168  @NotNull()
169  public ASN1Element encodeProtocolOp()
170  {
171    return new ASN1OctetString(LDAPMessage.PROTOCOL_OP_TYPE_DELETE_REQUEST, dn);
172  }
173
174
175
176  /**
177   * Decodes the provided ASN.1 element as a delete request protocol op.
178   *
179   * @param  element  The ASN.1 element to be decoded.
180   *
181   * @return  The decoded delete request protocol op.
182   *
183   * @throws  LDAPException  If the provided ASN.1 element cannot be decoded as
184   *                         a delete request protocol op.
185   */
186  @NotNull()
187  public static DeleteRequestProtocolOp decodeProtocolOp(
188                                             @NotNull final ASN1Element element)
189         throws LDAPException
190  {
191    try
192    {
193      return new DeleteRequestProtocolOp(
194           ASN1OctetString.decodeAsOctetString(element).stringValue());
195    }
196    catch (final Exception e)
197    {
198      Debug.debugException(e);
199      throw new LDAPException(ResultCode.DECODING_ERROR,
200           ERR_DELETE_REQUEST_CANNOT_DECODE.get(
201                StaticUtils.getExceptionMessage(e)),
202           e);
203    }
204  }
205
206
207
208  /**
209   * {@inheritDoc}
210   */
211  @Override()
212  public void writeTo(@NotNull final ASN1Buffer buffer)
213  {
214    buffer.addOctetString(LDAPMessage.PROTOCOL_OP_TYPE_DELETE_REQUEST, dn);
215  }
216
217
218
219  /**
220   * Creates a delete request from this protocol op.
221   *
222   * @param  controls  The set of controls to include in the delete request.
223   *                   It may be empty or {@code null} if no controls should be
224   *                   included.
225   *
226   * @return  The delete request that was created.
227   */
228  @NotNull()
229  public DeleteRequest toDeleteRequest(@Nullable final Control... controls)
230  {
231    return new DeleteRequest(dn, controls);
232  }
233
234
235
236  /**
237   * Retrieves a string representation of this protocol op.
238   *
239   * @return  A string representation of this protocol op.
240   */
241  @Override()
242  @NotNull()
243  public String toString()
244  {
245    final StringBuilder buffer = new StringBuilder();
246    toString(buffer);
247    return buffer.toString();
248  }
249
250
251
252  /**
253   * {@inheritDoc}
254   */
255  @Override()
256  public void toString(@NotNull final StringBuilder buffer)
257  {
258    buffer.append("DeleteRequestProtocolOp(dn='");
259    buffer.append(dn);
260    buffer.append("')");
261  }
262}