package studyactivedirectory;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NameAlreadyBoundException;
import javax.naming.NameClassPair;
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.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
public class ADManagement {
/**
* @param args
* @throws NamingException
*/
public DirContext ctx = null;
public void connectToAD(){
Hashtable env = new Hashtable(11);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
String loginName = "nick";
String password = "1234asdf";
env.put(Context.SECURITY_PRINCIPAL, "CN=" + loginName + ", ou=dms,ou=tom,dc=jerry,dc=test,dc=com");
//env.put(Context.SECURITY_PRINCIPAL, "CN=Administrator,CN=Users,DC=jerry,DC=TEST,DC=COM");
env.put(Context.SECURITY_CREDENTIALS, password);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://100.101.26.21:389");
//env.put(Context.REFERRAL, "follow");
//connect to AD
try{
ctx = new InitialLdapContext(env, null);
System.out.println("here come ctx..." + ctx.toString());
System.out.println("<------------connection successfully----------------------->");
}catch(Exception ex){
ex.printStackTrace();
}
}
public void addNewContext(){
// add context to AD
try {
//Create attributes to be associated with the new context
Attributes attrs = new BasicAttributes(true); // case-ignore
Attribute objclass = new BasicAttribute("objectclass");
objclass.add("top");
objclass.add("organizationalUnit");
attrs.put(objclass);
//Create the context
Context result = ctx.createSubcontext("ou=Fruits,dc=jerry,dc=test,dc=com", attrs);
System.out.println("<----------------add context to AD ending..------------------->");
} catch (NameAlreadyBoundException nex) {
System.out.println("User ID is already in use, please select a different user ID ...");
} catch (Exception exp) {
System.out.println("Failed to create user account... Please verify the user information...");
exp.printStackTrace();
}
}
public void addNewObject(){
// add element to AD
try {
BasicAttribute objClasses = new BasicAttribute("objectclass");
objClasses.add("top");
objClasses.add("testMailUser");
BasicAttributes attrs = new BasicAttributes();
attrs.put("cn", "Jerry1000");
attrs.put(objClasses);
attrs.put("name", "Jerry at tom");
ctx.bind("ou=dms,ou=tom,dc=jerry,dc=test,dc=com", attrs);
System.out.println("<----------------add element to AD ending..------------------->");
} catch (NameAlreadyBoundException nex) {
System.out.println("User ID is already in use, please select a different user ID ...");
} catch (Exception exp) {
System.out.println("Failed to create user account... Please verify the user information...");
exp.printStackTrace();
}
}
public void listAllElements(){
// list all elements of specified tree
try {
/* search for all entries*/
//NamingEnumeration results = ctx.search(Env.MY_SEARCHBASE, Env.MY_FILTER, constraints);
String searchbase = "ou=dms,ou=tom,dc=jerry,dc=test,dc=com";
String searchfilter = "(cn=*)";
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration results = ctx.search(searchbase, searchfilter, constraints);
/* for each entry print out name + all attrs and values */
//print searched elements.
while (results != null && results.hasMore()) {
SearchResult si = (SearchResult)results.next();
/* print its name */
System.out.println("name: " + si.getName());
Attributes attrs = si.getAttributes();
if (attrs == null) {
System.out.println("No attributes");
} else {
/* print each attribute */
for (NamingEnumeration ae = attrs.getAll(); ae.hasMoreElements();) {
Attribute attr = (Attribute)ae.next();
String attrId = attr.getID();
/* print each value */
for (Enumeration vals = attr.getAll(); vals.hasMoreElements();)
System.out.println(attrId + ": " + vals.nextElement());
}
}
System.out.println();
}
System.out.println("<--------------search ending..--------------------->");
} catch (NamingException e) {
System.err.println("Single Search failed.");
e.printStackTrace();
}
}
public void listSchema(){
// list schema
try{
DirContext schema = ctx.getSchema("");
//List the contents of root
NamingEnumeration bds = schema.list("");
while (bds.hasMore()) {
System.out.println(((NameClassPair)(bds.next())).getName());
}
System.out.println("<----------------list schema ending..------------------->");
}catch(Exception es){
es.printStackTrace();
}
}
public void addObjectClassToAD(){
// add new objectclass
try{
//Specify attributes for the schema object
Attributes attrs = new BasicAttributes(true); // Ignore case
attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.2.3.1.1.1");
attrs.put("NAME", "fooObjectClass");
attrs.put("DESC", "for test example only");
attrs.put("SUP", "top");
attrs.put("STRUCTURAL", "true");
Attribute must = new BasicAttribute("MUST", "cn");
must.add("objectclass");
attrs.put(must);
//Get the schema tree root
DirContext schema = ctx.getSchema("");
//Add the new schema object for "fooObjectClass"
DirContext newClass = schema.createSubcontext("ClassDefinition/fooObjectClass",attrs);
System.out.println("a new objectclass has been added--------->" + newClass.toString());
}catch(Exception exo){
exo.printStackTrace();
}
}
public static void main(String[] args) {
ADManagement ct = new ADManagement();
ct.connectToAD();
//ct.addNewContext();
ct.addNewObject();
//ct.listSchema();
//ct.addObjectClassToAD();
ct.listAllElements();
}
}