9.3. Writing LDIF

ChangeRecordWriters let you write requests to modify directory data, whereas EntryWriters let you write entries to a file or an output stream. Both of these are interfaces.

  • The ConnectionChangeRecordWriter and ConnectionEntryWriter classes let you write directly to a connection to the directory.

  • The LDIFChangeRecordWriter and LDIFEntryWriter classes let you write to a file or other output stream. Both classes offer methods to filter content.

The following excerpt shows a writer pushing LDIF changes to a directory server.

final LDIFChangeRecordReader reader = new LDIFChangeRecordReader(ldif);
final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port);
Connection connection = null;

try {
    connection = factory.getConnection();
    connection.bind(userDN, password.toCharArray());

    final ConnectionChangeRecordWriter writer =
            new ConnectionChangeRecordWriter(connection);
    while (reader.hasNext()) {
        ChangeRecord changeRecord = reader.readChangeRecord();
        writer.writeChangeRecord(changeRecord);
    }
} catch (final ErrorResultException e) {
    System.err.println(e.getMessage());
    System.exit(e.getResult().getResultCode().intValue());
    return;
} catch (final IOException e) {
    System.err.println(e.getMessage());
    System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
    return;
} finally {
    if (connection != null) {
        connection.close();
    }
}