Example usage for javax.naming NamingEnumeration close

List of usage examples for javax.naming NamingEnumeration close

Introduction

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

Prototype

public void close() throws NamingException;

Source Link

Document

Closes this enumeration.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Attributes matchAttrs = new BasicAttributes(true);
    matchAttrs.put(new BasicAttribute("sn", "YourName"));
    matchAttrs.put(new BasicAttribute("mail"));

    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "yourURL");

    DirContext ctx = new InitialDirContext(env);

    NamingEnumeration e = ctx.search("ou=People", matchAttrs);

    if (e.hasMore()) {
        SearchResult entry = (SearchResult) e.next();
        // Abandon rest of results
        e.close();
    }/*www.ja v  a 2 s .  c  o m*/
}

From source file:com.jaeksoft.searchlib.util.ActiveDirectory.java

public static void collectMemberOf(Attributes attrs, Collection<ADGroup> groups) throws NamingException {
    Attribute tga = attrs.get("memberOf");
    if (tga == null)
        return;//from   www  .  ja va  2 s .  c om
    NamingEnumeration<?> membersOf = tga.getAll();
    while (membersOf.hasMore()) {
        Object memberObject = membersOf.next();
        groups.add(new ADGroup(memberObject.toString()));
    }
    membersOf.close();
}

From source file:com.ktds.ldap.populator.LdapTestUtils.java

/**
 * Clear the directory sub-tree starting with the node represented by the
 * supplied distinguished name.//from   ww w  . j  a  v a  2 s . c o m
 *
 * @param ctx  The DirContext to use for cleaning the tree.
 * @param name the distinguished name of the root node.
 * @throws NamingException if anything goes wrong removing the sub-tree.
 */
public static void clearSubContexts(DirContext ctx, Name name) throws NamingException {

    NamingEnumeration enumeration = null;
    try {
        enumeration = ctx.listBindings(name);
        while (enumeration.hasMore()) {
            Binding element = (Binding) enumeration.next();
            Name childName = LdapUtils.newLdapName(element.getName());
            childName = LdapUtils.prepend(childName, name);

            try {
                ctx.unbind(childName);
            } catch (ContextNotEmptyException e) {
                clearSubContexts(ctx, childName);
                ctx.unbind(childName);
            }
        }
    } catch (NamingException e) {
        LOGGER.debug("Error cleaning sub-contexts", e);
    } finally {
        try {
            enumeration.close();
        } catch (Exception e) {
            // Never mind this
        }
    }
}

From source file:org.apache.archiva.redback.authentication.ldap.LdapBindAuthenticator.java

private void closeNamingEnumeration(NamingEnumeration<SearchResult> results) {
    try {/*  ww  w .  j  a  va2  s  .  c o m*/
        if (results != null) {
            results.close();
        }
    } catch (NamingException e) {
        log.warn("skip exception closing naming search result {}", e.getMessage());
    }
}

From source file:alpine.auth.LdapConnectionWrapper.java

/**
 * Closes a NamingEnumeration object without throwing any exceptions.
 * @param object the NamingEnumeration object to close
 * @since 1.4.0//from   w ww .  j  a  va 2s. c o m
 */
public void closeQuietly(final NamingEnumeration object) {
    try {
        if (object != null) {
            object.close();
        }
    } catch (final NamingException e) {
        // ignore
    }
}

From source file:it.infn.ct.security.utilities.LDAPUtils.java

public static boolean isCNregistered(String cn) {
    boolean registered = false;
    NamingEnumeration results = null;
    DirContext ctx = null;//from  ww w  .ja  va 2 s  . c  o  m
    try {
        ctx = getContext();
        SearchControls controls = new SearchControls();
        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        ResourceBundle rb = ResourceBundle.getBundle("ldap");

        results = ctx.search(rb.getString("peopleRoot"), "(cn=" + cn + ")", controls);
        if (results.hasMore()) {
            registered = true;
        }
    } catch (NameNotFoundException ex) {
        _log.error(ex);
    } catch (NamingException e) {
        registered = true;
    } finally {
        if (results != null) {
            try {
                results.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
    }

    return registered;
}

From source file:it.infn.ct.security.utilities.LDAPUtils.java

public static boolean isMailUsed(String mail) {
    boolean registered = false;
    NamingEnumeration results = null;
    DirContext ctx = null;/*from  w  ww . j  a v a  2 s  .co  m*/
    try {
        ctx = getContext();
        SearchControls controls = new SearchControls();
        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        ResourceBundle rb = ResourceBundle.getBundle("ldap");

        results = ctx.search(rb.getString("peopleRoot"), "(mail=" + mail + ")", controls);
        if (results.hasMore()) {
            registered = true;
        }
    } catch (NameNotFoundException ex) {
        _log.error(ex);
    } catch (NamingException e) {
        registered = true;
    } finally {
        if (results != null) {
            try {
                results.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
    }
    return registered;
}

From source file:edu.vt.middleware.ldap.auth.handler.CompareAuthorizationHandler.java

/** {@inheritDoc} */
public void process(final AuthenticationCriteria ac, final LdapContext ctx) throws NamingException {
    // make DN the first filter arg
    final List<Object> filterArgs = new ArrayList<Object>();
    filterArgs.add(ac.getDn());/*  w  ww .j a  va2  s. com*/
    filterArgs.addAll(this.searchFilter.getFilterArgs());

    // perform ldap compare operation
    NamingEnumeration<SearchResult> results = null;
    try {
        results = ctx.search(ac.getDn(), this.searchFilter.getFilter(), filterArgs.toArray(),
                LdapConfig.getCompareSearchControls());
        if (!results.hasMore()) {
            throw new AuthorizationException("Compare failed");
        }
    } finally {
        if (results != null) {
            results.close();
        }
    }
}

From source file:edu.vt.middleware.ldap.handler.CompareAuthorizationHandler.java

/** {@inheritDoc} */
public void process(final AuthenticationCriteria ac, final LdapContext ctx) throws NamingException {
    // make DN the first filter arg
    final List<Object> filterArgs = new ArrayList<Object>();
    filterArgs.add(ac.getDn());/*from   w w w .  j av  a  2s. c o m*/
    filterArgs.addAll(this.searchFilter.getFilterArgs());

    // perform ldap compare operation
    NamingEnumeration<SearchResult> results = null;
    try {
        results = ctx.search(ac.getDn(), this.searchFilter.getFilter(), filterArgs.toArray(),
                LdapConfig.getCompareSearchControls());
        if (!results.hasMore()) {
            throw new AuthenticationException("Compare failed");
        }
    } finally {
        if (results != null) {
            results.close();
        }
    }
}

From source file:es.udl.asic.user.OpenLdapDirectoryProvider.java

protected boolean userExists(String id) {
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_CREDENTIALS, "secret");

    try {//from w  w  w  . j a  v  a  2 s  . c  o  m
        DirContext ctx = new InitialDirContext(env);

        /*
         * Setup subtree scope to tell LDAP to recursively descend directory structure during searches.
         */
        SearchControls searchControls = new SearchControls();
        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);

        /*
         * Setup the directory entry attributes we want to search for. In this case it is the user's ID.
         */

        String filter = "(&(objectclass=person)(uid=" + escapeSearchFilterTerm(id) + "))";

        /* Execute the search, starting at the directory level of Users */

        NamingEnumeration hits = ctx.search(getBasePath(), filter, searchControls);

        /* All we need to know is if there were any hits at all. */

        if (hits.hasMore()) {
            hits.close();
            ctx.close();
            return true;
        } else {
            hits.close();
            ctx.close();
            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}