12/22/09

simple example using LDAP

package ldapsample1;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.NameAlreadyBoundException;
import javax.naming.directory.*;
import java.util.*;
import java.util.Hashtable;
import javax.naming.ldap.*;
import javax.naming.directory.*;
import javax.naming.*;
import javax.net.ssl.*;
import java.io.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.directory.*;
import javax.naming.*;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Properties;

/**
*
* @author toyopet
*/
public class Main {

/**
* @param args the command line arguments
*/
final static String ldapServerName = "localhost:389";
final static String rootdn = "cn=Directory Manager";
final static String rootpass = "admin";
final static String rootContext = "dc=example";
/* public static void main(String args[])
{
// The search base is the level in the hierarchy
// that our search will start at. Here was use ""
// which indicates the very root of the directory.
String base = "";
// LDAP filters are sort of like a WHERE clause. It
// is constructed in a standard way based on LDAP
// standards. The search here is a simple one that
// says to return any entry with an objectclass value.
// Since all entries must contain an objectclass, all
// entries will be returned.
String filter = "(objectclass=*)";
// Here we set some connection properties for JNDI.
Properties env = new Properties();
// The Sun provider is the most widely used JNDI
// provider and comes with Java 1.3+
env.put(DirContext.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
// The provider URL is an LDAP URL that tells JNDI
// where it will need to connect to.
env.put(DirContext.PROVIDER_URL,"ldap://localhost:389");
env.put( DirContext.SECURITY_PRINCIPAL, rootdn );
env.put( DirContext.SECURITY_CREDENTIALS, rootpass );
try {
// Here we create a DirContext object using
// the environment we setup above. This
// object will be used to communicate with
// the server.
DirContext dc = new InitialDirContext(env);
// Above we mentioned the filter and base.
// Another important part of the search criteria
// is the scope. There are three scopes: base (this
// entry only), onelevel (the direct children of this
// entry), and subtree (this entry and all of its
// decendents in the tree). In JNDI, OBJECT_SCOPE
// indicates a base search.
SearchControls sc = new SearchControls();
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration ne = null;
// Here we actually perform the search.
ne = dc.search(base, filter, sc);
// We cycle through the NamingEnumeration
// that is returned by the search.
while (ne.hasMore()) {
// Retrieve the result as a SearchResult
// and print it (not very pretty). There are
// methods for extracting the attributes and
// values without printing, as well.
SearchResult sr = (SearchResult) ne.next();
System.out.println(sr.toString()+"\n");
}

// Here we unbind from the LDAP server.
dc.close();
} catch (NamingException nex) {
// A number of exceptions are subclassed from
// NamingException. In a real application you'd
// probably want to handle many of them
// differently.
System.err.println("Error: " + nex.getMessage());
}
}*/

public static void main(String[] args) {


Properties env = new Properties();

env.put(DirContext.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
//env.put( DirContext.PROVIDER_URL, "ldap://" + ldapServerName+"/"+rootContext );
env.put(DirContext.PROVIDER_URL, "ldap://" + ldapServerName);
env.put(Context.SECURITY_PRINCIPAL, rootdn); // specify the username
env.put(Context.SECURITY_CREDENTIALS, rootpass);


try {
// obtain initial directory context using the environment
DirContext ctx = new InitialDirContext(env);

String name = "cn=kevin,o=sample1,dc=example,dc=com";





// now, create the root context, which is just a subcontext
// of this initial directory context.
//ctx.createSubcontext(rootContext );

String ag = "king";



// to fetch entry attribute value
//Attributes attrs = ctx.getAttributes("cn = boy,ou=human,o=sample1,dc=example,dc=com");
//System.out.println("sn: " + attrs.get("sn").get());

//to add modify delete
ModificationItem[] mods = new ModificationItem[2];
mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
new BasicAttribute("sn", "good"));
mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
new BasicAttribute("telephonenumber", "12345"));

ctx.modifyAttributes(name, mods);


// to create entry
// ctx.bind( "cn = guru,ou=human,o=sample1,dc=example,dc=com", ag );
// String a = (String) ctx.lookup( "cn=guru,ou=human,o=sample1,dc=example,dc=com" );
//System.out.println( "Retrieved a from directory with value: " + a );
} catch (NameAlreadyBoundException nabe) {
System.err.println(rootContext + " has already been bound!");
} catch (Exception e) {

System.err.println("error : " + e);
e.printStackTrace();
}
}
}

No comments:

Post a Comment

Popular Posts