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.dsml.protocol;
028    
029    import java.io.IOException;
030    
031    import org.opends.messages.Message;
032    import org.opends.server.protocols.asn1.ASN1Exception;
033    import org.opends.server.protocols.asn1.ASN1OctetString;
034    import org.opends.server.protocols.ldap.DeleteRequestProtocolOp;
035    import org.opends.server.protocols.ldap.DeleteResponseProtocolOp;
036    import org.opends.server.protocols.ldap.LDAPMessage;
037    import org.opends.server.protocols.ldap.ProtocolOp;
038    import org.opends.server.tools.LDAPConnection;
039    import org.opends.server.types.DN;
040    import org.opends.server.types.LDAPException;
041    
042    
043    /**
044     * This class provides the functionality for the performing an
045     * LDAP DELETE operation based on the specified DSML request.
046     *
047     *
048     * @author   Vivek Nagar
049     */
050    
051    
052    public class DSMLDeleteOperation
053    {
054      private LDAPConnection connection;
055    
056      /**
057       * Create an instance with the specified LDAP connection.
058       *
059       * @param connection    The LDAP connection to send the request on.
060       */
061      public DSMLDeleteOperation(LDAPConnection connection)
062      {
063        this.connection = connection;
064      }
065    
066      /**
067       * Perform the LDAP DELETE operation and send the result back to the
068       * client.
069       *
070       * @param  objFactory     The object factory for this operation.
071       * @param  deleteRequest  The delete request for this operation.
072       *
073       * @return  The result of the delete operation.
074       *
075       * @throws  IOException  If an I/O problem occurs.
076       *
077       * @throws  LDAPException  If an error occurs while interacting with an LDAP
078       *                         element.
079       *
080       * @throws  ASN1Exception  If an error occurs while interacting with an ASN.1
081       *                         element.
082       */
083      public LDAPResult doOperation(ObjectFactory objFactory,
084            DelRequest deleteRequest)
085        throws IOException, LDAPException, ASN1Exception
086      {
087        LDAPResult delResponse = objFactory.createLDAPResult();
088        delResponse.setRequestID(deleteRequest.getRequestID());
089    
090        // Create and send the LDAP delete request to the server.
091        ASN1OctetString dnStr = new ASN1OctetString(deleteRequest.getDn());
092        ProtocolOp op = new DeleteRequestProtocolOp(dnStr);
093        LDAPMessage msg = new LDAPMessage(DSMLServlet.nextMessageID(), op);
094        connection.getLDAPWriter().writeMessage(msg);
095    
096        // Read and decode the LDAP response from the server.
097        LDAPMessage responseMessage = connection.getLDAPReader().readMessage();
098    
099        DeleteResponseProtocolOp delOp =
100              responseMessage.getDeleteResponseProtocolOp();
101        int resultCode = delOp.getResultCode();
102        Message errorMessage = delOp.getErrorMessage();
103    
104        // Set the result code and error message for the DSML response.
105        delResponse.setErrorMessage(
106                errorMessage != null ? errorMessage.toString() : null);
107        ResultCode code = objFactory.createResultCode();
108        code.setCode(resultCode);
109        delResponse.setResultCode(code);
110    
111        // set the match DN
112        DN dn = delOp.getMatchedDN();
113        if ( dn != null ) {
114          delResponse.setMatchedDN(dn.toString());
115        }
116    
117        return delResponse;
118      }
119    
120    }
121