All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jaaslounge.ldap.LdapSidConverter Maven / Gradle / Ivy

There is a newer version: 0.2.0
Show newest version
package org.jaaslounge.ldap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class LdapSidConverter {

    private String directoryUrl;
    private String directoryBase;
    private int directoryTimeout;

    public LdapSidConverter(String directoryUrl, String directoryBase, int directoryTimeout) {
        this.directoryUrl = directoryUrl;
        this.directoryBase = directoryBase;
        this.directoryTimeout = directoryTimeout;
    }

    public List getGroupNames(List sids) throws NamingException {
        List names = null;

        Map environnement = new HashMap();
        environnement.put("java.naming.ldap.attributes.binary", "objectSid");
        KeepAliveLdapConnection ldapConnection = KeepAliveLdapConnection.getConnection(directoryUrl, directoryTimeout, environnement);

        SearchControls searchCtls = new SearchControls();
        searchCtls.setReturningAttributes(new String[]{"sAMAccountName"});
        searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        StringBuilder filterBuilder = new StringBuilder();
        filterBuilder.append("(&(objectClass=group)(|");
        for(String sid : sids)
            filterBuilder.append("(objectSid=" + sid + ")");
        filterBuilder.append("))");

        names = new ArrayList();
        NamingEnumeration answer = ldapConnection.search(directoryBase, filterBuilder
                .toString(), searchCtls);
        while(answer.hasMoreElements()) {
            Attributes resultAttrs = ((SearchResult)answer.nextElement()).getAttributes();
            if(resultAttrs != null)
                names.add((String)resultAttrs.get("sAMAccountName").get());
        }

        return names;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy