Search in sources :

Example 21 with SyntaxChecker

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

the class SchemaEntityFactory method getSyntaxChecker.

/**
 * {@inheritDoc}
 */
@Override
public SyntaxChecker getSyntaxChecker(SchemaManager schemaManager, Entry entry, Registries targetRegistries, String schemaName) throws LdapException {
    checkEntry(entry, SchemaConstants.SYNTAX_CHECKER);
    // The SyntaxChecker OID
    String oid = getOid(entry, SchemaConstants.SYNTAX_CHECKER, schemaManager.isStrict());
    // Get the schema
    if (!schemaManager.isSchemaLoaded(schemaName)) {
        // The schema is not loaded. We can't create the requested Normalizer
        String msg = I18n.err(I18n.ERR_16019_CANNOT_ADD_SC, 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_16020_CANNOT_ADD_SC_IN_REGISTRY, entry.getDn().getName(), schemaName);
        LOG.info(msg);
        schema = schemaManager.getLoadedSchema(schemaName);
    }
    // The FQCN
    String className = getFqcn(entry, SchemaConstants.SYNTAX_CHECKER);
    // The ByteCode
    Attribute byteCode = entry.get(MetaSchemaConstants.M_BYTECODE_AT);
    try {
        // Class load the syntaxChecker
        SyntaxChecker syntaxChecker = classLoadSyntaxChecker(schemaManager, oid, className, byteCode);
        // Update the common fields
        setSchemaObjectProperties(syntaxChecker, entry, schema);
        // return the resulting syntaxChecker
        return syntaxChecker;
    } catch (Exception e) {
        throw new LdapUnwillingToPerformException(ResultCodeEnum.UNWILLING_TO_PERFORM, e.getMessage(), e);
    }
}
Also used : SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) 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) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) InvocationTargetException(java.lang.reflect.InvocationTargetException) LdapUnwillingToPerformException(org.apache.directory.api.ldap.model.exception.LdapUnwillingToPerformException) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 22 with SyntaxChecker

use of org.apache.directory.api.ldap.model.schema.SyntaxChecker 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 23 with SyntaxChecker

use of org.apache.directory.api.ldap.model.schema.SyntaxChecker 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)

Example 24 with SyntaxChecker

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

the class StringValueAttributeTypeTest method testConstrainedString.

/**
 * Presumes an attribute which constrains it's values to some constant
 * strings: LOW, MEDIUM, HIGH.  Normalization does nothing. MatchingRules
 * are exact case matching.
 *
 * @throws Exception on errors
 */
@Test
public void testConstrainedString() throws LdapInvalidAttributeValueException {
    s.setSyntaxChecker(new SyntaxChecker("1.1.1.1") {

        public static final long serialVersionUID = 1L;

        public boolean isValidSyntax(Object value) {
            if (value instanceof String) {
                String strval = (String) value;
                return strval.equals("HIGH") || strval.equals("LOW") || strval.equals("MEDIUM");
            }
            return false;
        }
    });
    mr.setSyntax(s);
    mr.setNormalizer(new NoOpNormalizer(mr.getOid()));
    at.setEquality(mr);
    at.setSyntax(s);
    // check that normalization and syntax checks work as expected
    Value value = new Value(at, "HIGH");
    assertEquals(value.getValue(), value.getValue());
    try {
        new Value(at, "high");
        fail();
    } catch (LdapInvalidAttributeValueException liave) {
    // expected
    }
    // create a bunch to best tested for equals and in containers
    Value v0 = new Value(at, "LOW");
    Value v1 = new Value(at, "LOW");
    Value v2 = new Value(at, "MEDIUM");
    Value v3 = new Value(at, "HIGH");
    // check equals
    assertTrue(v0.equals(v1));
    assertTrue(v1.equals(v0));
    assertEquals(0, v0.compareTo(v1));
    assertFalse(v2.equals(v3));
    assertFalse(v3.equals(v2));
    assertTrue(v2.compareTo(v3) > 0);
    assertTrue(v3.compareTo(v2) < 0);
    // add all except v1 and v5 to a set
    HashSet<Value> set = new HashSet<Value>();
    set.add(v0);
    set.add(v2);
    set.add(v3);
    // check contains method
    assertTrue("since v1.equals( v0 ) and v0 was added then this should be true", set.contains(v1));
    // check ordering based on the comparator
    List<Value> list = new ArrayList<Value>();
    list.add(v1);
    list.add(v3);
    list.add(v0);
    list.add(v2);
    Collections.sort(list);
    // High, low, low, medium
    assertTrue("since v0 equals v1 either could be at index 0 & 1", list.get(0).equals(v3));
    assertTrue("since v0 equals v1 either could be at index 0 & 1", list.get(1).equals(v0));
    assertTrue("since v2 \"MEDIUM\" should be at index 2", list.get(2).equals(v1));
    assertTrue("since v3 \"HIGH\" should be at index 3", list.get(3).equals(v2));
    assertEquals(4, list.size());
}
Also used : NoOpNormalizer(org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer) OctetStringSyntaxChecker(org.apache.directory.api.ldap.model.schema.syntaxCheckers.OctetStringSyntaxChecker) SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) ArrayList(java.util.ArrayList) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) HashSet(java.util.HashSet) Test(org.junit.Test)

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