001    /*
002     * Copyright 2009-2016 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2009-2016 UnboundID Corp.
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     */
021    package com.unboundid.ldap.protocol;
022    
023    
024    
025    import com.unboundid.asn1.ASN1Buffer;
026    import com.unboundid.asn1.ASN1Element;
027    import com.unboundid.asn1.ASN1OctetString;
028    import com.unboundid.asn1.ASN1StreamReader;
029    import com.unboundid.ldap.sdk.Control;
030    import com.unboundid.ldap.sdk.DeleteRequest;
031    import com.unboundid.ldap.sdk.LDAPException;
032    import com.unboundid.ldap.sdk.ResultCode;
033    import com.unboundid.util.NotMutable;
034    import com.unboundid.util.InternalUseOnly;
035    import com.unboundid.util.ThreadSafety;
036    import com.unboundid.util.ThreadSafetyLevel;
037    
038    import static com.unboundid.ldap.protocol.ProtocolMessages.*;
039    import static com.unboundid.util.Debug.*;
040    import static com.unboundid.util.StaticUtils.*;
041    import static com.unboundid.util.Validator.*;
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)
051    public 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          ensureNotNull(dn);
109        }
110        catch (Exception e)
111        {
112          debugException(e);
113    
114          throw new LDAPException(ResultCode.DECODING_ERROR,
115               ERR_DELETE_REQUEST_CANNOT_DECODE.get(getExceptionMessage(e)), e);
116        }
117      }
118    
119    
120    
121      /**
122       * Retrieves the target entry DN for this delete request.
123       *
124       * @return  The target entry DN for this delete request.
125       */
126      public String getDN()
127      {
128        return dn;
129      }
130    
131    
132    
133      /**
134       * {@inheritDoc}
135       */
136      public byte getProtocolOpType()
137      {
138        return LDAPMessage.PROTOCOL_OP_TYPE_DELETE_REQUEST;
139      }
140    
141    
142    
143      /**
144       * {@inheritDoc}
145       */
146      public ASN1Element encodeProtocolOp()
147      {
148        return new ASN1OctetString(LDAPMessage.PROTOCOL_OP_TYPE_DELETE_REQUEST, dn);
149      }
150    
151    
152    
153      /**
154       * Decodes the provided ASN.1 element as a delete request protocol op.
155       *
156       * @param  element  The ASN.1 element to be decoded.
157       *
158       * @return  The decoded delete request protocol op.
159       *
160       * @throws  LDAPException  If the provided ASN.1 element cannot be decoded as
161       *                         a delete request protocol op.
162       */
163      public static DeleteRequestProtocolOp decodeProtocolOp(
164                                                 final ASN1Element element)
165             throws LDAPException
166      {
167        try
168        {
169          return new DeleteRequestProtocolOp(
170               ASN1OctetString.decodeAsOctetString(element).stringValue());
171        }
172        catch (final Exception e)
173        {
174          debugException(e);
175          throw new LDAPException(ResultCode.DECODING_ERROR,
176               ERR_DELETE_REQUEST_CANNOT_DECODE.get(getExceptionMessage(e)),
177               e);
178        }
179      }
180    
181    
182    
183      /**
184       * {@inheritDoc}
185       */
186      public void writeTo(final ASN1Buffer buffer)
187      {
188        buffer.addOctetString(LDAPMessage.PROTOCOL_OP_TYPE_DELETE_REQUEST, dn);
189      }
190    
191    
192    
193      /**
194       * Creates a delete request from this protocol op.
195       *
196       * @param  controls  The set of controls to include in the delete request.
197       *                   It may be empty or {@code null} if no controls should be
198       *                   included.
199       *
200       * @return  The delete request that was created.
201       */
202      public DeleteRequest toDeleteRequest(final Control... controls)
203      {
204        return new DeleteRequest(dn, controls);
205      }
206    
207    
208    
209      /**
210       * Retrieves a string representation of this protocol op.
211       *
212       * @return  A string representation of this protocol op.
213       */
214      @Override()
215      public String toString()
216      {
217        final StringBuilder buffer = new StringBuilder();
218        toString(buffer);
219        return buffer.toString();
220      }
221    
222    
223    
224      /**
225       * {@inheritDoc}
226       */
227      public void toString(final StringBuilder buffer)
228      {
229        buffer.append("DeleteRequestProtocolOp(dn='");
230        buffer.append(dn);
231        buffer.append("')");
232      }
233    }