UnboundID LDAP SDK for Java

Ping Identity
Product Information
Getting Started with the LDAP SDK

LDIF Processing

The UnboundID LDAP SDK for Java contains a number of classes that may be used for reading and writing entries and change records in the LDAP Data Interchange Format (LDIF), as specified in RFC 2849. LDIF provides a simple text-based mechanism for representing entries and change records.

Encoding an Entry in LDIF

The com.unboundid.ldap.sdk.Entry object provides the following methods for representing that entry in LDIF form:

In each of the above methods, no wrapping will be performed for long lines.

Also note that it is possible to construct an entry from an LDIF representation. This can be done using the Entry(String... entryLines) constructor, like:

Entry e = new Entry(
  "dn: dc=example,dc=com",
  "objectClass: top",
  "objectClass: domain",
  "dc: example");

LDIF Change Records

Add, delete, modify, and modify DN operations can also be represented in LDIF form, and the LDAP SDK provides LDIFAddChangeRecord, LDIFDeleteChangeRecord, LDIFModifyChangeRecord, and LDIFModifyDNChangeRecord classes (and their LDIFChangeRecord superclass) for interacting with them. Change records may be created from their corresponding request type (e.g., if you have a ModifyRequest object, you can use that to create an LDIFModifyChangeRecord object), and the same set of toLDIF and toLDIFString methods are available for obtaining the LDIF representation of those change records.

Further, it is possible to create LDAPRequest objects from LDIFChangeRecord objects. For example, the LDIFDeleteChangeRecord.toDeleteRequest method may be used to create a DeleteRequest object. In addition, all change record types include a processChange(LDAPConnection connection) method that can be used to process the operation over the provided connection.

Reading Entries and Change Records from LDIF

The LDIFReader class provides a means of reading data from a file or input stream and parsing that data into LDIF entries or change records. In particular, the readEntry() method will read an LDIF entry, and the readChangeRecord method will read a change record. If these methods return null, then it means that the end of the LDIF data has been reached. At that point, the LDIF reader may be closed using the close method.

In addition, the LDIFReader class provides static decodeEntry(String... ldifLines) and decodeChangeRecord(String... ldifLines) so that it is possible to decode the contents of a string array as an entry or change record without the need to read the data from a file or input stream.

Writing Entries and Change Records to LDIF

The LDIFWriter class provides a means of writing entries and change records in LDIF form to a specified file or output stream. It also provides the option to wrap long lines at a specified column width.

To write an entry in LDIF form, use either the writeEntry(Entry entry) or writeEntry(Entry entry, String comment) methods to write the provided entry and optionally include a comment immediately preceding the entry. Similarly, the writeChangeRecord(LDIFChangeRecord changeRecord) and writeChangeRecord(LDIFChangeRecord changeRecord, String comment) provide a means of writing change records, optionally immediately preceded by a comment. The close method should be used to close the LDIF writer when all entries and/or change records have been written.