javaldapjndiedirectory

Java eDirectory get user's count by using email


I want to get number of counts by searching the email into eDirectory.

For this I am using below code

import javax.naming.directory.DirContext;
import javax.naming.directory.SearchResult;
import java.util.Properties;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.NamingEnumeration;

public class WebService {
    // Get number of count for email address
    DirContext ldapConn = openLDAPConn();
    SearchResult result = findByEmail(ldapConn, fillObj.getEmail());

    public DirContext openLDAPConn() {
        DirContext context = null;
        try {
            propFile = LoadProp.getProperties();

            Properties properties = new Properties();
            properties.put(Context.INITIAL_CONTEXT_FACTORY, propFile.getProperty(Constants.INITIAL_CONTEXT_FACTORY));
            properties.put(Context.PROVIDER_URL, propFile.getProperty(Constants.PROVIDER_URL));
            properties.put(Context.SECURITY_PROTOCOL, propFile.getProperty(Constants.SECURITY_PROTOCOL));
            properties.put(Context.SECURITY_PRINCIPAL, propFile.getProperty(Constants.SECURITY_PRINCIPAL));
            properties.put(Context.SECURITY_CREDENTIALS, propFile.getProperty(Constants.SECURITY_CREDENTIALS));

            context = new InitialDirContext(properties);
            debugOutput("Open LDAP Connection", propFile);
        } catch (Exception e) {
            debugOutput("Inside openLDAPConn Exception :" + e, propFile);
        }
        return context;
    }

    public SearchResult findByEmail(DirContext ctx, String email) {
        String ldapSearchBase = "ou=users,o=data";
        String searchFilter = "(&(objectClass=user)(mail=" + email + "))";
        SearchResult searchResult = null;
        try {
            SearchControls searchControls = new SearchControls();
            searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            NamingEnumeration<SearchResult> results = ctx.search(ldapSearchBase, searchFilter, searchControls);
            if (results.hasMoreElements()) {
                searchResult = (SearchResult) results.nextElement();
            }
        } catch (Exception e) {
        }

        return searchResult;
    }
}

But the problem is that when I run this, SearchResult gives me only one user data not number of user's count.

So please help me to resolved it.

I am using Java and eDirectory.


Solution

  • I have worked with eDirectory and from my experience I can tell you that I have not come across any function that can give you an aggregate of the directory objects like count, sum, avg, etc.

    You will need to rely on Java to calculate the count of objects returned in your search results.

    For example,

    int count = 0;
    while (results.hasMoreElements()) {
        count ++;
        searchResult = (SearchResult) results.nextElement();
    }
    
    System.out.println("Total number of search results = " + count);
    

    In your example, I guess there is only one user with that email address. So, the count as per my code will return 1. If you search using a different criteria, a wild card for example, your results might differ.


    Hope this helps!