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