package com.liang.test;
import java.util.Hashtable;
import java.util.Vector;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapName;
import org.opends.server.admin.client.ldap.JNDIDirContextAdaptor;
public class OpenDSExamples {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
//DirContext dirContext= createLdapContext(); // OpenDS
//JNDIDirContextAdaptor adaptor=JNDIDirContextAdaptor.adapt(dirContext);
// do other stuff with the adaptor
try{
String newUserName="world";
BasicAttributes attributes=new BasicAttributes();
BasicAttribute objclassSet=new BasicAttribute("objectclass");
objclassSet.add("person");
objclassSet.add("top");
objclassSet.add("organizationalPerson");
objclassSet.add("inetOrgPerson");
attributes.put(objclassSet);
attributes.put("sn", newUserName);
attributes.put("uid",newUserName);
attributes.put("displayName","***********@163.com");
attributes.put("userPassword", "world");
LdapName dn=new LdapName("cn=world,dc=example,dc=com");
//
//adaptor.createEntry(dn, attributes);
//System.out.println(" cn=world");
//
//attributes.put("displayName", " ");
//adaptor.modifyEntry(dn, attributes);
//System.out.println(" cn=world");
//
// adaptor.deleteSubtree(dn);
// System.out.println(" cn=world");
// boolean isExist = adaptor.entryExists(dn);
// if(isExist){
// System.out.println(" : cn=world");
// }
// else{
// System.out.println(" : cn=world");
// }
//
// SearchControls constraints=new SearchControls();
//
// constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
//
// boolean isExists=adaptor.entryExists(dn);
//
// if(isExists){
// System.out.println(" : cn=world");
// }else{
// System.out.println(" : cn=world");
// }
Vector ve=new Vector();
NamingEnumeration em=getFilter("dc=example,dc=com","cn=*",10);
while(em!=null&&em.hasMoreElements()){
SearchResult rs=(SearchResult) em.nextElement();
String db=rs.getName();
System.out.println(" ----------"+rs.getName());
System.out.println(" --------"+rs.getAttributes());
NamingEnumeration<String> rsAttrsID=rs.getAttributes().getIDs();
while(rsAttrsID.hasMore()){
String attrName=rsAttrsID.nextElement();
System.err.print(" :"+attrName+"++++++++++");
System.err.println(" :"+rs.getAttributes().get(attrName));
}
System.out.println(" -------"+rs.getAttributes().get("displayname"));
if(db.trim().equals("")){
ve.add(dn);
}
else ve.add(db+","+dn);
}
System.err.println(" ----"+ve.size());
}catch(Exception e){
System.out.println("adaptor cn=admin ");
e.printStackTrace();
}
}
/**
*
* @param DN ou=test,dc=abcd,dc=com
* @param filter cn=*
* @param myserach
* @return LDAP
* @throws Exception
*/
@SuppressWarnings("rawtypes")
private static NamingEnumeration getFilter(String DN, String filter,
int myserach) throws Exception{
DirContext ctx=createLdapContext();
try{
NamingEnumeration em;
SearchControls con=new SearchControls();
con.setSearchScope(myserach);
em=ctx.search(DN, filter,con);
return em;
}finally{
//
}
}
/**
*
* @param dn
* @param filter
* @param level
* @return DN JAVA STRING
* @throws Exception
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public java.util.Vector getfilterNodes(String dn, String filter, int level)
throws Exception {
java.util.Vector ve = new java.util.Vector();
NamingEnumeration em = getFilter(dn, filter, level);
while (em != null && em.hasMoreElements()) {
SearchResult rs = (SearchResult) em.nextElement();
String db = rs.getName();
if (db.trim().equals(""))
ve.add(dn);
else
ve.add(db + "," + dn);
}
return ve;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private static DirContext createLdapContext() {
Hashtable env=new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://127.0.0.1:389");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=Directory Manager");
env.put(Context.SECURITY_CREDENTIALS, "root");
DirContext ctx=null;
try{
ctx=new InitialLdapContext(env, null);
System.err.println(" ");
}catch(Exception e){
System.err.println(" ");
e.printStackTrace();
}
return ctx;
}
}