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