Index: src/edu/ucsb/nceas/metacat/AuthLdap.java
===================================================================
--- src/edu/ucsb/nceas/metacat/AuthLdap.java	(revision 1987)
+++ src/edu/ucsb/nceas/metacat/AuthLdap.java	(revision 1988)
@@ -47,6 +47,7 @@
 import javax.naming.directory.SearchControls;
 import javax.naming.ReferralException;
 import javax.naming.ldap.*;
+import java.net.URLDecoder;
 import java.util.Iterator;
 import java.util.HashMap;
 import java.util.Hashtable;
@@ -72,7 +73,7 @@
   private String userName;
   private String userPassword;
   ReferralException refExc;
-  String uid=null;
+  //String uid=null;
 
   /** 
    * Construct an AuthLdap
@@ -102,7 +103,7 @@
     boolean authenticated = false;
     String identifier = user;
     //get uid here.
-    uid=user.substring(0, user.indexOf(","));
+    //uid=user.substring(0, user.indexOf(","));
   
     try {
         // Check the usename as passed in
@@ -111,7 +112,10 @@
         if (!authenticated) {
             MetaCatUtil.debugMessage("Looking up DN for: " + identifier, 35);
             identifier = getIdentifyingName(identifier,ldapUrl,ldapBase);
-            //System.out.println("identifier: "+identifier);
+            MetaCatUtil.debugMessage("DN found: " + identifier, 35);
+            String decoded = URLDecoder.decode(identifier);
+            MetaCatUtil.debugMessage("DN decoded: " + decoded, 35);
+            identifier = decoded;
             String refUrl = "";
             String refBase = "";
             if (identifier.startsWith("ldap")) {
@@ -179,8 +183,7 @@
   {
     double totStartTime = System.currentTimeMillis();
     boolean authenticated = false;
-    if (identifier != null && !password.equals("")) 
-    {
+    if (identifier != null && !password.equals("")) {
     
         //Pass the username and password to run() method
         userName=identifier;
@@ -191,8 +194,8 @@
               "com.sun.jndi.ldap.LdapCtxFactory");
         env.put(Context.REFERRAL, "throw");
         env.put(Context.PROVIDER_URL, directoryUrl + searchBase);
-        if ( !ldapsUrl.equals(ldapUrl) ) 
-        {
+        util.debugMessage("PROVIDER_URL set to: " + directoryUrl + searchBase, 35);
+        if ( !ldapsUrl.equals(ldapUrl) ) {
           // ldap is set on default port 389
           // ldaps is set on second port - 636 by default
           //env.put(Context.SECURITY_PROTOCOL, "ssl");
@@ -202,10 +205,9 @@
         env.put(Context.SECURITY_CREDENTIALS, password);
         // If our auth credentials are invalid, an exception will be thrown
         DirContext ctx = null;
-        try 
-        {
+        try {
           double startTime = System.currentTimeMillis();
-          //Here to check the autheration
+          //Here to check the authorization
           ctx = new InitialDirContext(env);
           double stopTime = System.currentTimeMillis();
           util.debugMessage("Connection time thru " + ldapsUrl + " was: " +
@@ -216,25 +218,17 @@
           this.ldapUrl = ldapUrl;
           this.ldapBase = ldapBase;
           //break;
-        } 
-        catch (AuthenticationException ae) 
-        {
+        } catch (AuthenticationException ae) {
           authenticated = false;
-          if ( ctx != null ) 
-          {
+          if ( ctx != null ) {
             ctx.close();
           }
-        } 
-        catch (javax.naming.InvalidNameException ine) 
-        {
+        } catch (javax.naming.InvalidNameException ine) {
             util.debugMessage("An invalid DN was provided!", 30);
-        } 
-        catch (javax.naming.ReferralException re) 
-        {
+        } catch (javax.naming.ReferralException re) {
           util.debugMessage("referral during authentication", 30);
           util.debugMessage("Referral information: "+re.getReferralInfo(), 30);
-          try
-          {
+          try {
             refExc = re;
 
             Thread t = new Thread(this);
@@ -242,25 +236,17 @@
             Thread.sleep(5000); //this is a manual override of ldap's 
                                 //hideously long time out period.
             util.debugMessage("Awake after 5 seconds.", 35);
-            if (referralContext == null)
-            {
+            if (referralContext == null) {
               t.interrupt();
               authenticated = false;
-            }
-            else
-            {
+            } else {
               authenticated = true;
-
             }
-          }
-          catch (Exception e)
-          {
+          } catch (Exception e) {
             authenticated = false;
           }
         }
-    } 
-    else 
-    { 
+    } else { 
         util.debugMessage("User not found", 30);
     }
     double totStopTime = System.currentTimeMillis();
@@ -314,23 +300,33 @@
                                     user.indexOf(",", position+1));
       MetaCatUtil.debugMessage("Second comp is: " + comp2, 35);
 
-      String filter = "(&(" + comp1 + ")(" + comp2 + "))";
+      String filter = "(&(" + comp1 + "))";
+      //String filter = "(&(" + comp1 + ")(" + comp2 + "))";
       MetaCatUtil.debugMessage("Filter is: " + filter, 35);
       MetaCatUtil.debugMessage("Provider URL is: " + ldapUrl + ldapBase, 35);
       NamingEnumeration answer;
       try {
+        util.debugMessage("Trying search 1: " + filter, 35);
         answer = ctx.search("", filter, ctls);
+        util.debugMessage("Search 1 complete", 35);
+        if (answer == null) {
+            util.debugMessage("Search 1 answer is null.", 35);
+        }
         if (answer.hasMore()) {
+          util.debugMessage("Search 1 has answers.", 35);
           SearchResult sr = (SearchResult)answer.next();
           identifier = sr.getName();
           util.debugMessage("Originally Found: " + identifier, 35);
           return identifier;
         }
-      } catch (InvalidSearchFilterException e) {}
+      } catch (InvalidSearchFilterException e) {
+          util.debugMessage("Invalid Filter exception thrown (if1)", 35);
+      }
 
       // That failed, so check if it is just a username
       filter = "(" + user + ")";
       try {
+        MetaCatUtil.debugMessage("Trying again: " + filter, 35);
         answer = ctx.search("", filter, ctls);
         if (answer.hasMore()) {
           SearchResult sr = (SearchResult)answer.next();
@@ -349,6 +345,7 @@
 
       // Maybe its a user id (uid)
       filter = "(uid=" + user + ")";
+      MetaCatUtil.debugMessage("Trying again: " + filter, 35);
       answer = ctx.search("", filter, ctls);
       if (answer.hasMore()) {
         SearchResult sr = (SearchResult)answer.next();
@@ -364,6 +361,7 @@
 
         // maybe its just a common name
         filter = "(cn=" + user + ")";
+        MetaCatUtil.debugMessage("Trying again: " + filter, 35);
         NamingEnumeration answer2 = ctx.search("", filter, ctls);
         if (answer2.hasMore()) {
           SearchResult sr = (SearchResult)answer2.next();
@@ -380,6 +378,7 @@
 
           // ok, last resort, is it a surname?
           filter = "(sn=" + user + ")";
+          MetaCatUtil.debugMessage("Trying again: " + filter, 35);
           NamingEnumeration answer3 = ctx.search("", filter, ctls);
           if (answer3.hasMore()) {
             SearchResult sr = (SearchResult)answer3.next();
@@ -402,6 +401,7 @@
       throw new NamingException(
       "Naming exception in AuthLdap.getIdentifyingName: " + e);
     }
+    MetaCatUtil.debugMessage("Returning found identifier as: " + identifier, 35);
     return identifier;
   }
 
@@ -1025,20 +1025,18 @@
       {  
         //revise environment variable
         referralInfo=(String)refExc.getReferralInfo();
-        env.put(Context.PROVIDER_URL,refExc.getReferralInfo());
-        //System.out.println("referral info: "+referralInfo);
-        //if (referralInfo.indexOf("piscoweb")!=-1)
-        //{
-          //userName=uid+",o=PISCO,dc=piscoweb,dc=org";
-        //}
-        env.put(Context.INITIAL_CONTEXT_FACTORY, 
-                "com.sun.jndi.ldap.LdapCtxFactory");
-        //System.out.println("principal: "+userName);
-        env.put(Context.SECURITY_PRINCIPAL, userName);
-        env.put(Context.SECURITY_CREDENTIALS, userPassword);
-        env.put(Context.REFERRAL, "throw");
-        //get a context object for referral in the new envriment
-        rContext = refExc.getReferralContext(env);
+        util.debugMessage("Processing referral (pr0): ", 35);
+        util.debugMessage("PROVIDER_URL set to (pr1): " + referralInfo, 35);
+        //env.put(Context.PROVIDER_URL,referralInfo);
+        //env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
+        //env.put(Context.SECURITY_PRINCIPAL, userName);
+        //env.put(Context.SECURITY_CREDENTIALS, userPassword);
+        //env.put(Context.REFERRAL, "throw");
+        //util.debugMessage("Processing referral (pr1.info): " + userName,35);
+        //util.debugMessage("Processing referral (pr2)",35);
+        //rContext = refExc.getReferralContext(env);
+        rContext = refExc.getReferralContext();
+        util.debugMessage("Processing referral (pr3)",35);
         //casting the context to dircontext and it will create a
         //autherntication or naming exception if DN and password is incorrect
         referralContext=rContext;
@@ -1046,10 +1044,13 @@
         refDirContext.close();
         //get context and jump out the while loop
         moreReferrals=false;
+        util.debugMessage("Processing referral (pr4)",35);
       }//try
       //if referral have another referral excption
       catch (ReferralException re)
       {
+        util.debugMessage("GOT referral exception (re1): " + re.getMessage(),35);
+        util.debugMessage("RE details (re2): " + re.toString(true),35);
         //keep running in while loop
         moreReferrals=true;
         //assign refExc to new referral exception re
@@ -1058,7 +1059,7 @@
       //catch a authentication exception
       catch (AuthenticationException ae)
       {
-        util.debugMessage("Error running referral handler thread: " + 
+        util.debugMessage("Error running referral handler thread (ae1): " + 
                           ae.getMessage(), 20);
         //check if has another referral
         moreReferrals=refExc.skipReferral();
@@ -1068,7 +1069,7 @@
       //catch a naming exception
       catch (NamingException ne)
       {
-        util.debugMessage("Error running referral handler thread: " + 
+        util.debugMessage("Error running referral handler thread (ne1): " + 
                           ne.getMessage(), 20);
         //check if has another referral
         moreReferrals=refExc.skipReferral();
@@ -1128,7 +1129,7 @@
         }
         catch (AuthenticationException ae)
         {
-          util.debugMessage("Error running referral handler thread: " + 
+          util.debugMessage("Error running referral handler thread (ae2): " + 
                           ae.getMessage(), 50);
           //check if has another referral
           moreReferrals=refExc.skipReferral();
@@ -1137,7 +1138,7 @@
         }
         catch (NamingException ne)
         {
-          util.debugMessage("Error running referral handler thread: " + 
+          util.debugMessage("Error running referral handler thread (ne2): " + 
                           ne.getMessage(), 50);
           //check if has another referral
           moreReferrals=refExc.skipReferral();
