Example usage for javax.naming NameClassPair getNameInNamespace

List of usage examples for javax.naming NameClassPair getNameInNamespace

Introduction

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

Prototype

public String getNameInNamespace() 

Source Link

Document

Retrieves the full name of this binding.

Usage

From source file:org.jasig.cas.adaptors.ldap.BindLdapAuthenticationHandler.java

protected final boolean authenticateUsernamePasswordInternal(final UsernamePasswordCredentials credentials)
        throws AuthenticationException {

    final List<String> cns = new ArrayList<String>();

    final SearchControls searchControls = getSearchControls();

    final String base = this.searchBase;
    final String transformedUsername = getPrincipalNameTransformer().transform(credentials.getUsername());
    final String filter = LdapUtils.getFilterWithValues(getFilter(), transformedUsername);
    this.getLdapTemplate().search(new SearchExecutor() {

        public NamingEnumeration executeSearch(final DirContext context) throws NamingException {
            return context.search(base, filter, searchControls);
        }/*from ww  w .  java2 s.  com*/
    }, new NameClassPairCallbackHandler() {

        public void handleNameClassPair(final NameClassPair nameClassPair) {
            cns.add(nameClassPair.getNameInNamespace());
        }
    });

    if (cns.isEmpty()) {
        log.info("Search for " + filter + " returned 0 results.");
        return false;
    }
    if (cns.size() > 1 && !this.allowMultipleAccounts) {
        log.warn("Search for " + filter + " returned multiple results, which is not allowed.");
        return false;
    }

    for (final String dn : cns) {
        DirContext test = null;
        String finalDn = composeCompleteDnToCheck(dn, credentials);
        try {
            this.log.debug("Performing LDAP bind with credential: " + dn);
            test = this.getContextSource().getContext(finalDn,
                    getPasswordEncoder().encode(credentials.getPassword()));

            if (test != null) {
                return true;
            }
        } catch (final Exception e) {
            if (this.log.isErrorEnabled())
                this.log.error(e.getMessage(), e);

            throw handleLdapError(e);
        } finally {
            LdapUtils.closeContext(test);
        }
    }

    return false;
}

From source file:ldap.SearchUtility.java

/**
 * recursively walks the tree to depth 'depth', and returns
 * a list of all names found at that depth.
 * @param treeNode//  w w  w. j a v a 2  s .c  om
 * @param depth
 * @return
 * @throws NamingException
 */

private List<LdapName> getElementNames(LdapName treeNode, int depth, DirContext context)
        throws NamingException {
    depth--;
    NamingEnumeration<NameClassPair> children = context.list(treeNode);
    List<LdapName> elementNames = new ArrayList<LdapName>();

    // cycle through all the children we've found.
    while (children.hasMore()) {
        NameClassPair child = children.next();
        LdapName childName = new LdapName(child.getNameInNamespace());
        if (depth == 0) // return value - these are what we're looking for!
            elementNames.add(childName);
        else
            elementNames.addAll(getElementNames(childName, depth, context)); // keep going down!
    }

    return elementNames;
}