Search in sources :

Example 16 with MutableAttributeType

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

the class AttributeTypeHelper method buildSuperior.

/**
 * Build the Superior AttributeType reference for an AttributeType
 */
private static boolean buildSuperior(MutableAttributeType attributeType, List<Throwable> errors, Registries registries) {
    MutableAttributeType currentSuperior;
    AttributeTypeRegistry attributeTypeRegistry = registries.getAttributeTypeRegistry();
    String superiorOid = attributeType.getSuperiorOid();
    if (superiorOid != null) {
        // This AT has a superior
        try {
            currentSuperior = (MutableAttributeType) attributeTypeRegistry.lookup(superiorOid);
        } catch (Exception e) {
            // Not allowed.
            String msg = I18n.err(I18n.ERR_13752_CANNOT_FIND_SUPERIOR, superiorOid, attributeType.getName());
            LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_NONEXISTENT_SUPERIOR, msg, e);
            ldapSchemaException.setSourceObject(attributeType);
            ldapSchemaException.setRelatedId(superiorOid);
            errors.add(ldapSchemaException);
            LOG.info(msg);
            // Get out now
            return false;
        }
        if (currentSuperior != null) {
            // a special case : if the superior is collective, this is an error
            if (currentSuperior.isCollective()) {
                String msg = I18n.err(I18n.ERR_13776_CANNOT_SUBTYPE_COLLECTIVE, currentSuperior, attributeType.getName());
                LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_CANNOT_SUBTYPE_COLLECTIVE_AT, msg);
                ldapSchemaException.setSourceObject(attributeType);
                errors.add(ldapSchemaException);
                LOG.info(msg);
                return false;
            }
            attributeType.setSuperior(currentSuperior);
            // handled.
            if (currentSuperior.getSuperior() == null) {
                registries.buildReference(errors, currentSuperior);
            }
            // Update the descendant MAP
            try {
                attributeTypeRegistry.registerDescendants(attributeType, currentSuperior);
            } catch (LdapException ne) {
                errors.add(ne);
                LOG.info(ne.getMessage());
                return false;
            }
            // Check for cycles now
            Set<String> superiors = new HashSet<>();
            superiors.add(attributeType.getOid());
            AttributeType tmp = currentSuperior;
            boolean isOk = true;
            while (tmp != null) {
                if (superiors.contains(tmp.getOid())) {
                    // There is a cycle : bad bad bad !
                    // Not allowed.
                    String msg = I18n.err(I18n.ERR_13753_CYCLE_DETECTED, attributeType.getName());
                    LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_CYCLE_TYPE_HIERARCHY, msg);
                    ldapSchemaException.setSourceObject(attributeType);
                    errors.add(ldapSchemaException);
                    LOG.info(msg);
                    isOk = false;
                    break;
                } else {
                    superiors.add(tmp.getOid());
                    tmp = tmp.getSuperior();
                }
            }
            superiors.clear();
            return isOk;
        } else {
            // Not allowed.
            String msg = I18n.err(I18n.ERR_13752_CANNOT_FIND_SUPERIOR, superiorOid, attributeType.getName());
            LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_NONEXISTENT_SUPERIOR, msg);
            ldapSchemaException.setSourceObject(attributeType);
            ldapSchemaException.setRelatedId(superiorOid);
            errors.add(ldapSchemaException);
            LOG.info(msg);
            // Get out now
            return false;
        }
    } else {
        // No superior, just return
        return true;
    }
}
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) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) AttributeTypeRegistry(org.apache.directory.api.ldap.model.schema.registries.AttributeTypeRegistry) HashSet(java.util.HashSet)

Example 17 with MutableAttributeType

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

the class OpenLdapSchemaParser method afterParse.

/**
 * Splits parsed schema descriptions and resolved
 * object identifier macros.
 *
 * @throws ParseException the parse exception
 */
private void afterParse() throws ParseException {
    objectClasses = new ArrayList<>();
    attributeTypes = new ArrayList<>();
    objectIdentifierMacros = new HashMap<>();
    // split parsed schema descriptions
    for (Object obj : schemaDescriptions) {
        if (obj instanceof OpenLdapObjectIdentifierMacro) {
            OpenLdapObjectIdentifierMacro oid = (OpenLdapObjectIdentifierMacro) obj;
            objectIdentifierMacros.put(oid.getName(), oid);
        } else if (obj instanceof AttributeType) {
            MutableAttributeType attributeType = (MutableAttributeType) obj;
            attributeTypes.add(attributeType);
        } else if (obj instanceof ObjectClass) {
            ObjectClass objectClass = (ObjectClass) obj;
            objectClasses.add(objectClass);
        }
    }
    if (isResolveObjectIdentifierMacros()) {
        // resolve object identifier macros
        for (OpenLdapObjectIdentifierMacro oid : objectIdentifierMacros.values()) {
            resolveObjectIdentifierMacro(oid);
        }
        // apply object identifier macros to object classes
        for (ObjectClass objectClass : objectClasses) {
            objectClass.setOid(getResolveOid(objectClass.getOid()));
        }
        // apply object identifier macros to attribute types
        for (MutableAttributeType attributeType : attributeTypes) {
            attributeType.setOid(getResolveOid(attributeType.getOid()));
            attributeType.setSyntaxOid(getResolveOid(attributeType.getSyntaxOid()));
        }
    }
}
Also used : ObjectClass(org.apache.directory.api.ldap.model.schema.ObjectClass) OpenLdapObjectIdentifierMacro(org.apache.directory.api.ldap.model.schema.syntaxCheckers.OpenLdapObjectIdentifierMacro) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) SchemaObject(org.apache.directory.api.ldap.model.schema.SchemaObject) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType)

Example 18 with MutableAttributeType

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

the class LdifReader method parseEntry.

/**
 * Parse a ldif file. The following rules are processed :
 * <pre>
 * &lt;ldif-file&gt; ::= &lt;ldif-attrval-record&gt; &lt;ldif-attrval-records&gt; |
 *     &lt;ldif-change-record&gt; &lt;ldif-change-records&gt;
 * &lt;ldif-attrval-record&gt; ::= &lt;dn-spec&gt; &lt;sep&gt; &lt;attrval-spec&gt; &lt;attrval-specs&gt;
 * &lt;ldif-change-record&gt; ::= &lt;dn-spec&gt; &lt;sep&gt; &lt;controls-e&gt; &lt;changerecord&gt;
 * &lt;dn-spec&gt; ::= "dn:" &lt;fill&gt; &lt;distinguishedName&gt; | "dn::" &lt;fill&gt; &lt;base64-distinguishedName&gt;
 * &lt;changerecord&gt; ::= "changetype:" &lt;fill&gt; &lt;change-op&gt;
 * </pre>
 *
 * @return the parsed ldifEntry
 * @exception LdapException If the ldif file does not contain a valid entry
 */
protected LdifEntry parseEntry() throws LdapException {
    if ((lines == null) || lines.isEmpty()) {
        LOG.debug(I18n.msg(I18n.MSG_13408_END_OF_LDIF));
        return null;
    }
    // The entry must start with a dn: or a dn::
    String line = lines.get(0);
    lineNumber -= (lines.size() - 1);
    String name = parseDn(line);
    Dn dn = null;
    try {
        dn = new Dn(schemaManager, name);
    } catch (LdapInvalidDnException lide) {
        // Deal with the RDN whihc is not in the schema
        // First parse the DN without the schema
        dn = new Dn(name);
        Rdn rdn = dn.getRdn();
        // Process each Ava
        for (Ava ava : rdn) {
            if ((schemaManager != null) && (schemaManager.getAttributeType(ava.getType()) == null) && schemaManager.isRelaxed()) {
                // Not found : create a new one
                MutableAttributeType newAttributeType = new MutableAttributeType("1.3.6.1.4.1.18060.0.9999." + oidCounter++);
                newAttributeType.setNames(ava.getType());
                newAttributeType.setSyntax(schemaManager.getLdapSyntaxRegistry().get(SchemaConstants.DIRECTORY_STRING_SYNTAX));
                schemaManager.add(newAttributeType);
            }
        }
        dn = new Dn(schemaManager, name);
    }
    // Ok, we have found a Dn
    LdifEntry entry = createLdifEntry(schemaManager);
    entry.setLengthBeforeParsing(entryLen);
    entry.setOffset(entryOffset);
    entry.setDn(dn);
    // We remove this dn from the lines
    lines.remove(0);
    // Now, let's iterate through the other lines
    Iterator<String> iter = lines.iterator();
    // This flag is used to distinguish between an entry and a change
    int type = LDIF_ENTRY;
    // The following boolean is used to check that a control is *not*
    // found elswhere than just after the dn
    boolean controlSeen = false;
    // We use this boolean to check that we do not have AttributeValues
    // after a change operation
    boolean changeTypeSeen = false;
    ChangeType operation = ChangeType.Add;
    String lowerLine;
    Control control;
    while (iter.hasNext()) {
        lineNumber++;
        // Each line could start either with an OID, an attribute type, with
        // "control:" or with "changetype:"
        line = iter.next();
        lowerLine = Strings.toLowerCaseAscii(line);
        // 3) The first line after the Dn is anything else
        if (lowerLine.startsWith("control:")) {
            if (containsEntries) {
                LOG.error(I18n.err(I18n.ERR_13401_CHANGE_NOT_ALLOWED, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13440_NO_CHANGE));
            }
            containsChanges = true;
            if (controlSeen) {
                LOG.error(I18n.err(I18n.ERR_13418_CONTROL_ALREADY_FOUND, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13457_MISPLACED_CONTROL));
            }
            // Parse the control
            control = parseControl(line.substring("control:".length()));
            entry.addControl(control);
        } else if (lowerLine.startsWith("changetype:")) {
            if (containsEntries) {
                LOG.error(I18n.err(I18n.ERR_13401_CHANGE_NOT_ALLOWED, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13440_NO_CHANGE));
            }
            containsChanges = true;
            if (changeTypeSeen) {
                LOG.error(I18n.err(I18n.ERR_13419_CHANGETYPE_ALREADY_FOUND, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13458_MISPLACED_CHANGETYPE));
            }
            // A change request
            type = CHANGE;
            controlSeen = true;
            operation = parseChangeType(line);
            // Parse the change operation in a separate function
            parseChange(entry, iter, operation);
            changeTypeSeen = true;
        } else if (line.indexOf(':') > 0) {
            if (containsChanges) {
                LOG.error(I18n.err(I18n.ERR_13401_CHANGE_NOT_ALLOWED, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13440_NO_CHANGE));
            }
            containsEntries = true;
            if (controlSeen || changeTypeSeen) {
                LOG.error(I18n.err(I18n.ERR_13420_AT_VALUE_NOT_ALLOWED_AFTER_CONTROL, lineNumber));
                throw new LdapLdifException(I18n.err(I18n.ERR_13459_MISPLACED_ATTRIBUTETYPE));
            }
            parseAttributeValue(entry, line, lowerLine);
            type = LDIF_ENTRY;
        } else {
            // Invalid attribute Value
            LOG.error(I18n.err(I18n.ERR_13421_ATTRIBUTE_TYPE_EXPECTED, lineNumber));
            throw new LdapLdifException(I18n.err(I18n.ERR_13460_BAD_ATTRIBUTE));
        }
    }
    if (type == LDIF_ENTRY) {
        LOG.debug(I18n.msg(I18n.MSG_13406_READ_ENTRY, entry));
    } else if (type == CHANGE) {
        entry.setChangeType(operation);
        LOG.debug(I18n.msg(I18n.MSG_13404_READ_MODIF, entry));
    } else {
        LOG.error(I18n.err(I18n.ERR_13422_UNKNOWN_ENTRY_TYPE, lineNumber));
        throw new LdapLdifException(I18n.err(I18n.ERR_13461_UNKNOWN_ENTRY));
    }
    return entry;
}
Also used : Dn(org.apache.directory.api.ldap.model.name.Dn) Ava(org.apache.directory.api.ldap.model.name.Ava) Control(org.apache.directory.api.ldap.model.message.Control) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) Rdn(org.apache.directory.api.ldap.model.name.Rdn) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)

Example 19 with MutableAttributeType

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

the class LdifReader method parseAttributeValue.

/**
 * Parse an AttributeType/AttributeValue
 *
 * @param entry The entry where to store the value
 * @param line The line to parse
 * @param lowerLine The same line, lowercased
 * @throws LdapException If anything goes wrong
 */
public void parseAttributeValue(LdifEntry entry, String line, String lowerLine) throws LdapException {
    int colonIndex = line.indexOf(':');
    String attributeType = lowerLine.substring(0, colonIndex);
    // We should *not* have a Dn twice
    if ("dn".equals(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);
    if (schemaManager != null) {
        AttributeType at = schemaManager.getAttributeType(attributeType);
        if (at != null) {
            if (at.getSyntax().isHumanReadable()) {
                if (attributeValue == null) {
                    attributeValue = "";
                } else if (attributeValue instanceof byte[]) {
                    attributeValue = Strings.utf8ToString((byte[]) attributeValue);
                }
            } else {
                if (attributeValue instanceof String) {
                    attributeValue = Strings.getBytesUtf8((String) attributeValue);
                }
            }
        }
    }
    // Update the entry
    try {
        entry.addAttribute(attributeType, attributeValue);
    } catch (Exception e) {
        // The attribute does not exist already, create a fake one
        if ((schemaManager != null) && schemaManager.isRelaxed()) {
            MutableAttributeType newAttributeType = new MutableAttributeType("1.3.6.1.4.1.18060.0.9999." + oidCounter++);
            newAttributeType.setNames(attributeType);
            newAttributeType.setSyntax(schemaManager.getLdapSyntaxRegistry().get(SchemaConstants.DIRECTORY_STRING_SYNTAX));
            schemaManager.add(newAttributeType);
            entry.addAttribute(attributeType, attributeValue);
        }
    }
}
Also used : AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) NoSuchElementException(java.util.NoSuchElementException) NotImplementedException(org.apache.directory.api.util.exception.NotImplementedException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException) FileNotFoundException(java.io.FileNotFoundException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 20 with MutableAttributeType

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

the class OpenLdapSchemaParserTest method testParseOpenLdapCollectiveSchema.

@Test
public void testParseOpenLdapCollectiveSchema() throws Exception {
    InputStream input = getClass().getResourceAsStream("collective.schema");
    parser.parse(input);
    List<MutableAttributeType> attributeTypes = parser.getAttributeTypes();
    List<ObjectClass> objectClassTypes = parser.getObjectClassTypes();
    Map<String, OpenLdapObjectIdentifierMacro> objectIdentifierMacros = parser.getObjectIdentifierMacros();
    assertEquals(13, attributeTypes.size());
    assertEquals(0, objectClassTypes.size());
    assertEquals(0, objectIdentifierMacros.size());
    for (AttributeType attributeTypeLiteral : attributeTypes) {
        assertTrue(attributeTypeLiteral.isCollective());
    }
}
Also used : ObjectClass(org.apache.directory.api.ldap.model.schema.ObjectClass) OpenLdapObjectIdentifierMacro(org.apache.directory.api.ldap.model.schema.syntaxCheckers.OpenLdapObjectIdentifierMacro) InputStream(java.io.InputStream) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) Test(org.junit.Test)

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