Today, I finally added, deleted, modified node names, attributes, and traversed nodes. I will paste the code first.
/**
*
* @ Author chenyi
*/
Import java. util. hashtable;
Import javax. Naming. Directory .*;
Import java. util .*;
Import javax. Naming .*;
Public class chenyi {
Dircontext Dc = NULL;
String account = "manager"; // the account that operates LDAP. The default value is Manager.
String Password = "secret"; // password of the account manager.
String root = "DC = example, Dc = com"; // dc of the LDAP Root Node
Public chenyi (){
Init ();
// Add (); // Add a node
// Delete ("ou = Hi, Dc = example, Dc = com"); // Delete "ou = Hi, Dc = example, Dc = com" Node
// Modifyinformation ("ou = Hi, Dc = example, Dc = com"); // modify "ou = Hi, Dc = example, Dc = com" attributes
// Renameentry ("ou = new, O = neworganization, Dc = example, Dc = com", "ou = neworganizationalunit, O = neworganization, Dc = example, dc = com "); // rename the node" ou = new, O = neworganization, Dc = example, Dc = com"
Searchinformation ("DC = example, Dc = com", "", "(objectclass = *)"); // traverse all root nodes
// Searchinformation ("O = neworganization, Dc = example, Dc = com", "", "(objectclass = *)"); // traverses the shard of the specified Node
Close ();
}
Public void Init (){
Hashtable Env = new hashtable ();
Env. Put (context. initial_context_factory, "com. Sun. JNDI. LDAP. ldapctxfactory ");
Env. Put (context. provider_url, "LDAP: // 192.168.100.221: 389 /");
Env. Put (context. security_authentication, "simple ");
Env. Put (context. security_principal, "cn =" + account + "," + root );
Env. Put (context. security_credentials, password );
Try {
Dc = new initialdircontext (ENV); // initialize the context
System. Out. println ("authentication successful"); // you can change it to an exception and throw it.
} Catch (javax. Naming. authenticationexception e ){
System. Out. println ("authentication failed ");
} Catch (exception e ){
System. Out. println ("authentication error:" + E );
}
}
Public void close (){
If (DC! = NULL ){
Try {
DC. Close ();
} Catch (namingexception e ){
System. Out. println ("namingexception in close ():" + E );
}
}
}
Public void add (){
Try {
String newusername = "hi ";
Basicattributes attrs = new basicattributes ();
Basicattribute objclassset = new basicattribute ("objectclass ");
Objclassset. Add ("TOP ");
Objclassset. Add ("organizationalunit ");
Attrs. Put (objclassset );
Attrs. Put ("ou", newusername );
DC. createsubcontext ("ou =" + newusername + "," + root, attrs );
} Catch (exception e ){
E. printstacktrace ();
System. Out. println ("exception in add ():" + E );
}
}
Public void Delete (string DN ){
Try {
DC. destroysubcontext (DN );
} Catch (exception e ){
E. printstacktrace ();
System. Out. println ("exception in Delete ():" + E );
}
}
Public Boolean modifyinformation (string DN ){
Try {
Modificationitem [] mod = new modificationitem [1];
/* Add attributes */
// Attribute attr0 = new basicattribute ("Description ",
// "Test ");
// Mod [0] = new modificationitem (dircontext. add_attribute, attr0 );
/* Modify attributes */
// Attribute attr0 = new basicattribute ("Description", "");
// Mod [0] = new modificationitem (dircontext. replace_attribute,
// Attr0 );
/* Delete attributes */
Attribute attr0 = new basicattribute ("Description ",
"Chen ");
MoD [0] = new modificationitem (dircontext. remove_attribute,
Attr0 );
DC. modifyattributes (DN, MoD );
Return true;
} Catch (namingexception ne ){
Ne. printstacktrace ();
System. Err. println ("error:" + NE. getmessage ());
Return false;
}
}
/**
* @ Param base: Root Node ("DC = example, Dc = com ")
* @ Param scope: search range, divided into "base" (current node), "one" (single layer), "" (traversal)
* @ Param filter: refers to the subnode (in the format of "(objectclass = *)", * refers to all. You can also specify a specific type of tree node)
*/
Public void searchinformation (string base, string scope, string filter ){
Searchcontrols SC = new searchcontrols ();
If (scope. Equals ("base ")){
SC. setsearchscope (searchcontrols. object_scope );
} Else if (scope. Equals ("one ")){
SC. setsearchscope (searchcontrols. onelevel_scope );
} Else {
SC. setsearchscope (searchcontrols. subtree_scope );
}
Namingenumeration Ne = NULL;
Try {
Ne = Dc. Search (base, filter, SC );
// Use the namingenumeration object to cycle through
// The result set.
While (ne. hasmore ()){
System. Out. println ();
Searchresult sr = (searchresult) NE. Next ();
String name = Sr. getname ();
If (base! = NULL &&! Base. Equals ("")){
System. Out. println ("entry:" + name + "," + base );
} Else {
System. Out. println ("entry:" + name );
}
Attributes at = Sr. getattributes ();
Namingenumeration ane = at. getall ();
While (ANE. hasmore ()){
Attribute ATTR = (attribute) ANE. Next ();
String attrtype = ATTR. GETID ();
Namingenumeration values = ATTR. getall ();
Vector Vals = new vector ();
// Another namingenumeration object, this time
// To iterate through attribute values.
While (values. hasmore ()){
Object oneval = values. nextelement ();
If (oneval instanceof string ){
System. Out. println (attrtype + ":" + (string) oneval );
} Else {
System. Out. println (attrtype + ":" + new string (byte []) oneval ));
}
}
}
}
} Catch (exception NEX ){
System. Err. println ("error:" + nex. getmessage ());
Nex. printstacktrace ();
}
}
Public Boolean renameentry (string olddn, string newdn ){
Try {
DC. Rename (olddn, newdn );
Return true;
} Catch (namingexception ne ){
System. Err. println ("error:" + NE. getmessage ());
Return false;
}
}
Public static void main (string [] ARGs ){
New chenyi ();
}
}