I'm trying to run an LDAP lookup from an XPage. To make it simple I've dumbed it down to 2 lines:
DirContext ctx = new InitialDirContext();
NamingEnumeration answer = ctx.search(url, null);
Either I put it in Java agent or LotusScript agent that calls Java class, or I call Java from XPage, JavaScript, the search() method fails with a message:
javax.naming.NotContextException: DirContext object is required.
at javax.naming.directory.InitialDirContext.castToDirContext(Unknown Source)
at javax.naming.directory.InitialDirContext.getURLOrDefaultInitDirCtx(Unknown Source)
at javax.naming.directory.InitialDirContext.search(Unknown Source)
However if I run the agent from Notes UI it works perfectly. DirContext object itself seems to get created ok.
I'd expect that the answer could be around the lines that XPages on client run in a different JVM
with different Java version and all that depends also on its
configuration. Or maybe newer Java versions are more attentive to the
search format. And providing one long url to the search method is not enough anymore?
EDITED. Well Oracle document states that we must be able to pass the URL only. But somehow it does not work anymore. The solution:
Hashtable env = new Hashtable(2);
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://bluepages.ibm.com:389/o=ibm.com");
// Create initial context
DirContext dircontext = new InitialDirContext(env);
// this way I did not get an error anymore, but also did not have the joy of getting anything back. Yet...
static String DN = "ou=bluepages";
String filter = "(&(objectClass=person)(" + searchtype + "=" + searchvalue + "))";
String[] attrIDs = attrlist.split(","); // return attributes
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
// specifying SCOPE was the last missing element... by default scope is SINGLELEVEL
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration answer = dircontext.search(DN, filter, ctls);