Search in sources :

Example 1 with LdapLdifException

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

the class LdifAttributesReaderTest method testLdifParserRFC2849Sample5WithSizeLimit.

@Test
public void testLdifParserRFC2849Sample5WithSizeLimit() throws Exception {
    String ldif = "objectclass: top\n" + "objectclass: person\n" + "objectclass: organizationalPerson\n" + "cn: Horatio Jensen\n" + "cn: Horatio N Jensen\n" + "sn: Jensen\n" + "uid: hjensen\n" + "telephonenumber: +1 408 555 1212\n" + "jpegphoto:< file:" + HJENSEN_JPEG_FILE.getAbsolutePath() + "\n";
    LdifAttributesReader reader = new LdifAttributesReader();
    reader.setSizeLimit(128);
    try {
        reader.parseEntry(ldif);
        fail();
    } catch (LdapLdifException ne) {
        assertTrue(I18n.err(I18n.ERR_13442_ERROR_PARSING_LDIF_BUFFER), ne.getMessage().startsWith(I18n.ERR_13442_ERROR_PARSING_LDIF_BUFFER.getErrorCode()));
    }
    reader.close();
}
Also used : LdapLdifException(org.apache.directory.api.ldap.model.ldif.LdapLdifException) LdifAttributesReader(org.apache.directory.api.ldap.model.ldif.LdifAttributesReader) Test(org.junit.Test)

Example 2 with LdapLdifException

use of org.apache.directory.api.ldap.model.ldif.LdapLdifException 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

LdapLdifException (org.apache.directory.api.ldap.model.ldif.LdapLdifException)2 LdifAttributesReader (org.apache.directory.api.ldap.model.ldif.LdifAttributesReader)2 IOException (java.io.IOException)1 LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)1 Test (org.junit.Test)1