Search in sources :

Example 46 with AttributeType

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

the class StringAnonymizer 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) {
                // TODO : handle that
                }
            } 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 47 with AttributeType

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

the class Rdn method addAVA.

/**
 * Add an Ava to the current Rdn
 *
 * @param upType The user provided type of the added Rdn.
 * @param type The normalized provided type of the added Rdn.
 * @param upValue The user provided value of the added Rdn
 * @param value The normalized provided value of the added Rdn
 * @throws LdapInvalidDnException
 *             If the Rdn is invalid
 */
private void addAVA(SchemaManager schemaManager, String type, Value value) throws LdapInvalidDnException {
    // First, let's normalize the type
    AttributeType attributeType;
    String normalizedType = Strings.lowerCaseAscii(type);
    this.schemaManager = schemaManager;
    if (schemaManager != null) {
        attributeType = schemaManager.getAttributeType(normalizedType);
        if (!value.isSchemaAware()) {
            if (attributeType != null) {
                try {
                    value = new Value(attributeType, value);
                } catch (LdapInvalidAttributeValueException liave) {
                    throw new LdapInvalidDnException(liave.getMessage(), liave);
                }
            }
        } else {
            if (attributeType != null) {
                normalizedType = attributeType.getOid();
            }
        }
    }
    Ava newAva = new Ava(schemaManager, type, normalizedType, value);
    switch(nbAvas) {
        case 0:
            // This is the first Ava. Just stores it.
            ava = newAva;
            nbAvas = 1;
            avaType = normalizedType;
            hashCode();
            return;
        case 1:
            // before adding a new one, if it's not already present
            if (ava.equals(newAva)) {
                return;
            }
            // First, create the List and the HashMap
            avas = new ArrayList<>();
            avaTypes = new HashMap<>();
            List<Ava> avaList = new ArrayList<>();
            // and store the existing Ava into it.
            avas.add(ava);
            avaList.add(ava);
            avaTypes.put(avaType, avaList);
            nbAvas++;
            ava = null;
        default:
            // add a new Ava, if it's not already present
            avaList = avaTypes.get(newAva.getNormType());
            if (avaList == null) {
                // Not present, we can add it
                avaList = new ArrayList<>();
                avaList.add(newAva);
                avaTypes.put(newAva.getNormType(), avaList);
                avas.add(newAva);
                nbAvas++;
            } else {
                // We have at least one Ava with the same type, check if it's the same value
                if (!avaList.contains(newAva)) {
                    // Ok, we can add it
                    avaList.add(newAva);
                    avas.add(newAva);
                    nbAvas++;
                }
            }
    }
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) Value(org.apache.directory.api.ldap.model.entry.Value) ArrayList(java.util.ArrayList) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)

Example 48 with AttributeType

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

the class FilterParser method parseItem.

/**
 * Parse the following grammar :
 *
 * <pre>
 * item           = simple / present / substring / extensible
 * simple         = attr WSP* filtertype WSP* assertionvalue
 * filtertype     = '=' / '~=' / '>=' / '<='
 * present        = attr WSP* '=' '*'
 * substring      = attr WSP* '=' WSP* [initial] any [final]
 * extensible     = ( attr [":dn"] [':' oid] ":=" assertionvalue )
 *                  / ( [":dn"] ':' oid ":=" assertionvalue )
 * matchingrule   = ":" oid
 * </pre>
 * An item starts with an attribute or a colon.
 */
@SuppressWarnings({ "rawtypes" })
private static ExprNode parseItem(SchemaManager schemaManager, byte[] filterBytes, Position pos, byte b, boolean relaxed) throws ParseException, LdapException {
    String attribute;
    if (b == '\0') {
        throw new ParseException(I18n.err(I18n.ERR_13310_BAD_CHAR), pos.start);
    }
    if (b == ':') {
        // If we have a colon, then the item is an extensible one
        return parseExtensible(schemaManager, null, filterBytes, pos, relaxed);
    } else {
        // We must have an attribute
        attribute = AttributeUtils.parseAttribute(filterBytes, pos, true, relaxed);
        // Skip spaces
        skipWhiteSpaces(filterBytes, pos);
        // Now, we may have a present, substring, simple or an extensible
        b = Strings.byteAt(filterBytes, pos.start);
        switch(b) {
            case '=':
                // It can be a presence, an equal or a substring
                pos.start++;
                return parsePresenceEqOrSubstring(schemaManager, attribute, filterBytes, pos);
            case '~':
                // Approximate node
                pos.start++;
                // Check that we have a '='
                if (!Strings.isCharASCII(filterBytes, pos.start, '=')) {
                    throw new ParseException(I18n.err(I18n.ERR_13311_EXPECTING_EQUAL), pos.start);
                }
                pos.start++;
                // Parse the value and create the node
                if (schemaManager == null) {
                    return new ApproximateNode(attribute, parseAssertionValue(schemaManager, attribute, filterBytes, pos).getBytes());
                } else {
                    AttributeType attributeType = schemaManager.getAttributeType(attribute);
                    if (attributeType != null) {
                        return new ApproximateNode(attributeType, parseAssertionValue(schemaManager, attribute, filterBytes, pos));
                    } else {
                        return UndefinedNode.UNDEFINED_NODE;
                    }
                }
            case '>':
                // Greater or equal node
                pos.start++;
                // Check that we have a '='
                if (!Strings.isCharASCII(filterBytes, pos.start, '=')) {
                    throw new ParseException(I18n.err(I18n.ERR_13311_EXPECTING_EQUAL), pos.start);
                }
                pos.start++;
                // Parse the value and create the node
                if (schemaManager == null) {
                    return new GreaterEqNode(attribute, parseAssertionValue(schemaManager, attribute, filterBytes, pos).getBytes());
                } else {
                    AttributeType attributeType = schemaManager.getAttributeType(attribute);
                    if (attributeType != null) {
                        return new GreaterEqNode(attributeType, parseAssertionValue(schemaManager, attribute, filterBytes, pos));
                    } else {
                        return UndefinedNode.UNDEFINED_NODE;
                    }
                }
            case '<':
                // Less or equal node
                pos.start++;
                // Check that we have a '='
                if (!Strings.isCharASCII(filterBytes, pos.start, '=')) {
                    throw new ParseException(I18n.err(I18n.ERR_13311_EXPECTING_EQUAL), pos.start);
                }
                pos.start++;
                // Parse the value and create the node
                if (schemaManager == null) {
                    return new LessEqNode(attribute, parseAssertionValue(schemaManager, attribute, filterBytes, pos).getBytes());
                } else {
                    AttributeType attributeType = schemaManager.getAttributeType(attribute);
                    if (attributeType != null) {
                        return new LessEqNode(attributeType, parseAssertionValue(schemaManager, attribute, filterBytes, pos));
                    } else {
                        return UndefinedNode.UNDEFINED_NODE;
                    }
                }
            case ':':
                // An extensible node
                pos.start++;
                return parseExtensible(schemaManager, attribute, filterBytes, pos, relaxed);
            default:
                // This is an error
                throw new ParseException(I18n.err(I18n.ERR_13312_ITEM_EXPECTED), pos.start);
        }
    }
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) ParseException(java.text.ParseException)

Example 49 with AttributeType

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

the class FilterParser method parseAssertionValue.

/**
 * An assertion value :
 *
 * <pre>
 * assertionvalue = valueencoding
 * valueencoding  = 0*(normal / escaped)
 * normal         = UTF1SUBSET / UTFMB
 * escaped        = '\' HEX HEX
 * HEX            = '0'-'9' / 'A'-'F' / 'a'-'f'
 * UTF1SUBSET     = %x01-27 / %x2B-5B / %x5D-7F (Everything but '\0', '*', '(', ')' and '\')
 * UTFMB          = UTF2 / UTF3 / UTF4
 * UTF0           = %x80-BF
 * UTF2           = %xC2-DF UTF0
 * UTF3           = %xE0 %xA0-BF UTF0 / %xE1-EC UTF0 UTF0 / %xED %x80-9F UTF0 / %xEE-EF UTF0 UTF0
 * UTF4           = %xF0 %x90-BF UTF0 UTF0 / %xF1-F3 UTF0 UTF0 UTF0 / %xF4 %x80-8F UTF0 UTF0
 * </pre>
 *
 * With the specific constraints (RFC 4515):
 *
 * <pre>
 *    "The <valueencoding> rule ensures that the entire filter string is a"
 *    "valid UTF-8 string and provides that the octets that represent the"
 *    "ASCII characters "*" (ASCII 0x2a), "(" (ASCII 0x28), ")" (ASCII"
 *    "0x29), "\" (ASCII 0x5c), and NUL (ASCII 0x00) are represented as a"
 *    "backslash "\" (ASCII 0x5c) followed by the two hexadecimal digits"
 *    "representing the value of the encoded octet."
 * </pre>
 *
 * The incoming String is already transformed from UTF-8 to unicode, so we must assume that the
 * grammar we have to check is the following :
 *
 * <pre>
 * assertionvalue = valueencoding
 * valueencoding  = 0*(normal / escaped)
 * normal         = unicodeSubset
 * escaped        = '\' HEX HEX
 * HEX            = '0'-'9' / 'A'-'F' / 'a'-'f'
 * unicodeSubset     = %x01-27 / %x2B-5B / %x5D-FFFF
 * </pre>
 *
 * @throws LdapInvalidAttributeValueException
 */
private static Value parseAssertionValue(SchemaManager schemaManager, String attribute, byte[] filterBytes, Position pos) throws ParseException, LdapInvalidAttributeValueException {
    byte b = Strings.byteAt(filterBytes, pos.start);
    // Create a buffer big enough to contain the value once converted
    byte[] value = new byte[filterBytes.length - pos.start];
    int current = 0;
    do {
        if (Unicode.isUnicodeSubset(b)) {
            value[current++] = b;
            pos.start++;
        } else if (Strings.isCharASCII(filterBytes, pos.start, '\\')) {
            // Maybe an escaped
            pos.start++;
            // First hex
            if (Chars.isHex(filterBytes, pos.start)) {
                pos.start++;
            } else {
                throw new ParseException(I18n.err(I18n.ERR_13308_NOT_A_VALID_ESCAPED_VALUE), pos.start);
            }
            // second hex
            if (Chars.isHex(filterBytes, pos.start)) {
                value[current++] = Hex.getHexValue(filterBytes[pos.start - 1], filterBytes[pos.start]);
                pos.start++;
            } else {
                throw new ParseException(I18n.err(I18n.ERR_13308_NOT_A_VALID_ESCAPED_VALUE), pos.start);
            }
        } else {
            // not a valid char, so let's get out
            break;
        }
        b = Strings.byteAt(filterBytes, pos.start);
    } while (b != '\0');
    if (current != 0) {
        if (schemaManager != null) {
            AttributeType attributeType = schemaManager.getAttributeType(attribute);
            if (attributeType == null) {
                byte[] bytes = new byte[current];
                System.arraycopy(value, 0, bytes, 0, current);
                return new Value(bytes);
            }
            if (attributeType.getSyntax().isHumanReadable()) {
                return new Value(attributeType, Strings.utf8ToString(value, current));
            } else {
                byte[] bytes = new byte[current];
                System.arraycopy(value, 0, bytes, 0, current);
                return new Value(attributeType, bytes);
            }
        } else {
            byte[] bytes = new byte[current];
            System.arraycopy(value, 0, bytes, 0, current);
            return new Value(bytes);
        }
    } else {
        if (schemaManager != null) {
            AttributeType attributeType = schemaManager.getAttributeType(attribute);
            if (attributeType.getEquality().getSyntax().isHumanReadable()) {
                return new Value(attributeType, (String) null);
            } else {
                return new Value(attributeType, (byte[]) null);
            }
        } else {
            return new Value((byte[]) null);
        }
    }
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) Value(org.apache.directory.api.ldap.model.entry.Value) ParseException(java.text.ParseException)

Example 50 with AttributeType

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

the class SearchParams method normalize.

/**
 * Normalize the ReturningAttributes. It reads all the String from the returningAttributesString,
 * and grab the associated AttributeType from the schema to store it into the returningAttributes
 * Set.
 *
 * @param schemaManager The schema manager
 */
public void normalize(SchemaManager schemaManager) {
    for (String returnAttribute : returningAttributesStr) {
        try {
            String id = SchemaUtils.stripOptions(returnAttribute);
            Set<String> options = SchemaUtils.getOptions(returnAttribute);
            AttributeType attributeType = schemaManager.lookupAttributeTypeRegistry(id);
            AttributeTypeOptions attrOptions = new AttributeTypeOptions(attributeType, options);
            returningAttributes.add(attrOptions);
        } catch (LdapException ne) {
            LOG.warn(I18n.msg(I18n.MSG_13500_ATTRIBUTE_NOT_IN_SCHEMA, returnAttribute));
        // Unknown attributes should be silently ignored, as RFC 2251 states
        }
    }
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) AttributeTypeOptions(org.apache.directory.api.ldap.model.schema.AttributeTypeOptions)

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