go to  ForumEasy.com   
LdapPro  
 
 
   Home  |  MyForum  |  FAQ  |  Archive    You are not logged in. [Login] or [Register]  
Forum Home » Native LDAP Clients -- JDNI, ADSI, etc. » JNDI Backslash Problem: Part I -- SearchResult.getName()
Email To Friend  |   Set Alert To This Topic Rewarding Points Availabe: 0 (What's this) New Topic  |   Post Reply
Author Topic: JNDI Backslash Problem: Part I -- SearchResult.getName()
JNDI
member
offline   
 
posts: 19
joined: 05/18/2007
from: GA
  posted on: 05/18/2007 08:18:11 PM    Edit  |   Quote  |   Report 
JNDI Backslash Problem: Part I -- SearchResult.getName()
import javax.naming.*;
import javax.naming.ldap.*;
import javax.naming.directory.*;
import java.util.Hashtable;

/**
 * Backslash.java
 * 
 * -- A sample code to demonstrate JNDI backslash (\) problem.
 * 
 * In LDAP DIT there is an entry with the common name (cn) as part of dn and 
 * cn has a single backslash character inside. The ldif looks as follows:
 * 
 * dn: cn=temp\\tester,cn=users,dc=mydomain,dc=com
 * objectclass: top
 * objectclass: person
 * objectclass: organizationalPerson
 * objectclass: inetOrgPerson
 * cn: cn=temp\tester
 * sn: tester
 * givenName: temp
 * telephoneNumber: (555)-1234
 *
 */
public class Backslash
{
   public static void main(String args[])
   {
       Hashtable env = new Hashtable();
       env.put(Context.INITIAL_CONTEXT_FACTORY,
                         "com.sun.jndi.ldap.LdapCtxFactory");
       env.put(Context.PROVIDER_URL, "ldap://myServer:389/");
       env.put(Context.SECURITY_AUTHENTICATION, "simple");
       env.put(Context.SECURITY_PRINCIPAL, "cn=admin" );
       env.put(Context.SECURITY_CREDENTIALS, "password" );
       
       DirContext ctx = null;
       try{
         /* Open an LDAP connection for the provided principal and credentials */
         ctx = new InitialLdapContext(env, null);
           
         /* search constraints */
         SearchControls ctls = new SearchControls();
         ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
           
         /* search */
         String base_dn = "cn=users,dc=mydomain,dc=com";
         NamingEnumeration enu = ctx.search(base_dn, "sn=test*", ctls);
           
         /* process results */
         while(enu.hasMore()){
            SearchResult sr = (SearchResult)enu.next();
            String dn = sr.getName(); // should return 'cn=temp\\tester'
            if(sr.isRelative()){
              dn += "," + base_dn;
            }
            System.out.println("dn: " + dn);  
            
            NamingEnumeration attrs = sr.getAttributes().getAll();
            while(attrs.hasMoreElements()){
              BasicAttribute attr = (BasicAttribute)attrs.nextElement();
              String attrType = attr.getID();
              NamingEnumeration vals = attr.getAll();
              while(vals.hasMoreElements()){
                String attrValue = vals.nextElement().toString();
                System.out.println(attrType + ": " + attrValue);
              }
            }
         }
       }catch(Exception e){
         e.printStackTrace();
       }
   }
}


 Profile | Reply Points Earned: 0
JNDI
member
offline   
 
posts: 19
joined: 05/18/2007
from: GA
  posted on: 05/18/2007 08:38:28 PM    Edit  |   Quote  |   Report 
Output
After running the above code, the method
        sr.getName(); 

should return 'cn=temp\\tester' but 'cn=temp\\\tester' instead.

IT HAS THREE (3) BACKSLASH CHARACTERS INSIDE.

The output is as the follows:

   dn: cn=temp\\\tester,cn=users,dc=mydomain,dc=com
   objectclass: top
   objectclass: person
   objectclass: organizationalPerson
   objectclass: inetOrgPerson
   cn: cn=temp\tester
   sn: tester
   givenName: temp
   telephoneNumber: (555)-1234


Notice that the attribute method brings 'cn' back correctly.

 Profile | Reply Points Earned: 0
JNDI
member
offline   
 
posts: 19
joined: 05/18/2007
from: GA
  posted on: 05/18/2007 08:56:32 PM    Edit  |   Quote  |   Report 
Work Around Solution
While insisting that this strange behavior is not a bug, those smart guys in Sun provide a whole different perspective in interpreting those three backslashes.

(see http://java.sun.com/products/jndi/tutorial/beyond/names/syntax.html)

The solution is to process everything in JDNI world:

         /* process results */
         while(enu.hasMore()){
            SearchResult sr = (SearchResult)enu.next();
            String dn = new CompositeName(sr.getName()).get(0); 
            if(sr.isRelative()){
              dn += "," + base_dn;
            }
            System.out.println("dn: " + dn);  
            
            NamingEnumeration attrs = sr.getAttributes().getAll();
            while(attrs.hasMoreElements()){
              BasicAttribute attr = (BasicAttribute)attrs.nextElement();
              String attrType = attr.getID();
              NamingEnumeration vals = attr.getAll();
              while(vals.hasMoreElements()){
                String attrValue = vals.nextElement().toString();
                System.out.println(attrType + ": " + attrValue);
              }
            }
         }


Then the output are correct.

   dn: cn=temp\\tester,cn=users,dc=mydomain,dc=com
   objectclass: top
   objectclass: person
   objectclass: organizationalPerson
   objectclass: inetOrgPerson
   cn: cn=temp\tester
   sn: tester
   givenName: temp
   telephoneNumber: (555)-1234


 Profile | Reply Points Earned: 0

 
Powered by ForumEasy © 2003-2005, All Rights Reserved. | Privacy Policy | Terms of Use
 
Get your own forum today. It's easy and free.