Reading and Writing LDAP Results

LDIF

LDIF can be written to any java.io.Writer using an LdifWriter.

StringWriter writer = new StringWriter();
LdifWriter ldifWriter = new LdifWriter(writer);
SearchOperation search = new SearchOperation(new DefaultConnectionFactory("ldap://directory.ldaptive.org"));
SearchResponse response = search.execute(SearchRequest.builder()
  .dn("dc=ldaptive,dc=org")
  .filter("(uid=dfisher)")
  .returnAttributes("mail")
  .build());
ldifWriter.write(response);
System.out.println(writer.toString());

produces:

dn: uid=dfisher,ou=people,dc=ldaptive,dc=org
mail: dfisher@ldaptive.org

LDIF can be read using any java.io.Reader using an LdifReader.

FileReader reader = new FileReader("entry.ldif");
LdifReader ldifReader = new LdifReader(reader);
SearchResponse response = ldifReader.read();