001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.protocols.ldap;
028    import org.opends.messages.Message;
029    
030    
031    
032    import org.opends.server.protocols.asn1.ASN1Element;
033    import org.opends.server.protocols.asn1.ASN1OctetString;
034    import org.opends.server.types.DebugLogLevel;
035    import org.opends.server.types.LDAPException;
036    
037    import static org.opends.server.loggers.debug.DebugLogger.*;
038    import org.opends.server.loggers.debug.DebugTracer;
039    import static org.opends.messages.ProtocolMessages.*;
040    import static org.opends.server.protocols.ldap.LDAPConstants.*;
041    import static org.opends.server.protocols.ldap.LDAPResultCode.*;
042    import static org.opends.server.util.ServerConstants.*;
043    
044    
045    /**
046     * This class defines the structures and methods for an LDAP delete request
047     * protocol op, which is used to remove an entry from the Directory Server.
048     */
049    public class DeleteRequestProtocolOp
050           extends ProtocolOp
051    {
052      /**
053       * The tracer object for the debug logger.
054       */
055      private static final DebugTracer TRACER = getTracer();
056    
057      // The DN for this delete request.
058      private ASN1OctetString dn;
059    
060    
061    
062      /**
063       * Creates a new delete request protocol op with the specified DN.
064       *
065       * @param  dn  The DN for this delete request protocol op.
066       */
067      public DeleteRequestProtocolOp(ASN1OctetString dn)
068      {
069        this.dn = dn;
070      }
071    
072    
073    
074      /**
075       * Retrieves the DN for this delete request.
076       *
077       * @return  The DN for this delete request.
078       */
079      public ASN1OctetString getDN()
080      {
081        return dn;
082      }
083    
084    
085    
086      /**
087       * Specifies the DN for this delete request.
088       *
089       * @param  dn  The DN for this delete request.
090       */
091      public void setDN(ASN1OctetString dn)
092      {
093        this.dn = dn;
094      }
095    
096    
097    
098      /**
099       * Retrieves the BER type for this protocol op.
100       *
101       * @return  The BER type for this protocol op.
102       */
103      public byte getType()
104      {
105        return OP_TYPE_DELETE_REQUEST;
106      }
107    
108    
109    
110      /**
111       * Retrieves the name for this protocol op type.
112       *
113       * @return  The name for this protocol op type.
114       */
115      public String getProtocolOpName()
116      {
117        return "Delete Request";
118      }
119    
120    
121    
122      /**
123       * Encodes this protocol op to an ASN.1 element suitable for including in an
124       * LDAP message.
125       *
126       * @return  The ASN.1 element containing the encoded protocol op.
127       */
128      public ASN1Element encode()
129      {
130        dn.setType(OP_TYPE_DELETE_REQUEST);
131        return dn;
132      }
133    
134    
135    
136      /**
137       * Decodes the provided ASN.1 element as an LDAP delete request protocol op.
138       *
139       * @param  element  The ASN.1 element to be decoded.
140       *
141       * @return  The decoded delete request protocol op.
142       *
143       * @throws  LDAPException  If a problem occurs while decoding the provided
144       *                         ASN.1 element as a delete request protocol op.
145       */
146      public static DeleteRequestProtocolOp decodeDeleteRequest(ASN1Element element)
147             throws LDAPException
148      {
149        try
150        {
151          return new DeleteRequestProtocolOp(element.decodeAsOctetString());
152        }
153        catch (Exception e)
154        {
155          if (debugEnabled())
156          {
157            TRACER.debugCaught(DebugLogLevel.ERROR, e);
158          }
159    
160          Message message =
161              ERR_LDAP_DELETE_REQUEST_DECODE_DN.get(String.valueOf(e));
162          throw new LDAPException(PROTOCOL_ERROR, message, e);
163        }
164      }
165    
166    
167    
168      /**
169       * Appends a string representation of this LDAP protocol op to the provided
170       * buffer.
171       *
172       * @param  buffer  The buffer to which the string should be appended.
173       */
174      public void toString(StringBuilder buffer)
175      {
176        buffer.append("DeleteRequest(dn=");
177        dn.toString(buffer);
178        buffer.append(")");
179      }
180    
181    
182    
183      /**
184       * Appends a multi-line string representation of this LDAP protocol op to the
185       * provided buffer.
186       *
187       * @param  buffer  The buffer to which the information should be appended.
188       * @param  indent  The number of spaces from the margin that the lines should
189       *                 be indented.
190       */
191      public void toString(StringBuilder buffer, int indent)
192      {
193        StringBuilder indentBuf = new StringBuilder(indent);
194        for (int i=0 ; i < indent; i++)
195        {
196          indentBuf.append(' ');
197        }
198    
199        buffer.append(indentBuf);
200        buffer.append("Delete Request");
201        buffer.append(EOL);
202    
203        buffer.append(indentBuf);
204        buffer.append("  Entry DN:  ");
205        dn.toString(buffer);
206        buffer.append(EOL);
207      }
208    }
209