Example usage for javax.naming.directory SearchResult getAttributes

List of usage examples for javax.naming.directory SearchResult getAttributes

Introduction

In this page you can find the example usage for javax.naming.directory SearchResult getAttributes.

Prototype

public Attributes getAttributes() 

Source Link

Document

Retrieves the attributes in this search result.

Usage

From source file:com.globalsight.everest.usermgr.UserLdapHelper.java

static List getListOfEmailInfo(NamingEnumeration p_searchResults) throws NamingException {

    List result = new ArrayList();

    while (p_searchResults.hasMoreElements()) {
        Object searchResultObj = p_searchResults.nextElement();
        if (searchResultObj instanceof SearchResult) {
            SearchResult tmpSearchResult = (SearchResult) searchResultObj;
            Attributes entry = tmpSearchResult.getAttributes();
            result.add(getUserEmailInfo(entry));
        }/*from w  w w  .  jav  a  2  s .c o m*/
    }

    p_searchResults.close();
    return result.size() == 0 ? null : result;
}

From source file:com.globalsight.everest.usermgr.UserLdapHelper.java

/**
 * Generates a collection of User objects from a NamingEnumeration after a
 * search is carried out./*from  w  ww .  java 2  s.  c  o m*/
 */
static Vector<User> getUsersFromSearchResults(NamingEnumeration p_SearchResults) throws NamingException {

    Vector<User> userList = new Vector<User>();
    while (p_SearchResults.hasMoreElements()) {
        /* Next directory entry */
        Object searchResultObj = p_SearchResults.nextElement();
        if (searchResultObj instanceof SearchResult) {
            SearchResult tempSearchResult = (SearchResult) searchResultObj;
            Attributes entry = tempSearchResult.getAttributes();
            userList.addElement(getUserFromLDAPEntry(entry));
        }
    }

    p_SearchResults.close();
    return userList;
}

From source file:com.globalsight.everest.usermgr.UserLdapHelper.java

/**
 * Generates a collection of UserInfo objects from a NamingEnumeration after
 * a search is carried out.//from   w  ww.  j  av  a2 s .co m
 */
static Vector getUserInfosFromSearchResults(NamingEnumeration p_SearchResults) throws NamingException {

    Vector userInfoList = new Vector();
    while (p_SearchResults.hasMoreElements()) {
        /* Next directory entry */
        Object searchResultObj = p_SearchResults.nextElement();
        if (searchResultObj instanceof SearchResult) {
            SearchResult tempSearchResult = (SearchResult) searchResultObj;
            Attributes entry = tempSearchResult.getAttributes();
            userInfoList.addElement(getUserInfoFromLDAPEntry(entry));
        }
    }
    p_SearchResults.close();

    return userInfoList;
}

From source file:com.globalsight.everest.usermgr.UserLdapHelper.java

static Vector getNamesVectorFromSearchResults(NamingEnumeration p_SearchResults) throws NamingException {
    Vector userNames = new Vector();

    while (p_SearchResults.hasMoreElements()) {
        /* Next directory entry */
        Object searchResultObj = p_SearchResults.nextElement();
        if (searchResultObj instanceof SearchResult) {
            SearchResult tempSearchResult = (SearchResult) searchResultObj;
            Attributes entry = tempSearchResult.getAttributes();
            String userName = getSingleAttributeValue(entry.get(LDAP_ATTR_USER_NAME));
            userNames.addElement(userName);
        }/*from   w  ww .  j  a v a2  s  . com*/
    }

    p_SearchResults.close();

    return userNames;
}

From source file:com.globalsight.everest.usermgr.UserLdapHelper.java

/**
 * Get the Uset ID array from a NamingEnumeration
 *///www.j  a v  a 2s  .com
static Vector getUIDsVectorFromSearchResults(NamingEnumeration p_SearchResults) throws NamingException {

    Vector uids = new Vector();

    while (p_SearchResults.hasMoreElements()) {
        /* Next directory entry */
        Object searchResultObj = p_SearchResults.nextElement();
        if (searchResultObj instanceof SearchResult) {
            SearchResult tempSearchResult = (SearchResult) searchResultObj;
            Attributes entry = tempSearchResult.getAttributes();
            String uid = getSingleAttributeValue(entry.get(LDAP_ATTR_USERID));
            uids.addElement(uid);
        }
    }

    p_SearchResults.close();

    return uids;
}

From source file:org.sipfoundry.sipxconfig.bulk.ldap.LdapRowInserter.java

@Override
protected void insertRow(SearchResult searchResult) {
    Attributes attrs = searchResult.getAttributes();
    LOG.info("Inserting:" + attrs.toString());
    insertRow(searchResult, attrs);/*from ww  w .  j  a v a2s.c o  m*/
}

From source file:com.globalsight.everest.usermgr.UserLdapHelper.java

/**
 * Get the company names from a NamingEnumeration
 *//*w  w w.j a  v a 2s. c  o m*/
static String[] getCompanyNamesFromSearchResults(NamingEnumeration p_searchResults) throws NamingException {

    // use a set so duplicates are not saved
    Set companyNames = new TreeSet();

    while (p_searchResults.hasMoreElements()) {

        String cName = null;
        Object searchResultObj = p_searchResults.nextElement();
        if (searchResultObj instanceof SearchResult) {
            SearchResult tempSearchResult = (SearchResult) searchResultObj;
            Attributes entry = tempSearchResult.getAttributes();
            cName = getSingleAttributeValue(entry.get(LDAP_ATTR_COMPANY));
        }

        if (cName != null && cName.trim().length() > 0) {
            // adds it to the set
            // if it already exists just returns (NOP)
            companyNames.add(cName);
        }
    }
    p_searchResults.close();

    String[] cns = new String[companyNames.size()];
    return (String[]) companyNames.toArray(cns);
}

From source file:com.surevine.chat.auth.GroupAuthorisationFilter.java

/**
 * Get a list of the members of a group, searching for the group using an
 * LDAP filter expression and scope./*w  w w.  ja va 2s.  c o  m*/
 * 
 * @param filter
 *            LDAP search filter (see RFC2254)
 * @param scope
 *            One of SearchControls.OBJECT_SCOPE,
 *            SearchControls.ONELEVEL_SCOPE, or SearchControls.SUBTREE_SCOPE
 *            (see javax.naming.directory.SearchControls)
 * @return List of usernames
 * @throws NamingException
 * @throws LdapException
 *             On any LDAP error
 */
private Collection<String> getGroupMembers(final String groupName) throws NamingException {
    _logger.debug("Looking for members of " + groupName);
    String filter = "cn=" + groupName;
    Collection<String> memberList = new HashSet<String>(20);

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

    NamingEnumeration<SearchResult> objects;
    DirContext ctx = getLdapConnection();

    objects = ctx.search("ou=groups", filter, controls);

    while (objects.hasMore()) {
        SearchResult sr = (SearchResult) objects.next();
        Attributes attributes = sr.getAttributes();
        Attribute attribute = attributes.get("member");

        if (attribute != null) {
            NamingEnumeration<?> valueEnum = attribute.getAll();

            while (valueEnum.hasMore()) {
                String value = valueEnum.next().toString();

                final String searchFor = "cn=";
                int start = value.indexOf(searchFor);
                int end = value.indexOf(',', start);

                if (start >= 0 && end >= 0) {
                    String name = value.substring(start + searchFor.length(), end);
                    _logger.debug(name + " is a chatter");
                    memberList.add(name);
                }
            }
        }
    }
    _logger.debug("Returning a total of " + memberList.size() + " chatters");
    return memberList;
}

From source file:net.officefloor.plugin.jndi.ldap.CredentialStoreTest.java

/**
 * Ensure able to obtain the roles.//w w  w  . j a va 2  s. c  om
 */
public void testObtainRoles() throws Exception {

    // Obtain the context
    DirContext context = this.ldap.getDirContext();

    // Obtain the People context
    DirContext people = (DirContext) context.lookup("ou=People,dc=officefloor,dc=net");
    assertNotNull("Should have People context", people);

    // Search for person
    NamingEnumeration<SearchResult> personResults = people.search("",
            "(&(objectClass=inetOrgPerson)(uid=daniel))", null);
    assertTrue("Expecting to find daniel entry", personResults.hasMore());
    SearchResult daniel = personResults.next();
    assertFalse("Should only have the daniel entry", personResults.hasMore());

    // Obtain the Groups context
    DirContext groups = (DirContext) context.lookup("ou=Groups,dc=officefloor,dc=net");
    assertNotNull("Should have Groups context", groups);

    // Search for groups containing daniel
    String danielDn = daniel.getNameInNamespace();
    NamingEnumeration<SearchResult> groupResults = groups.search("",
            "(&(objectClass=groupOfNames)(member=" + danielDn + "))", null);

    // Obtain the listing of roles for daniel
    List<String> roles = new ArrayList<String>(2);
    for (; groupResults.hasMore();) {
        SearchResult group = groupResults.next();

        // Obtain the role from the group
        String role = (String) group.getAttributes().get("ou").get();

        // Add role to listing
        roles.add(role);
    }

    // Ensure the correct roles
    assertEquals("Incorrect number of roles", 2, roles.size());
    assertTrue("Missing user role", roles.contains("developer"));
    assertTrue("Missing developer role", roles.contains("committer"));
}

From source file:org.sipfoundry.sipxconfig.bulk.ldap.LdapRowInserter.java

@Override
protected RowResult checkRowData(SearchResult sr) {
    Attributes attrs = sr.getAttributes();
    String idAttrName = m_attrMap.getIdentityAttributeName();
    if (attrs.get(idAttrName) == null) {
        return new RowResult(RowStatus.FAILURE);
    }//from   w w w . j a  v  a  2 s. c  om
    RowStatus status = RowStatus.SUCCESS;
    try {
        String userName = m_userMapper.getUserName(attrs);
        // check username
        if (!UserValidationUtils.isValidUserName(userName)
                || (m_importedUserNames != null && m_importedUserNames.contains(userName))) {
            return new RowResult(RowStatus.FAILURE);
        }
        Set<String> aliases = m_userMapper.getAliasesSet(attrs);
        if (aliases != null) {
            Set<String> aliasesToRemove = new TreeSet<String>();
            for (String alias : aliases) {
                if (StringUtils.equals(userName, alias)
                        || m_coreContext.isAliasInUseForOthers(alias, userName)) {
                    aliasesToRemove.add(alias);
                    status = RowStatus.WARNING_ALIAS_COLLISION;
                }
            }
            if (!aliasesToRemove.isEmpty()) {
                aliases.removeAll(aliasesToRemove);
            }
        }
        m_aliases = aliases;
    } catch (Exception e) {
        return new RowResult(RowStatus.FAILURE);
    }
    return new RowResult(status);
}