Skip to main content

Working with Java LDAP API

import java.io.Serializable;
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.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

import org.apache.log4j.Logger;



/**
* @author Kiran
*
*/
public class LDAPInfo implements Serializable {

private static Logger log = Logger
.getLogger(LDAPInfo.class);
private final static String CLASS_NAME = "LDAPInfo";
private static final long serialVersionUID = 19931047L;

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String userName = null;
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://dsxstage.mycompany.com:389");
// env.put(Context.SECURITY_AUTHENTICATION,"OU=ccoentities,O=cco.mycompany.com");
env.put(Context.SECURITY_PRINCIPAL,"liferay_stage.gen"); // specify the username
env.put(Context.SECURITY_CREDENTIALS,"1!f3ray"); // specify the password
DirContext ctx = new InitialDirContext(env);
Attributes matchAttrs = new BasicAttributes(true);
log.info("The Connection Established to the LDAP Server Succeessfully");
log.info("Now trying to search the values in the LDAP Server");

userName = "kiran";
Attributes answer = ctx.getAttributes("uid="+userName+", OU=ccoentities,O=cco.mycompany.com");

for (NamingEnumeration ae = answer.getAll(); ae.hasMore();) {
Attribute attr = (Attribute)ae.next();
System.out.println("attribute: " + attr.getID());
/* Print each value */
for (NamingEnumeration e = attr.getAll(); e.hasMore();
System.out.println("value: " + e.next()))
;
}


Attribute at = null;
// at.
// matchAttrs.put(attr);

//NamingEnumeration answer = ctx.search("Country",matchAttrs);


//ctx.getSchema("")
// ctx.bind(name, obj)
//ctx.search(name, matchingAttributes);


} catch(NamingException ex) {

log.error("The Exception while connecting to the LDAP Server is:"+ ex.getMessage());
ex.printStackTrace();


} catch(Exception ex) {
log.error("The Exception while retrieving the attribute values from the LDAP Server is:"+ ex.getMessage());
ex.printStackTrace();


}
}

}

Comments

Popular posts from this blog

Design Patterns using Java

According to Gang of Four(GOF) any software is classified in to one of the three categories. I read so many books about design patterns which provide a lot of information about Design Patterns in a language neutral way or related to a particular programming language. I am trying to complement the great books by providing the precise and concise information that is required in the day to day programming of a Java Developer. Any software can be classified into one of the three categories -Framework, Toolkit, Application. Framework - Framework defines a set of steps to create an application. Framework provides design reuse. Toolkit - Toolkit provides some utility functions to an existing application. Toolkit provides code reuse. Application - Application is some thing that is specific to the project, and is not useful outside the context of the current application. Gang of Four divided the Design Patterns in to 3 types based on their usage. There are 3 types of Gang of Fo