Search code examples
javaactive-directoryldapjndi

JNDI InvalidNameException


I'm always getting an error when getting a name from my active directory server. The error is the following:

javax.naming.InvalidNameException: Invalid name: "CN=»OGMA Serviço LAN/WAN",cn=Recipients,cn=Users,,dc=intra

After googling a bit, I found the following information

https://bugs.java.com/bugdatabase/view_bug?bug_id=4307193 http://docs.oracle.com/javase/jndi/tutorial/beyond/names/syntax.html

As you can see, I tried both with the parser approach and the composite name approach, but the error continues! What am I missing?

Here's my code that does those operations:

DirContext ctx = new InitialDirContext( (Hashtable<String,String>) env);

Name n2 = new CompositeName().add(usersContainer);
NamingEnumeration contentsEnum = ctx.list(n2);

String[] attName = {"cn"};

while ( contentsEnum.hasNext() ) {
    NameClassPair ncp = (NameClassPair) contentsEnum.next();
    NameParser ldapParser = ctx.getNameParser("");

    String name = ncp.getName() + "," + usersContainer;
    Name n = ldapParser.parse(name);

    ctx.lookup(n);

The variable name is "CN=»OGMA Serviço LAN/WAN" + cn=Recipients,cn=Users,,dc=intra


Solution

  • I don't understand why you're doing anything so elaborate in the first place. If you had used Context.listBindings() instead of Context.list(), you could avoid building the name and the lookup altogether, as you would already have both the name and the binding already. Your code would reduce to:

    NamingEnumeration<Binding> contentsEnum = ctx.listBindings(n2);
    
    String[] attName = {"cn"};
    
    while ( contentsEnum.hasNext() ) {
        Binding binding = contentsEnum.next();
        Object o = binding.getObject();
        // etc, whatever you were intending to do with the result of lookup(), which is now in 'o'.
    }