package com.liming.msad;
import java.util.Enumeration;
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.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.ModificationItem;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
/**
* Test LDAP with add, delete, modify and find operator.
*
* @author Liming
* @time Apr 16, 2009 9:36:12 AM
* @version 1.0
*/
public class TestLDAP {
private DirContext dirContext = null;
private TestLDAP() {
this.getDirContext();
}
public void getDirContext() {
String contextFactory = "com.sun.jndi.ldap.LdapCtxFactory";
String providerUrl = "";
/*
* Level: "none", "simple", "strong"
*/
String authLevel = "simple";
String userName = "";
String password = "xukunwzq";
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory);
env.put(Context.PROVIDER_URL, providerUrl);
env.put(Context.SECURITY_AUTHENTICATION, authLevel);
env.put(Context.SECURITY_PRINCIPAL, userName);
env.put(Context.SECURITY_CREDENTIALS, password);
try {
dirContext = new InitialDirContext(env);
System.out.println("success to get dir context.");
} catch (NamingException e) {
System.out
.println("fail to get dir context, NamingException happened.");
e.printStackTrace();
}
}
public void find() throws NamingException {
String root = "cn=Users,DC=idm,DC=hfyefan.com";
String filter = "(givenName=s*)";
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<?> ne = dirContext.search(root, filter, constraints);
if (ne == null) {
System.out
.println("Fail to init NamingEnumeration, NullPointerException");
}
if (!ne.hasMoreElements()) {
System.out
.println("There is no element by the condition you have provided");
}
while (ne != null && ne.hasMoreElements()) {
Object obj = ne.nextElement();
if (obj instanceof SearchResult) {
SearchResult sr = (SearchResult) obj;
System.err.println("\tName: " + sr.getName());
Attributes attrs = sr.getAttributes();
if (attrs == null) {
System.out.println("\tNo attributes");
} else {
for (NamingEnumeration<?> ae = attrs.getAll();ae
.hasMoreElements();) {
Attribute attr = (Attribute) ae.next();
String attrId = attr.getID();
for (Enumeration<?> vals = attr.getAll();vals
.hasMoreElements();) {
System.out.print("\t\t" + attrId + ": ");
Object o = vals.nextElement();
if (o instanceof byte[])
System.out.println(new String((byte[]) o));
else
System.out.println(o);
}
}
}
} else {
System.out.println(obj);
}
}
}
public void close() {
if (dirContext != null) {
try {
dirContext.close();
} catch (NamingException e) {
System.out.println("NamingException in close():" + e);
}
}
}
public void add() throws NamingException {
BasicAttributes attrs = new BasicAttributes();
BasicAttribute objclassSet = new BasicAttribute("objectclass");
objclassSet.add("person");
objclassSet.add("top");
objclassSet.add("organizationalPerson");
objclassSet.add("inetOrgPerson");
objclassSet.add("wlsUser");
attrs.put(objclassSet);
// Other mandatory attributes -- required in MUST list
attrs.put("cn", "Joe Smith"); // required by 'person'
attrs.put("sn", "Smith"); // required by 'person'
// Optional attributes -- but they must be defined in schema
attrs.put("givenName", "Joe");
attrs.put("mail", "");
attrs.put("employeeNumber", "999-99-9999");
attrs.put("surName", "Smith");
// Other optional attributes -- but they are defined in schema as alias
attrs.put("locality", "San Jose"); /* does not work for AD */
// Create the context
// Context result = dirContext.createSubcontext(dn, attrs);
dirContext.createSubcontext("cn=Joe Smith,cn=Users,dc=idm,dc=hfyefan.com",
attrs);
}
public void delete() {
try {
dirContext.destroySubcontext("(cn=short short)");
} catch (Exception e) {
System.out.println("Exception in edit():" + e);
e.printStackTrace();
}
}
public void modify() {
try {
String account = "stella";// 修改以前旧的值
String sn = "stella sn";// 修改以后新的值
ModificationItem modificationItem[] = new ModificationItem[1];
modificationItem[0] = new ModificationItem(
DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("sn", sn));// 所修改的属性
dirContext.modifyAttributes("uid=" + account, modificationItem); // 执行修改操作
} catch (Exception e) {
System.out.println("Exception in edit():" + e);
}
}
public static void main(String[] args) throws NamingException {
TestLDAP t = new TestLDAP();
t.add();
// t.delete();
t.find();
t.close();
}
}