From a030e2933d31f9fa47988508298b2651dfaa5e03 Mon Sep 17 00:00:00 2001
From: Chris Ridd <chris.ridd@forgerock.com>
Date: Thu, 25 Oct 2012 15:42:46 +0000
Subject: [PATCH] Fix OPENDJ-624 DSML servlet should encode some kinds of extended responses as strings

---
 opends/src/dsml/org/opends/dsml/protocol/DSMLExtendedOperation.java |   38 +++++++++++++++++--
 opends/resource/dsml/webapp/web.xml                                 |    8 ++++
 opends/src/dsml/org/opends/dsml/protocol/DSMLServlet.java           |   27 ++++++++++++-
 3 files changed, 67 insertions(+), 6 deletions(-)

diff --git a/opends/resource/dsml/webapp/web.xml b/opends/resource/dsml/webapp/web.xml
index da07b86..7a33a03 100644
--- a/opends/resource/dsml/webapp/web.xml
+++ b/opends/resource/dsml/webapp/web.xml
@@ -72,6 +72,14 @@
   </context-param>
 -->
 
+<!-- Add an extra <context-param> like the one below for each extended operation
+     that is known to return a string in the LDAP response. -->
+  <context-param>
+  	<description>The Who Am I? [RFC 4532] extended operation returns a string.</description>
+    <param-name>ldap.exop.string.1.3.6.1.4.1.4203.1.11.3</param-name>
+    <param-value>true</param-value>
+  </context-param>
+ 
   <servlet>
     <servlet-name>DSMLServlet</servlet-name>
     <servlet-class>org.opends.dsml.protocol.DSMLServlet</servlet-class>
diff --git a/opends/src/dsml/org/opends/dsml/protocol/DSMLExtendedOperation.java b/opends/src/dsml/org/opends/dsml/protocol/DSMLExtendedOperation.java
index 560ed5b..b66b77e 100644
--- a/opends/src/dsml/org/opends/dsml/protocol/DSMLExtendedOperation.java
+++ b/opends/src/dsml/org/opends/dsml/protocol/DSMLExtendedOperation.java
@@ -30,6 +30,7 @@
 
 
 import java.io.IOException;
+import java.util.Set;
 
 import org.opends.messages.Message;
 import org.opends.server.protocols.asn1.ASN1Exception;
@@ -51,17 +52,39 @@
 public class DSMLExtendedOperation
 {
   private LDAPConnection connection;
+  private Set<String> stringResponses;
 
   /**
    * Create an instance with the specified LDAP connection.
    *
    * @param connection    The LDAP connection to send the request on.
+   * @param stringResponses The OIDs of any operations that have results that
+   *                        should be returned as strings instead of binary.
    */
-  public DSMLExtendedOperation(LDAPConnection connection)
+  public DSMLExtendedOperation(LDAPConnection connection,
+      Set<String> stringResponses)
   {
     this.connection = connection;
+    this.stringResponses = stringResponses;
   }
 
+
+
+  /**
+   * Determine if the response to a given LDAP extended operation (specified by
+   * OID) should be treated as a string. The default is binary.
+   *
+   * @param oid The OID of the extended operation.
+   * @return <CODE>true</CODE> if the extended operation is known to return a
+   *         string, <CODE>false</CODE> otherwise.
+   */
+  public boolean responseIsString(String oid)
+  {
+    return stringResponses.contains(oid);
+  }
+
+
+
   /**
    * Perform the LDAP EXTENDED operation and send the result back to the
    * client.
@@ -132,11 +155,18 @@
     // Set the result code and error message for the DSML response.
     extendedResponse.setResponseName(extendedOp.getOID());
 
-    asnValue = extendedOp.getValue();
+    ByteString rawValue = extendedOp.getValue();
     value = null;
-    if (asnValue != null)
+    if (rawValue != null)
     {
-      value = asnValue.toByteArray();
+      if (responseIsString(requestName))
+      {
+        value = rawValue.toString();
+      }
+      else
+      {
+        value = rawValue.toByteArray();
+      }
     }
     extendedResponse.setResponse(value);
     extendedResponse.setErrorMessage(
diff --git a/opends/src/dsml/org/opends/dsml/protocol/DSMLServlet.java b/opends/src/dsml/org/opends/dsml/protocol/DSMLServlet.java
index d1e8dc0..8d90851 100644
--- a/opends/src/dsml/org/opends/dsml/protocol/DSMLServlet.java
+++ b/opends/src/dsml/org/opends/dsml/protocol/DSMLServlet.java
@@ -23,7 +23,7 @@
  *
  *
  *      Copyright 2006-2010 Sun Microsystems, Inc.
- *      Portions Copyright 2011 ForgeRock AS
+ *      Portions Copyright 2011-2012 ForgeRock AS
  */
 package org.opends.dsml.protocol;
 
@@ -39,6 +39,7 @@
 import java.text.ParseException;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.Enumeration;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.StringTokenizer;
@@ -97,6 +98,7 @@
   private static final String TRUSTSTOREPASSWORD = "ldap.truststore.password";
   private static final String TRUSTALLCERTS = "ldap.trustall";
   private static final String USEHTTPAUTHZID = "ldap.authzidtypeisid";
+  private static final String EXOPSTRINGPREFIX = "ldap.exop.string.";
   private static final long serialVersionUID = -3748022009593442973L;
   private static final AtomicInteger nextMessageID = new AtomicInteger(1);
 
@@ -134,6 +136,8 @@
   private String trustStorePasswordValue;
   private Boolean trustAll;
   private Boolean useHTTPAuthzID;
+  private HashSet<String> exopStrings = new HashSet<String>();
+
   /**
    * This method will be called by the Servlet Container when
    * this servlet is being placed into service.
@@ -172,6 +176,24 @@
       useHTTPAuthzID = Boolean.valueOf(
           config.getServletContext().getInitParameter(USEHTTPAUTHZID));
 
+      /*
+       * Find all the param-names matching the pattern:
+       * ldap.exop.string.1.2.3.4.5
+       * and if the value's true then mark that OID (1.2.3.4.5) as one returning
+       * a string value.
+       */
+      Enumeration<String> names = config.getServletContext()
+          .getInitParameterNames();
+      while (names.hasMoreElements())
+      {
+        String name = names.nextElement().toString();
+        if (name.startsWith(EXOPSTRINGPREFIX) &&
+            Boolean.valueOf(config.getServletContext().getInitParameter(name)))
+        {
+          exopStrings.add(name.substring(EXOPSTRINGPREFIX.length()));
+        }
+      }
+
       if(jaxbContext==null)
         jaxbContext = JAXBContext.newInstance(PKG_NAME,
                 this.getClass().getClassLoader());
@@ -545,7 +567,8 @@
       } else if (request instanceof ExtendedRequest) {
         // Process the extended request.
         ExtendedRequest er = (ExtendedRequest) request;
-        DSMLExtendedOperation eo = new DSMLExtendedOperation(connection);
+        DSMLExtendedOperation eo = new DSMLExtendedOperation(connection,
+            exopStrings);
         ExtendedResponse extendedResponse = eo.doOperation(objFactory, er);
         return objFactory.createBatchResponseExtendedResponse(extendedResponse);
 

--
Gitblit v1.10.0