Search in sources :

Example 6 with SyntaxChecker

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

the class DefaultSchemaManagerTest method testSyntaxCheckersDoNotThrowException.

/**
 * Verifies that all syntax checkers are properly initialized
 * when loading the default schema.
 */
@Test
public void testSyntaxCheckersDoNotThrowException() throws Exception {
    DefaultSchemaManager schemaManager = new DefaultSchemaManager();
    schemaManager.loadAllEnabled();
    for (SyntaxChecker sc : schemaManager.getSyntaxCheckerRegistry()) {
        sc.isValidSyntax("foo");
    }
}
Also used : SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) Test(org.junit.Test)

Example 7 with SyntaxChecker

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

the class TestEntryUtils method getCaseIgnoringAttributeNoNumbersType.

/* no protection*/
static AttributeType getCaseIgnoringAttributeNoNumbersType() {
    MutableAttributeType attributeType = new MutableAttributeType("1.1.3.1");
    LdapSyntax syntax = new LdapSyntax("1.1.1.1", "", true);
    syntax.setSyntaxChecker(new SyntaxChecker("1.1.2.1") {

        /**
         * The mandatory serialVersionUID field
         */
        public static final long serialVersionUID = 1L;

        public boolean isValidSyntax(Object value) {
            if (!(value instanceof String)) {
                return false;
            }
            String strval = (String) value;
            for (char c : strval.toCharArray()) {
                if (Character.isDigit(c)) {
                    return false;
                }
            }
            return true;
        }
    });
    MutableMatchingRule matchingRule = new MutableMatchingRule("1.1.2.1");
    matchingRule.setSyntax(syntax);
    matchingRule.setLdapComparator(new LdapComparator<String>(matchingRule.getOid()) {

        /**
         * The mandatory serialVersionUID field
         */
        public static final long serialVersionUID = 1L;

        public int compare(String o1, String o2) {
            return (o1 == null ? (o2 == null ? 0 : -1) : (o2 == null ? 1 : o1.compareTo(o2)));
        }
    });
    Normalizer normalizer = new Normalizer("1.1.1") {

        /**
         * The mandatory serialVersionUID field
         */
        public static final long serialVersionUID = 1L;

        public String normalize(String value) throws LdapException {
            return normalize(value, AssertionType.ATTRIBUTE_VALUE);
        }

        public String normalize(String value, PrepareString.AssertionType assertionType) throws LdapException {
            return Strings.toLowerCaseAscii(value);
        }
    };
    matchingRule.setNormalizer(normalizer);
    attributeType.setEquality(matchingRule);
    attributeType.setSyntax(syntax);
    return attributeType;
}
Also used : MutableMatchingRule(org.apache.directory.api.ldap.model.schema.MutableMatchingRule) SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) DeepTrimToLowerNormalizer(org.apache.directory.api.ldap.model.schema.normalizers.DeepTrimToLowerNormalizer) Normalizer(org.apache.directory.api.ldap.model.schema.Normalizer) LdapSyntax(org.apache.directory.api.ldap.model.schema.LdapSyntax) PrepareString(org.apache.directory.api.ldap.model.schema.PrepareString) AssertionType(org.apache.directory.api.ldap.model.schema.PrepareString.AssertionType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType)

Example 8 with SyntaxChecker

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

the class EntryUtils method getIA5StringAttributeType.

/* no protection*/
static AttributeType getIA5StringAttributeType() {
    MutableAttributeType attributeType = new MutableAttributeType("1.1");
    attributeType.addName("1.1");
    LdapSyntax syntax = new LdapSyntax("1.1.1", "", true);
    syntax.setSyntaxChecker(new SyntaxChecker("1.1.2") {

        public static final long serialVersionUID = 1L;

        public boolean isValidSyntax(Object value) {
            String strValue = Strings.deepTrim((String) value);
            return (strValue == null) || (strValue.length() < 7);
        }
    });
    MutableMatchingRule matchingRule = new MutableMatchingRule("1.1.2");
    matchingRule.setSyntax(syntax);
    matchingRule.setLdapComparator(new LdapComparator<String>(matchingRule.getOid()) {

        public static final long serialVersionUID = 1L;

        public int compare(String o1, String o2) {
            return ((o1 == null) ? (o2 == null ? 0 : -1) : (o2 == null ? 1 : o1.compareTo(o2)));
        }
    });
    matchingRule.setNormalizer(new DeepTrimToLowerNormalizer(matchingRule.getOid()));
    attributeType.setEquality(matchingRule);
    attributeType.setSyntax(syntax);
    return attributeType;
}
Also used : MutableMatchingRule(org.apache.directory.api.ldap.model.schema.MutableMatchingRule) SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) DeepTrimToLowerNormalizer(org.apache.directory.api.ldap.model.schema.normalizers.DeepTrimToLowerNormalizer) LdapSyntax(org.apache.directory.api.ldap.model.schema.LdapSyntax) PrepareString(org.apache.directory.api.ldap.model.schema.PrepareString) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType)

Example 9 with SyntaxChecker

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

the class Registries method unregister.

/**
 * Unregister a SchemaObject from the registries
 *
 * @param schemaObject The SchemaObject we want to deregister
 * @throws LdapException If the removal failed
 */
private SchemaObject unregister(List<Throwable> errors, SchemaObject schemaObject) throws LdapException {
    LOG.debug("Unregistering {}:{}", schemaObject.getObjectType(), schemaObject.getOid());
    // Check that the SchemaObject is present in the registries
    if (!(schemaObject instanceof LoadableSchemaObject) && !globalOidRegistry.contains(schemaObject.getOid())) {
        String msg = I18n.err(I18n.ERR_13751_UNREGISTERING_FAILED_NOT_PRESENT, schemaObject.getObjectType(), schemaObject.getOid());
        LOG.error(msg);
        throw new LdapUnwillingToPerformException(ResultCodeEnum.UNWILLING_TO_PERFORM, msg);
    }
    SchemaObject unregistered;
    // First call the specific registry's register method
    switch(schemaObject.getObjectType()) {
        case ATTRIBUTE_TYPE:
            unregistered = attributeTypeRegistry.unregister((AttributeType) schemaObject);
            break;
        case COMPARATOR:
            unregistered = comparatorRegistry.unregister((LdapComparator<?>) schemaObject);
            break;
        case DIT_CONTENT_RULE:
            unregistered = ditContentRuleRegistry.unregister((DitContentRule) schemaObject);
            break;
        case DIT_STRUCTURE_RULE:
            unregistered = ditStructureRuleRegistry.unregister((DitStructureRule) schemaObject);
            break;
        case LDAP_SYNTAX:
            unregistered = ldapSyntaxRegistry.unregister((LdapSyntax) schemaObject);
            break;
        case MATCHING_RULE:
            unregistered = matchingRuleRegistry.unregister((MatchingRule) schemaObject);
            break;
        case MATCHING_RULE_USE:
            unregistered = matchingRuleUseRegistry.unregister((MatchingRuleUse) schemaObject);
            break;
        case NAME_FORM:
            unregistered = nameFormRegistry.unregister((NameForm) schemaObject);
            break;
        case NORMALIZER:
            unregistered = normalizerRegistry.unregister((Normalizer) schemaObject);
            break;
        case OBJECT_CLASS:
            unregistered = objectClassRegistry.unregister((ObjectClass) schemaObject);
            break;
        case SYNTAX_CHECKER:
            unregistered = syntaxCheckerRegistry.unregister((SyntaxChecker) schemaObject);
            break;
        default:
            throw new IllegalArgumentException(I18n.err(I18n.ERR_13718_UNEXPECTED_SCHEMA_OBJECT_TYPE, schemaObject.getObjectType()));
    }
    return unregistered;
}
Also used : LdapComparator(org.apache.directory.api.ldap.model.schema.LdapComparator) SchemaObject(org.apache.directory.api.ldap.model.schema.SchemaObject) LoadableSchemaObject(org.apache.directory.api.ldap.model.schema.LoadableSchemaObject) SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) ObjectClass(org.apache.directory.api.ldap.model.schema.ObjectClass) MatchingRuleUse(org.apache.directory.api.ldap.model.schema.MatchingRuleUse) LdapUnwillingToPerformException(org.apache.directory.api.ldap.model.exception.LdapUnwillingToPerformException) NameForm(org.apache.directory.api.ldap.model.schema.NameForm) Normalizer(org.apache.directory.api.ldap.model.schema.Normalizer) 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)

Example 10 with SyntaxChecker

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

the class LdapSyntaxHelper method addToRegistries.

/**
 * Inject the LdapSyntax into the registries, updating the references to
 * other SchemaObject
 *
 * @param ldapSyntax The LdapSyntax to add to the Registries
 * @param errors The errors we got while adding the LdapSyntax to the Registries
 * @param registries The Registries
 * @throws LdapException If the addition failed
 */
public static void addToRegistries(LdapSyntax ldapSyntax, List<Throwable> errors, Registries registries) throws LdapException {
    if (registries != null) {
        try {
            ldapSyntax.unlock();
            SyntaxChecker syntaxChecker = null;
            try {
                // Gets the associated SyntaxChecker
                syntaxChecker = registries.getSyntaxCheckerRegistry().lookup(ldapSyntax.getOid());
            } catch (LdapException ne) {
                // No SyntaxChecker ? Associate the Syntax to a catch all SyntaxChecker
                syntaxChecker = OctetStringSyntaxChecker.builder().setOid(ldapSyntax.getOid()).build();
            }
            // S -> SC
            if (syntaxChecker != null) {
                registries.addReference(ldapSyntax, syntaxChecker);
                ldapSyntax.setSyntaxChecker(syntaxChecker);
            }
        } finally {
            ldapSyntax.lock();
        }
    }
}
Also used : SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) OctetStringSyntaxChecker(org.apache.directory.api.ldap.model.schema.syntaxCheckers.OctetStringSyntaxChecker) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Aggregations

SyntaxChecker (org.apache.directory.api.ldap.model.schema.SyntaxChecker)24 LdapSyntax (org.apache.directory.api.ldap.model.schema.LdapSyntax)9 Test (org.junit.Test)9 MutableAttributeType (org.apache.directory.api.ldap.model.schema.MutableAttributeType)7 MutableMatchingRule (org.apache.directory.api.ldap.model.schema.MutableMatchingRule)7 SchemaManager (org.apache.directory.api.ldap.model.schema.SchemaManager)7 DefaultSchemaManager (org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager)7 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)6 PrepareString (org.apache.directory.api.ldap.model.schema.PrepareString)5 DeepTrimToLowerNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.DeepTrimToLowerNormalizer)5 OctetStringSyntaxChecker (org.apache.directory.api.ldap.model.schema.syntaxCheckers.OctetStringSyntaxChecker)5 Normalizer (org.apache.directory.api.ldap.model.schema.Normalizer)4 BooleanSyntaxChecker (org.apache.directory.api.ldap.model.schema.syntaxCheckers.BooleanSyntaxChecker)4 LdapProtocolErrorException (org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException)3 LdapSchemaException (org.apache.directory.api.ldap.model.exception.LdapSchemaException)3 LdapUnwillingToPerformException (org.apache.directory.api.ldap.model.exception.LdapUnwillingToPerformException)3 NoOpNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer)3 RegexSyntaxChecker (org.apache.directory.api.ldap.model.schema.syntaxCheckers.RegexSyntaxChecker)3 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2