I need to make several queries to LDAP directory server and I'm using JNDI directory facilities for it. Then I need to do some work with the objects from the query result using their distinguished names. Some of the entries returned by the server contain forward slash character, and because of this JNDI escapes the whole name with double quotes:
NamingEnumeration<SearchResult> results = dirContext.search(queryRoot, queryFilter, controls);
for (SearchResult result : Collections.list(results)) {
String objectName = result.getName();
System.out.println(objectName);
}
If one of the objects in the query results has a name, say, 'b=id/10,a=1', it is printed like this
"b=id/10,a=1"
Note the double quotes around the name. Because of these quotes I cannot create javax.naming.ldap.LdapName
from it directly: it fails with NamingException
"Invalid name".
I understand that I can remove these quotes manually, but this feels hacky. Is there a way to avoid such escaping? Or maybe there are cleaner methods to do what I need?
P.S. It is funny that official JNDI tutorial suggests using LdapName
to achieve "easy name manipulation" and even mentions the escape problem but does not provide any links on the problem described above.