Search in sources :

Example 11 with MutableAttributeType

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

the class SchemaEntityFactory method getAttributeType.

/**
 * {@inheritDoc}
 * @throws LdapInvalidAttributeValueException If the AttributeType does not exist
 * @throws LdapUnwillingToPerformException If the schema is not loaded
 */
@Override
public AttributeType getAttributeType(SchemaManager schemaManager, Entry entry, Registries targetRegistries, String schemaName) throws LdapInvalidAttributeValueException, LdapUnwillingToPerformException {
    checkEntry(entry, SchemaConstants.ATTRIBUTE_TYPE);
    // The AttributeType OID
    String oid = getOid(entry, SchemaConstants.ATTRIBUTE_TYPE, schemaManager.isStrict());
    // Get the schema
    if (!schemaManager.isSchemaLoaded(schemaName)) {
        // The schema is not loaded, this is an error
        String msg = I18n.err(I18n.ERR_16032_CANNOT_ADD_AT, 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_16033_CANNOT_ADD_AT_IN_REGISTRY, entry.getDn().getName(), schemaName);
        LOG.info(msg);
        schema = schemaManager.getLoadedSchema(schemaName);
    }
    // Create the new AttributeType
    MutableAttributeType attributeType = new MutableAttributeType(oid);
    if (schemaManager.isRelaxed()) {
        attributeType.setRelaxed(true);
    }
    // Syntax
    Attribute mSyntax = entry.get(MetaSchemaConstants.M_SYNTAX_AT);
    if ((mSyntax != null) && (mSyntax.get() != null)) {
        attributeType.setSyntaxOid(mSyntax.getString());
    }
    // Syntax Length
    Attribute mSyntaxLength = entry.get(MetaSchemaConstants.M_LENGTH_AT);
    if (mSyntaxLength != null) {
        attributeType.setSyntaxLength(Integer.parseInt(mSyntaxLength.getString()));
    }
    // Equality
    Attribute mEquality = entry.get(MetaSchemaConstants.M_EQUALITY_AT);
    if (mEquality != null) {
        attributeType.setEqualityOid(mEquality.getString());
    }
    // Ordering
    Attribute mOrdering = entry.get(MetaSchemaConstants.M_ORDERING_AT);
    if (mOrdering != null) {
        attributeType.setOrderingOid(mOrdering.getString());
    }
    // Substr
    Attribute mSubstr = entry.get(MetaSchemaConstants.M_SUBSTR_AT);
    if (mSubstr != null) {
        attributeType.setSubstringOid(mSubstr.getString());
    }
    Attribute mSupAttributeType = entry.get(MetaSchemaConstants.M_SUP_ATTRIBUTE_TYPE_AT);
    // Sup
    if (mSupAttributeType != null) {
        attributeType.setSuperiorOid(mSupAttributeType.getString());
    }
    // isCollective
    Attribute mCollective = entry.get(MetaSchemaConstants.M_COLLECTIVE_AT);
    if (mCollective != null) {
        String val = mCollective.getString();
        attributeType.setCollective("TRUE".equalsIgnoreCase(val));
    }
    // isSingleValued
    Attribute mSingleValued = entry.get(MetaSchemaConstants.M_SINGLE_VALUE_AT);
    if (mSingleValued != null) {
        String val = mSingleValued.getString();
        attributeType.setSingleValued("TRUE".equalsIgnoreCase(val));
    }
    // isReadOnly
    Attribute mNoUserModification = entry.get(MetaSchemaConstants.M_NO_USER_MODIFICATION_AT);
    if (mNoUserModification != null) {
        String val = mNoUserModification.getString();
        attributeType.setUserModifiable(!"TRUE".equalsIgnoreCase(val));
    }
    // Usage
    Attribute mUsage = entry.get(MetaSchemaConstants.M_USAGE_AT);
    if (mUsage != null) {
        attributeType.setUsage(UsageEnum.getUsage(mUsage.getString()));
    }
    // Common properties
    setSchemaObjectProperties(attributeType, entry, schema);
    return attributeType;
}
Also used : 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) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType)

Example 12 with MutableAttributeType

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

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

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

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

the class AttributeTypeHelper method buildEquality.

/**
 * Build the EQUALITY MR reference for an AttributeType
 */
private static void buildEquality(MutableAttributeType attributeType, List<Throwable> errors, Registries registries) {
    String equalityOid = attributeType.getEqualityOid();
    // The equality MR. It can be null
    if (equalityOid != null) {
        MatchingRule currentEquality = null;
        try {
            currentEquality = registries.getMatchingRuleRegistry().lookup(equalityOid);
        } catch (LdapException ne) {
            // Not allowed.
            String msg = I18n.err(I18n.ERR_13756_CANNOT_FIND_EQUALITY_MR_OBJECT, equalityOid, attributeType.getName());
            LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_NONEXISTENT_EQUALITY_MATCHING_RULE, msg, ne);
            ldapSchemaException.setSourceObject(attributeType);
            ldapSchemaException.setRelatedId(equalityOid);
            errors.add(ldapSchemaException);
            LOG.info(msg);
            return;
        }
        if (currentEquality != null) {
            attributeType.setEquality(currentEquality);
            // Restore the old equality OID to preserve the user's provided value
            attributeType.setEqualityOid(equalityOid);
        } else {
            // Not allowed.
            String msg = I18n.err(I18n.ERR_13757_CANNOT_FIND_EQUALITY_MR_INSTANCE, equalityOid, attributeType.getName());
            LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_NONEXISTENT_EQUALITY_MATCHING_RULE, msg);
            ldapSchemaException.setSourceObject(attributeType);
            ldapSchemaException.setRelatedId(equalityOid);
            errors.add(ldapSchemaException);
            LOG.info(msg);
        }
    } else {
        AttributeType superior = attributeType.getSuperior();
        // If the AT has a superior, take its Equality MR if any
        if ((superior != null) && (superior.getEquality() != null)) {
            attributeType.setEquality(superior.getEquality());
        }
    }
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException) MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Aggregations

MutableAttributeType (org.apache.directory.api.ldap.model.schema.MutableAttributeType)55 Test (org.junit.Test)37 SchemaManager (org.apache.directory.api.ldap.model.schema.SchemaManager)24 DefaultSchemaManager (org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager)24 AttributeType (org.apache.directory.api.ldap.model.schema.AttributeType)18 LdapSchemaException (org.apache.directory.api.ldap.model.exception.LdapSchemaException)17 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)10 LdapSyntax (org.apache.directory.api.ldap.model.schema.LdapSyntax)7 PrepareString (org.apache.directory.api.ldap.model.schema.PrepareString)7 MutableMatchingRule (org.apache.directory.api.ldap.model.schema.MutableMatchingRule)6 SyntaxChecker (org.apache.directory.api.ldap.model.schema.SyntaxChecker)6 Normalizer (org.apache.directory.api.ldap.model.schema.Normalizer)5 ObjectClass (org.apache.directory.api.ldap.model.schema.ObjectClass)5 DeepTrimToLowerNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.DeepTrimToLowerNormalizer)5 OpenLdapObjectIdentifierMacro (org.apache.directory.api.ldap.model.schema.syntaxCheckers.OpenLdapObjectIdentifierMacro)5 InputStream (java.io.InputStream)4 MatchingRule (org.apache.directory.api.ldap.model.schema.MatchingRule)4 ByteArrayComparator (org.apache.directory.api.ldap.model.schema.comparators.ByteArrayComparator)4 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)2 LdapProtocolErrorException (org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException)2