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.opends.server.tools.LDAPConnection;
036import org.forgerock.opendj.ldap.DecodeException;
037import org.opends.server.protocols.ldap.LDAPMessage;
038import org.opends.server.protocols.ldap.ModifyDNRequestProtocolOp;
039import org.opends.server.protocols.ldap.ModifyDNResponseProtocolOp;
040import org.opends.server.protocols.ldap.ProtocolOp;
041import org.forgerock.opendj.ldap.ByteString;
042import org.opends.server.types.LDAPException;
043
044
045
046/**
047 * This class provides the functionality for the performing an
048 * LDAP MODIFY_DN operation based on the specified DSML request.
049 */
050public class DSMLModifyDNOperation
051{
052
053  private LDAPConnection connection;
054
055  /**
056   * Create the instance with the specified connection.
057   *
058   * @param connection    The LDAP connection to send the request on.
059   */
060  public DSMLModifyDNOperation(LDAPConnection connection)
061  {
062    this.connection = connection;
063  }
064
065  /**
066   * Perform the LDAP Modify DN operation and send the result back to the
067   * client.
068   *
069   * @param  objFactory       The object factory for this operation.
070   * @param  modifyDNRequest  The modify DN request for this operation.
071   * @param  controls         Any required controls (e.g. for proxy authz).
072   *
073   * @return  The result of the modify DN 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  DecodeException  If an error occurs while interacting with an ASN.1
081   *                         element.
082   */
083  public LDAPResult doOperation(ObjectFactory objFactory,
084        ModifyDNRequest modifyDNRequest,
085        List<org.opends.server.types.Control> controls)
086    throws IOException, LDAPException, DecodeException
087  {
088    LDAPResult modDNResponse = objFactory.createLDAPResult();
089    modDNResponse.setRequestID(modifyDNRequest.getRequestID());
090    ByteString dnStr = ByteString.valueOf(modifyDNRequest.getDn());
091    ProtocolOp op = null;
092
093    if (modifyDNRequest.getNewSuperior() != null)
094    {
095      op = new ModifyDNRequestProtocolOp(dnStr, ByteString
096          .valueOf(modifyDNRequest.getNewrdn()), modifyDNRequest
097          .isDeleteoldrdn(), ByteString.valueOf(modifyDNRequest
098          .getNewSuperior()));
099    }
100    else
101    {
102      op = new ModifyDNRequestProtocolOp(dnStr, ByteString
103          .valueOf(modifyDNRequest.getNewrdn()), modifyDNRequest
104          .isDeleteoldrdn());
105    }
106
107    // Create and send the LDAP request to the server.
108    LDAPMessage msg = new LDAPMessage(DSMLServlet.nextMessageID(), op,
109        controls);
110    connection.getLDAPWriter().writeMessage(msg);
111
112    // Read and decode the LDAP response from the server.
113    LDAPMessage responseMessage = connection.getLDAPReader().readMessage();
114
115    ModifyDNResponseProtocolOp modDNOp =
116         responseMessage.getModifyDNResponseProtocolOp();
117    int resultCode = modDNOp.getResultCode();
118    LocalizableMessage errorMessage = modDNOp.getErrorMessage();
119
120    modDNResponse.setErrorMessage(
121            errorMessage != null ? errorMessage.toString() : null);
122    ResultCode code = ResultCodeFactory.create(objFactory, resultCode);
123    modDNResponse.setResultCode(code);
124
125    return modDNResponse;
126  }
127}
128