Search in sources :

Example 81 with AttributeType

use of org.apache.directory.api.ldap.model.schema.AttributeType in project directory-ldap-api by apache.

the class CaseSensitiveStringAnonymizer 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) {
    AttributeType attributeType = attribute.getAttributeType();
    Attribute result = new DefaultAttribute(attributeType);
    for (Value value : attribute) {
        if (value.isHumanReadable()) {
            Value anonymized = valueMap.get(value);
            if (anonymized != null) {
                try {
                    result.add(anonymized);
                } catch (LdapInvalidAttributeValueException e) {
                }
            } else {
                String strValue = value.getValue();
                String newValue = computeNewValue(strValue);
                try {
                    result.add(newValue);
                    Value anonValue = new Value(attribute.getAttributeType(), newValue);
                    valueMap.put((Value) value, anonValue);
                    valueSet.add(anonValue);
                } catch (LdapInvalidAttributeValueException e) {
                    throw new RuntimeException(I18n.err(I18n.ERR_13436_ERROR_ANONYMIZING_VALUE, strValue));
                }
            }
        }
    }
    return result;
}
Also used : Attribute(org.apache.directory.api.ldap.model.entry.Attribute) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) 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 82 with AttributeType

use of org.apache.directory.api.ldap.model.schema.AttributeType 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 83 with AttributeType

use of org.apache.directory.api.ldap.model.schema.AttributeType in project directory-ldap-api by apache.

the class Ava method apply.

/**
 * Apply a SchemaManager to the Ava. It will normalize the Ava.<br/>
 * If the Ava already had a SchemaManager, then the new SchemaManager will be
 * used instead.
 *
 * @param schemaManager The SchemaManager instance to use
 * @throws LdapInvalidDnException If the Ava can't be normalized accordingly
 * to the given SchemaManager
 */
private void apply(SchemaManager schemaManager) throws LdapInvalidDnException {
    if (schemaManager != null) {
        this.schemaManager = schemaManager;
        AttributeType tmpAttributeType = null;
        try {
            tmpAttributeType = schemaManager.lookupAttributeTypeRegistry(normType);
        } catch (LdapException le) {
            if (schemaManager.isRelaxed()) {
                // No attribute in the schema, but the schema is relaxed : get out
                return;
            } else {
                String message = I18n.err(I18n.ERR_13600_TYPE_IS_NULL_OR_EMPTY);
                LOG.error(message);
                throw new LdapInvalidDnException(ResultCodeEnum.INVALID_DN_SYNTAX, message, le);
            }
        }
        if (this.attributeType == tmpAttributeType) {
            // No need to normalize again
            return;
        } else {
            this.attributeType = tmpAttributeType;
        }
        try {
            value = new Value(tmpAttributeType, value);
        } catch (LdapException le) {
            String message = I18n.err(I18n.ERR_13600_TYPE_IS_NULL_OR_EMPTY);
            LOG.error(message);
            throw new LdapInvalidDnException(ResultCodeEnum.INVALID_DN_SYNTAX, message, le);
        }
        hashCode();
    }
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) Value(org.apache.directory.api.ldap.model.entry.Value) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)

Example 84 with AttributeType

use of org.apache.directory.api.ldap.model.schema.AttributeType in project directory-ldap-api by apache.

the class AttributeTypeDescriptionSchemaParserTest method testFull.

/**
 * Test full attribute type description.
 *
 * @throws ParseException
 */
@Test
public void testFull() throws ParseException {
    String value = null;
    AttributeType attributeType = null;
    value = "( 1.2.3.4.5.6.7.8.9.0 NAME ( 'abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789' 'test' ) DESC 'Descripton \u00E4\u00F6\u00FC\u00DF \u90E8\u9577' OBSOLETE SUP abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789 EQUALITY 2.3.4.5.6.7.8.9.0.1 ORDERING 3.4.5.6.7.8.9.0.1.2 SUBSTR 4.5.6.7.8.9.0.1.2.3 SYNTAX 5.6.7.8.9.0.1.2.3.4{1234567890} SINGLE-VALUE NO-USER-MODIFICATION USAGE dSAOperation X-TEST-a ('test1-1' 'test1-2') X-TEST-b ('test2-1' 'test2-2') )";
    attributeType = parser.parseAttributeTypeDescription(value);
    assertEquals("1.2.3.4.5.6.7.8.9.0", attributeType.getOid());
    assertEquals(2, attributeType.getNames().size());
    assertEquals("abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789", attributeType.getNames().get(0));
    assertEquals("test", attributeType.getNames().get(1));
    assertEquals("Descripton \u00E4\u00F6\u00FC\u00DF \u90E8\u9577", attributeType.getDescription());
    assertTrue(attributeType.isObsolete());
    assertEquals("abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789", attributeType.getSuperiorOid());
    assertEquals("2.3.4.5.6.7.8.9.0.1", attributeType.getEqualityOid());
    assertEquals("3.4.5.6.7.8.9.0.1.2", attributeType.getOrderingOid());
    assertEquals("4.5.6.7.8.9.0.1.2.3", attributeType.getSubstringOid());
    assertEquals("5.6.7.8.9.0.1.2.3.4", attributeType.getSyntaxOid());
    assertEquals(1234567890, attributeType.getSyntaxLength());
    assertTrue(attributeType.isSingleValued());
    assertFalse(attributeType.isCollective());
    assertFalse(attributeType.isUserModifiable());
    assertEquals(UsageEnum.DSA_OPERATION, attributeType.getUsage());
    assertEquals(2, attributeType.getExtensions().size());
    assertNotNull(attributeType.getExtension("X-TEST-a"));
    assertEquals(2, attributeType.getExtension("X-TEST-a").size());
    assertEquals("test1-1", attributeType.getExtension("X-TEST-a").get(0));
    assertEquals("test1-2", attributeType.getExtension("X-TEST-a").get(1));
    assertNotNull(attributeType.getExtension("X-TEST-b"));
    assertEquals(2, attributeType.getExtension("X-TEST-b").size());
    assertEquals("test2-1", attributeType.getExtension("X-TEST-b").get(0));
    assertEquals("test2-2", attributeType.getExtension("X-TEST-b").get(1));
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) Test(org.junit.Test)

Example 85 with AttributeType

use of org.apache.directory.api.ldap.model.schema.AttributeType in project directory-ldap-api by apache.

the class AttributeTypeDescriptionSchemaParserTest method testEquality.

/**
 * Tests EQUALITY and its values.
 * Very similar to SUP, so here are less test cases.
 *
 * @throws ParseException
 */
@Test
public void testEquality() throws ParseException {
    String value = null;
    AttributeType attributeType = null;
    // no EQUALITY
    value = "( 1.1 SYNTAX 1.1 )";
    attributeType = parser.parseAttributeTypeDescription(value);
    assertNull(attributeType.getEqualityOid());
    // EQUALITY numericoid
    value = "( 1.1 SYNTAX 1.1 EQUALITY 1.2.3.4567.8.9.0 )";
    attributeType = parser.parseAttributeTypeDescription(value);
    assertEquals("1.2.3.4567.8.9.0", attributeType.getEqualityOid());
    // EQUALITY descr, no space
    value = "(1.1 SYNTAX1.1 EQUALITYabcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789)";
    attributeType = parser.parseAttributeTypeDescription(value);
    assertEquals("abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789", attributeType.getEqualityOid());
    // EQUALITY descr, newline
    value = "\n(\n1.1\nSYNTAX\n1.1\nEQUALITY\nabcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789\n)\n";
    attributeType = parser.parseAttributeTypeDescription(value);
    assertEquals("abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789", attributeType.getEqualityOid());
    // quoted value
    value = "( 1.1 SYNTAX 1.1 EQUALITY 'caseExcactMatch' )";
    attributeType = parser.parseAttributeTypeDescription(value);
    assertEquals("caseExcactMatch", attributeType.getEqualityOid());
    // quote value in parentheses
    value = "( 1.1 SYNTAX 1.1 EQUALITY ('caseExcactMatch') )";
    attributeType = parser.parseAttributeTypeDescription(value);
    assertEquals("caseExcactMatch", attributeType.getEqualityOid());
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) Test(org.junit.Test)

Aggregations

AttributeType (org.apache.directory.api.ldap.model.schema.AttributeType)135 Test (org.junit.Test)68 MutableAttributeType (org.apache.directory.api.ldap.model.schema.MutableAttributeType)42 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)25 ParseException (java.text.ParseException)15 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)15 Value (org.apache.directory.api.ldap.model.entry.Value)15 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)11 LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)11 HashSet (java.util.HashSet)10 LdapSchemaException (org.apache.directory.api.ldap.model.exception.LdapSchemaException)10 ObjectClass (org.apache.directory.api.ldap.model.schema.ObjectClass)10 MatchingRule (org.apache.directory.api.ldap.model.schema.MatchingRule)9 IOException (java.io.IOException)7 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)7 Before (org.junit.Before)7 SchemaManager (org.apache.directory.api.ldap.model.schema.SchemaManager)6 DefaultSchemaManager (org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager)6 ArrayList (java.util.ArrayList)5 Dn (org.apache.directory.api.ldap.model.name.Dn)4