Example usage for javax.naming ReferralException getReferralContext

List of usage examples for javax.naming ReferralException getReferralContext

Introduction

In this page you can find the example usage for javax.naming ReferralException getReferralContext.

Prototype

public abstract Context getReferralContext() throws NamingException;

Source Link

Document

Retrieves the context at which to continue the method.

Usage

From source file:com.openkm.principal.LdapPrincipalAdapter.java

@SuppressWarnings("unchecked")
private List<String> ldapSearch(List<String> searchBases, String searchFilter, String attribute) {
    log.debug("ldapSearch({}, {}, {})", new Object[] { searchBases, searchFilter, attribute });
    List<String> al = new ArrayList<String>();
    DirContext ctx = null;//from   www .  j av a 2 s  .c om
    Hashtable<String, String> env = getEnvironment();

    try {
        ctx = new InitialDirContext(env);
        SearchControls searchCtls = new SearchControls();
        searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

        for (String searchBase : searchBases) {
            NamingEnumeration<SearchResult> results = ctx.search(searchBase, searchFilter, searchCtls);

            while (results.hasMore()) {
                SearchResult searchResult = (SearchResult) results.next();
                Attributes attributes = searchResult.getAttributes();

                if (attribute.equals("")) {
                    StringBuilder sb = new StringBuilder();

                    for (NamingEnumeration<?> ne = attributes.getAll(); ne.hasMore();) {
                        Attribute attr = (Attribute) ne.nextElement();
                        sb.append(attr.toString());
                        sb.append("\n");
                    }

                    al.add(sb.toString());
                } else {
                    Attribute attrib = attributes.get(attribute);

                    if (attrib != null) {
                        // Handle multi-value attributes
                        for (NamingEnumeration<?> ne = attrib.getAll(); ne.hasMore();) {
                            String value = (String) ne.nextElement();

                            // If FQDN get only main part
                            if (value.startsWith("CN=") || value.startsWith("cn=")) {
                                String cn = value.substring(3, value.indexOf(','));
                                log.debug("FQDN: {}, CN: {}", value, cn);
                                al.add(cn);
                            } else {
                                al.add(value);
                            }
                        }
                    }
                }
            }
        }
    } catch (ReferralException e) {
        log.error("ReferralException: {}", e.getMessage());
        log.error("ReferralInfo: {}", e.getReferralInfo());
        log.error("ResolvedObj: {}", e.getResolvedObj());

        try {
            log.error("ReferralContext: {}", e.getReferralContext());
        } catch (NamingException e1) {
            log.error("NamingException logging context: {}", e1.getMessage());
        }
    } catch (NamingException e) {
        log.error("NamingException: {} (Base: {} - Filter: {} - Attribute: {})",
                new Object[] { e.getMessage(), searchBases, searchFilter, attribute });
    } finally {
        try {
            if (ctx != null) {
                ctx.close();
            }
        } catch (NamingException e) {
            log.error("NamingException closing context: {}", e.getMessage());
        }
    }

    log.debug("ldapSearch: {}", al);
    return al;
}

From source file:org.apache.directory.studio.connection.core.io.jndi.JNDIConnectionWrapper.java

/**
 * Retrieves all referrals from the ReferralException and
 * creates or updates the ReferralsInfo.
 * //from  w w w. jav  a  2 s  . co m
 * @param referralException the referral exception
 * @param initialReferralsInfo the initial referrals info, may be null
 * 
 * @return the created or updated referrals info
 * 
 * @throws NamingException if a loop was encountered.
 */
static ReferralsInfo handleReferralException(ReferralException referralException,
        ReferralsInfo initialReferralsInfo) throws NamingException {
    if (initialReferralsInfo == null) {
        initialReferralsInfo = new ReferralsInfo(true);
    }

    Referral referral = handleReferralException(referralException, initialReferralsInfo, null);

    while (referralException.skipReferral()) {
        try {
            Context ctx = referralException.getReferralContext();
            ctx.list(StringUtils.EMPTY); //$NON-NLS-1$
        } catch (NamingException ne) {
            if (ne instanceof ReferralException) {
                if (ne != referralException) {
                    // what a hack: 
                    // if the same exception is throw, we have another URL for the same Referral/SearchResultReference
                    // if another exception is throws, we have a new Referral/SearchResultReference
                    // in the latter case we null out the reference, a new one will be created by handleReferral()
                    referral = null;
                }

                referralException = (ReferralException) ne;

                referral = handleReferralException(referralException, initialReferralsInfo, referral);
            } else {
                break;
            }
        }
    }

    return initialReferralsInfo;
}