【问题标题】:Export and Import apacheds data into LDIF programmatically from java从 java 以编程方式将 apacheds 数据导出和导入到 LDIF
【发布时间】:2016-11-15 04:17:03
【问题描述】:

我在 Apache Directory Studio 中创建了一个服务器。我还创建了一个分区,并从 Java 向该服务器插入了一些条目。现在我想以编程方式在 LDIF 文件中备份和恢复这些数据。我是 LDAP 新手。因此,请向我展示一个详细的方法,以使用我的服务器中的 java 以编程方式将条目导出和导入到 LDIF。

目前的解决方案:

现在我正在使用这种方法进行备份:

  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();

为了恢复我使用这个:

  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);
  }

但我不确定这是否是正确的方法。

这个解决方案的问题:

当我备份我的数据库时,它会以随机方式将条目写入 LDIF,因此在我手动修复条目顺序之前,还原不起作用。我有什么更好的办法吗?请有人帮助我。

【问题讨论】:

    标签: java ldap apacheds ldif


    【解决方案1】:

    经过长时间的搜索,我实际上明白了恢复条目的解决方案是一个简单的递归。在备份过程中不以随机方式打印条目,它保持树顺序。所以一个简单的递归可以很好地对条目进行排序。这是我使用的示例代码-

    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);
      }
    }
    

    【讨论】:

      【解决方案2】:

      好吧,您标记为 Java 并查看 UnboundID LDAP SDK 或者当您使用 APacheDS 时,为什么不查看 Apache LDAP API

      其中任何一个都可以。我目前使用具有 [LDIF 特定 API].3 的 [UnboundID LDAP SDK]。我假设 [Apache LDAP API] 也可以,但我没有使用它们。

      【讨论】:

      • 我正在使用eclipse和jetty。
      • 我提到的两个 SDK 都是用于 Java 的。它们应该可以与 Eclipse 和 Jetty 一起正常工作。
      猜你喜欢
      • 2010-12-07
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      • 2020-08-06
      • 2011-03-25
      • 1970-01-01
      相关资源
      最近更新 更多