Export and Import apacheds data into LDIF programmatically from java
Asked Answered
S

2

7

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";
  }
  writer.write(st);
  writer.close();

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.setEntry(entry);
    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.

Skeen answered 24/12, 2014 at 11:38 Comment(0)
S
9

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 {
  sb.append(LdifUtils.convertToLdif(entry));
  sb.append("\n");
  EntryCursor cursor = connection.search(entry.getDn(), "(ObjectClass=*)", SearchScope.ONELEVEL, "*", "+");
  while (cursor.next()) {
    findEntry(connection, cursor.get(), sb);
  }
}
Skeen answered 25/3, 2015 at 10:45 Comment(0)
K
1

Well, you tagged as Java and so look at the UnboundID LDAP SDK or as you are using APacheDS, why not look at the Apache LDAP API

Either of them will work. I currently use [UnboundID LDAP SDK] which have [LDIF specific APIs].3. I assume [Apache LDAP API] does also, But I have not used them.

Kaiserdom answered 25/12, 2014 at 10:40 Comment(2)
I am using eclipse and jetty.Skeen
Both of of the SDKs I mentioned are for Java. They should work fine with Eclipse and Jetty.Kaiserdom

© 2022 - 2024 — McMap. All rights reserved.