Subject: Work Around Solution
Author: JNDI
In response to: javax.naming.InvalidNameException
Posted on: 05/18/2007 10:06:24 PM
The DirConext.search method expects a string in JDNI naming convention rather than in LDAP convention. The ldap dn:
String base_dn = "cn=temp\\\\tester,cn=users,dc=mydomain,dc=com";
must be converted into JNDI Name:
Name jndi_name = new CompositeName().add(base_dn);
or String in JNDI naming convention:
String jndi_dn = new CompositeName().add(base_dn).toString();
Now we have the modified code:
DirContext ctx = null;
try{
/* Open an LDAP connection for the provided principal and credentials */
ctx = new InitialLdapContext(env, null);
/* base search */
SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.OBJECT_SCOPE);
/* search */
String base_dn = "cn=temp\\\\tester,cn=users,dc=mydomain,dc=com";
String jndi_dn = new CompositeName().add(base_dn).toString();
NamingEnumeration enu = ctx.search(jndi_dn, "sn=test*", ctls);
/* process results */
while(enu.hasMore()){
SearchResult sr = (SearchResult)enu.next();
System.out.println("dn: " + base_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();
}
And the correct output:
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
>
> On 05/18/2007 09:37:41 PM
JNDI wrote:
While passing the exact dn "cn=temp\\\\tester,cn=users,dc=mydomain,dc=com" (the extra two backslashes are due to Java String presentation) into DirConext method search() and running the above code against AD failed and brought back error messages:
javax.naming.InvalidNameException: cn=temp\tester,cn=users,dc=mydomain,dc=com:
[LDAP: error code 34 - 0000208F: LdapErr: DSID-0C090654, comment: Error processing name, data 0, vece
at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2593)
at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2411)
at com.sun.jndi.ldap.LdapCtx.searchAux(LdapCtx.java:1586)
at com.sun.jndi.ldap.LdapCtx.c_search(LdapCtx.java:1509)
at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_search(ComponentDirContext.java:371)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.search(PartialCompositeDirContext.java:331)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.search(PartialCompositeDirContext.java:316)
at javax.naming.directory.InitialDirContext.search(InitialDirContext.java:241)
It seemed that DirContext.search() had eaten one backslash (\) internally.
References: