JavaBlog.fr / Java.lu DEVELOPMENT,Java,Tools Java : LDAP : Utility Class LDAPHelper

Java : LDAP : Utility Class LDAPHelper

Hi,

Just a mini-post concerning an utility class/helper in order to request LDAP/AD LDAPHelper:

package com.huo.lu.db.extract.ldap;

import java.io.IOException;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

import com.huo.lu.db.extract.AppConstant;

/**
 * Helper in order to request LDAP/AD.
 */
public class LDAPHelper {

	private String ldapUrl = null;
	private String ldapBind = null;
	private String ldapPassword = null;
	private String searchOu = null;
	private String mfAgentSearchCriteria = null;
	private String phoneSearchCriteria = null;
	private String eMailSearchCriteria = null;
	private String nameSearchCriteria = null;
	private String adLoginSearchCriteria = null;

	private LdapContext ldapContext = null;
	private SearchControls searchCtls = null;


	public LDAPHelper(String ldapUrl, String ldapBind, String ldapPassword, String searchOu,
			String mfAgentSearchCriteria, String phoneSearchCriteria, String eMailSearchCriteria, 
			String nameSearchCriteria, String adLoginSearchCriteria) throws NamingException, IOException {

		this.initLDAP(ldapUrl, ldapBind, ldapPassword, searchOu, mfAgentSearchCriteria, phoneSearchCriteria, 
				eMailSearchCriteria, nameSearchCriteria, adLoginSearchCriteria);
	}

	
	private void initLDAP(String ldapUrl, String ldapBind, String ldapPassword, String searchOu,
			String mfAgentSearchCriteria, String phoneSearchCriteria, String eMailSearchCriteria, 
			String nameSearchCriteria, String adLoginSearchCriteria) throws NamingException, IOException {

		this.ldapUrl = ldapUrl;
		this.ldapBind = ldapBind;
		this.ldapPassword = ldapPassword;
		this.searchOu = searchOu;
		this.mfAgentSearchCriteria = mfAgentSearchCriteria;
		this.phoneSearchCriteria = phoneSearchCriteria;
		this.eMailSearchCriteria = eMailSearchCriteria;
		this.nameSearchCriteria = nameSearchCriteria;
		this.adLoginSearchCriteria = adLoginSearchCriteria;
		
		if (this.ldapUrl != null && !this.ldapUrl.equals("")) {
			Hashtable<String, String> ldapEnvironment = new Hashtable<String, String>(5);
			ldapEnvironment.put(Context.INITIAL_CONTEXT_FACTORY, AppConstant.INITIAL_CONTEXT_FACTORY);
			ldapEnvironment.put(Context.PROVIDER_URL, this.ldapUrl);
			ldapEnvironment.put(Context.SECURITY_AUTHENTICATION, AppConstant.SECURITY_AUTHENTICATION);
			ldapEnvironment.put(Context.SECURITY_PRINCIPAL, this.ldapBind);
			ldapEnvironment.put(Context.SECURITY_CREDENTIALS, this.ldapPassword);
			// Create initial context
			ldapContext = new InitialLdapContext(ldapEnvironment, null);
			searchCtls = new SearchControls();
			searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
		}
	}


		
	public void closeLDAP() throws NamingException {
		// Close the context when we're done
		ldapContext.close();
	}

	public String query(String mfUserName, String ldapResAttr) throws NamingException {
		NamingEnumeration<SearchResult> answer = getUserAttributes(mfUserName);
		return getSearchResult(answer, ldapResAttr);
	}

	public NamingEnumeration<SearchResult> getUserAttributes(String mfUserName) throws NamingException {
		NamingEnumeration<SearchResult> answer = ldapContext.search(searchOu, "(" + mfAgentSearchCriteria + "=" + mfUserName + ")", searchCtls);
		return answer;
	}

	public String getSearchResult(NamingEnumeration<SearchResult> ldapAnswer, String ldapResAttr) throws NamingException {
		String result = null;
		while (ldapAnswer.hasMore() && (result == null || result.equals(""))) {
			SearchResult sr = (SearchResult) ldapAnswer.next();
			Attributes attrs = sr.getAttributes();
			Attribute dn = attrs.get(ldapResAttr);
			if (dn != null) {
				result = (String) dn.get();
			}
			return result;
		}
		return result;
	}

	public String getSearchResult(Attributes attrs, String ldapResAttr) throws NamingException {
		String result = null;
		Attribute dn = attrs.get(ldapResAttr);
		if (dn != null) {
			result = (String) dn.get();
		}
		return result;
	}
	
	public String getPhoneNumber(String mfUserName) throws NamingException {
		return query(mfUserName, phoneSearchCriteria);
	}

	public String getPhoneNumber(NamingEnumeration<SearchResult> userAttr) throws NamingException {
		return getSearchResult(userAttr, phoneSearchCriteria);
	}

	public String getEMail(String mfUserName) throws NamingException {
		return query(mfUserName, eMailSearchCriteria);
	}

	public String getEMail(NamingEnumeration<SearchResult> userAttr) throws NamingException {
		return getSearchResult(userAttr, eMailSearchCriteria);
	}

	public String getName(String mfUserName) throws NamingException {
		return query(mfUserName, nameSearchCriteria);
	}

	public String getName(NamingEnumeration<SearchResult> userAttr) throws NamingException {
		return getSearchResult(userAttr, nameSearchCriteria);
	}

	public void finalize() {
		try {
			this.closeLDAP();
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}
	
	
	public NamingEnumeration<SearchResult> getUserAttributesFromADLogin(String adLogin) throws NamingException {
		NamingEnumeration<SearchResult> answer = ldapContext.search(searchOu, "(" + adLoginSearchCriteria + "=" + adLogin + ")", searchCtls);
		return answer;
	}

}

That’s all!!!!

Huseyin OZVEREN

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

Related Post