Search in sources :

Example 11 with MutableMatchingRule

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

the class SchemaManagerAddTest method testAddMatchingRuleNotExistingSyntax.

/**
 * Try to inject a new MatchingRule with a not existing Syntax
 */
@Test
public void testAddMatchingRuleNotExistingSyntax() throws Exception {
    SchemaManager schemaManager = loadSystem();
    int mrrSize = schemaManager.getMatchingRuleRegistry().size();
    int goidSize = schemaManager.getGlobalOidRegistry().size();
    MutableMatchingRule matchingRule = new MutableMatchingRule("1.1.0");
    matchingRule.setNames("Test");
    matchingRule.setSyntaxOid("1.1.1");
    // It should fail
    assertFalse(schemaManager.add(matchingRule));
    List<Throwable> errors = schemaManager.getErrors();
    assertEquals(1, errors.size());
    Throwable error = errors.get(0);
    assertTrue(error instanceof LdapSchemaException);
    assertEquals(mrrSize, schemaManager.getMatchingRuleRegistry().size());
    assertEquals(goidSize, schemaManager.getGlobalOidRegistry().size());
}
Also used : MutableMatchingRule(org.apache.directory.api.ldap.model.schema.MutableMatchingRule) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException) SchemaManager(org.apache.directory.api.ldap.model.schema.SchemaManager) DefaultSchemaManager(org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager) Test(org.junit.Test)

Example 12 with MutableMatchingRule

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

the class SchemaEntityFactory method getMatchingRule.

/**
 * {@inheritDoc}
 * @throws LdapInvalidAttributeValueException If the MatchingRule does not exist
 * @throws LdapUnwillingToPerformException If the schema is not loaded
 */
@Override
public MatchingRule getMatchingRule(SchemaManager schemaManager, Entry entry, Registries targetRegistries, String schemaName) throws LdapUnwillingToPerformException, LdapInvalidAttributeValueException {
    checkEntry(entry, SchemaConstants.MATCHING_RULE);
    // The MatchingRule OID
    String oid = getOid(entry, SchemaConstants.MATCHING_RULE, schemaManager.isStrict());
    // Get the schema
    if (!schemaManager.isSchemaLoaded(schemaName)) {
        // The schema is not loaded. We can't create the requested MatchingRule
        String msg = I18n.err(I18n.ERR_16028_CANNOT_ADD_MR, entry.getDn().getName(), schemaName);
        LOG.warn(msg);
        throw new LdapUnwillingToPerformException(ResultCodeEnum.UNWILLING_TO_PERFORM, msg);
    }
    Schema schema = getSchema(schemaName, targetRegistries);
    if (schema == null) {
        // The schema is disabled. We still have to update the backend
        String msg = I18n.err(I18n.ERR_16029_CANNOT_ADD_MR_IN_REGISTRY, entry.getDn().getName(), schemaName);
        LOG.info(msg);
        schema = schemaManager.getLoadedSchema(schemaName);
    }
    MutableMatchingRule matchingRule = new MutableMatchingRule(oid);
    // The syntax field
    Attribute mSyntax = entry.get(MetaSchemaConstants.M_SYNTAX_AT);
    if (mSyntax != null) {
        matchingRule.setSyntaxOid(mSyntax.getString());
    }
    // The normalizer and comparator fields will be updated when we will
    // apply the registry
    // Common properties
    setSchemaObjectProperties(matchingRule, entry, schema);
    return matchingRule;
}
Also used : MutableMatchingRule(org.apache.directory.api.ldap.model.schema.MutableMatchingRule) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) LdapUnwillingToPerformException(org.apache.directory.api.ldap.model.exception.LdapUnwillingToPerformException) DefaultSchema(org.apache.directory.api.ldap.model.schema.registries.DefaultSchema) Schema(org.apache.directory.api.ldap.model.schema.registries.Schema)

Example 13 with MutableMatchingRule

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

the class EntryUtils 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") {

        public static final long serialVersionUID = 1L;

        public boolean isValidSyntax(Object value) {
            if (value == null) {
                return true;
            }
            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()) {

        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") {

        public static final long serialVersionUID = 1L;

        public String normalize(String value) throws LdapException {
            return normalize(value, PrepareString.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) NoOpNormalizer(org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer) 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) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType)

Example 14 with MutableMatchingRule

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

the class EntryUtils method getBytesAttributeType.

/* No protection */
static AttributeType getBytesAttributeType() {
    MutableAttributeType attributeType = new MutableAttributeType("1.2");
    LdapSyntax syntax = new LdapSyntax("1.2.1", "", false);
    syntax.setSyntaxChecker(new SyntaxChecker("1.2.1") {

        public static final long serialVersionUID = 1L;

        public boolean isValidSyntax(Object value) {
            return (value == null) || (((byte[]) value).length < 5);
        }
    });
    MutableMatchingRule matchingRule = new MutableMatchingRule("1.2.2");
    matchingRule.setSyntax(syntax);
    matchingRule.setLdapComparator(new ByteArrayComparator("1.2.2"));
    matchingRule.setNormalizer(new NoOpNormalizer("1.1.1"));
    attributeType.setEquality(matchingRule);
    attributeType.setSyntax(syntax);
    return attributeType;
}
Also used : MutableMatchingRule(org.apache.directory.api.ldap.model.schema.MutableMatchingRule) NoOpNormalizer(org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer) SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) LdapSyntax(org.apache.directory.api.ldap.model.schema.LdapSyntax) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) ByteArrayComparator(org.apache.directory.api.ldap.model.schema.comparators.ByteArrayComparator)

Aggregations

MutableMatchingRule (org.apache.directory.api.ldap.model.schema.MutableMatchingRule)14 LdapSyntax (org.apache.directory.api.ldap.model.schema.LdapSyntax)6 MutableAttributeType (org.apache.directory.api.ldap.model.schema.MutableAttributeType)6 SchemaManager (org.apache.directory.api.ldap.model.schema.SchemaManager)6 SyntaxChecker (org.apache.directory.api.ldap.model.schema.SyntaxChecker)6 DefaultSchemaManager (org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager)6 Test (org.junit.Test)6 PrepareString (org.apache.directory.api.ldap.model.schema.PrepareString)5 DeepTrimToLowerNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.DeepTrimToLowerNormalizer)5 LdapSchemaException (org.apache.directory.api.ldap.model.exception.LdapSchemaException)4 MatchingRule (org.apache.directory.api.ldap.model.schema.MatchingRule)3 Normalizer (org.apache.directory.api.ldap.model.schema.Normalizer)3 NoOpNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer)3 AssertionType (org.apache.directory.api.ldap.model.schema.PrepareString.AssertionType)2 ByteArrayComparator (org.apache.directory.api.ldap.model.schema.comparators.ByteArrayComparator)2 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)1 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)1 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)1 LdapUnwillingToPerformException (org.apache.directory.api.ldap.model.exception.LdapUnwillingToPerformException)1 ComparableComparator (org.apache.directory.api.ldap.model.schema.comparators.ComparableComparator)1