Search in sources :

Example 11 with LdapSyntax

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

the class Registries method check.

/**
 * Check the registries for invalid relations. This check stops at the first error.
 *
 * @return true if the Registries is consistent, false otherwise
 */
public boolean check() {
    // Check the Syntaxes : check for a SyntaxChecker
    LOG.debug("Checking Syntaxes");
    for (LdapSyntax syntax : ldapSyntaxRegistry) {
        // Check that each Syntax has a SyntaxChecker
        if (syntax.getSyntaxChecker() == null) {
            LOG.debug("The Syntax {} has no SyntaxChecker", syntax);
            return false;
        }
        if (!syntaxCheckerRegistry.contains(syntax.getSyntaxChecker().getOid())) {
            LOG.debug("Cannot find the SyntaxChecker {} for the Syntax {}", syntax.getSyntaxChecker().getOid(), syntax);
            return false;
        }
        // Check the references : Syntax -> SyntaxChecker and SyntaxChecker -> Syntax
        if (!checkReferences(syntax, syntax.getSyntaxChecker(), "SyntaxChecker")) {
            return false;
        }
    }
    // Check the MatchingRules : check for a Normalizer, a Comparator and a Syntax
    LOG.debug("Checking MatchingRules...");
    for (MatchingRule matchingRule : matchingRuleRegistry) {
        // Check that each MatchingRule has a Normalizer
        if (matchingRule.getNormalizer() == null) {
            LOG.debug("The MatchingRule {} has no Normalizer", matchingRule);
            return false;
        }
        // Check that each MatchingRule has a Normalizer
        if (!normalizerRegistry.contains(matchingRule.getNormalizer().getOid())) {
            LOG.debug("Cannot find the Normalizer {} for the MatchingRule {}", matchingRule.getNormalizer().getOid(), matchingRule);
            return false;
        }
        // Check that each MatchingRule has a Comparator
        if (matchingRule.getLdapComparator() == null) {
            LOG.debug("The MatchingRule {} has no Comparator", matchingRule);
            return false;
        }
        if (!comparatorRegistry.contains(matchingRule.getLdapComparator().getOid())) {
            LOG.debug("Cannot find the Comparator {} for the MatchingRule {}", matchingRule.getLdapComparator().getOid(), matchingRule);
            return false;
        }
        // Check that each MatchingRule has a Syntax
        if (matchingRule.getSyntax() == null) {
            LOG.debug("The MatchingRule {} has no Syntax", matchingRule);
            return false;
        }
        if (!ldapSyntaxRegistry.contains(matchingRule.getSyntax().getOid())) {
            LOG.debug("Cannot find the Syntax {} for the MatchingRule {}", matchingRule.getSyntax().getOid(), matchingRule);
            return false;
        }
        // Check the references : MR -> S and S -> MR
        if (!checkReferences(matchingRule, matchingRule.getSyntax(), "Syntax")) {
            return false;
        }
        // Check the references : MR -> N
        if (!checkReferences(matchingRule, matchingRule.getNormalizer(), "Normalizer")) {
            return false;
        }
        // Check the references : MR -> C and C -> MR
        if (!checkReferences(matchingRule, matchingRule.getLdapComparator(), "Comparator")) {
            return false;
        }
    }
    // Check the ObjectClasses : check for MAY, MUST, SUPERIORS
    LOG.debug("Checking ObjectClasses...");
    for (ObjectClass objectClass : objectClassRegistry) {
        // Check that each ObjectClass has all the MAY AttributeTypes
        if (objectClass.getMayAttributeTypes() != null) {
            for (AttributeType may : objectClass.getMayAttributeTypes()) {
                if (!attributeTypeRegistry.contains(may.getOid())) {
                    LOG.debug("Cannot find the AttributeType {} for the ObjectClass {} MAY", may, objectClass);
                    return false;
                }
                // Check the references : OC -> AT  and AT -> OC (MAY)
                if (!checkReferences(objectClass, may, "AttributeType")) {
                    return false;
                }
            }
        }
        // Check that each ObjectClass has all the MUST AttributeTypes
        if (objectClass.getMustAttributeTypes() != null) {
            for (AttributeType must : objectClass.getMustAttributeTypes()) {
                if (!attributeTypeRegistry.contains(must.getOid())) {
                    LOG.debug("Cannot find the AttributeType {} for the ObjectClass {} MUST", must, objectClass);
                    return false;
                }
                // Check the references : OC -> AT  and AT -> OC (MUST)
                if (!checkReferences(objectClass, must, "AttributeType")) {
                    return false;
                }
            }
        }
        // Check that each ObjectClass has all the SUPERIORS ObjectClasses
        if (objectClass.getSuperiors() != null) {
            for (ObjectClass superior : objectClass.getSuperiors()) {
                if (!objectClassRegistry.contains(objectClass.getOid())) {
                    LOG.debug("Cannot find the ObjectClass {} for the ObjectClass {} SUPERIORS", superior, objectClass);
                    return false;
                }
                // Check the references : OC -> OC  and OC -> OC (SUPERIORS)
                if (!checkReferences(objectClass, superior, "ObjectClass")) {
                    return false;
                }
            }
        }
    }
    // Check the AttributeTypes : check for MatchingRules, Syntaxes
    LOG.debug("Checking AttributeTypes...");
    for (AttributeType attributeType : attributeTypeRegistry) {
        // Check that each AttributeType has a SYNTAX
        if (attributeType.getSyntax() == null) {
            LOG.debug("The AttributeType {} has no Syntax", attributeType);
            return false;
        }
        if (!ldapSyntaxRegistry.contains(attributeType.getSyntax().getOid())) {
            LOG.debug("Cannot find the Syntax {} for the AttributeType {}", attributeType.getSyntax().getOid(), attributeType);
            return false;
        }
        // Check the references for AT -> S and S -> AT
        if (!checkReferences(attributeType, attributeType.getSyntax(), "AttributeType")) {
            return false;
        }
        // Check the EQUALITY MatchingRule
        if (attributeType.getEquality() != null) {
            if (!matchingRuleRegistry.contains(attributeType.getEquality().getOid())) {
                LOG.debug("Cannot find the MatchingRule {} for the AttributeType {}", attributeType.getEquality().getOid(), attributeType);
                return false;
            }
            // Check the references for AT -> MR and MR -> AT
            if (!checkReferences(attributeType, attributeType.getEquality(), "AttributeType")) {
                return false;
            }
        }
        // Check the ORDERING MatchingRule
        if (attributeType.getOrdering() != null) {
            if (!matchingRuleRegistry.contains(attributeType.getOrdering().getOid())) {
                LOG.debug("Cannot find the MatchingRule {} for the AttributeType {}", attributeType.getOrdering().getOid(), attributeType);
                return false;
            }
            // Check the references for AT -> MR and MR -> AT
            if (!checkReferences(attributeType, attributeType.getOrdering(), "AttributeType")) {
                return false;
            }
        }
        // Check the SUBSTR MatchingRule
        if (attributeType.getSubstring() != null) {
            if (!matchingRuleRegistry.contains(attributeType.getSubstring().getOid())) {
                LOG.debug("Cannot find the MatchingRule {} for the AttributeType {}", attributeType.getSubstring().getOid(), attributeType);
                return false;
            }
            // Check the references for AT -> MR and MR -> AT
            if (!checkReferences(attributeType, attributeType.getSubstring(), "AttributeType")) {
                return false;
            }
        }
        // Check the SUP
        if (attributeType.getSuperior() != null) {
            AttributeType superior = attributeType.getSuperior();
            if (!attributeTypeRegistry.contains(superior.getOid())) {
                LOG.debug("Cannot find the AttributeType {} for the AttributeType {} SUPERIOR", superior, attributeType);
                return false;
            }
            // Check the references : AT -> AT  and AT -> AT (SUPERIOR)
            if (!checkReferences(attributeType, superior, "AttributeType")) {
                return false;
            }
        }
    }
    return true;
}
Also used : ObjectClass(org.apache.directory.api.ldap.model.schema.ObjectClass) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) LdapSyntax(org.apache.directory.api.ldap.model.schema.LdapSyntax) MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule) MutableMatchingRule(org.apache.directory.api.ldap.model.schema.MutableMatchingRule)

Example 12 with LdapSyntax

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

the class AttributeTypeHelper method buildSyntax.

/**
 * Build the SYNTAX reference for an AttributeType
 */
private static void buildSyntax(MutableAttributeType attributeType, List<Throwable> errors, Registries registries) {
    String syntaxOid = attributeType.getSyntaxOid();
    if (syntaxOid != null) {
        LdapSyntax currentSyntax = null;
        try {
            currentSyntax = registries.getLdapSyntaxRegistry().lookup(syntaxOid);
        } catch (LdapException ne) {
            // Not allowed.
            String msg = I18n.err(I18n.ERR_13754_CANNOT_FIND_SYNTAX, syntaxOid, attributeType.getName());
            LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_NONEXISTENT_SYNTAX, msg, ne);
            ldapSchemaException.setSourceObject(attributeType);
            ldapSchemaException.setRelatedId(syntaxOid);
            errors.add(ldapSchemaException);
            LOG.info(msg);
            return;
        }
        if (currentSyntax != null) {
            // Update the Syntax reference
            attributeType.setSyntax(currentSyntax);
        } else {
            // Not allowed.
            String msg = I18n.err(I18n.ERR_13754_CANNOT_FIND_SYNTAX, syntaxOid, attributeType.getName());
            LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_NONEXISTENT_SYNTAX, msg);
            ldapSchemaException.setSourceObject(attributeType);
            ldapSchemaException.setRelatedId(syntaxOid);
            errors.add(ldapSchemaException);
            LOG.info(msg);
            return;
        }
    } else {
        // We inherit from the superior's syntax, if any
        if (attributeType.getSuperior() != null) {
            if (attributeType.getSuperior().getSyntax() != null) {
                attributeType.setSyntax(attributeType.getSuperior().getSyntax());
            } else {
                String msg = I18n.err(I18n.ERR_13754_CANNOT_FIND_SYNTAX, syntaxOid, attributeType.getName());
                LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_NONEXISTENT_SYNTAX, msg);
                ldapSchemaException.setSourceObject(attributeType);
                ldapSchemaException.setRelatedId(syntaxOid);
                errors.add(ldapSchemaException);
                LOG.info(msg);
                return;
            }
        } else {
            // Not allowed.
            String msg = I18n.err(I18n.ERR_13755_AT_MUST_HAVE_A_SYNTAX_OID, attributeType.getName());
            LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_SYNTAX_OR_SUPERIOR_REQUIRED, msg);
            ldapSchemaException.setSourceObject(attributeType);
            errors.add(ldapSchemaException);
            LOG.info(msg);
            return;
        }
    }
}
Also used : LdapSyntax(org.apache.directory.api.ldap.model.schema.LdapSyntax) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 13 with LdapSyntax

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

the class MatchingRuleHelper method addToRegistries.

/**
 * Inject the MatchingRule into the Registries, updating the references to
 * other SchemaObject
 *
 * @param matchingRule The MatchingRule to add to the Registries
 * @param errors The errors we got while adding the MatchingRule to the registries
 * @param registries The Registries
 * @throws LdapException If the addition failed
 */
@SuppressWarnings("rawtypes")
public static void addToRegistries(MutableMatchingRule matchingRule, List<Throwable> errors, Registries registries) throws LdapException {
    if (registries != null) {
        try {
            matchingRule.unlock();
            LdapComparator<?> ldapComparator = null;
            Normalizer normalizer = null;
            LdapSyntax ldapSyntax = null;
            try {
                // Gets the associated Comparator
                ldapComparator = registries.getComparatorRegistry().lookup(matchingRule.getOid());
            } catch (LdapException ne) {
                // Default to a catch all comparator
                ldapComparator = new ComparableComparator(matchingRule.getOid());
            }
            try {
                // Gets the associated Normalizer
                normalizer = registries.getNormalizerRegistry().lookup(matchingRule.getOid());
            } catch (LdapException ne) {
                // Default to the NoOp normalizer
                normalizer = new NoOpNormalizer(matchingRule.getOid());
            }
            try {
                // Get the associated LdapSyntax
                ldapSyntax = registries.getLdapSyntaxRegistry().lookup(matchingRule.getSyntaxOid());
            } catch (LdapException ne) {
                // The Syntax is a mandatory element, it must exist.
                String msg = I18n.err(I18n.ERR_13765_MR_MUST_REFER_EXISTING_SYNTAX);
                LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.MR_NONEXISTENT_SYNTAX, msg, ne);
                ldapSchemaException.setSourceObject(matchingRule);
                ldapSchemaException.setRelatedId(matchingRule.getSyntaxOid());
                errors.add(ldapSchemaException);
                LOG.info(msg);
            }
            /**
             * Add the MR references (using and usedBy) :
             * MR -> C
             * MR -> N
             * MR -> S
             */
            if (ldapComparator != null) {
                registries.addReference(matchingRule, ldapComparator);
                matchingRule.setLdapComparator(ldapComparator);
            }
            if (normalizer != null) {
                registries.addReference(matchingRule, normalizer);
                matchingRule.setNormalizer(normalizer);
            }
            if (ldapSyntax != null) {
                registries.addReference(matchingRule, ldapSyntax);
                matchingRule.setSyntax(ldapSyntax);
            }
        } finally {
            matchingRule.lock();
        }
    }
}
Also used : NoOpNormalizer(org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer) NoOpNormalizer(org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer) Normalizer(org.apache.directory.api.ldap.model.schema.Normalizer) LdapSyntax(org.apache.directory.api.ldap.model.schema.LdapSyntax) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException) ComparableComparator(org.apache.directory.api.ldap.model.schema.comparators.ComparableComparator) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 14 with LdapSyntax

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

the class SchemaBinaryAttributeDetector method isBinary.

/**
 * {@inheritDoc}
 */
@Override
public boolean isBinary(String attributeId) {
    String attrId = Strings.toLowerCaseAscii(attributeId);
    if (attrId.endsWith(";binary")) {
        return true;
    }
    if (schemaManager != null) {
        AttributeType attributeType = schemaManager.getAttributeType(attrId);
        if (attributeType == null) {
            return false;
        }
        LdapSyntax ldapSyntax = attributeType.getSyntax();
        return (ldapSyntax != null) && !ldapSyntax.isHumanReadable();
    }
    return false;
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) LdapSyntax(org.apache.directory.api.ldap.model.schema.LdapSyntax)

Example 15 with LdapSyntax

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

the class DefaultAttribute method isValid.

/**
 * {@inheritDoc}
 */
@Override
public boolean isValid(AttributeType attributeType) throws LdapInvalidAttributeValueException {
    LdapSyntax syntax = attributeType.getSyntax();
    if (syntax == null) {
        return false;
    }
    SyntaxChecker syntaxChecker = syntax.getSyntaxChecker();
    if (syntaxChecker == null) {
        return false;
    }
    // Check that we can have no value for this attributeType
    if (values.isEmpty()) {
        return syntaxChecker.isValidSyntax(null);
    }
    // Check that we can't have more than one value if the AT is single-value
    if ((attributeType.isSingleValued()) && (values.size() > 1)) {
        return false;
    }
    // Now check the values
    for (Value value : values) {
        try {
            if (!value.isValid(syntaxChecker)) {
                return false;
            }
        } catch (LdapException le) {
            return false;
        }
    }
    return true;
}
Also used : SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) LdapSyntax(org.apache.directory.api.ldap.model.schema.LdapSyntax) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Aggregations

LdapSyntax (org.apache.directory.api.ldap.model.schema.LdapSyntax)34 Test (org.junit.Test)12 MutableAttributeType (org.apache.directory.api.ldap.model.schema.MutableAttributeType)10 MutableMatchingRule (org.apache.directory.api.ldap.model.schema.MutableMatchingRule)9 SchemaManager (org.apache.directory.api.ldap.model.schema.SchemaManager)9 SyntaxChecker (org.apache.directory.api.ldap.model.schema.SyntaxChecker)9 DefaultSchemaManager (org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager)9 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)7 Normalizer (org.apache.directory.api.ldap.model.schema.Normalizer)5 PrepareString (org.apache.directory.api.ldap.model.schema.PrepareString)5 DeepTrimToLowerNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.DeepTrimToLowerNormalizer)5 LdapProtocolErrorException (org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException)4 AttributeType (org.apache.directory.api.ldap.model.schema.AttributeType)4 MatchingRule (org.apache.directory.api.ldap.model.schema.MatchingRule)4 SchemaObject (org.apache.directory.api.ldap.model.schema.SchemaObject)4 LdapSchemaException (org.apache.directory.api.ldap.model.exception.LdapSchemaException)3 NoOpNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer)3 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)2 Entry (org.apache.directory.api.ldap.model.entry.Entry)2 Value (org.apache.directory.api.ldap.model.entry.Value)2