use of javax.naming.directory.InitialDirContext in project CzechIdMng by bcvsolutions.
the class AdUserConnectorType method deleteTestUser.
/**
* Delete the wizard test user.
*/
protected void deleteTestUser(String entryDN, String port, String host, String adUser, String adPassword, boolean ssl) {
DirContext ldapContext = null;
try {
// Init LDAP context.
Hashtable<String, String> ldapEnv = getAdEnvironment(host, port, adUser, adPassword, ssl);
ldapContext = new InitialDirContext(ldapEnv);
// Delete the entry.
ldapContext.destroySubcontext(entryDN);
} catch (CommunicationException ex) {
throw new ResultCodeException(AccResultCode.WIZARD_AD_COMMUNICATION_EXCEPTION, ImmutableMap.of("host", host), ex);
} catch (NamingException ex) {
throw new ResultCodeException(AccResultCode.WIZARD_AD_OPERATION_FAILED, ImmutableMap.of("dn", entryDN), ex);
} finally {
if (ldapContext != null) {
try {
ldapContext.close();
} catch (NamingException e) {
// Only log it.
LOG.error(e.getLocalizedMessage(), e);
}
}
}
}
use of javax.naming.directory.InitialDirContext in project CzechIdMng by bcvsolutions.
the class AdUserConnectorType method findDnsHostName.
/**
* Find dnsHostName (domain) on the AD.
*/
protected String findDnsHostName(String port, String host, String adUser, String adPassword, boolean ssl) {
DirContext ldapContext = null;
try {
// Init LDAP context.
Hashtable<String, String> ldapEnv = getAdEnvironment(host, port, adUser, adPassword, ssl);
ldapContext = new InitialDirContext(ldapEnv);
// Get the configuration naming context.
Attributes ldapContextAttributes = ldapContext.getAttributes("");
return ldapContextAttributes.get("dnsHostName").get().toString();
} catch (CommunicationException ex) {
throw new ResultCodeException(AccResultCode.WIZARD_AD_COMMUNICATION_EXCEPTION, ImmutableMap.of("host", host), ex);
} catch (NamingException ex) {
throw new ResultCodeException(AccResultCode.WIZARD_AD_OPERATION_FAILED, ImmutableMap.of("dn", "dnsHostName"), ex);
} finally {
if (ldapContext != null) {
try {
ldapContext.close();
} catch (NamingException e) {
// Only log it.
LOG.error(e.getLocalizedMessage(), e);
}
}
}
}
use of javax.naming.directory.InitialDirContext in project CzechIdMng by bcvsolutions.
the class AdUserConnectorType method createTestUser.
/**
* Create test certificates.
*/
protected String createTestUser(String username, String entryDN, String port, String host, String adUser, String adPassword, boolean ssl) {
DirContext ldapContext = null;
try {
// Init LDAP context.
Hashtable<String, String> ldapEnv = getAdEnvironment(host, port, adUser, adPassword, ssl);
ldapContext = new InitialDirContext(ldapEnv);
// Entry's attributes.
Attribute cn = new BasicAttribute("cn", username);
Attribute oc = new BasicAttribute("objectClass");
oc.add("top");
oc.add("person");
oc.add("organizationalPerson");
oc.add("inetOrgPerson");
// Build the entry
BasicAttributes entry = new BasicAttributes();
entry.put(cn);
entry.put(oc);
// Add the entry.
DirContext context = ldapContext.createSubcontext(MessageFormat.format("CN={0},{1}", username, entryDN), entry);
Attributes attributes = context.getAttributes("");
return (String) attributes.get("distinguishedname").get();
} catch (NameAlreadyBoundException ex) {
throw new ResultCodeException(AccResultCode.WIZARD_AD_CONNECTOR_DN_ALREADY_EXISTS, ImmutableMap.of("dn", MessageFormat.format("CN={0},{1}", username, entryDN)), ex);
} catch (CommunicationException ex) {
throw new ResultCodeException(AccResultCode.WIZARD_AD_COMMUNICATION_EXCEPTION, ImmutableMap.of("host", host), ex);
} catch (NamingException ex) {
throw new ResultCodeException(AccResultCode.WIZARD_AD_OPERATION_FAILED, ImmutableMap.of("dn", MessageFormat.format("CN={0},{1}", username, entryDN)), ex);
} finally {
if (ldapContext != null) {
try {
ldapContext.close();
} catch (NamingException e) {
// Only log it.
LOG.error(e.getLocalizedMessage(), e);
}
}
}
}
use of javax.naming.directory.InitialDirContext in project CzechIdMng by bcvsolutions.
the class AdUserConnectorType method assignTestUserToGroup.
/**
* Assign the wizard test user to test group.
*/
protected void assignTestUserToGroup(String userDN, String groupDN, String port, String host, String adUser, String adPassword, boolean ssl) {
DirContext ldapContext = null;
try {
// Init LDAP context.
Hashtable<String, String> ldapEnv = getAdEnvironment(host, port, adUser, adPassword, ssl);
ldapContext = new InitialDirContext(ldapEnv);
ModificationItem[] roleMods = new ModificationItem[] { new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("member", userDN)) };
ldapContext.modifyAttributes(groupDN, roleMods);
} catch (CommunicationException ex) {
throw new ResultCodeException(AccResultCode.WIZARD_AD_COMMUNICATION_EXCEPTION, ImmutableMap.of("host", host), ex);
} catch (NamingException ex) {
throw new ResultCodeException(AccResultCode.WIZARD_AD_OPERATION_FAILED, ImmutableMap.of("dn", userDN), ex);
} finally {
if (ldapContext != null) {
try {
ldapContext.close();
} catch (NamingException e) {
// Only log it.
LOG.error(e.getLocalizedMessage(), e);
}
}
}
}
use of javax.naming.directory.InitialDirContext in project rabbitmq-java-client by rabbitmq.
the class DnsSrvRecordAddressResolver method lookupSrvRecords.
protected List<SrvRecord> lookupSrvRecords(String service, String dnsUrls) throws IOException {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
env.put("java.naming.provider.url", dnsUrls);
List<SrvRecord> records = new ArrayList<SrvRecord>();
try {
DirContext ctx = new InitialDirContext(env);
Attributes attributes = ctx.getAttributes(service, new String[] { "SRV" });
NamingEnumeration<?> servers = attributes.get("srv").getAll();
while (servers.hasMore()) {
records.add(mapSrvRecord((String) servers.next()));
}
} catch (NamingException e) {
throw new IOException("Error during DNS SRV query", e);
}
return records;
}
Aggregations