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-2015 ForgeRock AS.
016 */
017package org.opends.dsml.protocol;
018
019
020
021import java.io.IOException;
022import java.util.List;
023
024import org.forgerock.i18n.LocalizableMessage;
025import org.opends.server.tools.LDAPConnection;
026import org.forgerock.opendj.ldap.DecodeException;
027import org.opends.server.protocols.ldap.LDAPMessage;
028import org.opends.server.protocols.ldap.ModifyDNRequestProtocolOp;
029import org.opends.server.protocols.ldap.ModifyDNResponseProtocolOp;
030import org.opends.server.protocols.ldap.ProtocolOp;
031import org.forgerock.opendj.ldap.ByteString;
032import org.opends.server.types.LDAPException;
033
034
035
036/**
037 * This class provides the functionality for the performing an
038 * LDAP MODIFY_DN operation based on the specified DSML request.
039 */
040public class DSMLModifyDNOperation
041{
042
043  private LDAPConnection connection;
044
045  /**
046   * Create the instance with the specified connection.
047   *
048   * @param connection    The LDAP connection to send the request on.
049   */
050  public DSMLModifyDNOperation(LDAPConnection connection)
051  {
052    this.connection = connection;
053  }
054
055  /**
056   * Perform the LDAP Modify DN operation and send the result back to the
057   * client.
058   *
059   * @param  objFactory       The object factory for this operation.
060   * @param  modifyDNRequest  The modify DN request for this operation.
061   * @param  controls         Any required controls (e.g. for proxy authz).
062   *
063   * @return  The result of the modify DN operation.
064   *
065   * @throws  IOException  If an I/O problem occurs.
066   *
067   * @throws  LDAPException  If an error occurs while interacting with an LDAP
068   *                         element.
069   *
070   * @throws  DecodeException  If an error occurs while interacting with an ASN.1
071   *                         element.
072   */
073  public LDAPResult doOperation(ObjectFactory objFactory,
074        ModifyDNRequest modifyDNRequest,
075        List<org.opends.server.types.Control> controls)
076    throws IOException, LDAPException, DecodeException
077  {
078    LDAPResult modDNResponse = objFactory.createLDAPResult();
079    modDNResponse.setRequestID(modifyDNRequest.getRequestID());
080    ByteString dnStr = ByteString.valueOfUtf8(modifyDNRequest.getDn());
081    ProtocolOp op = null;
082
083    if (modifyDNRequest.getNewSuperior() != null)
084    {
085      op = new ModifyDNRequestProtocolOp(dnStr, ByteString
086          .valueOfUtf8(modifyDNRequest.getNewrdn()), modifyDNRequest
087          .isDeleteoldrdn(), ByteString.valueOfUtf8(modifyDNRequest
088          .getNewSuperior()));
089    }
090    else
091    {
092      op = new ModifyDNRequestProtocolOp(dnStr, ByteString
093          .valueOfUtf8(modifyDNRequest.getNewrdn()), modifyDNRequest
094          .isDeleteoldrdn());
095    }
096
097    // Create and send the LDAP request to the server.
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    ModifyDNResponseProtocolOp modDNOp =
106         responseMessage.getModifyDNResponseProtocolOp();
107    int resultCode = modDNOp.getResultCode();
108    LocalizableMessage errorMessage = modDNOp.getErrorMessage();
109
110    modDNResponse.setErrorMessage(
111            errorMessage != null ? errorMessage.toString() : null);
112    ResultCode code = ResultCodeFactory.create(objFactory, resultCode);
113    modDNResponse.setResultCode(code);
114
115    return modDNResponse;
116  }
117}
118