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.protocols.asn1.ASN1Exception;
035    import org.opends.server.protocols.asn1.ASN1OctetString;
036    import org.opends.server.protocols.ldap.ExtendedRequestProtocolOp;
037    import org.opends.server.protocols.ldap.ExtendedResponseProtocolOp;
038    import org.opends.server.protocols.ldap.LDAPMessage;
039    import org.opends.server.protocols.ldap.ProtocolOp;
040    import org.opends.server.tools.LDAPConnection;
041    import org.opends.server.types.LDAPException;
042    
043    
044    
045    /**
046     * This class provides the functionality for the performing an
047     * LDAP EXTENDED operation based on the specified DSML request.
048     */
049    public class DSMLExtendedOperation
050    {
051      private LDAPConnection connection;
052    
053      /**
054       * Create an instance with the specified LDAP connection.
055       *
056       * @param connection    The LDAP connection to send the request on.
057       */
058      public DSMLExtendedOperation(LDAPConnection connection)
059      {
060        this.connection = connection;
061      }
062    
063      /**
064       * Perform the LDAP EXTENDED operation and send the result back to the
065       * client.
066       *
067       * @param  objFactory       The object factory for this operation.
068       * @param  extendedRequest  The extended request for this operation.
069       *
070       * @return  The result of the extended operation.
071       *
072       * @throws  IOException  If an I/O problem occurs.
073       *
074       * @throws  LDAPException  If an error occurs while interacting with an LDAP
075       *                         element.
076       *
077       * @throws  ASN1Exception  If an error occurs while interacting with an ASN.1
078       *                         element.
079       */
080      public ExtendedResponse doOperation(ObjectFactory objFactory,
081                  ExtendedRequest extendedRequest)
082        throws IOException, LDAPException, ASN1Exception
083      {
084        ExtendedResponse extendedResponse = objFactory.createExtendedResponse();
085        extendedResponse.setRequestID(extendedRequest.getRequestID());
086    
087        String requestName = extendedRequest.getRequestName();
088        Object value = extendedRequest.getRequestValue();
089        ASN1OctetString asnValue = new ASN1OctetString(value.toString());
090    
091        // Create and send the LDAP request to the server.
092        ProtocolOp op = new ExtendedRequestProtocolOp(requestName, asnValue);
093        LDAPMessage msg = new LDAPMessage(DSMLServlet.nextMessageID(), op);
094        connection.getLDAPWriter().writeMessage(msg);
095    
096        // Read and decode the LDAP response from the server.
097        LDAPMessage responseMessage = connection.getLDAPReader().readMessage();
098    
099        ExtendedResponseProtocolOp extendedOp =
100              responseMessage.getExtendedResponseProtocolOp();
101        int resultCode = extendedOp.getResultCode();
102        Message errorMessage = extendedOp.getErrorMessage();
103    
104        // Set the result code and error message for the DSML response.
105        extendedResponse.setResponseName(extendedOp.getOID());
106        extendedResponse.setResponse(extendedOp.getValue());
107        extendedResponse.setErrorMessage(
108                errorMessage != null ? errorMessage.toString() : null);
109        ResultCode code = objFactory.createResultCode();
110        code.setCode(resultCode);
111        extendedResponse.setResultCode(code);
112    
113        return extendedResponse;
114      }
115    
116    }
117