From 9ddb8a08f2e80d9a9eaae5f03d93106ec792138c Mon Sep 17 00:00:00 2001
From: neil_a_wilson <neil_a_wilson@localhost>
Date: Wed, 09 May 2007 20:41:56 +0000
Subject: [PATCH] Update the DSML gateway so that it properly treats the request ID as an optional element rather than required.  Also, update the DSML search processing code to be a little more forgiving when it comes to the search scope and deref policy strings.

---
 opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLAbandonOperation.java  |   14 ----
 opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLModifyDNOperation.java |   14 ----
 opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLModifyOperation.java   |   14 ----
 opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLExtendedOperation.java |   16 ----
 opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLDeleteOperation.java   |   16 ----
 opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLAddOperation.java      |   16 ----
 opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLCompareOperation.java  |   22 ------
 opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLSearchOperation.java   |   39 +++++++------
 opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLServlet.java           |   23 +++++++
 9 files changed, 57 insertions(+), 117 deletions(-)

diff --git a/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLAbandonOperation.java b/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLAbandonOperation.java
index c581673..822b0ec 100644
--- a/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLAbandonOperation.java
+++ b/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLAbandonOperation.java
@@ -70,18 +70,8 @@
   {
     LDAPResult abandonResponse = objFactory.createLDAPResult();
 
-    String requestID = abandonRequest.getRequestID();
-    int reqID = 0;
-    try
-    {
-      reqID = Integer.parseInt(requestID);
-    } catch (NumberFormatException nfe)
-    {
-      throw new IOException(nfe.getMessage());
-    }
-
     // Set the id for the response.
-    abandonResponse.setRequestID(requestID);
+    abandonResponse.setRequestID(abandonRequest.getRequestID());
 
     String abandonIdStr = abandonRequest.getAbandonID();
     int abandonId = 0;
@@ -95,7 +85,7 @@
 
     // Create and send an LDAP request to the server.
     ProtocolOp op = new AbandonRequestProtocolOp(abandonId);
-    LDAPMessage msg = new LDAPMessage(reqID, op);
+    LDAPMessage msg = new LDAPMessage(DSMLServlet.nextMessageID(), op);
     int numBytes = connection.getASN1Writer().writeElement(msg.encode());
 
     return abandonResponse;
diff --git a/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLAddOperation.java b/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLAddOperation.java
index a7a41b6..6c5b996 100644
--- a/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLAddOperation.java
+++ b/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLAddOperation.java
@@ -85,19 +85,7 @@
     throws IOException, LDAPException, ASN1Exception
   {
     LDAPResult addResponse = objFactory.createLDAPResult();
-
-    String requestID = addRequest.getRequestID();
-    int reqID = 1;
-    try
-    {
-      reqID = Integer.parseInt(requestID);
-    } catch (NumberFormatException nfe)
-    {
-      throw new IOException(nfe.getMessage());
-    }
-
-    // Set the response id.
-    addResponse.setRequestID(requestID);
+    addResponse.setRequestID(addRequest.getRequestID());
 
     ASN1OctetString dnStr = new ASN1OctetString(addRequest.getDn());
     ArrayList<RawAttribute> attributes = new ArrayList<RawAttribute>();
@@ -117,7 +105,7 @@
 
     // Create and send the LDAP request to the server.
     ProtocolOp op = new AddRequestProtocolOp(dnStr, attributes);
-    LDAPMessage msg = new LDAPMessage(reqID, op);
+    LDAPMessage msg = new LDAPMessage(DSMLServlet.nextMessageID(), op);
     int numBytes = connection.getASN1Writer().writeElement(msg.encode());
 
     // Read and decode the LDAP response from the server.
diff --git a/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLCompareOperation.java b/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLCompareOperation.java
index 1ded6cb..303e36c 100644
--- a/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLCompareOperation.java
+++ b/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLCompareOperation.java
@@ -83,25 +83,7 @@
     throws IOException, LDAPException, ASN1Exception
   {
     LDAPResult compareResponse = objFactory.createLDAPResult();
-
-    String requestID = compareRequest.getRequestID();
-    int reqID = 1;
-    try
-    {
-      if(requestID != null)
-      {
-        reqID = Integer.parseInt(requestID);
-      }
-    } catch (NumberFormatException nfe)
-    {
-      throw new IOException(nfe.getMessage());
-    }
-
-    // Set the response id.
-    if(requestID != null)
-    {
-      compareResponse.setRequestID(requestID);
-    }
+    compareResponse.setRequestID(compareRequest.getRequestID());
 
     // Read the attribute name and value for the compare request.
     AttributeValueAssertion attrValAssertion = compareRequest.getAssertion();
@@ -113,7 +95,7 @@
 
     // Create and send the LDAP compare request to the server.
     ProtocolOp op = new CompareRequestProtocolOp(dnStr, attrName, attrValue);
-    LDAPMessage msg = new LDAPMessage(reqID, op);
+    LDAPMessage msg = new LDAPMessage(DSMLServlet.nextMessageID(), op);
     int numBytes = connection.getASN1Writer().writeElement(msg.encode());
 
     // Read and decode the LDAP response from the server.
diff --git a/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLDeleteOperation.java b/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLDeleteOperation.java
index 7a3ef58..505d615 100644
--- a/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLDeleteOperation.java
+++ b/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLDeleteOperation.java
@@ -85,24 +85,12 @@
     throws IOException, LDAPException, ASN1Exception
   {
     LDAPResult delResponse = objFactory.createLDAPResult();
-
-    String requestID = deleteRequest.getRequestID();
-    int reqID = 1;
-    try
-    {
-      reqID = Integer.parseInt(requestID);
-    } catch (NumberFormatException nfe)
-    {
-      throw new IOException(nfe.getMessage());
-    }
-
-    // Set the response id.
-    delResponse.setRequestID(requestID);
+    delResponse.setRequestID(deleteRequest.getRequestID());
 
     // Create and send the LDAP delete request to the server.
     ASN1OctetString dnStr = new ASN1OctetString(deleteRequest.getDn());
     ProtocolOp op = new DeleteRequestProtocolOp(dnStr);
-    LDAPMessage msg = new LDAPMessage(reqID, op);
+    LDAPMessage msg = new LDAPMessage(DSMLServlet.nextMessageID(), op);
     int numBytes = connection.getASN1Writer().writeElement(msg.encode());
 
     // Read and decode the LDAP response from the server.
diff --git a/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLExtendedOperation.java b/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLExtendedOperation.java
index 7628c3d..cb95db7 100644
--- a/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLExtendedOperation.java
+++ b/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLExtendedOperation.java
@@ -83,19 +83,7 @@
     throws IOException, LDAPException, ASN1Exception
   {
     ExtendedResponse extendedResponse = objFactory.createExtendedResponse();
-
-    String requestID = extendedRequest.getRequestID();
-    int reqID = 1;
-    try
-    {
-      reqID = Integer.parseInt(requestID);
-    } catch (NumberFormatException nfe)
-    {
-      throw new IOException(nfe.getMessage());
-    }
-
-    // Set the response id.
-    extendedResponse.setRequestID(requestID);
+    extendedResponse.setRequestID(extendedRequest.getRequestID());
 
     String requestName = extendedRequest.getRequestName();
     Object value = extendedRequest.getRequestValue();
@@ -103,7 +91,7 @@
 
     // Create and send the LDAP request to the server.
     ProtocolOp op = new ExtendedRequestProtocolOp(requestName, asnValue);
-    LDAPMessage msg = new LDAPMessage(reqID, op);
+    LDAPMessage msg = new LDAPMessage(DSMLServlet.nextMessageID(), op);
     int numBytes = connection.getASN1Writer().writeElement(msg.encode());
 
     // Read and decode the LDAP response from the server.
diff --git a/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLModifyDNOperation.java b/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLModifyDNOperation.java
index cd8a830..bfd1184 100644
--- a/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLModifyDNOperation.java
+++ b/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLModifyDNOperation.java
@@ -84,17 +84,7 @@
     throws IOException, LDAPException, ASN1Exception
   {
     LDAPResult modDNResponse = objFactory.createLDAPResult();
-
-    String requestID = modifyDNRequest.getRequestID();
-    int reqID = 1;
-    try
-    {
-      reqID = Integer.parseInt(requestID);
-    } catch (NumberFormatException nfe)
-    {
-      throw new IOException(nfe.getMessage());
-    }
-    modDNResponse.setRequestID(requestID);
+    modDNResponse.setRequestID(modifyDNRequest.getRequestID());
 
     ASN1OctetString dnStr = new ASN1OctetString(modifyDNRequest.getDn());
 
@@ -114,7 +104,7 @@
     }
 
     // Create and send the LDAP request to the server.
-    LDAPMessage msg = new LDAPMessage(reqID, op);
+    LDAPMessage msg = new LDAPMessage(DSMLServlet.nextMessageID(), op);
     int numBytes = connection.getASN1Writer().writeElement(msg.encode());
 
     // Read and decode the LDAP response from the server.
diff --git a/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLModifyOperation.java b/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLModifyOperation.java
index b62428b..a332844 100644
--- a/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLModifyOperation.java
+++ b/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLModifyOperation.java
@@ -89,17 +89,7 @@
         throws IOException, LDAPException, ASN1Exception
   {
     LDAPResult modResponse = objFactory.createLDAPResult();
-    String requestID = modifyRequest.getRequestID();
-    int reqID = 1;
-    try
-    {
-      reqID = Integer.parseInt(requestID);
-    } catch (NumberFormatException nfe)
-    {
-      throw new IOException(nfe.getMessage());
-    }
-
-    modResponse.setRequestID(requestID);
+    modResponse.setRequestID(modifyRequest.getRequestID());
 
     ArrayList<RawModification> modifications =
          new ArrayList<RawModification> ();
@@ -138,7 +128,7 @@
 
     // Create and send the LDAP request to the server.
     ProtocolOp op = new ModifyRequestProtocolOp(dnStr, modifications);
-    LDAPMessage msg = new LDAPMessage(reqID, op);
+    LDAPMessage msg = new LDAPMessage(DSMLServlet.nextMessageID(), op);
     int numBytes = connection.getASN1Writer().writeElement(msg.encode());
 
     // Read and parse the LDAP response from the server.
diff --git a/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLSearchOperation.java b/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLSearchOperation.java
index 81d1ebc..614df02 100644
--- a/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLSearchOperation.java
+++ b/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLSearchOperation.java
@@ -93,18 +93,7 @@
          throws IOException, LDAPException
   {
     SearchResponse searchResponse = objFactory.createSearchResponse();
-
-    String requestID = searchRequest.getRequestID();
-    int reqID = 1;
-    try
-    {
-      reqID = Integer.parseInt(requestID);
-    } catch (NumberFormatException nfe)
-    {
-      throw new IOException(nfe.getMessage());
-    }
-
-    searchResponse.setRequestID(requestID);
+    searchResponse.setRequestID(searchRequest.getRequestID());
 
     ArrayList<LDAPFilter> filters = new ArrayList<LDAPFilter> ();
     LDAPFilter f = null;
@@ -124,12 +113,27 @@
     {
       filters.add(f);
     }
-    DereferencePolicy derefPolicy = DereferencePolicy.DEREF_IN_SEARCHING;
+    DereferencePolicy derefPolicy = DereferencePolicy.NEVER_DEREF_ALIASES;
+    String derefStr = searchRequest.getDerefAliases().toLowerCase();
+    if (derefStr.equals("derefinsearching"))
+    {
+      derefPolicy = DereferencePolicy.DEREF_IN_SEARCHING;
+    }
+    else if (derefStr.equals("dereffindingbaseobj"))
+    {
+      derefPolicy = DereferencePolicy.DEREF_FINDING_BASE_OBJECT;
+    }
+    else if (derefStr.equals("derefalways"))
+    {
+      derefPolicy = DereferencePolicy.DEREF_ALWAYS;
+    }
+
     SearchScope scope = SearchScope.WHOLE_SUBTREE;
-    if(searchRequest.getScope().equals("singleLevel"))
+    String scopeStr = searchRequest.getScope().toLowerCase();
+    if(scopeStr.equals("singlelevel") || scopeStr.equals("one"))
     {
       scope = SearchScope.SINGLE_LEVEL;
-    } else if(searchRequest.getScope().equals("baseObject"))
+    } else if(scopeStr.equals("baseobject") || scopeStr.equals("base"))
     {
       scope = SearchScope.BASE_OBJECT;
     }
@@ -157,7 +161,8 @@
           false, filter, attributes);
       try
       {
-        LDAPMessage msg = new LDAPMessage(reqID, protocolOp);
+        LDAPMessage msg = new LDAPMessage(DSMLServlet.nextMessageID(),
+                                          protocolOp);
         int numBytes = connection.getASN1Writer().writeElement(msg.encode());
 
         byte opType;
@@ -250,8 +255,6 @@
         ae.printStackTrace();
         throw new IOException(ae.getMessage());
       }
-
-      reqID++;
     }
     return searchResponse;
   }
diff --git a/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLServlet.java b/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLServlet.java
index f59e435..2e1144f 100644
--- a/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLServlet.java
+++ b/opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/DSMLServlet.java
@@ -65,6 +65,7 @@
   private static final String PORT = "ldap.port";
   private static final String HOST = "ldap.host";
   private static final long serialVersionUID = -3748022009593442973L;
+  private static final AtomicInteger nextMessageID = new AtomicInteger(1);
 
   private Unmarshaller unmarshaller;
   private Marshaller marshaller;
@@ -303,7 +304,7 @@
       }
     } finally {
       if (connection != null) {
-        connection.close(new AtomicInteger(1));
+        connection.close(nextMessageID);
       }
     }
   }
@@ -347,5 +348,25 @@
     reply.writeTo(os);
     os.flush();
   }
+
+
+
+  /**
+   * Retrieves a message ID that may be used for the next LDAP message sent to
+   * the Directory Server.
+   *
+   * @return  A message ID that may be used for the next LDAP message sent to
+   *          the Directory Server.
+   */
+  public static int nextMessageID()
+  {
+    int nextID = nextMessageID.getAndIncrement();
+    if (nextID == Integer.MAX_VALUE)
+    {
+      nextMessageID.set(1);
+    }
+
+    return nextID;
+  }
 }
 

--
Gitblit v1.10.0