Search in sources :

Example 11 with LdifAttributesReader

use of org.apache.directory.api.ldap.model.ldif.LdifAttributesReader in project directory-ldap-api by apache.

the class LdifAttributesReaderTest method testLdifComments.

@Test
public void testLdifComments() throws LdapLdifException, IOException {
    String ldif = "#Comment 1\r" + "#\r" + " th\n" + " is is still a comment\n" + "\n";
    LdifAttributesReader reader = new LdifAttributesReader();
    Entry entry = reader.parseEntry(ldif);
    assertNull(entry);
    reader.close();
}
Also used : Entry(org.apache.directory.api.ldap.model.entry.Entry) LdifAttributesReader(org.apache.directory.api.ldap.model.ldif.LdifAttributesReader) Test(org.junit.Test)

Example 12 with LdifAttributesReader

use of org.apache.directory.api.ldap.model.ldif.LdifAttributesReader in project directory-ldap-api by apache.

the class LdifAttributesReaderTest method testLdifParserRFC2849Sample4.

@Test
public void testLdifParserRFC2849Sample4() throws NamingException, Exception {
    String ldif = "# dn:: ou=���������,o=Airius\n" + "objectclass: top\n" + "objectclass: organizationalUnit\n" + "ou:: 5Za25qWt6YOo\n" + "# ou:: ���������\n" + "ou;lang-ja:: 5Za25qWt6YOo\n" + "# ou;lang-ja:: ���������\n" + "ou;lang-ja;phonetic:: 44GI44GE44GO44KH44GG44G2\n" + "# ou;lang-ja:: ������������������\n" + "ou;lang-en: Sales\n" + "description: Japanese office\n";
    LdifAttributesReader reader = new LdifAttributesReader();
    Attributes attributes = reader.parseAttributes(ldif);
    String[][] values = { { "objectclass", "top" }, { "objectclass", "organizationalUnit" }, { "ou", "\u55b6\u696d\u90e8" }, { "ou;lang-ja", "\u55b6\u696d\u90e8" }, // 3048 = ���, 3044 = ���, 304e = ���
    { "ou;lang-ja;phonetic", "\u3048\u3044\u304e\u3087\u3046\u3076" }, // 3087 = ���, 3046 = ���, 3076 = ���
    { "ou;lang-en", "Sales" }, { "description", "Japanese office" } };
    for (int j = 0; j < values.length; j++) {
        javax.naming.directory.Attribute attr = attributes.get(values[j][0]);
        if (attr.contains(values[j][1])) {
            assertTrue(true);
        } else {
            assertTrue(attr.contains(values[j][1].getBytes(StandardCharsets.UTF_8)));
        }
    }
    reader.close();
}
Also used : Attributes(javax.naming.directory.Attributes) LdifAttributesReader(org.apache.directory.api.ldap.model.ldif.LdifAttributesReader) Test(org.junit.Test)

Example 13 with LdifAttributesReader

use of org.apache.directory.api.ldap.model.ldif.LdifAttributesReader in project directory-ldap-api by apache.

the class LdifAttributesReaderTest method testLdifEmptyLines.

@Test
public void testLdifEmptyLines() throws LdapLdifException, IOException {
    String ldif = "\n\n\r\r\n";
    LdifAttributesReader reader = new LdifAttributesReader();
    Entry entry = reader.parseEntry(ldif);
    assertNull(entry);
    reader.close();
}
Also used : Entry(org.apache.directory.api.ldap.model.entry.Entry) LdifAttributesReader(org.apache.directory.api.ldap.model.ldif.LdifAttributesReader) Test(org.junit.Test)

Example 14 with LdifAttributesReader

use of org.apache.directory.api.ldap.model.ldif.LdifAttributesReader in project directory-ldap-api by apache.

the class LdifAttributesReaderTest method testLdifParserRFC2849Sample3.

@Test
public void testLdifParserRFC2849Sample3() throws LdapLdifException, Exception {
    String ldif = "objectclass: top\n" + "objectclass: person\n" + "objectclass: organizationalPerson\n" + "cn: Gern Jensen\n" + "cn: Gern O Jensen\n" + "sn: Jensen\n" + "uid: gernj\n" + "telephonenumber: +1 408 555 1212\n" + "description:: V2hhdCBhIGNhcmVmdWwgcmVhZGVyIHlvdSBhcmUhICBUaGlzIHZhbHVl\n" + " IGlzIGJhc2UtNjQtZW5jb2RlZCBiZWNhdXNlIGl0IGhhcyBhIGNvbnRyb2wgY2hhcmFjdG\n" + " VyIGluIGl0IChhIENSKS4NICBCeSB0aGUgd2F5LCB5b3Ugc2hvdWxkIHJlYWxseSBnZXQg\n" + " b3V0IG1vcmUu";
    LdifAttributesReader reader = new LdifAttributesReader();
    Attributes attributes = reader.parseAttributes(ldif);
    javax.naming.directory.Attribute attr = attributes.get("objectclass");
    assertTrue(attr.contains("top"));
    assertTrue(attr.contains("person"));
    assertTrue(attr.contains("organizationalPerson"));
    attr = attributes.get("cn");
    assertTrue(attr.contains("Gern Jensen"));
    assertTrue(attr.contains("Gern O Jensen"));
    attr = attributes.get("sn");
    assertTrue(attr.contains("Jensen"));
    attr = attributes.get("uid");
    assertTrue(attr.contains("gernj"));
    attr = attributes.get("telephonenumber");
    assertTrue(attr.contains("+1 408 555 1212"));
    attr = attributes.get("description");
    assertTrue(attr.contains("What a careful reader you are!  This value is base-64-encoded because it has a control character in it (a CR).\r  By the way, you should really get out more.".getBytes(StandardCharsets.UTF_8)));
    reader.close();
}
Also used : Attributes(javax.naming.directory.Attributes) LdifAttributesReader(org.apache.directory.api.ldap.model.ldif.LdifAttributesReader) Test(org.junit.Test)

Example 15 with LdifAttributesReader

use of org.apache.directory.api.ldap.model.ldif.LdifAttributesReader in project directory-ldap-api by apache.

the class DefaultEntry method createEntry.

// -------------------------------------------------------------------------
// Helper methods
// -------------------------------------------------------------------------
private Entry createEntry(SchemaManager schemaManager, Object... elements) throws LdapInvalidAttributeValueException, LdapLdifException {
    StringBuilder sb = new StringBuilder();
    int pos = 0;
    boolean valueExpected = false;
    for (Object element : elements) {
        if (!valueExpected) {
            if (!(element instanceof String)) {
                throw new LdapInvalidAttributeValueException(ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(I18n.ERR_13233_ATTRIBUTE_ID_MUST_BE_A_STRING, pos + 1));
            }
            String attribute = (String) element;
            sb.append(attribute);
            if (attribute.indexOf(':') != -1) {
                sb.append('\n');
            } else {
                valueExpected = true;
            }
        } else {
            if (element instanceof String) {
                sb.append(": ").append((String) element).append('\n');
            } else if (element instanceof byte[]) {
                sb.append(":: ");
                sb.append(new String(Base64.encode((byte[]) element)));
                sb.append('\n');
            } else {
                throw new LdapInvalidAttributeValueException(ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(I18n.ERR_13234_ATTRIBUTE_VAL_STRING_OR_BYTE, pos + 1));
            }
            valueExpected = false;
        }
    }
    if (valueExpected) {
        throw new LdapInvalidAttributeValueException(ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(I18n.ERR_12087));
    }
    try (LdifAttributesReader reader = new LdifAttributesReader()) {
        return reader.parseEntry(schemaManager, sb.toString());
    } catch (IOException e) {
        throw new LdapLdifException(I18n.err(I18n.ERR_13248_CANNOT_READ_ENTRY));
    }
}
Also used : LdapLdifException(org.apache.directory.api.ldap.model.ldif.LdapLdifException) LdifAttributesReader(org.apache.directory.api.ldap.model.ldif.LdifAttributesReader) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) IOException(java.io.IOException)

Aggregations

LdifAttributesReader (org.apache.directory.api.ldap.model.ldif.LdifAttributesReader)21 Test (org.junit.Test)20 Entry (org.apache.directory.api.ldap.model.entry.Entry)13 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)9 Attributes (javax.naming.directory.Attributes)6 LdapLdifException (org.apache.directory.api.ldap.model.ldif.LdapLdifException)2 IOException (java.io.IOException)1 LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)1