From c3d27cc0dea5d8915fbd09b9e84a800ebcfbbe33 Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Wed, 13 Jan 2010 01:54:09 +0000
Subject: [PATCH] Fix for issue 4484 (Follow Referrals functionality is broken). The code was not complete.  Fix some issues in different areas.

---
 opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/BrowseEntriesPanel.java      |   11 +++++
 opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/browser/BrowserController.java  |   13 +++---
 opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/browser/NodeSearcherQueue.java  |   16 +++----
 opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/browser/LDAPConnectionPool.java |   26 ++++++++++--
 4 files changed, 45 insertions(+), 21 deletions(-)

diff --git a/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/browser/BrowserController.java b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/browser/BrowserController.java
index 46cbd87..d2c2f54 100644
--- a/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/browser/BrowserController.java
+++ b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/browser/BrowserController.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2008-2009 Sun Microsystems, Inc.
+ *      Copyright 2008-2010 Sun Microsystems, Inc.
  */
 
 package org.opends.guitools.controlpanel.browser;
@@ -1148,7 +1148,8 @@
       boolean isConfigurationNode)
   throws NamingException {
     InitialLdapContext result;
-    if (followReferrals && (node.getRemoteUrl() != null)) {
+    if (followReferrals && (node.getRemoteUrl() != null))
+    {
       result = connectionPool.getConnection(node.getRemoteUrl());
     }
     else {
@@ -1676,7 +1677,7 @@
       // Note: logically we should unconditionaly call:
       //  startRefreshNode(child, false, true);
       //
-      // However doing that saturates _refreshQueue
+      // However doing that saturates refreshQueue
       // with many nodes. And, by design, RefreshTask
       // won't do anything on a node if:
       //    - this node has no subordinates
@@ -2080,7 +2081,8 @@
   {
     String[] result = null;
     Set<String> values = ConnectionUtils.getValues(entry, "objectClass");
-    if (values != null) {
+    if (values != null)
+    {
       for (String value : values)
       {
         boolean isReferral = value.equalsIgnoreCase("referral");
@@ -2091,10 +2093,9 @@
           {
             result = new String[refValues.size()];
             refValues.toArray(result);
-            break;
           }
+          break;
         }
-        break;
       }
     }
     return result;
diff --git a/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/browser/LDAPConnectionPool.java b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/browser/LDAPConnectionPool.java
index d83fe14..70eaa3f 100644
--- a/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/browser/LDAPConnectionPool.java
+++ b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/browser/LDAPConnectionPool.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2008-2009 Sun Microsystems, Inc.
+ *      Copyright 2008-2010 Sun Microsystems, Inc.
  */
 
 package org.opends.guitools.controlpanel.browser;
@@ -39,6 +39,7 @@
 import org.opends.admin.ads.util.ApplicationTrustManager;
 import org.opends.admin.ads.util.ConnectionUtils;
 import org.opends.guitools.controlpanel.event.ReferralAuthenticationListener;
+import org.opends.server.types.DN;
 import org.opends.server.types.LDAPURL;
 import org.opends.server.types.SearchScope;
 
@@ -59,7 +60,7 @@
  * returned connection is simply connected (ie anonymous bind).
  * <BR><BR>
  * LDAPConnectionPool shares connections and maintains a usage counter
- * for each connection: two calls to getConnection() withe the same URL
+ * for each connection: two calls to getConnection() with the same URL
  * will return the same connection. Two calls to releaseConnection() will
  * be needed to make the connection 'potentially disconnectable'.
  * <BR><BR>
@@ -209,9 +210,20 @@
     synchronized(cr) {
       try {
         if (cr.ctx == null) {
-          cr.ctx = createLDAPConnection(ldapUrl,
-              authTable.get(key));
+          boolean registerAuth = false;
+          AuthRecord authRecord = authTable.get(key);
+          if (authRecord == null)
+          {
+            // Best-effort: try with an already registered authentication
+            authRecord = authTable.values().iterator().next();
+            registerAuth = true;
+          }
+          cr.ctx = createLDAPConnection(ldapUrl, authRecord);
           cr.ctx.setRequestControls(requestControls);
+          if (registerAuth)
+          {
+            authTable.put(key, authRecord);
+          }
         }
       }
       catch(NamingException x) {
@@ -309,7 +321,7 @@
    * If authentication data are already available for the protocol/host/port
    * specified in the LDAPURl, they are replaced by the new data.
    * If true is passed as 'connect' parameter, registerAuth() creates the
-   * connection and attemps to connect() and bind() . If connect() or bind()
+   * connection and attempts to connect() and bind() . If connect() or bind()
    * fail, registerAuth() forwards the NamingException and does not register
    * the authentication data.
    * @param ldapUrl the LDAP URL of the server.
@@ -495,6 +507,10 @@
   {
     InitialLdapContext ctx;
 
+    // Take the base DN out of the URL and only keep the protocol, host and port
+    ldapUrl = new LDAPURL(ldapUrl.getScheme(), ldapUrl.getHost(),
+          ldapUrl.getPort(), (DN)null, null, null, null, null);
+
     if (isSecureLDAPUrl(ldapUrl))
     {
       ctx = ConnectionUtils.createLdapsContext(ldapUrl.toString(), ar.dn,
diff --git a/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/browser/NodeSearcherQueue.java b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/browser/NodeSearcherQueue.java
index d460d12..b637318 100644
--- a/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/browser/NodeSearcherQueue.java
+++ b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/browser/NodeSearcherQueue.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2008-2009 Sun Microsystems, Inc.
+ *      Copyright 2008-2010 Sun Microsystems, Inc.
  */
 
 package org.opends.guitools.controlpanel.browser;
@@ -233,9 +233,10 @@
    */
   public void run() {
     boolean interrupted = false;
-
-    while (!interrupted) {
-      try {
+    while (!interrupted)
+    {
+      try
+      {
         // Fetch and process a node also
         // taking care of update events
         AbstractNodeTask task = fetch();
@@ -246,11 +247,8 @@
         interrupted = true;
       }
       catch(Exception x) {
-        // At this level, either it's an interruption
-        // either it's a bug...
-        if (! (x instanceof InterruptedException)) {
-          x.printStackTrace();
-        }
+        // At this level it is a bug...
+        x.printStackTrace();
       }
     }
   }
diff --git a/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/BrowseEntriesPanel.java b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/BrowseEntriesPanel.java
index 1b40e85..1ebd3f5 100644
--- a/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/BrowseEntriesPanel.java
+++ b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/BrowseEntriesPanel.java
@@ -543,7 +543,16 @@
     }
     if (node != null)
     {
-      String dn = node.getDN();
+      String dn;
+      if (controller.getFollowReferrals() && node.getRemoteUrl() != null)
+      {
+        dn = node.getRemoteUrl().getRawBaseDN();
+      }
+      else
+      {
+        dn = node.getDN();
+      }
+
       try
       {
         InitialLdapContext ctx =

--
Gitblit v1.10.0