private LdapConnection openLdapConnection(Endpoint endpoint) throws DataRetrievalException {
java.net.URI u;
try {
u = new java.net.URI(endpoint.getEndpointUrl());
} catch (URISyntaxException e) {
throw new DataRetrievalException("Invalid enpooint URI", e);
}
LdapConnectionConfig config = new LdapConnectionConfig();
config.setLdapHost(u.getHost());
config.setLdapPort(u.getPort() > 0 ? u.getPort() : 389);
if (endpoint.hasProperty("loginDN")) {
config.setName(endpoint.getProperty("loginDN"));
}
if (endpoint.hasProperty("loginPW")) {
config.setName(endpoint.getProperty("loginPW"));
}
LdapNetworkConnection connection = new LdapNetworkConnection(config);
try {
connection.bind();
} catch (Exception e) {
throw new DataRetrievalException("LDAP connnection could not be bind", e);
}
if (connection.isAuthenticated()) {
return connection;
} else {
throw new DataRetrievalException("LDAP connnection could not be stablished");
}
}