Example usage for javax.naming CompositeName CompositeName

List of usage examples for javax.naming CompositeName CompositeName

Introduction

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

Prototype

public CompositeName() 

Source Link

Document

Constructs a new empty composite name.

Usage

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

protected String getIdForDn(LDAPSession session, String dn) {
    // the entry id is not based on the rdn, we thus need to
    // fetch the LDAP entry to grab it
    String[] attributeIdsToCollect = { session.idAttribute };
    Attributes entry;/*from   www.  java 2s.com*/
    try {

        if (log.isDebugEnabled()) {
            log.debug(String.format(
                    "LDAPReference.getIdForDn(session, %s): LDAP get dn='%s'"
                            + " attribute ids to collect='%s' [%s]",
                    dn, dn, StringUtils.join(attributeIdsToCollect, ", "), this));
        }

        Name name = new CompositeName().add(dn);
        entry = session.dirContext.getAttributes(name, attributeIdsToCollect);
    } catch (NamingException e) {
        return null;
    }
    // NXP-2461: check that id field is filled
    Attribute attr = entry.get(session.idAttribute);
    if (attr != null) {
        try {
            return attr.get().toString();
        } catch (NamingException e) {
        }
    }
    return null;
}

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

/**
 * Retrieve the elements referenced by the filter/BaseDN/Scope request.
 *
 * @param attributes Attributes of the referencer element
 * @param directoryDn Dn of the Directory
 * @param linkDn Dn specified in the parent
 * @param filter Filter expression specified in the parent
 * @param scope scope for the search// w w w .j ava  2 s  .c o  m
 * @return The list of the referenced elements.
 * @throws DirectoryException
 * @throws NamingException
 */
private Set<String> getReferencedElements(Attributes attributes, String directoryDn, String linkDn,
        String filter, int scope) throws DirectoryException, NamingException {

    Set<String> targetIds = new TreeSet<>();

    LDAPDirectoryDescriptor targetDirconfig = getTargetDirectoryDescriptor();
    LDAPDirectory ldapTargetDirectory = (LDAPDirectory) getTargetDirectory();
    LDAPSession targetSession = (LDAPSession) ldapTargetDirectory.getSession();

    // use the most specific scope between the one specified in the
    // Directory and the specified in the Parent
    String dn = directoryDn.endsWith(linkDn) && directoryDn.length() > linkDn.length() ? directoryDn : linkDn;

    // combine the ldapUrl search query with target
    // directory own constraints
    SearchControls scts = new SearchControls();

    // use the most specific scope
    scts.setSearchScope(Math.min(scope, targetDirconfig.getSearchScope()));

    // only fetch the ids of the targets
    scts.setReturningAttributes(new String[] { targetSession.idAttribute });

    // combine the filter of the target directory with the
    // provided filter if any
    String targetFilter = targetDirconfig.getSearchFilter();
    if (filter == null || filter.length() == 0) {
        filter = targetFilter;
    } else if (targetFilter != null && targetFilter.length() > 0) {
        filter = String.format("(&(%s)(%s))", targetFilter, filter);
    }

    // perform the request and collect the ids
    if (log.isDebugEnabled()) {
        log.debug(String.format(
                "LDAPReference.getLdapTargetIds(%s): LDAP search dn='%s' " + " filter='%s' scope='%s' [%s]",
                attributes, dn, dn, scts.getSearchScope(), this));
    }

    Name name = new CompositeName().add(dn);
    NamingEnumeration<SearchResult> results = targetSession.dirContext.search(name, filter, scts);
    try {
        while (results.hasMore()) {
            // NXP-2461: check that id field is filled
            Attribute attr = results.next().getAttributes().get(targetSession.idAttribute);
            if (attr != null) {
                String collectedId = attr.get().toString();
                if (collectedId != null) {
                    targetIds.add(collectedId);
                }
            }

        }
    } finally {
        results.close();
    }

    return targetIds;
}