0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
0N/A * Common Development and Distribution License, Version 1.0 only
0N/A * (the "License"). You may not use this file except in compliance
0N/A * with the License.
0N/A *
0N/A * You can obtain a copy of the license at
0N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
0N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
0N/A * See the License for the specific language governing permissions
0N/A * and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at
0N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
0N/A * add the following below this CDDL HEADER, with the fields enclosed
873N/A * by brackets "[]" replaced with your own identifying information:
0N/A * Portions Copyright [yyyy] [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A *
0N/A *
3231N/A * Copyright 2006-2008 Sun Microsystems, Inc.
5970N/A * Portions Copyright 2012 ForgeRock AS.
0N/A */
0N/Apackage org.opends.dsml.protocol;
0N/A
0N/A
0N/A
0N/Aimport java.io.IOException;
5982N/Aimport java.util.List;
5980N/Aimport java.util.Set;
0N/A
2086N/Aimport org.opends.messages.Message;
0N/Aimport org.opends.server.protocols.asn1.ASN1Exception;
0N/Aimport org.opends.server.protocols.ldap.ExtendedRequestProtocolOp;
0N/Aimport org.opends.server.protocols.ldap.ExtendedResponseProtocolOp;
0N/Aimport org.opends.server.protocols.ldap.LDAPMessage;
0N/Aimport org.opends.server.protocols.ldap.ProtocolOp;
0N/Aimport org.opends.server.tools.LDAPConnection;
4134N/Aimport org.opends.server.types.ByteString;
1177N/Aimport org.opends.server.types.LDAPException;
0N/A
0N/A
0N/A/**
0N/A * This class provides the functionality for the performing an
0N/A * LDAP EXTENDED operation based on the specified DSML request.
0N/A */
0N/Apublic class DSMLExtendedOperation
0N/A{
0N/A private LDAPConnection connection;
5980N/A private Set<String> stringResponses;
0N/A
0N/A /**
0N/A * Create an instance with the specified LDAP connection.
0N/A *
0N/A * @param connection The LDAP connection to send the request on.
5980N/A * @param stringResponses The OIDs of any operations that have results that
5980N/A * should be returned as strings instead of binary.
0N/A */
5980N/A public DSMLExtendedOperation(LDAPConnection connection,
5980N/A Set<String> stringResponses)
0N/A {
0N/A this.connection = connection;
5980N/A this.stringResponses = stringResponses;
0N/A }
0N/A
5980N/A
5980N/A
5980N/A /**
5980N/A * Determine if the response to a given LDAP extended operation (specified by
5980N/A * OID) should be treated as a string. The default is binary.
5980N/A *
5980N/A * @param oid The OID of the extended operation.
5980N/A * @return <CODE>true</CODE> if the extended operation is known to return a
5980N/A * string, <CODE>false</CODE> otherwise.
5980N/A */
5980N/A public boolean responseIsString(String oid)
5980N/A {
5980N/A return stringResponses.contains(oid);
5980N/A }
5980N/A
5980N/A
5980N/A
0N/A /**
0N/A * Perform the LDAP EXTENDED operation and send the result back to the
0N/A * client.
0N/A *
0N/A * @param objFactory The object factory for this operation.
0N/A * @param extendedRequest The extended request for this operation.
5982N/A * @param controls Any required controls (e.g. for proxy authz).
0N/A *
0N/A * @return The result of the extended operation.
0N/A *
0N/A * @throws IOException If an I/O problem occurs.
0N/A *
0N/A * @throws LDAPException If an error occurs while interacting with an LDAP
0N/A * element.
0N/A *
0N/A * @throws ASN1Exception If an error occurs while interacting with an ASN.1
0N/A * element.
0N/A */
0N/A public ExtendedResponse doOperation(ObjectFactory objFactory,
5982N/A ExtendedRequest extendedRequest,
5982N/A List<org.opends.server.types.Control> controls)
0N/A throws IOException, LDAPException, ASN1Exception
0N/A {
0N/A ExtendedResponse extendedResponse = objFactory.createExtendedResponse();
1309N/A extendedResponse.setRequestID(extendedRequest.getRequestID());
0N/A
0N/A String requestName = extendedRequest.getRequestName();
0N/A Object value = extendedRequest.getRequestValue();
5992N/A ByteString asnValue = ByteStringUtility.convertValue(value);
0N/A
0N/A // Create and send the LDAP request to the server.
0N/A ProtocolOp op = new ExtendedRequestProtocolOp(requestName, asnValue);
5982N/A LDAPMessage msg = new LDAPMessage(DSMLServlet.nextMessageID(), op,
5982N/A controls);
1924N/A connection.getLDAPWriter().writeMessage(msg);
0N/A
0N/A // Read and decode the LDAP response from the server.
1924N/A LDAPMessage responseMessage = connection.getLDAPReader().readMessage();
0N/A
0N/A ExtendedResponseProtocolOp extendedOp =
0N/A responseMessage.getExtendedResponseProtocolOp();
0N/A int resultCode = extendedOp.getResultCode();
2086N/A Message errorMessage = extendedOp.getErrorMessage();
0N/A
0N/A // Set the result code and error message for the DSML response.
0N/A extendedResponse.setResponseName(extendedOp.getOID());
5970N/A
5980N/A ByteString rawValue = extendedOp.getValue();
5970N/A value = null;
5980N/A if (rawValue != null)
5970N/A {
5980N/A if (responseIsString(requestName))
5980N/A {
5980N/A value = rawValue.toString();
5980N/A }
5980N/A else
5980N/A {
5980N/A value = rawValue.toByteArray();
5980N/A }
5970N/A }
5970N/A extendedResponse.setResponse(value);
2086N/A extendedResponse.setErrorMessage(
2086N/A errorMessage != null ? errorMessage.toString() : null);
5971N/A ResultCode code = ResultCodeFactory.create(objFactory, resultCode);
0N/A extendedResponse.setResultCode(code);
0N/A
0N/A return extendedResponse;
0N/A }
0N/A
0N/A}
0N/A