// AD Connection
private LdapContext getADConnection(final String itResource, Map<String, String> TEST_AD_IT_RESOURSE_PARAMETERS) {
logger.info("Entering into getADConnection method : ");
String adminName = null;
String adminPassword = null;
String hostName = null;
String userName = null;
String containerDN = null;
InitialLdapContext ctx = null;
TEST_AD_IT_RESOURSE_PARAMETERS = getITResourcesProperties(itResource);
if (null != TEST_AD_IT_RESOURSE_PARAMETERS && !TEST_AD_IT_RESOURSE_PARAMETERS.isEmpty()) {
hostName = TEST_AD_IT_RESOURSE_PARAMETERS.get("LDAPHostName");
userName = TEST_AD_IT_RESOURSE_PARAMETERS.get("DirectoryAdminName");
containerDN = TEST_AD_IT_RESOURSE_PARAMETERS.get("Container");
if (userName.contains("\\")) {
userName = userName.substring(userName.indexOf("\\") + 1);
userName = "cn=" + userName + ",cn=users," + containerDN;
}
adminName = userName;
adminPassword = TEST_AD_IT_RESOURSE_PARAMETERS.get("DirectoryAdminPassword");
}
if (adminPassword.equals("Null Password")) {
System.out.println("Null Password: Connection Failed");
return null;
}
Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
env.put("java.naming.security.authentication", "simple");
env.put("java.naming.security.principal", adminName);
env.put("java.naming.security.credentials", adminPassword);
env.put("java.naming.provider.url", "ldap://" + hostName + ":389");
try {
ctx = new InitialLdapContext(env, null);
}
catch (NamingException e) {
System.out.println("Error while getting the connection : " + e.getMessage());
}
System.out.println("AD connection is success : ");
return ctx;
}
No comments:
Post a Comment