Java- how to tell LDAP DirContext.search (...). hasMore () return false instead of throwing a PartialResultException

I configured LDAP DirContext.search (...) to ignore referrals, but I still get the referral exception when I call NamingEnumeration.hasMore ().

Exception in thread "main" javax.naming.PartialResultException: Unprocessed Continuation Reference(s); remaining name 'DC=company,DC=com'
    at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2846)
    at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2820)
    at com.sun.jndi.ldap.LdapNamingEnumeration.getNextBatch(LdapNamingEnumeration.java:129)
    at com.sun.jndi.ldap.LdapNamingEnumeration.hasMoreImpl(LdapNamingEnumeration.java:198)
    at com.sun.jndi.ldap.LdapNamingEnumeration.hasMore(LdapNamingEnumeration.java:171)

Is it possible to tell DirContext.search to ignore referrals, so that NamingEnumeration.hasMore () returns false instead of throwing an exception?

this is disabled:

import javax.naming.*;
import javax.naming.directory.*;

Properties p = new Properties();
p.setProperty(Context.INITIAL_CONTEXT_FACTORY, ldapInitContextFactory);
p.setProperty(Context.PROVIDER_URL, ldapURL);
p.setProperty(Context.SECURITY_CREDENTIALS, ldapPassword);
p.setProperty(Context.SECURITY_PRINCIPAL, ldapUser);
p.setProperty(Context.SECURITY_AUTHENTICATION, "simple");
p.setProperty(Context.REFERRAL, "ignore");
DirContext ctx = new InitialDirContext(p);

SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setDerefLinkFlag(false);

NamingEnumeration e = ctx.search(ldapBaseDN, ldapQuery, null, searchControls);

for (; e.hasMore();) {
    SearchResult sr = (SearchResult) e.next();
    System.out.println("\nSearch Result: " + sr.getName());
}

Note: if I allow referrals, I get an LdapReferralException expcetion when I call NamingEnumeration.hasMore ().

+5
source share
1 answer
javax/naming/NamingEnumeration.java
public interface NamingEnumeration<T> extends Enumeration<T> {
    public boolean hasMore() throws NamingException;
    public T next() throws NamingException;
    ...
}

java/util/Enumeration.java
public interface Enumeration<E> {
    boolean hasMoreElements();
    E nextElement();
}

e.hasMoreElements() e.hasMore() . false ( , ), .

, , , NamingException ( CommunicationException).

DirContext.search , NamingEnumeration.hasMore() false , . ?

JDK http://download.java.net/openjdk/jdk6/ → openjdk-6-src-b24-14_nov_2011.tar.gz JDK,  . JDK1.6 ( com.sun.jndi. *)

JDK "false" , .

./jdk/src/share/classes/com/sun/jndi/ldap/LdapCtx.java
protected void processReturnCode(LdapResult res, Name resolvedName, Object resolvedObj, Name remainName, Hashtable envprops, String fullDN) throws NamingException {
    NamingException e;

    switch (res.status) {
    case LdapClient.LDAP_SUCCESS:

        // handle Search continuation references
        if (res.referrals != null) {
            msg = "Unprocessed Continuation Reference(s)";

            if (handleReferrals == LdapClient.LDAP_REF_IGNORE) {
                e = new PartialResultException(msg);
                break;
            }
        [...]
    }
    [...]
    throw e;
}

.

, , DirContext.search , NamingEnumeration.hasMore() false .

?

+7

All Articles