Search in sources :

Example 76 with AttributeType

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

the class AttributeTypeHelper method buildOrdering.

/**
 * Build the ORDERING MR reference for an AttributeType
 */
private static void buildOrdering(MutableAttributeType attributeType, List<Throwable> errors, Registries registries) {
    String orderingOid = attributeType.getOrderingOid();
    if (orderingOid != null) {
        MatchingRule currentOrdering = null;
        try {
            currentOrdering = registries.getMatchingRuleRegistry().lookup(orderingOid);
        } catch (LdapException ne) {
            // Not allowed.
            String msg = I18n.err(I18n.ERR_13758_CANNOT_FIND_ORDERING_MR_OBJECT, orderingOid, attributeType.getName());
            LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_NONEXISTENT_ORDERING_MATCHING_RULE, msg, ne);
            ldapSchemaException.setSourceObject(attributeType);
            ldapSchemaException.setRelatedId(orderingOid);
            errors.add(ldapSchemaException);
            LOG.info(msg);
            return;
        }
        if (currentOrdering != null) {
            attributeType.setOrdering(currentOrdering);
        } else {
            // Not allowed.
            String msg = I18n.err(I18n.ERR_13759_CANNOT_FIND_ORDERING_MR_INSTANCE, orderingOid, attributeType.getName());
            LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_NONEXISTENT_ORDERING_MATCHING_RULE, msg);
            ldapSchemaException.setSourceObject(attributeType);
            ldapSchemaException.setRelatedId(orderingOid);
            errors.add(ldapSchemaException);
            LOG.info(msg);
        }
    } else {
        AttributeType superior = attributeType.getSuperior();
        // If the AT has a superior, take its Ordering MR if any
        if ((superior != null) && (superior.getOrdering() != null)) {
            attributeType.setOrdering(superior.getOrdering());
        }
    }
}
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)

Example 77 with AttributeType

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

the class ConcreteNameComponentNormalizer method normalizeByName.

/**
 * {@inheritDoc}
 */
@Override
public Object normalizeByName(String name, String value) throws LdapException {
    AttributeType attributeType = schemaManager.lookupAttributeTypeRegistry(name);
    Normalizer normalizer = lookup(name);
    if (attributeType.getSyntax().isHumanReadable()) {
        return normalizer.normalize(value);
    } else {
        String unescaped = unescape(value);
        return normalizer.normalize(unescaped);
    }
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) Normalizer(org.apache.directory.api.ldap.model.schema.Normalizer)

Example 78 with AttributeType

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

the class ConcreteNameComponentNormalizer method lookup.

/**
 * Looks up the Normalizer to use for a name component using the attributeId
 * for the name component.  First the attribute is resolved, then its
 * equality matching rule is looked up.  The normalizer of that matching
 * rule is returned.
 *
 * @param id the name or oid of the attribute in the name component to
 * normalize the value of
 * @return the Normalizer to use for normalizing the value of the attribute
 * @throws LdapException if there are failures resolving the Normalizer
 */
private Normalizer lookup(String id) throws LdapException {
    AttributeType type = schemaManager.lookupAttributeTypeRegistry(id);
    MatchingRule mrule = type.getEquality();
    if (mrule == null) {
        return new NoOpNormalizer(id);
    }
    return mrule.getNormalizer();
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule)

Example 79 with AttributeType

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

the class DefaultAttributeTypeRegistry method descendants.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public Iterator<AttributeType> descendants(String ancestorId) throws LdapException {
    try {
        String oid = getOidByName(ancestorId);
        Set<AttributeType> descendants = oidToDescendantSet.get(oid);
        if (descendants == null) {
            return Collections.EMPTY_SET.iterator();
        }
        return descendants.iterator();
    } catch (LdapException ne) {
        throw new LdapNoSuchAttributeException(ne.getMessage(), ne);
    }
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapNoSuchAttributeException(org.apache.directory.api.ldap.model.exception.LdapNoSuchAttributeException)

Example 80 with AttributeType

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

the class LdifReader method parseModify.

/**
 * Parse a modify change type.
 *
 * The grammar is :
 * <pre>
 * &lt;changerecord&gt; ::= "changetype:" FILL "modify" SEP &lt;mod-spec&gt; &lt;mod-specs-e&gt;
 * &lt;mod-spec&gt; ::= "add:" &lt;mod-val&gt; | "delete:" &lt;mod-val-del&gt; | "replace:" &lt;mod-val&gt;
 * &lt;mod-specs-e&gt; ::= &lt;mod-spec&gt;
 * &lt;mod-specs-e&gt; | e
 * &lt;mod-val&gt; ::= FILL ATTRIBUTE-DESCRIPTION SEP ATTRVAL-SPEC &lt;attrval-specs-e&gt; "-" SEP
 * &lt;mod-val-del&gt; ::= FILL ATTRIBUTE-DESCRIPTION SEP &lt;attrval-specs-e&gt; "-" SEP
 * &lt;attrval-specs-e&gt; ::= ATTRVAL-SPEC &lt;attrval-specs&gt; | e
 * </pre>
 *
 * @param entry The entry to feed
 * @param iter The lines
 * @exception LdapLdifException If the modify operation is invalid
 */
private void parseModify(LdifEntry entry, Iterator<String> iter) throws LdapLdifException {
    int state = MOD_SPEC;
    String modified = null;
    ModificationOperation modificationType = ModificationOperation.ADD_ATTRIBUTE;
    Attribute attribute = null;
    // The following flag is used to deal with empty modifications
    boolean isEmptyValue = true;
    while (iter.hasNext()) {
        String line = iter.next();
        String lowerLine = Strings.toLowerCaseAscii(line);
        if (lowerLine.startsWith("-")) {
            if ((state != ATTRVAL_SPEC_OR_SEP) && (state != ATTRVAL_SPEC)) {
                String msg = I18n.err(I18n.ERR_13413_BAD_MODIFY_SEPARATOR, lineNumber);
                LOG.error(msg);
                throw new LdapLdifException(msg);
            } else {
                if (isEmptyValue) {
                    if (state == ATTRVAL_SPEC_OR_SEP) {
                        entry.addModification(modificationType, modified);
                    } else {
                        // Update the entry with a null value
                        entry.addModification(modificationType, modified, null);
                    }
                } else {
                    // Update the entry with the attribute
                    entry.addModification(modificationType, attribute);
                }
                state = MOD_SPEC;
                isEmptyValue = true;
            }
        } else if (lowerLine.startsWith("add:")) {
            if ((state != MOD_SPEC) && (state != ATTRVAL_SPEC)) {
                String msg = I18n.err(I18n.ERR_13414_BAD_MODIFY_SEPARATOR_2, lineNumber);
                LOG.error(msg);
                throw new LdapLdifException(msg);
            }
            modified = Strings.trim(line.substring("add:".length()));
            modificationType = ModificationOperation.ADD_ATTRIBUTE;
            attribute = new DefaultAttribute(modified);
            state = ATTRVAL_SPEC;
        } else if (lowerLine.startsWith("delete:")) {
            if ((state != MOD_SPEC) && (state != ATTRVAL_SPEC)) {
                String msg = I18n.err(I18n.ERR_13414_BAD_MODIFY_SEPARATOR_2, lineNumber);
                LOG.error(msg);
                throw new LdapLdifException(msg);
            }
            modified = Strings.trim(line.substring("delete:".length()));
            modificationType = ModificationOperation.REMOVE_ATTRIBUTE;
            attribute = new DefaultAttribute(modified);
            isEmptyValue = false;
            state = ATTRVAL_SPEC_OR_SEP;
        } else if (lowerLine.startsWith("replace:")) {
            if ((state != MOD_SPEC) && (state != ATTRVAL_SPEC)) {
                String msg = I18n.err(I18n.ERR_13414_BAD_MODIFY_SEPARATOR_2, lineNumber);
                LOG.error(msg);
                throw new LdapLdifException(msg);
            }
            modified = Strings.trim(line.substring("replace:".length()));
            modificationType = ModificationOperation.REPLACE_ATTRIBUTE;
            if (schemaManager != null) {
                AttributeType attributeType = schemaManager.getAttributeType(modified);
                attribute = new DefaultAttribute(modified, attributeType);
            } else {
                attribute = new DefaultAttribute(modified);
            }
            state = ATTRVAL_SPEC_OR_SEP;
        } else {
            if ((state != ATTRVAL_SPEC) && (state != ATTRVAL_SPEC_OR_SEP)) {
                String msg = I18n.err(I18n.ERR_13413_BAD_MODIFY_SEPARATOR, lineNumber);
                LOG.error(msg);
                throw new LdapLdifException(msg);
            }
            // A standard AttributeType/AttributeValue pair
            int colonIndex = line.indexOf(':');
            String attributeType = line.substring(0, colonIndex);
            if (!attributeType.equalsIgnoreCase(modified)) {
                LOG.error(I18n.err(I18n.ERR_13415_MOD_ATTR_AND_VALUE_SPEC_NOT_EQUAL, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13454_BAD_MODIFY_ATTRIBUTE));
            }
            // We should *not* have a Dn twice
            if ("dn".equalsIgnoreCase(attributeType)) {
                LOG.error(I18n.err(I18n.ERR_13400_ENTRY_WITH_TWO_DNS, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13439_LDIF_ENTRY_WITH_TWO_DNS));
            }
            Object attributeValue = parseValue(attributeType, line, colonIndex);
            try {
                if (attributeValue instanceof String) {
                    attribute.add((String) attributeValue);
                } else {
                    attribute.add((byte[]) attributeValue);
                }
            } catch (LdapInvalidAttributeValueException liave) {
                throw new LdapLdifException(liave.getMessage(), liave);
            }
            isEmptyValue = false;
            state = ATTRVAL_SPEC_OR_SEP;
        }
    }
    if (state != MOD_SPEC) {
        String msg = I18n.err(I18n.ERR_13414_BAD_MODIFY_SEPARATOR_2, lineNumber);
        LOG.error(msg);
        throw new LdapLdifException(msg);
    }
}
Also used : ModificationOperation(org.apache.directory.api.ldap.model.entry.ModificationOperation) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)

Aggregations

AttributeType (org.apache.directory.api.ldap.model.schema.AttributeType)135 Test (org.junit.Test)68 MutableAttributeType (org.apache.directory.api.ldap.model.schema.MutableAttributeType)42 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)25 ParseException (java.text.ParseException)15 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)15 Value (org.apache.directory.api.ldap.model.entry.Value)15 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)11 LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)11 HashSet (java.util.HashSet)10 LdapSchemaException (org.apache.directory.api.ldap.model.exception.LdapSchemaException)10 ObjectClass (org.apache.directory.api.ldap.model.schema.ObjectClass)10 MatchingRule (org.apache.directory.api.ldap.model.schema.MatchingRule)9 IOException (java.io.IOException)7 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)7 Before (org.junit.Before)7 SchemaManager (org.apache.directory.api.ldap.model.schema.SchemaManager)6 DefaultSchemaManager (org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager)6 ArrayList (java.util.ArrayList)5 Dn (org.apache.directory.api.ldap.model.name.Dn)4