下面列出了javax.naming.directory.DirContext#unbind ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* delete ldap AD group with provided group name
*
* @param groupName ldap AD group name to delete
* @throws NamingException
*/
public static void deleteAdGroup(String groupName) throws NamingException {
LOGGER.info(String.format("Remove AD group: %s", groupName));
DirContext ldapContext = getLdapContext(User.getLdapAdminUser());
String groupDn = constructGroupDn(groupName, OU_GROUPS);
ldapContext.unbind(groupDn);
}
/**
* Delete ldap user from ldap entry
*
* @param userId ldap user id
* @throws NamingException
*/
private static void deleteEntry(String userId, String ou) throws NamingException {
LOGGER.info(String.format("Delete user: %s", userId));
String entryDN = constructEntryCn(userId, ou);
DirContext ldapContext = getLdapContext(User.getLdapAdminUser());
ldapContext.unbind(entryDN);
}
public static void cleanAndLoad(String deleteFromDn,
String ldifResourcePath,
String ldapHost,
int ldapPort,
String ldapUser,
String ldapPass,
DirContext context) throws Exception {
// Cleanup everything used for testing.
List<String> dns = new LinkedList<>();
dns.add(deleteFromDn);
while (!dns.isEmpty()) {
String name = dns.get(dns.size() - 1);
Context currentContext = (Context) context.lookup(name);
NamingEnumeration<NameClassPair> namingEnum = currentContext.list("");
if (namingEnum.hasMore()) {
while (namingEnum.hasMore()) {
dns.add(namingEnum.next().getNameInNamespace());
}
} else {
context.unbind(name);
dns.remove(dns.size() - 1);
}
}
// A bit of a hacked approach to loading an LDIF into OpenLDAP since there isn't an easy way to do it
// otherwise. This approach invokes the command line tool programmatically but has
// to short-circuit the call to System.exit that the command line tool makes when it finishes.
// We are assuming that there isn't already a security manager in place.
final SecurityManager securityManager = new SecurityManager() {
@Override
public void checkPermission(java.security.Permission permission) {
if (permission.getName().contains("exitVM")) {
throw new SecurityException("System.exit calls disabled for the moment.");
}
}
};
System.setSecurityManager(securityManager);
File file = new File(AbstractCachedLDAPAuthorizationMapLegacyTest.class.getClassLoader().getResource(ldifResourcePath).toURI());
Class<?> clazz = Class.forName("LDAPModify");
Method mainMethod = clazz.getMethod("main", String[].class);
try {
mainMethod.invoke(null, new Object[]{new String[]{"-v", "-h", ldapHost, "-p", String.valueOf(ldapPort), "-D", ldapUser, "-w", ldapPass, "-a", "-f", file.toString()}});
} catch (InvocationTargetException e) {
if (!(e.getTargetException() instanceof SecurityException)) {
throw e;
}
}
System.setSecurityManager(null);
}
public void delete(final DirContext ctx, final T obj) throws NamingException
{
final String dn = buildDn(null, obj);
log.info("Delete " + getObjectClass() + ": " + dn + ": " + getLogInfo(obj));
ctx.unbind(dn);
}