Search in sources :

Example 11 with Normalizer

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

the class BinaryValueTest method initAT.

/**
 * Initialize an AttributeType and the associated MatchingRule
 * and Syntax
 */
@Before
public void initAT() {
    s = EntryUtils.syntaxFactory("1.1.1.1", false);
    s.setSyntaxChecker(OctetStringSyntaxChecker.INSTANCE);
    mr = EntryUtils.matchingRuleFactory("1.1.2.1");
    mr.setSyntax(s);
    mr.setLdapComparator(new ByteArrayComparator("1.1.1"));
    mr.setNormalizer(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 {
            byte[] val = Strings.getBytesUtf8(value);
            // each byte will be changed to be > 0, and spaces will be trimmed
            byte[] newVal = new byte[val.length];
            int i = 0;
            for (byte b : val) {
                newVal[i++] = (byte) (b & 0x007F);
            }
            return Strings.utf8ToString(Strings.trim(newVal));
        }
    });
    at = new MutableAttributeType("1.1.3.1");
    at.setEquality(mr);
    at.setOrdering(mr);
    at.setSubstring(mr);
    at.setSyntax(s);
}
Also used : PrepareString(org.apache.directory.api.ldap.model.schema.PrepareString) Normalizer(org.apache.directory.api.ldap.model.schema.Normalizer) PrepareString(org.apache.directory.api.ldap.model.schema.PrepareString) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) ByteArrayComparator(org.apache.directory.api.ldap.model.schema.comparators.ByteArrayComparator) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) Before(org.junit.Before)

Example 12 with Normalizer

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

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

the class Registries method resolve.

/**
 * Check if the Comparator, Normalizer and the syntax are
 * existing for a matchingRule.
 */
private void resolve(MatchingRule matchingRule, List<Throwable> errors) {
    // Process the Syntax. It can't be null
    String syntaxOid = matchingRule.getSyntaxOid();
    if (syntaxOid != null) {
        // Check if the Syntax is present in the registries
        try {
            ldapSyntaxRegistry.lookup(syntaxOid);
        } catch (LdapException ne) {
            // This MR's syntax has not been loaded into the Registries.
            LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.OID_ALREADY_REGISTERED, I18n.err(I18n.ERR_13748_MATCHING_RULE_NO_SYNTAX, matchingRule.getOid()), ne);
            ldapSchemaException.setSourceObject(matchingRule);
            errors.add(ldapSchemaException);
        }
    } else {
        // This is an error.
        LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.OID_ALREADY_REGISTERED, I18n.err(I18n.ERR_13748_MATCHING_RULE_NO_SYNTAX, matchingRule.getOid()));
        ldapSchemaException.setSourceObject(matchingRule);
        errors.add(ldapSchemaException);
    }
    // Process the Normalizer
    Normalizer normalizer = matchingRule.getNormalizer();
    if (normalizer == null) {
        // Ok, no normalizer, this is an error
        Throwable error = new LdapSchemaViolationException(ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(I18n.ERR_13220_NO_NORMALIZER, matchingRule.getOid()));
        errors.add(error);
    }
    // Process the Comparator
    LdapComparator<?> comparator = matchingRule.getLdapComparator();
    if (comparator == null) {
        // Ok, no comparator, this is an error
        Throwable error = new LdapSchemaViolationException(ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(I18n.ERR_04296, matchingRule.getOid()));
        errors.add(error);
    }
}
Also used : LdapSchemaViolationException(org.apache.directory.api.ldap.model.exception.LdapSchemaViolationException) Normalizer(org.apache.directory.api.ldap.model.schema.Normalizer) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 14 with Normalizer

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

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

the class DefaultNormalizerRegistry method toString.

/**
 * @see Object#toString()
 */
@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(schemaObjectType).append(": ");
    boolean isFirst = true;
    for (Map.Entry<String, Normalizer> entry : byName.entrySet()) {
        if (isFirst) {
            isFirst = false;
        } else {
            sb.append(", ");
        }
        Normalizer normalizer = entry.getValue();
        String fqcn = normalizer.getFqcn();
        int lastDotPos = fqcn.lastIndexOf('.');
        sb.append('<').append(normalizer.getOid()).append(", ");
        if (lastDotPos > 0) {
            sb.append(fqcn.substring(lastDotPos + 1));
        } else {
            sb.append(fqcn);
        }
        sb.append('>');
    }
    return sb.toString();
}
Also used : Normalizer(org.apache.directory.api.ldap.model.schema.Normalizer) Map(java.util.Map)

Aggregations

Normalizer (org.apache.directory.api.ldap.model.schema.Normalizer)78 Test (org.junit.Test)59 DeepTrimToLowerNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.DeepTrimToLowerNormalizer)20 DeepTrimNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.DeepTrimNormalizer)15 TelephoneNumberNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.TelephoneNumberNormalizer)12 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)10 NumericNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.NumericNormalizer)9 SchemaManager (org.apache.directory.api.ldap.model.schema.SchemaManager)7 NoOpNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer)7 DefaultSchemaManager (org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager)7 MutableAttributeType (org.apache.directory.api.ldap.model.schema.MutableAttributeType)6 PrepareString (org.apache.directory.api.ldap.model.schema.PrepareString)6 LdapSchemaException (org.apache.directory.api.ldap.model.exception.LdapSchemaException)5 LdapSyntax (org.apache.directory.api.ldap.model.schema.LdapSyntax)5 MatchingRule (org.apache.directory.api.ldap.model.schema.MatchingRule)5 MutableMatchingRule (org.apache.directory.api.ldap.model.schema.MutableMatchingRule)4 SyntaxChecker (org.apache.directory.api.ldap.model.schema.SyntaxChecker)4 ByteArrayComparator (org.apache.directory.api.ldap.model.schema.comparators.ByteArrayComparator)4 BooleanNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.BooleanNormalizer)4 LdapProtocolErrorException (org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException)3