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