Example usage for javax.naming.directory SearchControls getSearchScope

List of usage examples for javax.naming.directory SearchControls getSearchScope

Introduction

In this page you can find the example usage for javax.naming.directory SearchControls getSearchScope.

Prototype

public int getSearchScope() 

Source Link

Document

Retrieves the search scope of these SearchControls.

Usage

From source file:org.nuxeo.ecm.directory.ldap.LDAPTreeReference.java

/**
 * Fetches children, onelevel or subtree given the reference configuration.
 * <p>/*from w  w w . j a  v  a  2 s  .co  m*/
 * Removes entries with same id than parent to only get real children.
 *
 * @see org.nuxeo.ecm.directory.Reference#getTargetIdsForSource(String)
 */
// TODO: optimize reusing the same ldap session (see LdapReference optim
// method)
@Override
public List<String> getTargetIdsForSource(String sourceId) throws DirectoryException {
    Set<String> targetIds = new TreeSet<>();
    String sourceDn = null;

    // step #1: fetch the dn of the sourceId entry in the source
    // directory by the static dn valued strategy
    LDAPDirectory sourceDir = getSourceLDAPDirectory();
    try (LDAPSession sourceSession = (LDAPSession) sourceDir.getSession()) {
        SearchResult sourceLdapEntry = sourceSession.getLdapEntry(sourceId, true);
        if (sourceLdapEntry == null) {
            throw new DirectoryException(sourceId + " does not exist in " + sourceDirectoryName);
        }
        sourceDn = pseudoNormalizeDn(sourceLdapEntry.getNameInNamespace());
    } catch (NamingException e) {
        throw new DirectoryException("error fetching " + sourceId, e);
    }

    // step #2: search for entries with sourceDn as base dn and collect
    // their ids
    LDAPDirectory ldapTargetDirectory = getTargetLDAPDirectory();

    String filterExpr = String.format("(&%s)", ldapTargetDirectory.getBaseFilter());
    String[] filterArgs = {};

    // get a copy of original search controls
    SearchControls sctls = ldapTargetDirectory.getSearchControls(true);
    sctls.setSearchScope(getScope());
    try (LDAPSession targetSession = (LDAPSession) ldapTargetDirectory.getSession()) {
        if (log.isDebugEnabled()) {
            log.debug(String.format(
                    "LDAPReference.getTargetIdsForSource(%s): LDAP search search base='%s'"
                            + " filter='%s' args='%s' scope='%s' [%s]",
                    sourceId, sourceDn, filterExpr, StringUtils.join(filterArgs, ", "), sctls.getSearchScope(),
                    this));
        }
        NamingEnumeration<SearchResult> results = targetSession.dirContext.search(sourceDn, filterExpr,
                filterArgs, sctls);

        try {
            while (results.hasMore()) {
                Attributes attributes = results.next().getAttributes();
                // NXP-2461: check that id field is filled
                Attribute attr = attributes.get(targetSession.idAttribute);
                if (attr != null) {
                    Object value = attr.get();
                    if (value != null) {
                        // always remove self as child
                        String targetId = value.toString();
                        if (!sourceId.equals(targetId)) {
                            targetIds.add(targetId);
                        }
                    }
                }
            }
        } finally {
            results.close();
        }
    } catch (NamingException e) {
        throw new DirectoryException("error during reference search for " + sourceDn, e);
    }

    return new ArrayList<>(targetIds);
}