Search in sources :

Example 71 with AttributeType

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

the class Registries method clone.

/**
 * Clone the Registries. This is done in two steps :
 * - first clone the SchemaObjetc registries
 * - second restore the relation between them
 */
// False positive
@Override
public Registries clone() throws CloneNotSupportedException {
    // First clone the structure
    Registries clone = (Registries) super.clone();
    // Now, clone the oidRegistry
    clone.globalOidRegistry = globalOidRegistry.copy();
    // We have to clone every SchemaObject registries now
    clone.attributeTypeRegistry = attributeTypeRegistry.copy();
    clone.comparatorRegistry = comparatorRegistry.copy();
    clone.ditContentRuleRegistry = ditContentRuleRegistry.copy();
    clone.ditStructureRuleRegistry = ditStructureRuleRegistry.copy();
    clone.ldapSyntaxRegistry = ldapSyntaxRegistry.copy();
    clone.matchingRuleRegistry = matchingRuleRegistry.copy();
    clone.matchingRuleUseRegistry = matchingRuleUseRegistry.copy();
    clone.nameFormRegistry = nameFormRegistry.copy();
    clone.normalizerRegistry = normalizerRegistry.copy();
    clone.objectClassRegistry = objectClassRegistry.copy();
    clone.syntaxCheckerRegistry = syntaxCheckerRegistry.copy();
    // Store all the SchemaObjects into the globalOid registry
    for (AttributeType attributeType : clone.attributeTypeRegistry) {
        clone.globalOidRegistry.put(attributeType);
    }
    for (DitContentRule ditContentRule : clone.ditContentRuleRegistry) {
        clone.globalOidRegistry.put(ditContentRule);
    }
    for (DitStructureRule ditStructureRule : clone.ditStructureRuleRegistry) {
        clone.globalOidRegistry.put(ditStructureRule);
    }
    for (MatchingRule matchingRule : clone.matchingRuleRegistry) {
        clone.globalOidRegistry.put(matchingRule);
    }
    for (MatchingRuleUse matchingRuleUse : clone.matchingRuleUseRegistry) {
        clone.globalOidRegistry.put(matchingRuleUse);
    }
    for (NameForm nameForm : clone.nameFormRegistry) {
        clone.globalOidRegistry.put(nameForm);
    }
    for (ObjectClass objectClass : clone.objectClassRegistry) {
        clone.globalOidRegistry.put(objectClass);
    }
    for (LdapSyntax syntax : clone.ldapSyntaxRegistry) {
        clone.globalOidRegistry.put(syntax);
    }
    // Clone the schema list
    clone.loadedSchemas = new HashMap<>();
    for (Map.Entry<String, Set<SchemaObjectWrapper>> entry : schemaObjects.entrySet()) {
        // We don't clone the schemas
        clone.loadedSchemas.put(entry.getKey(), loadedSchemas.get(entry.getKey()));
    }
    // Clone the Using and usedBy structures
    // They will be empty
    clone.using = new HashMap<>();
    clone.usedBy = new HashMap<>();
    // Last, rebuild the using and usedBy references
    clone.buildReferences();
    // Now, check the registries. We don't care about errors
    clone.checkRefInteg();
    clone.schemaObjects = new HashMap<>();
    // SchemaObjects
    for (Map.Entry<String, Set<SchemaObjectWrapper>> entry : schemaObjects.entrySet()) {
        Set<SchemaObjectWrapper> objects = new HashSet<>();
        for (SchemaObjectWrapper schemaObjectWrapper : entry.getValue()) {
            SchemaObject original = schemaObjectWrapper.get();
            try {
                if (!(original instanceof LoadableSchemaObject)) {
                    SchemaObject copy = clone.globalOidRegistry.getSchemaObject(original.getOid());
                    SchemaObjectWrapper newWrapper = new SchemaObjectWrapper(copy);
                    objects.add(newWrapper);
                } else {
                    SchemaObjectWrapper newWrapper = new SchemaObjectWrapper(original);
                    objects.add(newWrapper);
                }
            } catch (LdapException ne) {
            // Nothing to do
            }
        }
        clone.schemaObjects.put(entry.getKey(), objects);
    }
    return clone;
}
Also used : SchemaObject(org.apache.directory.api.ldap.model.schema.SchemaObject) LoadableSchemaObject(org.apache.directory.api.ldap.model.schema.LoadableSchemaObject) ObjectClass(org.apache.directory.api.ldap.model.schema.ObjectClass) HashSet(java.util.HashSet) Set(java.util.Set) MatchingRuleUse(org.apache.directory.api.ldap.model.schema.MatchingRuleUse) NameForm(org.apache.directory.api.ldap.model.schema.NameForm) SchemaObjectWrapper(org.apache.directory.api.ldap.model.schema.SchemaObjectWrapper) LoadableSchemaObject(org.apache.directory.api.ldap.model.schema.LoadableSchemaObject) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) DitStructureRule(org.apache.directory.api.ldap.model.schema.DitStructureRule) LdapSyntax(org.apache.directory.api.ldap.model.schema.LdapSyntax) DitContentRule(org.apache.directory.api.ldap.model.schema.DitContentRule) MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule) MutableMatchingRule(org.apache.directory.api.ldap.model.schema.MutableMatchingRule) HashMap(java.util.HashMap) Map(java.util.Map) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) HashSet(java.util.HashSet)

Example 72 with AttributeType

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

the class Registries method resolveRecursive.

/**
 * Check AttributeType referential integrity
 */
private void resolveRecursive(AttributeType attributeType, Set<String> processed, List<Throwable> errors) {
    // Process the Superior, if any
    String superiorOid = attributeType.getSuperiorOid();
    AttributeType superior = null;
    if (superiorOid != null) {
        // Check if the Superior is present in the registries
        try {
            superior = attributeTypeRegistry.lookup(superiorOid);
        } catch (LdapException ne) {
            // This AT's superior has not been loaded into the Registries.
            if (!processed.contains(superiorOid)) {
                errors.add(ne);
            }
        }
        // processed yet.
        if (superior != null) {
            if (!processed.contains(superiorOid)) {
                resolveRecursive(superior, processed, errors);
                processed.add(attributeType.getOid());
            } else {
                // Not allowed : we have a cyle
                Throwable error = new LdapSchemaViolationException(ResultCodeEnum.OTHER, I18n.err(I18n.ERR_13749_AT_WITH_CYCLE, attributeType.getOid()));
                errors.add(error);
                return;
            }
        }
    }
    // Process the Syntax. If it's null, the attributeType must have
    // a Superior.
    String syntaxOid = attributeType.getSyntaxOid();
    if (syntaxOid != null) {
        // Check if the Syntax is present in the registries
        try {
            ldapSyntaxRegistry.lookup(syntaxOid);
        } catch (LdapException ne) {
            // This AT's syntax has not been loaded into the Registries.
            errors.add(ne);
        }
    } else {
        // No Syntax : get it from the AttributeType's superior
        if (superior == null) {
            // This is an error. if the AT does not have a Syntax,
            // then it must have a superior, which syntax is get from.
            Throwable error = new LdapSchemaViolationException(ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(I18n.ERR_04298, attributeType.getOid()));
            errors.add(error);
        }
    }
    // Process the EQUALITY MatchingRule. It may be null, but if it's not
    // it must have been processed before
    String equalityOid = attributeType.getEqualityOid();
    if (equalityOid != null) {
        // Check if the MatchingRule is present in the registries
        try {
            matchingRuleRegistry.lookup(equalityOid);
        } catch (LdapException ne) {
            // This AT's EQUALITY matchingRule has not been loaded into the Registries.
            errors.add(ne);
        }
    }
    // Process the ORDERING MatchingRule. It may be null, but if it's not
    // it must have been processed before
    String orderingOid = attributeType.getOrderingOid();
    if (orderingOid != null) {
        // Check if the MatchingRule is present in the registries
        try {
            matchingRuleRegistry.lookup(orderingOid);
        } catch (LdapException ne) {
            // This AT's ORDERING matchingRule has not been loaded into the Registries.
            errors.add(ne);
        }
    }
    // Process the SUBSTR MatchingRule. It may be null, but if it's not
    // it must have been processed before
    String substringOid = attributeType.getSubstringOid();
    if (substringOid != null) {
        // Check if the MatchingRule is present in the registries
        try {
            matchingRuleRegistry.lookup(substringOid);
        } catch (LdapException ne) {
            // This AT's SUBSTR matchingRule has not been loaded into the Registries.
            errors.add(ne);
        }
    }
}
Also used : LdapSchemaViolationException(org.apache.directory.api.ldap.model.exception.LdapSchemaViolationException) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 73 with AttributeType

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

the class AttributeTypeHelper method checkUsage.

/**
 * Check the constraints for the Usage field.
 */
private static void checkUsage(AttributeType attributeType, List<Throwable> errors) {
    AttributeType superior = attributeType.getSuperior();
    // Check that the AT usage is the same that its superior
    if ((superior != null) && (attributeType.getUsage() != superior.getUsage())) {
        // This is an error
        String msg = I18n.err(I18n.ERR_13762_AT_MUST_HAVE_SUPERIOR_USAGE, attributeType.getName());
        LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_MUST_HAVE_SAME_USAGE_THAN_SUPERIOR, msg);
        ldapSchemaException.setSourceObject(attributeType);
        errors.add(ldapSchemaException);
        LOG.info(msg);
        return;
    }
    // Now, check that the AttributeType's USAGE does not conflict
    if (!attributeType.isUserModifiable() && (attributeType.getUsage() == UsageEnum.USER_APPLICATIONS)) {
        // Cannot have a not user modifiable AT which is not an operational AT
        String msg = I18n.err(I18n.ERR_13763_AT_MUST_BE_USER_MODIFIABLE, attributeType.getName());
        LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_USER_APPLICATIONS_USAGE_MUST_BE_USER_MODIFIABLE, msg);
        ldapSchemaException.setSourceObject(attributeType);
        errors.add(ldapSchemaException);
        LOG.info(msg);
    }
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException)

Example 74 with AttributeType

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

the class AttributeTypeHelper method buildSubstring.

/**
 * Build the SUBSTR MR reference for an AttributeType
 */
private static void buildSubstring(MutableAttributeType attributeType, List<Throwable> errors, Registries registries) {
    String substringOid = attributeType.getSubstringOid();
    // The Substring MR. It can be null
    if (substringOid != null) {
        MatchingRule currentSubstring = null;
        try {
            currentSubstring = registries.getMatchingRuleRegistry().lookup(substringOid);
        } catch (LdapException ne) {
            // Not allowed.
            String msg = I18n.err(I18n.ERR_13760_CANNOT_FIND_SUBSTR_MR_OBJECT, substringOid, attributeType.getName());
            LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_NONEXISTENT_SUBSTRING_MATCHING_RULE, msg, ne);
            ldapSchemaException.setSourceObject(attributeType);
            ldapSchemaException.setRelatedId(substringOid);
            errors.add(ldapSchemaException);
            LOG.info(msg);
            return;
        }
        if (currentSubstring != null) {
            attributeType.setSubstring(currentSubstring);
        } else {
            // Not allowed.
            String msg = I18n.err(I18n.ERR_13761_CANNOT_FIND_SUBSTR_MR_INSTANCE, substringOid, attributeType.getName());
            LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_NONEXISTENT_SUBSTRING_MATCHING_RULE, msg);
            ldapSchemaException.setSourceObject(attributeType);
            ldapSchemaException.setRelatedId(substringOid);
            errors.add(ldapSchemaException);
            LOG.info(msg);
            return;
        }
    } else {
        AttributeType superior = attributeType.getSuperior();
        // If the AT has a superior, take its Substring MR if any
        if ((superior != null) && (superior.getSubstring() != null)) {
            attributeType.setSubstring(superior.getSubstring());
        }
    }
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException) MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 75 with AttributeType

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

the class AttributeTypeHelper method checkCollective.

/**
 * Check the constraints for the Collective field.
 */
private static void checkCollective(MutableAttributeType attributeType, List<Throwable> errors) {
    AttributeType superior = attributeType.getSuperior();
    if ((superior != null) && superior.isCollective()) {
        // An AttributeType will be collective if its superior is collective
        attributeType.setCollective(true);
    }
    if (attributeType.isCollective() && (attributeType.getUsage() != UsageEnum.USER_APPLICATIONS)) {
        // An AttributeType which is collective must be a USER attributeType
        String msg = I18n.err(I18n.ERR_13764_AT_COLLECTIVE_SHOULD_BE_USER_APP, attributeType.getName());
        LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_COLLECTIVE_MUST_HAVE_USER_APPLICATIONS_USAGE, msg);
        ldapSchemaException.setSourceObject(attributeType);
        errors.add(ldapSchemaException);
        LOG.info(msg);
    }
    if (attributeType.isCollective() && attributeType.isSingleValued()) {
        // A collective attribute must be multi-valued
        String msg = I18n.err(I18n.ERR_13777_COLLECTIVE_NOT_MULTI_VALUED, attributeType.getName());
        LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_COLLECTIVE_CANNOT_BE_SINGLE_VALUED, msg);
        ldapSchemaException.setSourceObject(attributeType);
        errors.add(ldapSchemaException);
        LOG.info(msg);
    }
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException)

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