private class LdapReader implements Runnable {
public void run() {
BulkRequestBuilder bulkRequest = client.prepareBulk();
while (true) {
if (closed) {
return;
}
DirContext ctx = null;
Properties environment = new Properties();
try {
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
if (userDn != null && !"".equals(userDn)) {
environment.put(Context.SECURITY_AUTHENTICATION, "simple");
environment.put(Context.SECURITY_PRINCIPAL, userDn);
environment.put(Context.SECURITY_CREDENTIALS, credentials);
} else {
environment.put(Context.SECURITY_AUTHENTICATION, "none");
}
if (ssl) {
environment.put(Context.PROVIDER_URL, "ldaps://" + host + ":" + port);
environment.put(Context.SECURITY_PROTOCOL, "ssl");
} else {
environment.put(Context.PROVIDER_URL, "ldap://" + host + ":" + port);
}
ctx = new InitialDirContext(environment);
int count = 0;
SearchControls constraints = new SearchControls();
if ("object".equalsIgnoreCase(scope)) {
constraints.setSearchScope(SearchControls.OBJECT_SCOPE);
} else if ("onelevel".equalsIgnoreCase(scope)) {
constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
} else {
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
}
if (attributes != null && attributes.length > 0) {
constraints.setReturningAttributes(attributes);
}
long start = System.currentTimeMillis();
NamingEnumeration<SearchResult> results = ctx.search(baseDn, filter, constraints);
logger.debug("LDAP search executed in {} ms", System.currentTimeMillis() - start);
while (results != null && results.hasMore()) {
SearchResult sr = (SearchResult) results.next();
IndexRequest indexRequest = new IndexRequest(indexName);
XContentBuilder builder = jsonBuilder();
builder.startObject();
String dn = sr.getName();
logger.debug("Reading ldap object dn [{}]", dn);
Attributes ldapAttributes = sr.getAttributes();
NamingEnumeration<String> ldapAttributesIds = ldapAttributes.getIDs();
while (ldapAttributesIds.hasMoreElements()) {
String id = ldapAttributesIds.next();
logger.debug("\treading attribute id [{}]", id);
List<String> fieldValues = new ArrayList<String>();
Attribute attribute = ldapAttributes.get(id);
NamingEnumeration<?> values = attribute.getAll();
while (values.hasMoreElements()) {
Object value = values.next();
logger.debug("\t\tvalue: [{}]", value.toString());
fieldValues.add(value.toString());
}
String fieldName = resolveFieldName(id);
if(fieldValues.size() > 1){
builder.array(fieldName, fieldValues.toArray());
} else {
if (!"_id".equals(fieldName)) {
builder.field(fieldName, fieldValues.get(0));
} else {
indexRequest.id(fieldValues.get(0));
}
}
}
builder.endObject();
indexRequest.type(typeName).source(builder);
bulkRequest.add(indexRequest);
count++;
if((count % bulkSize) == 0){
BulkResponse bulkResponse = bulkRequest.execute().actionGet(bulkTimeout);
logger.info("{} objects indexed with ", count, bulkResponse.hasFailures()? "errors" : "success");
}
}
if(bulkRequest.numberOfActions() > 0){
BulkResponse bulkResponse = bulkRequest.setRefresh(true).execute().actionGet(bulkTimeout);
logger.info("{} objects indexed with ", count, bulkResponse.hasFailures()? "errors" : "success");
}
} catch (Exception e) {
logger.error("Exception when accessing to LDAP server", e);