我想删除ldap节点并删除其下的所有子树节点。 例如,我想删除ou = F115630019及其子节点,即cn = xxxxx和快照中显示的ou = 115630019。
运行以下代码时遇到以下错误。请让我知道如何解决此问题。
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1911)
at deleteorg.deleteDN(deleteorg.java:28)
at deleteorg.deleteDN(deleteorg.java:58)
at deleteorg.main(deleteorg.java:96)
java代码: -
import java.util.*;
import javax.naming.Context;
import javax.naming.ContextNotEmptyException;
import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
public class deleteorg {
public boolean deleteDN(String srchost, String srcport, String srcuserId,String srcpasswd, String srcBaseDn, String deleteDn) {
String attribList = "";
String temp = null;
try {
DirContext srcCtx = createContext(srchost, srcport, srcBaseDn,
srcuserId, srcpasswd);
SearchControls srcont = new SearchControls();
srcont.setSearchScope(SearchControls.SUBTREE_SCOPE);
int i = deleteDn.indexOf(",");
System.out.println("i: "+i);
System.out.println("deleteDN="+deleteDn);
deleteDn = deleteDn.substring(0,deleteDn.indexOf(","));
System.out.println("index->>"+i+"------" + deleteDn);
NamingEnumeration answer = null;
try {
answer = srcCtx.search(deleteDn, "objectclass=*", srcont);
System.out.println("Answer"+ answer);
} catch (NameNotFoundException ne) {
System.out.println("name not found exception");
}
if (answer != null) {
while (answer.hasMore()) {
Attributes attrs = new BasicAttributes(true);
System.out.println("Attr"+ attrs);
SearchResult sr = (SearchResult) answer.next();
System.out.println("Search Result"+sr);
String addDN = sr.getName() + "," + deleteDn;
System.out.println("adDN"+addDN);
String attribnamevalues = "";
if (addDN.indexOf(',') == 0)
addDN = addDN.substring(1); // get rid of the leading
// ","
try {
System.out.println("AddDN"+addDN);
srcCtx.destroySubcontext(addDN);
System.out.println("destroyed");
} catch (ContextNotEmptyException e) {
}
}
deleteDN(srchost, srcport, srcuserId, srcpasswd, srcBaseDn,
deleteDn);
}
srcCtx.close();
return true;
} catch (NamingException e) {
e.printStackTrace();
return false;
}
}
public DirContext createContext(String servername, String port, String basedn, String username, String password) {
System.out.println("sername=" + servername + "#port=" + port + "#baseDn="+ basedn + "#uid=" + username + "#pwd=" + password);
DirContext ctx;
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://" + servername + ":" + port + "/"
+ basedn);
env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
try {
ctx = new InitialDirContext(env);
return (ctx);
} catch (NamingException e) {
e.printStackTrace();
return (null);
}
}
public static void main(String[] args) {
deleteorg ctx = new deleteorg();
ctx.deleteDN("localhost", "1389","cn=Directory Manager","Welcome1","ou=organizations,dc=xyz,dc=com","ou=F115630019 ,ou=organizations,dc=xyz,dc=com");
}
}
答案 0 :(得分:-1)
只能从ldap中删除叶节点。您需要按照以下步骤操作。