Search in sources :

Example 21 with LdapInvalidAttributeValueException

use of org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException in project directory-ldap-api by apache.

the class IntegerAnonymizer method anonymize.

/**
 * Anonymize an attribute using pure random values (either chars of bytes, depending on the Attribute type)
 */
@Override
public Attribute anonymize(Map<Value, Value> valueMap, Set<Value> valueSet, Attribute attribute) {
    Attribute result = new DefaultAttribute(attribute.getAttributeType());
    for (Value value : attribute) {
        if (value.isHumanReadable()) {
            Value anonymized = valueMap.get(value);
            if (anonymized != null) {
                try {
                    result.add(anonymized);
                } catch (LdapInvalidAttributeValueException e) {
                // Handle me...
                }
            } else {
                String strValue = value.getValue();
                String newValue = computeNewIntegerValue(strValue);
                try {
                    result.add(newValue);
                    Value anonValue = new Value(attribute.getAttributeType(), newValue);
                    valueMap.put((Value) value, anonValue);
                    valueSet.add(anonValue);
                } catch (LdapInvalidAttributeValueException e) {
                // TODO : handle that
                }
            }
        }
    }
    return result;
}
Also used : Attribute(org.apache.directory.api.ldap.model.entry.Attribute) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Value(org.apache.directory.api.ldap.model.entry.Value) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)

Example 22 with LdapInvalidAttributeValueException

use of org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException in project directory-ldap-api by apache.

the class LdifAttributesReader method parseEntryAttribute.

/**
 * Parse an AttributeType/AttributeValue
 *
 * @param schemaManager The SchemaManager
 * @param entry The entry where to store the value
 * @param line The line to parse
 * @param lowerLine The same line, lowercased
 * @throws LdapLdifException If anything goes wrong
 */
private void parseEntryAttribute(SchemaManager schemaManager, Entry entry, String line, String lowerLine) throws LdapLdifException {
    int colonIndex = line.indexOf(':');
    String attributeName = lowerLine.substring(0, colonIndex);
    AttributeType attributeType = null;
    // We should *not* have a Dn twice
    if ("dn".equals(attributeName)) {
        LOG.error(I18n.err(I18n.ERR_13400_ENTRY_WITH_TWO_DNS));
        throw new LdapLdifException(I18n.err(I18n.ERR_13439_LDIF_ENTRY_WITH_TWO_DNS));
    }
    if (schemaManager != null) {
        attributeType = schemaManager.getAttributeType(attributeName);
        if (attributeType == null) {
            String msg = I18n.err(I18n.ERR_13475_UNKNOWN_ATTRIBUTETYPE, attributeName);
            LOG.error(msg);
            throw new LdapLdifException(msg);
        }
    }
    Object attributeValue = parseValue(attributeName, line, colonIndex);
    // Update the entry
    Attribute attribute;
    if (schemaManager == null) {
        attribute = entry.get(attributeName);
    } else {
        attribute = entry.get(attributeType);
    }
    if (attribute == null) {
        if (schemaManager == null) {
            if (attributeValue instanceof String) {
                entry.put(attributeName, (String) attributeValue);
            } else {
                entry.put(attributeName, (byte[]) attributeValue);
            }
        } else {
            try {
                if (attributeValue instanceof String) {
                    entry.put(attributeName, attributeType, (String) attributeValue);
                } else {
                    entry.put(attributeName, attributeType, (byte[]) attributeValue);
                }
            } catch (LdapException le) {
                throw new LdapLdifException(I18n.err(I18n.ERR_13460_BAD_ATTRIBUTE), le);
            }
        }
    } else {
        try {
            if (attributeValue instanceof String) {
                attribute.add((String) attributeValue);
            } else {
                attribute.add((byte[]) attributeValue);
            }
        } catch (LdapInvalidAttributeValueException liave) {
            throw new LdapLdifException(liave.getMessage(), liave);
        }
    }
}
Also used : Attribute(org.apache.directory.api.ldap.model.entry.Attribute) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 23 with LdapInvalidAttributeValueException

use of org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException in project directory-ldap-api by apache.

the class SchemaAwareAttributeTest method testGetBytes.

/**
 * Test method getBytes()
 */
@Test
public void testGetBytes() throws LdapInvalidAttributeValueException {
    Attribute attr1 = new DefaultAttribute(atPwd);
    attr1.add((byte[]) null);
    assertNull(attr1.getBytes());
    Attribute attr2 = new DefaultAttribute(atPwd);
    attr2.add(BYTES1, BYTES2);
    assertTrue(Arrays.equals(BYTES1, attr2.getBytes()));
    Attribute attr3 = new DefaultAttribute(atCN);
    attr3.add("a", "b");
    try {
        attr3.getBytes();
        fail();
    } catch (LdapInvalidAttributeValueException ivae) {
        assertTrue(true);
    }
}
Also used : DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) Test(org.junit.Test)

Example 24 with LdapInvalidAttributeValueException

use of org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException in project directory-ldap-api by apache.

the class Value method deserialize.

/**
 * Deserialize a StringValue from a byte[], starting at a given position
 *
 * @param buffer The buffer containing the StringValue
 * @param pos The position in the buffer
 * @return The new position
 * @throws IOException If the serialized value is not a StringValue
 * @throws LdapInvalidAttributeValueException If the value is invalid
 */
public int deserialize(byte[] buffer, int pos) throws IOException, LdapInvalidAttributeValueException {
    if ((pos < 0) || (pos >= buffer.length)) {
        throw new ArrayIndexOutOfBoundsException();
    }
    // Read the isHR flag
    isHR = Serialize.deserializeBoolean(buffer, pos);
    pos++;
    if (isHR) {
        // Read the user provided value, if it's not null
        boolean hasValue = Serialize.deserializeBoolean(buffer, pos);
        pos++;
        if (hasValue) {
            bytes = Serialize.deserializeBytes(buffer, pos);
            pos += 4 + bytes.length;
            upValue = Strings.utf8ToString(bytes);
        }
        // Read the prepared value, if not null
        boolean hasPreparedValue = Serialize.deserializeBoolean(buffer, pos);
        pos++;
        if (hasPreparedValue) {
            byte[] preparedBytes = Serialize.deserializeBytes(buffer, pos);
            pos += 4 + preparedBytes.length;
            normValue = Strings.utf8ToString(preparedBytes);
        }
    } else {
        // Read the user provided value, if it's not null
        boolean hasBytes = Serialize.deserializeBoolean(buffer, pos);
        pos++;
        if (hasBytes) {
            bytes = Serialize.deserializeBytes(buffer, pos);
            pos += 4 + bytes.length;
        }
    }
    if (attributeType != null) {
        try {
            computeNormValue();
        } catch (LdapException le) {
            throw new LdapInvalidAttributeValueException(ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, le.getMessage());
        }
    }
    hashCode();
    return pos;
}
Also used : LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 25 with LdapInvalidAttributeValueException

use of org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException in project directory-ldap-api by apache.

the class SchemaEntityFactory method classLoadComparator.

/**
 * Class load a comparator instances
 */
private LdapComparator<?> classLoadComparator(SchemaManager schemaManager, String oid, String className, Attribute byteCode) throws LdapException {
    // Try to class load the comparator
    LdapComparator<?> comparator;
    Class<?> clazz;
    String byteCodeStr = StringConstants.EMPTY;
    if (byteCode == null) {
        try {
            clazz = Class.forName(className);
        } catch (ClassNotFoundException cnfe) {
            LOG.error(I18n.err(I18n.ERR_16056_CANNOT_FIND_CMP_CTOR, className));
            throw new LdapSchemaException(I18n.err(I18n.ERR_16057_CANNOT_FIND_CMP_CLASS, cnfe.getMessage()));
        }
    } else {
        classLoader.setAttribute(byteCode);
        try {
            clazz = classLoader.loadClass(className);
        } catch (ClassNotFoundException cnfe) {
            LOG.error(I18n.err(I18n.ERR_16058_CANNOT_LOAD_CMP_CTOR, className));
            throw new LdapSchemaException(I18n.err(I18n.ERR_16059_CANNOT_LOAD_CMP_CLASS, cnfe.getMessage()));
        }
        byteCodeStr = new String(Base64.encode(byteCode.getBytes()));
    }
    // or we have one which takes an OID. Lets try the one with an OID argument first
    try {
        Constructor<?> constructor = clazz.getConstructor(new Class[] { String.class });
        try {
            comparator = (LdapComparator<?>) constructor.newInstance(new Object[] { oid });
        } catch (InvocationTargetException ite) {
            LOG.error(I18n.err(I18n.ERR_16060_CANNOT_INVOKE_CMP_CTOR, className));
            throw new LdapSchemaException(I18n.err(I18n.ERR_16061_CANNOT_INVOKE_CMP_CLASS, ite.getMessage()));
        } catch (InstantiationException ie) {
            LOG.error(I18n.err(I18n.ERR_16062_CANNOT_INST_CMP_CTOR_CLASS, className));
            throw new LdapSchemaException(I18n.err(I18n.ERR_16063_CANNOT_INST_CMP_CLASS, ie.getMessage()));
        } catch (IllegalAccessException ie) {
            LOG.error(I18n.err(I18n.ERR_16064_CANNOT_ACCESS_CMP_CTOR, className));
            throw new LdapSchemaException(I18n.err(I18n.ERR_16065_CANNOT_ACCESS_CMP_CLASS, ie.getMessage()));
        }
    } catch (NoSuchMethodException nsme) {
        // the one we got in the Comparator entry
        try {
            clazz.getConstructor();
        } catch (NoSuchMethodException nsme2) {
            LOG.error(I18n.err(I18n.ERR_16066_CANNOT_FIND_CMP_CTOR_METH_CLASS, className));
            throw new LdapSchemaException(I18n.err(I18n.ERR_16067_CANNOT_FIND_CMP_CTOR_METH, nsme2.getMessage()));
        }
        try {
            comparator = (LdapComparator<?>) clazz.newInstance();
        } catch (InstantiationException ie) {
            LOG.error(I18n.err(I18n.ERR_16062_CANNOT_INST_CMP_CTOR_CLASS, className));
            throw new LdapSchemaException(I18n.err(I18n.ERR_16063_CANNOT_INST_CMP_CLASS, ie.getMessage()));
        } catch (IllegalAccessException iae) {
            LOG.error(I18n.err(I18n.ERR_16064_CANNOT_ACCESS_CMP_CTOR, className));
            throw new LdapSchemaException(I18n.err(I18n.ERR_16065_CANNOT_ACCESS_CMP_CLASS, iae.getMessage()));
        }
        if (!comparator.getOid().equals(oid)) {
            String msg = I18n.err(I18n.ERR_16021_DIFFERENT_COMPARATOR_OID, oid, comparator.getOid());
            throw new LdapInvalidAttributeValueException(ResultCodeEnum.UNWILLING_TO_PERFORM, msg, nsme);
        }
    }
    // Update the loadable fields
    comparator.setBytecode(byteCodeStr);
    comparator.setFqcn(className);
    // Inject the SchemaManager for the comparator who needs it
    comparator.setSchemaManager(schemaManager);
    return comparator;
}
Also used : LdapComparator(org.apache.directory.api.ldap.model.schema.LdapComparator) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)28 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)12 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)9 AttributeType (org.apache.directory.api.ldap.model.schema.AttributeType)7 Test (org.junit.Test)7 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)6 Value (org.apache.directory.api.ldap.model.entry.Value)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)3 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)3 Modification (org.apache.directory.api.ldap.model.entry.Modification)3 PrepareString (org.apache.directory.api.ldap.model.schema.PrepareString)3 Nonnull (javax.annotation.Nonnull)2 ModificationOperation (org.apache.directory.api.ldap.model.entry.ModificationOperation)2 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)2 LdapNoPermissionException (org.apache.directory.api.ldap.model.exception.LdapNoPermissionException)2 LdapNoSuchObjectException (org.apache.directory.api.ldap.model.exception.LdapNoSuchObjectException)2 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)2 ExternalIdentityRef (org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityRef)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1