From ca8222868cc9467a49dea172db7f0ac80d94faff 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.

---
 opends/src/guitools/org/opends/guitools/controlpanel/browser/LDAPConnectionPool.java |   26 +++++++++++++++++++++-----
 1 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/opends/src/guitools/org/opends/guitools/controlpanel/browser/LDAPConnectionPool.java b/opends/src/guitools/org/opends/guitools/controlpanel/browser/LDAPConnectionPool.java
index d83fe14..70eaa3f 100644
--- a/opends/src/guitools/org/opends/guitools/controlpanel/browser/LDAPConnectionPool.java
+++ b/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,

--
Gitblit v1.10.0