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 legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2012-2014 ForgeRock AS.
026 */
027package org.opends.dsml.protocol;
028
029
030
031import java.io.IOException;
032import java.util.List;
033
034import org.forgerock.i18n.LocalizableMessage;
035import org.forgerock.opendj.ldap.DecodeException;
036import org.opends.server.protocols.ldap.DeleteRequestProtocolOp;
037import org.opends.server.protocols.ldap.DeleteResponseProtocolOp;
038import org.opends.server.protocols.ldap.LDAPMessage;
039import org.opends.server.protocols.ldap.ProtocolOp;
040import org.opends.server.tools.LDAPConnection;
041import org.forgerock.opendj.ldap.ByteString;
042import org.opends.server.types.DN;
043import org.opends.server.types.LDAPException;
044
045
046/**
047 * This class provides the functionality for the performing an
048 * LDAP DELETE operation based on the specified DSML request.
049 *
050 *
051 * @author   Vivek Nagar
052 */
053
054
055public class DSMLDeleteOperation
056{
057  private LDAPConnection connection;
058
059  /**
060   * Create an instance with the specified LDAP connection.
061   *
062   * @param connection    The LDAP connection to send the request on.
063   */
064  public DSMLDeleteOperation(LDAPConnection connection)
065  {
066    this.connection = connection;
067  }
068
069  /**
070   * Perform the LDAP DELETE operation and send the result back to the
071   * client.
072   *
073   * @param  objFactory     The object factory for this operation.
074   * @param  deleteRequest  The delete request for this operation.
075   * @param  controls       Any required controls (e.g. for proxy authz).
076   *
077   * @return  The result of the delete operation.
078   *
079   * @throws  IOException  If an I/O problem occurs.
080   *
081   * @throws  LDAPException  If an error occurs while interacting with an LDAP
082   *                         element.
083   *
084   * @throws  DecodeException  If an error occurs while interacting with an ASN.1
085   *                         element.
086   */
087  public LDAPResult doOperation(ObjectFactory objFactory,
088        DelRequest deleteRequest,
089        List<org.opends.server.types.Control> controls)
090    throws IOException, LDAPException, DecodeException
091  {
092    LDAPResult delResponse = objFactory.createLDAPResult();
093    delResponse.setRequestID(deleteRequest.getRequestID());
094
095    // Create and send the LDAP delete request to the server.
096    ByteString dnStr = ByteString.valueOf(deleteRequest.getDn());
097    ProtocolOp op = new DeleteRequestProtocolOp(dnStr);
098    LDAPMessage msg = new LDAPMessage(DSMLServlet.nextMessageID(), op,
099        controls);
100    connection.getLDAPWriter().writeMessage(msg);
101
102    // Read and decode the LDAP response from the server.
103    LDAPMessage responseMessage = connection.getLDAPReader().readMessage();
104
105    DeleteResponseProtocolOp delOp =
106          responseMessage.getDeleteResponseProtocolOp();
107    int resultCode = delOp.getResultCode();
108    LocalizableMessage errorMessage = delOp.getErrorMessage();
109
110    // Set the result code and error message for the DSML response.
111    delResponse.setErrorMessage(
112            errorMessage != null ? errorMessage.toString() : null);
113    ResultCode code = ResultCodeFactory.create(objFactory, resultCode);
114    delResponse.setResultCode(code);
115
116    // set the match DN
117    DN dn = delOp.getMatchedDN();
118    if ( dn != null ) {
119      delResponse.setMatchedDN(dn.toString());
120    }
121
122    return delResponse;
123  }
124
125}
126