Emdadul Sawon Emdadul Sawon - 1 year ago 173
Java Question

Export and Import apacheds data into LDIF programmatically from java

I have create a server in Apache Directory Studio. I also created a partition and inserted some entries to that server form Java. Now I want to Backup and Restore this data in and LDIF file programmatically. I am new to LDAP. So please show me a detailed way to Export and Import entries programmatically using java from my server into LDIF.

Current solution:

Now I am using this approach to backup:

EntryCursor cursor = connection.search(new Dn("o=partition"), "(ObjectClass=*)", SearchScope.SUBTREE, "*", "+");
Charset charset = Charset.forName("UTF-8");
Path filePath = Paths.get("src/main/resources", "backup.ldif");
BufferedWriter writer = Files.newBufferedWriter(filePath, charset);
String st = "";

while (cursor.next()) {
Entry entry = cursor.get();
String ss = LdifUtils.convertToLdif(entry);
st += ss + "\n";

For restore I am using this:

InputStream is = new FileInputStream(filepath);
LdifReader entries = new LdifReader(is);

for (LdifEntry ldifEntry : entries) {
Entry entry = ldifEntry.getEntry();

AddRequest addRequest = new AddRequestImpl();
addRequest.addControl(new ManageDsaITImpl());

AddResponse res = connection.add(addRequest);

But I am not sure whether this is the correct way.

Problem of this solution:

When I backup my database, it writes entries into LDIF in a random way, so restore does not works until I fix the order of entries manually. I there any better way? Please someone help me.

Answer Source

After a long search, I actually understand that the solution of restore the entries is a simple recursion. In backup procedure does not print the entries in random way, it maintain the tree order. So a simple recursion can order the entries well. Here is a sample code which I use-

void findEntry(LdapConnection connection, Entry entry, StringBuilder sb)
    throws LdapException, CursorException {
  EntryCursor cursor = connection.search(entry.getDn(), "(ObjectClass=*)", SearchScope.ONELEVEL, "*", "+");
  while (cursor.next()) {
    findEntry(connection, cursor.get(), sb);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download