Example usage for javax.naming NamingEnumeration hasMore

List of usage examples for javax.naming NamingEnumeration hasMore

Introduction

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

Prototype

public boolean hasMore() throws NamingException;

Source Link

Document

Determines whether there are any more elements in the enumeration.

Usage

From source file:org.apache.james.user.ldap.ReadOnlyLDAPGroupRestriction.java

/**
 * Extracts the DNs for members of the group with the given LDAP context
 * attributes. This is achieved by extracting all the values of the LDAP
 * attribute, with name equivalent to the field value
 * {@link #memberAttribute}, from the attributes collection.
 *
 * @param groupAttributes The attributes taken from the group's LDAP context.
 * @return A collection of distinguished-names for the users belonging to
 *         the group with the specified attributes.
 * @throws NamingException Propagated from underlying LDAP communication layer.
 *//*from  w w w . ja v  a 2s .  c  om*/
private Collection<String> extractMembers(Attributes groupAttributes) throws NamingException {
    Collection<String> result = new ArrayList<String>();
    Attribute members = groupAttributes.get(memberAttribute);
    NamingEnumeration<?> memberDNs = members.getAll();

    while (memberDNs.hasMore())
        result.add(memberDNs.next().toString());

    return result;
}

From source file:com.seyren.core.security.ldap.LdapUserManagement.java

@Override
public String[] autoCompleteUsers(String name) {
    List<String> users = new ArrayList<String>();
    try {//  w w w.  ja  v  a 2 s  .co  m
        DirContext readOnlyContext = contextSource.getReadOnlyContext();
        SearchControls ctls = new SearchControls();
        ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        String[] attrIDs = { USERNAME };
        ctls.setReturningAttributes(attrIDs);
        NamingEnumeration<SearchResult> results = readOnlyContext.search("", "(sAMAccountName=" + name + "*)",
                ctls);
        while (results.hasMore()) {
            SearchResult rslt = results.next();
            Attributes attrs = rslt.getAttributes();
            if (attrs.get(USERNAME) != null) {
                users.add((String) attrs.get(USERNAME).get());
            }
        }
    } catch (NamingException e) {

    }
    return users.toArray(new String[users.size()]);
}

From source file:io.kamax.mxisd.backend.ldap.LdapBackend.java

public List<String> getAttributes(Entry entry, String attName) {
    List<String> values = new ArrayList<>();
    javax.naming.directory.Attribute att = AttributeUtils.toAttributes(entry).get(attName);
    if (att == null) {
        return values;
    }/*from www .  j a  v  a 2  s.  c o  m*/

    try {
        NamingEnumeration<?> list = att.getAll();
        while (list.hasMore()) {
            values.add(list.next().toString());
        }
    } catch (NamingException e) {
        log.warn("Error while processing LDAP attribute {}, result could be incomplete!", attName, e);
    }
    return values;
}

From source file:no.dusken.momus.ldap.LdapSyncer.java

private List<Person> getAllPersonsFromLdap() {
    final int[] activeCount = { 0 };

    List<Person> persons = ldapTemplate.search("ou=Users", "(objectClass=person)",
            new AttributesMapper<Person>() {
                @Override/*from  w  w  w .  j a va  2 s . c  om*/
                public Person mapFromAttributes(Attributes attributes) throws NamingException {
                    Long id = Long.valueOf((String) attributes.get("uidNumber").get());
                    String firstName = attributes.get("givenName") != null
                            ? (String) attributes.get("givenName").get()
                            : "";
                    String fullName = attributes.get("cn") != null ? (String) attributes.get("cn").get() : "";
                    String userName = attributes.get("uid") != null ? (String) attributes.get("uid").get() : "";
                    String email = attributes.get("mail") != null ? (String) attributes.get("mail").get() : "";
                    String telephoneNumber = attributes.get("telephoneNumber") != null
                            ? (String) attributes.get("telephoneNumber").get()
                            : "";
                    boolean isActive = false;

                    Attribute memberOf1 = attributes.get("memberOf");
                    if (memberOf1 != null) {
                        NamingEnumeration<?> memberOf = memberOf1.getAll();
                        while (memberOf.hasMore()) {
                            String group = (String) memberOf.next();
                            if (group.equalsIgnoreCase(
                                    "cn=Active,ou=Sections,ou=Org,ou=Groups,dc=studentmediene,dc=no")) {
                                isActive = true;
                                activeCount[0]++;
                                break;
                            }
                        }

                    }

                    return new Person(id, userName, firstName, fullName, email, telephoneNumber, isActive);
                }
            });
    logger.info("Number of users from LDAP: {}", persons.size());
    logger.info("Number of active: {}", activeCount[0]);

    return persons;
}

From source file:binky.reportrunner.ui.actions.datasource.SetupEditJNDIDataSource.java

private List<String> listJNDINames(Context ctx, String ident) throws NamingException {
    List<String> names = new LinkedList<String>();

    String indent = "";

    NamingEnumeration<Binding> list = ctx.listBindings("");
    while (list.hasMore()) {
        Binding item = (Binding) list.next();
        String className = item.getClassName();
        String name = item.getName();
        logger.debug(indent + className + " " + name);
        Object o = item.getObject();
        if (o instanceof javax.naming.Context) {
            names.addAll(listJNDINames((Context) o, name));
        } else {/*w  ww .  j  a  va2 s  .c o m*/
            names.add(ident + "/" + name);
        }
    }

    return names;
}

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

protected void destroyRecursively(String dn, DirContext ctx, int limit) throws NamingException {
    if (limit == 0) {
        log.warn("Reach recursion limit, stopping deletion at" + dn);
        return;/*from  w w w  .  j  a va  2s.co  m*/
    }
    SearchControls scts = new SearchControls();
    scts.setSearchScope(SearchControls.ONELEVEL_SCOPE);
    NamingEnumeration<SearchResult> children = ctx.search(dn, "(objectClass=*)", scts);
    try {
        while (children.hasMore()) {
            SearchResult child = children.next();
            String subDn = child.getName();

            subDn = subDn + ',' + dn;
            destroyRecursively(subDn, ctx, limit);
        }
    } catch (SizeLimitExceededException e) {
        log.warn("SizeLimitExceededException: trying again on partial results " + dn);
        if (limit == -1) {
            limit = 100;
        }
        destroyRecursively(dn, ctx, limit - 1);
    }
    ctx.destroySubcontext(dn);
}

From source file:binky.reportrunner.ui.actions.datasource.SaveDataSource.java

private List<String> listJNDINames(Context ctx, String ident) throws NamingException {
    List<String> names = new LinkedList<String>();

    NamingEnumeration<Binding> list = ctx.listBindings("");
    while (list.hasMore()) {
        Binding item = (Binding) list.next();

        String name = item.getName();

        Object o = item.getObject();
        if (o instanceof javax.naming.Context) {
            names.addAll(listJNDINames((Context) o, name));
        } else {//  w w w . j ava 2s . c om
            names.add(ident + "/" + name);
        }
    }

    return names;
}

From source file:org.archone.ad.dao.CommonDao.java

public List<ShoadUser> listUsers(String domain) throws NamingException {
    List<ShoadUser> users = new LinkedList<ShoadUser>();
    NamingEnumeration<SearchResult> searchResults = dirContext.search(nameConvertor.getUsersBaseDn(domain),
            "(uid=*)", defaultSearchControls);

    while (searchResults.hasMore()) {
        SearchResult sr = searchResults.next();
        ShoadUser shoadUser = (ShoadUser) this.dirContext.lookup(sr.getNameInNamespace());
        users.add(shoadUser);/*  w w  w.j  a v  a 2  s .  c  om*/
    }

    return users;
}

From source file:com.swdouglass.joid.server.DirectoryUserManagerImpl.java

private Attributes findAttributes(String inUsername, InitialDirContext ctx) throws NamingException {

    SearchControls ctls = new SearchControls();
    ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);

    // perform the search
    NamingEnumeration results = ctx.search("", "(uid={0})", new Object[] { inUsername }, ctls);

    Attributes outAttrs = null;/*from w  w  w .jav  a2  s  .c o  m*/
    if (results.hasMore()) {
        log.info("Found username \"" + inUsername + "\" in directory");
        outAttrs = ((SearchResult) results.next()).getAttributes();
    } else {
        log.info("Could NOT find username \"" + inUsername + "\" in directory");
    }
    return outAttrs;
}

From source file:org.acegisecurity.userdetails.ldap.LdapUserDetailsMapper.java

public Object mapAttributes(String dn, Attributes attributes) throws NamingException {
    LdapUserDetailsImpl.Essence essence = new LdapUserDetailsImpl.Essence();

    essence.setDn(dn);/* www .  j  a va2 s  . c om*/
    essence.setAttributes(attributes);

    Attribute passwordAttribute = attributes.get(passwordAttributeName);

    if (passwordAttribute != null) {
        essence.setPassword(mapPassword(passwordAttribute));
    }

    // Map the roles
    for (int i = 0; (roleAttributes != null) && (i < roleAttributes.length); i++) {
        Attribute roleAttribute = attributes.get(roleAttributes[i]);

        if (roleAttribute == null) {
            logger.debug("Couldn't read role attribute '" + roleAttributes[i] + "' for user " + dn);
            continue;
        }

        NamingEnumeration attributeRoles = roleAttribute.getAll();

        while (attributeRoles.hasMore()) {
            GrantedAuthority authority = createAuthority(attributeRoles.next());

            if (authority != null) {
                essence.addAuthority(authority);
            } else {
                logger.debug(
                        "Failed to create an authority value from attribute with Id: " + roleAttribute.getID());
            }
        }
    }

    return essence;
}