001/*
002 * Copyright 2009-2018 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2009-2018 Ping Identity Corporation
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.ldap.protocol;
022
023
024
025import com.unboundid.asn1.ASN1Buffer;
026import com.unboundid.asn1.ASN1Element;
027import com.unboundid.asn1.ASN1OctetString;
028import com.unboundid.asn1.ASN1StreamReader;
029import com.unboundid.ldap.sdk.Control;
030import com.unboundid.ldap.sdk.DeleteRequest;
031import com.unboundid.ldap.sdk.LDAPException;
032import com.unboundid.ldap.sdk.ResultCode;
033import com.unboundid.util.Debug;
034import com.unboundid.util.InternalUseOnly;
035import com.unboundid.util.NotMutable;
036import com.unboundid.util.StaticUtils;
037import com.unboundid.util.ThreadSafety;
038import com.unboundid.util.ThreadSafetyLevel;
039import com.unboundid.util.Validator;
040
041import static com.unboundid.ldap.protocol.ProtocolMessages.*;
042
043
044
045/**
046 * This class provides an implementation of an LDAP delete request protocol op.
047 */
048@InternalUseOnly()
049@NotMutable()
050@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
051public final class DeleteRequestProtocolOp
052       implements ProtocolOp
053{
054  /**
055   * The serial version UID for this serializable class.
056   */
057  private static final long serialVersionUID = 1577020640104649789L;
058
059
060
061  // The entry DN for this delete request.
062  private final String dn;
063
064
065
066  /**
067   * Creates a new delete request protocol op with the provided information.
068   *
069   * @param  dn  The entry DN for this delete request.
070   */
071  public DeleteRequestProtocolOp(final String dn)
072  {
073    this.dn = dn;
074  }
075
076
077
078  /**
079   * Creates a new delete request protocol op from the provided delete request
080   * object.
081   *
082   * @param  request  The delete request object to use to create this protocol
083   *                  op.
084   */
085  public DeleteRequestProtocolOp(final DeleteRequest request)
086  {
087    dn = request.getDN();
088  }
089
090
091
092  /**
093   * Creates a new delete request protocol op read from the provided ASN.1
094   * stream reader.
095   *
096   * @param  reader  The ASN.1 stream reader from which to read the delete
097   *                 request protocol op.
098   *
099   * @throws  LDAPException  If a problem occurs while reading or parsing the
100   *                         delete request.
101   */
102  DeleteRequestProtocolOp(final ASN1StreamReader reader)
103       throws LDAPException
104  {
105    try
106    {
107      dn = reader.readString();
108      Validator.ensureNotNull(dn);
109    }
110    catch (final Exception e)
111    {
112      Debug.debugException(e);
113
114      throw new LDAPException(ResultCode.DECODING_ERROR,
115           ERR_DELETE_REQUEST_CANNOT_DECODE.get(
116                StaticUtils.getExceptionMessage(e)),
117           e);
118    }
119  }
120
121
122
123  /**
124   * Retrieves the target entry DN for this delete request.
125   *
126   * @return  The target entry DN for this delete request.
127   */
128  public String getDN()
129  {
130    return dn;
131  }
132
133
134
135  /**
136   * {@inheritDoc}
137   */
138  @Override()
139  public byte getProtocolOpType()
140  {
141    return LDAPMessage.PROTOCOL_OP_TYPE_DELETE_REQUEST;
142  }
143
144
145
146  /**
147   * {@inheritDoc}
148   */
149  @Override()
150  public ASN1Element encodeProtocolOp()
151  {
152    return new ASN1OctetString(LDAPMessage.PROTOCOL_OP_TYPE_DELETE_REQUEST, dn);
153  }
154
155
156
157  /**
158   * Decodes the provided ASN.1 element as a delete request protocol op.
159   *
160   * @param  element  The ASN.1 element to be decoded.
161   *
162   * @return  The decoded delete request protocol op.
163   *
164   * @throws  LDAPException  If the provided ASN.1 element cannot be decoded as
165   *                         a delete request protocol op.
166   */
167  public static DeleteRequestProtocolOp decodeProtocolOp(
168                                             final ASN1Element element)
169         throws LDAPException
170  {
171    try
172    {
173      return new DeleteRequestProtocolOp(
174           ASN1OctetString.decodeAsOctetString(element).stringValue());
175    }
176    catch (final Exception e)
177    {
178      Debug.debugException(e);
179      throw new LDAPException(ResultCode.DECODING_ERROR,
180           ERR_DELETE_REQUEST_CANNOT_DECODE.get(
181                StaticUtils.getExceptionMessage(e)),
182           e);
183    }
184  }
185
186
187
188  /**
189   * {@inheritDoc}
190   */
191  @Override()
192  public void writeTo(final ASN1Buffer buffer)
193  {
194    buffer.addOctetString(LDAPMessage.PROTOCOL_OP_TYPE_DELETE_REQUEST, dn);
195  }
196
197
198
199  /**
200   * Creates a delete request from this protocol op.
201   *
202   * @param  controls  The set of controls to include in the delete request.
203   *                   It may be empty or {@code null} if no controls should be
204   *                   included.
205   *
206   * @return  The delete request that was created.
207   */
208  public DeleteRequest toDeleteRequest(final Control... controls)
209  {
210    return new DeleteRequest(dn, controls);
211  }
212
213
214
215  /**
216   * Retrieves a string representation of this protocol op.
217   *
218   * @return  A string representation of this protocol op.
219   */
220  @Override()
221  public String toString()
222  {
223    final StringBuilder buffer = new StringBuilder();
224    toString(buffer);
225    return buffer.toString();
226  }
227
228
229
230  /**
231   * {@inheritDoc}
232   */
233  @Override()
234  public void toString(final StringBuilder buffer)
235  {
236    buffer.append("DeleteRequestProtocolOp(dn='");
237    buffer.append(dn);
238    buffer.append("')");
239  }
240}