首页 > 解决方案 > UnboundID LDIFReader 无法读取条目

问题描述

我正在使用下面的代码从http://www.ietf.org/rfc/rfc2849.txt读取示例 6我正在使用unboundid-ldapsdkversion 4.0.6。我得到以下异常 Entries read:3 Entries with error:1 LDIFException(lineNumber=23, mayContinueReading=true, message='The record starting at or near line number 23 contains a line that does not begin with an attribute name followed by a colon.', dataLines='dn: ou=PD Accountants, ou=Product Development, dc=airius, dc=com{end-of-line}changetype: modrdn{end-of-line}newrdn: ou=Product Development Accountants{end-of-line}deleteoldrdn: 0{end-of-line}newsuperior: ou=Accounting, dc=airius, dc=com{end-of-line}dn: cn=Paula Jensen, ou=Product Development, dc=airius, dc=com{end-of-line}changetype: modify{end-of-line}add: postaladdress{end-of-line}postaladdress: 123 Anystreet $ Sunnyvale, CA $ 94086{end-of-line}-{end-of-line}') at com.unboundid.ldif.LDIFReader.parseAttributes(LDIFReader.java:2744) at com.unboundid.ldif.LDIFReader.decodeEntry(LDIFReader.java:2070)

用于解析 LDIF 文件的 UnboudId 实现中是否缺少某些内容?尝试读取实际的 LDIF 文件时,我遇到了类似的错误。我浏览了文档,其中提到它符合 RFC 2849。

示例代码 -

public class ReadLDFDemo {
    private final String example6 = "version: 1\n"
            + "# Add a new entry\n"
            + "dn: cn=Fiona Jensen, ou=Marketing, dc=airius, dc=com\n"
            + "changetype: add\n"
            + "objectclass: top\n"
            + "objectclass: person\n"
            + "objectclass: organizationalPerson\n"
            + "cn: Fiona Jensen\n"
            + "sn: Jensen\n"
            + "uid: fiona\n"
            + "telephonenumber: +1 408 555 1212\n"
//            + "jpegphoto:< file:///usr/local/directory/photos/fiona.jpg\n"
            + "\n"
            + "# Delete an existing entry\n"
            + "dn: cn=Robert Jensen, ou=Marketing, dc=airius, dc=com\n"
            + "changetype: delete\n"
            + "\n"
            + "# Modify an entry's relative distinguished name\n"
            + "dn: cn=Paul Jensen, ou=Product Development, dc=airius, dc=com\n"
            + "changetype: modrdn\n"
            + "newrdn: cn=Paula Jensen\n"
            + "deleteoldrdn: 1\n"
            + "\n"
            + "# Rename an entry and move all of its children to a new location in\n"
            + "# the directory tree (only implemented by LDAPv3 servers).\n"
            + "dn: ou=PD Accountants, ou=Product Development, dc=airius, dc=com\n"
            + "changetype: modrdn\n"
            + "newrdn: ou=Product Development Accountants\n"
            + "deleteoldrdn: 0\n"
            + "newsuperior: ou=Accounting, dc=airius, dc=com\n"
            + "# Modify an entry: add an additional value to the postaladdress\n"
            + "# attribute, completely delete the description attribute, replace\n"
            + "# the telephonenumber attribute with two values, and delete a specific\n"
            + "# value from the facsimiletelephonenumber attribute\n"
            + "dn: cn=Paula Jensen, ou=Product Development, dc=airius, dc=com\n"
            + "changetype: modify\n"
            + "add: postaladdress\n"
            + "postaladdress: 123 Anystreet $ Sunnyvale, CA $ 94086\n"
            + "-\n"
            + "\n"
            + "delete: description\n"
            + "-\n"
            + "replace: telephonenumber\n"
            + "telephonenumber: +1 408 555 1234\n"
            + "telephonenumber: +1 408 555 5678\n"
            + "-\n"
            + "delete: facsimiletelephonenumber\n"
            + "facsimiletelephonenumber: +1 408 555 9876\n"
            + "-\n"
            + "\n"
            + "# Modify an entry: replace the postaladdress attribute with an empty\n"
            + "# set of values (which will cause the attribute to be removed), and\n"
            + "# delete the entire description attribute. Note that the first will\n"
            + "# always succeed, while the second will only succeed if at least\n"
            + "# one value for the description attribute is present.\n"
            + "dn: cn=Ingrid Jensen, ou=Product Support, dc=airius, dc=com\n"
            + "changetype: modify\n"
            + "replace: postaladdress\n"
            + "-\n"
            + "delete: description\n"
            + "-";

public void readLDFSamples() {
        System.out.println(example6);
        InputStream inputStream =
                new ByteArrayInputStream(example6.getBytes());
        readUnboundIdLdifFile(inputStream);
    }

    private void readUnboundIdLdifFile(InputStream inputStream) {
        LDIFReader ldifReader = null;
        try {
            ldifReader = new LDIFReader(inputStream);
            Entry entry = null;
            long count = 0;
            long errorCount = 0;
            while (true) {
                try {
                    entry = ldifReader.readEntry();
                    count++;
                    if (entry == null) {
                        break;
                    }
                } catch (LDIFException ldifE) {
                    errorCount++;
                    ldifE.printStackTrace();
                    break;
                }
            }
            System.out.println("Entries read:" + count + " Entries with error:"
                    + errorCount);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ldifReader != null) {
                    ldifReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

标签: ldapunboundid-ldap-sdkunboundid

解决方案


您的源代码中这两行之间缺少一个空分隔线:

        + "newsuperior: ou=Accounting, dc=airius, dc=com\n"
        + "# Modify an entry: add an additional value to the postaladdress\n"

因此,第二个更改记录被解析为modrdn更改记录的延续。


推荐阅读