Comprehensive Java LDAP SDK providing full LDAPv3 protocol support, connection pooling, schema handling, and persistence framework for LDAP directory operations.
—
Complete support for LDAP Data Interchange Format (LDIF) reading, writing, and manipulation for importing, exporting, and processing LDAP directory data.
Primary class for reading LDIF files and processing entries and change records.
/**
* Reader for LDIF (LDAP Data Interchange Format) files
*/
public class LDIFReader implements Closeable {
// Constructors
public LDIFReader(String filePath) throws IOException;
public LDIFReader(File file) throws IOException;
public LDIFReader(InputStream inputStream);
public LDIFReader(BufferedReader reader);
// Reading operations
public LDIFRecord readLDIFRecord() throws IOException, LDIFException;
public Entry readEntry() throws IOException, LDIFException;
public LDIFChangeRecord readChangeRecord() throws IOException, LDIFException;
// Configuration
public void setDuplicateValueBehavior(DuplicateValueBehavior behavior);
public DuplicateValueBehavior getDuplicateValueBehavior();
public void setTrailingSpaceBehavior(TrailingSpaceBehavior behavior);
public TrailingSpaceBehavior getTrailingSpaceBehavior();
public void setSchema(Schema schema);
public Schema getSchema();
// Statistics
public long getEntriesRead();
public long getChangeRecordsRead();
public long getLinesRead();
// Resource management
public void close() throws IOException;
}
/**
* Behavior for handling duplicate attribute values
*/
public enum DuplicateValueBehavior {
STRIP,
RETAIN,
REJECT;
}
/**
* Behavior for handling trailing spaces in attribute values
*/
public enum TrailingSpaceBehavior {
STRIP,
RETAIN,
REJECT;
}Primary class for writing LDIF files with entries and change records.
/**
* Writer for LDIF files
*/
public class LDIFWriter implements Closeable {
// Constructors
public LDIFWriter(String filePath) throws IOException;
public LDIFWriter(File file) throws IOException;
public LDIFWriter(OutputStream outputStream);
public LDIFWriter(BufferedWriter writer);
// Writing operations
public void writeEntry(Entry entry) throws IOException;
public void writeEntry(Entry entry, String comment) throws IOException;
public void writeChangeRecord(LDIFChangeRecord changeRecord) throws IOException;
public void writeChangeRecord(LDIFChangeRecord changeRecord, String comment) throws IOException;
public void writeLDIFRecord(LDIFRecord record) throws IOException;
public void writeComment(String comment) throws IOException;
// Configuration
public void setWrapColumn(int wrapColumn);
public int getWrapColumn();
public void setBase64EncodeLength(int base64EncodeLength);
public int getBase64EncodeLength();
// Statistics
public long getEntriesWritten();
public long getChangeRecordsWritten();
// Resource management
public void close() throws IOException;
}Base class for all LDIF record types.
/**
* Base class for LDIF records (entries and change records)
*/
public abstract class LDIFRecord implements Serializable {
public abstract String getDN();
public abstract int hashCode();
public abstract boolean equals(Object obj);
public abstract String toString();
public abstract String toLDIFString();
public abstract String toLDIFString(int wrapColumn);
}Base class for LDIF change records (add, delete, modify, modifyDN).
/**
* Base class for LDIF change records
*/
public abstract class LDIFChangeRecord extends LDIFRecord {
public abstract ChangeType getChangeType();
public abstract LDAPRequest toLDAPRequest();
public abstract LDAPResult processChange(LDAPInterface connection) throws LDAPException;
// Factory methods
public static LDIFChangeRecord decode(String... ldifLines) throws LDIFException;
public static List<LDIFChangeRecord> parseChangeRecords(String ldifContent) throws LDIFException;
}
/**
* Change type enumeration
*/
public enum ChangeType {
ADD("add"),
DELETE("delete"),
MODIFY("modify"),
MODIFY_DN("moddn");
public String getName();
public static ChangeType forName(String name);
}LDIF change record for add operations.
/**
* LDIF change record for add operations
*/
public class LDIFAddChangeRecord extends LDIFChangeRecord {
// Constructors
public LDIFAddChangeRecord(String dn, Attribute... attributes);
public LDIFAddChangeRecord(String dn, Collection<Attribute> attributes);
public LDIFAddChangeRecord(Entry entry);
// Access methods
public String getDN();
public List<Attribute> getAttributes();
public Attribute getAttribute(String name);
public String getAttributeValue(String name);
public ChangeType getChangeType(); // Returns ADD
// Conversion methods
public AddRequest toAddRequest();
public LDAPRequest toLDAPRequest();
public Entry toEntry();
public LDAPResult processChange(LDAPInterface connection) throws LDAPException;
}LDIF change record for delete operations.
/**
* LDIF change record for delete operations
*/
public class LDIFDeleteChangeRecord extends LDIFChangeRecord {
// Constructors
public LDIFDeleteChangeRecord(String dn);
// Access methods
public String getDN();
public ChangeType getChangeType(); // Returns DELETE
// Conversion methods
public DeleteRequest toDeleteRequest();
public LDAPRequest toLDAPRequest();
public LDAPResult processChange(LDAPInterface connection) throws LDAPException;
}LDIF change record for modify operations.
/**
* LDIF change record for modify operations
*/
public class LDIFModifyChangeRecord extends LDIFChangeRecord {
// Constructors
public LDIFModifyChangeRecord(String dn, Modification... modifications);
public LDIFModifyChangeRecord(String dn, List<Modification> modifications);
// Access methods
public String getDN();
public List<Modification> getModifications();
public ChangeType getChangeType(); // Returns MODIFY
// Conversion methods
public ModifyRequest toModifyRequest();
public LDAPRequest toLDAPRequest();
public LDAPResult processChange(LDAPInterface connection) throws LDAPException;
}LDIF change record for modify DN operations.
/**
* LDIF change record for modify DN operations
*/
public class LDIFModifyDNChangeRecord extends LDIFChangeRecord {
// Constructors
public LDIFModifyDNChangeRecord(String dn, String newRDN, boolean deleteOldRDN);
public LDIFModifyDNChangeRecord(String dn, String newRDN, boolean deleteOldRDN, String newSuperiorDN);
// Access methods
public String getDN();
public String getNewRDN();
public boolean deleteOldRDN();
public String getNewSuperiorDN();
public ChangeType getChangeType(); // Returns MODIFY_DN
// Conversion methods
public ModifyDNRequest toModifyDNRequest();
public LDAPRequest toLDAPRequest();
public LDAPResult processChange(LDAPInterface connection) throws LDAPException;
}Entry source backed by LDIF data for streaming processing.
/**
* Entry source backed by LDIF data
*/
public class LDIFEntrySource implements EntrySource {
// Constructors
public LDIFEntrySource(LDIFReader ldifReader);
public LDIFEntrySource(String filePath) throws IOException;
public LDIFEntrySource(File file) throws IOException;
public LDIFEntrySource(InputStream inputStream);
// EntrySource implementation
public Entry nextEntry() throws EntrySourceException;
public void close();
// Statistics
public long getEntriesRead();
}import com.unboundid.ldap.sdk.*;
import com.unboundid.ldif.*;
// Read entries from LDIF file
try (LDIFReader ldifReader = new LDIFReader("data.ldif")) {
// Configure reader behavior
ldifReader.setDuplicateValueBehavior(DuplicateValueBehavior.STRIP);
ldifReader.setTrailingSpaceBehavior(TrailingSpaceBehavior.STRIP);
Entry entry;
int entryCount = 0;
while ((entry = ldifReader.readEntry()) != null) {
entryCount++;
System.out.println("Entry " + entryCount + ": " + entry.getDN());
// Process entry attributes
for (Attribute attr : entry.getAttributes()) {
System.out.println(" " + attr.getName() + ": " + Arrays.toString(attr.getValues()));
}
}
System.out.println("Total entries read: " + entryCount);
} catch (IOException | LDIFException e) {
System.err.println("Error reading LDIF: " + e.getMessage());
}import com.unboundid.ldap.sdk.*;
import com.unboundid.ldif.*;
// Write entries to LDIF file
try (LDIFWriter ldifWriter = new LDIFWriter("output.ldif")) {
// Configure writer
ldifWriter.setWrapColumn(80);
// Write comment
ldifWriter.writeComment("Exported user data");
ldifWriter.writeComment("Generated on " + new Date());
// Create and write entries
Entry entry1 = new Entry(
"cn=John Doe,ou=people,dc=example,dc=com",
new Attribute("objectClass", "inetOrgPerson"),
new Attribute("cn", "John Doe"),
new Attribute("sn", "Doe"),
new Attribute("givenName", "John"),
new Attribute("mail", "john.doe@example.com")
);
ldifWriter.writeEntry(entry1, "Primary user account");
Entry entry2 = new Entry(
"cn=Jane Smith,ou=people,dc=example,dc=com",
new Attribute("objectClass", "inetOrgPerson"),
new Attribute("cn", "Jane Smith"),
new Attribute("sn", "Smith"),
new Attribute("givenName", "Jane"),
new Attribute("mail", "jane.smith@example.com")
);
ldifWriter.writeEntry(entry2);
System.out.println("Entries written: " + ldifWriter.getEntriesWritten());
} catch (IOException e) {
System.err.println("Error writing LDIF: " + e.getMessage());
}import com.unboundid.ldap.sdk.*;
import com.unboundid.ldif.*;
// Read and process LDIF change records
try (LDIFReader ldifReader = new LDIFReader("changes.ldif")) {
LDIFRecord record;
while ((record = ldifReader.readLDIFRecord()) != null) {
if (record instanceof LDIFChangeRecord) {
LDIFChangeRecord changeRecord = (LDIFChangeRecord) record;
System.out.println("Change Type: " + changeRecord.getChangeType());
System.out.println("DN: " + changeRecord.getDN());
switch (changeRecord.getChangeType()) {
case ADD:
LDIFAddChangeRecord addRecord = (LDIFAddChangeRecord) changeRecord;
System.out.println("Adding entry with " + addRecord.getAttributes().size() + " attributes");
break;
case DELETE:
System.out.println("Deleting entry");
break;
case MODIFY:
LDIFModifyChangeRecord modifyRecord = (LDIFModifyChangeRecord) changeRecord;
System.out.println("Modifying entry with " + modifyRecord.getModifications().size() + " modifications");
for (Modification mod : modifyRecord.getModifications()) {
System.out.println(" " + mod.getModificationType() + " " + mod.getAttributeName());
}
break;
case MODIFY_DN:
LDIFModifyDNChangeRecord modDNRecord = (LDIFModifyDNChangeRecord) changeRecord;
System.out.println("Renaming to: " + modDNRecord.getNewRDN());
break;
}
System.out.println("---");
} else if (record instanceof Entry) {
Entry entry = (Entry) record;
System.out.println("Entry: " + entry.getDN() + " (" + entry.getAttributes().size() + " attributes)");
}
}
} catch (IOException | LDIFException e) {
System.err.println("Error processing LDIF: " + e.getMessage());
}import com.unboundid.ldap.sdk.*;
import com.unboundid.ldif.*;
// Apply LDIF changes to LDAP server
LDAPConnection connection = new LDAPConnection("ldap.example.com", 389);
try {
connection.bind("cn=admin,dc=example,dc=com", "password");
try (LDIFReader ldifReader = new LDIFReader("changes.ldif")) {
LDIFChangeRecord changeRecord;
int successCount = 0;
int errorCount = 0;
while ((changeRecord = ldifReader.readChangeRecord()) != null) {
try {
LDAPResult result = changeRecord.processChange(connection);
if (result.getResultCode() == ResultCode.SUCCESS) {
successCount++;
System.out.println("Applied " + changeRecord.getChangeType() + " for " + changeRecord.getDN());
} else {
errorCount++;
System.err.println("Failed " + changeRecord.getChangeType() + " for " + changeRecord.getDN() +
": " + result.getDiagnosticMessage());
}
} catch (LDAPException e) {
errorCount++;
System.err.println("Error applying change for " + changeRecord.getDN() + ": " + e.getMessage());
}
}
System.out.println("Changes applied successfully: " + successCount);
System.out.println("Changes failed: " + errorCount);
}
} finally {
connection.close();
}import com.unboundid.ldap.sdk.*;
import com.unboundid.ldif.*;
// Transform LDIF data during processing
try (LDIFReader reader = new LDIFReader("input.ldif");
LDIFWriter writer = new LDIFWriter("transformed.ldif")) {
Entry entry;
while ((entry = reader.readEntry()) != null) {
// Transform the entry
Entry transformedEntry = transformEntry(entry);
if (transformedEntry != null) {
writer.writeEntry(transformedEntry);
}
}
System.out.println("Transformation complete");
}
private static Entry transformEntry(Entry originalEntry) {
// Skip entries without mail attribute
if (!originalEntry.hasAttribute("mail")) {
return null;
}
// Create new entry with transformed DN
String originalDN = originalEntry.getDN();
String newDN = originalDN.replace("dc=old,dc=com", "dc=new,dc=com");
Entry newEntry = new Entry(newDN);
// Copy and transform attributes
for (Attribute attr : originalEntry.getAttributes()) {
String attrName = attr.getName();
if ("mail".equals(attrName)) {
// Transform email addresses
String[] oldValues = attr.getValues();
String[] newValues = new String[oldValues.length];
for (int i = 0; i < oldValues.length; i++) {
newValues[i] = oldValues[i].replace("@old.com", "@new.com");
}
newEntry.addAttribute(new Attribute(attrName, newValues));
} else if ("telephoneNumber".equals(attrName)) {
// Skip telephone numbers in transformation
continue;
} else {
// Copy attribute as-is
newEntry.addAttribute(attr);
}
}
return newEntry;
}Install with Tessl CLI
npx tessl i tessl/maven-com-unboundid--unboundid-ldapsdk