Search in sources :

Example 11 with LdapProtocolErrorException

use of org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException in project directory-ldap-api by apache.

the class SchemaManagerDelTest method testDeleteExistingComparatorUsedByRemovedMatchingRule.

/**
 * Check that a Comparator which has been used by a deleted MatchingRule
 * can be removed
 */
@Test
public void testDeleteExistingComparatorUsedByRemovedMatchingRule() throws Exception {
    SchemaManager schemaManager = loadSchema("system");
    int ctrSize = schemaManager.getComparatorRegistry().size();
    int mrrSize = schemaManager.getMatchingRuleRegistry().size();
    int goidSize = schemaManager.getGlobalOidRegistry().size();
    String OID = "2.5.13.33";
    // Check that the MR and C are present
    assertTrue(isMatchingRulePresent(schemaManager, OID));
    assertTrue(isComparatorPresent(schemaManager, OID));
    // Now try to remove the C
    LdapComparator<?> lc = schemaManager.lookupComparatorRegistry(OID);
    // shouldn't be deleted cause there is a MR associated with it
    assertFalse(schemaManager.delete(lc));
    List<Throwable> errors = schemaManager.getErrors();
    assertFalse(errors.isEmpty());
    assertTrue(errors.get(0) instanceof LdapProtocolErrorException);
    // Now delete the using MR : it should be OK
    MatchingRule mr = new MatchingRule(OID);
    assertTrue(schemaManager.delete(mr));
    assertEquals(mrrSize - 1, schemaManager.getMatchingRuleRegistry().size());
    assertEquals(goidSize - 1, schemaManager.getGlobalOidRegistry().size());
    assertFalse(isMatchingRulePresent(schemaManager, OID));
    // and try to delete the Comparator again
    assertTrue(schemaManager.delete(lc));
    assertFalse(isComparatorPresent(schemaManager, OID));
    assertEquals(ctrSize - 1, schemaManager.getComparatorRegistry().size());
    assertEquals(goidSize - 1, schemaManager.getGlobalOidRegistry().size());
}
Also used : LdapProtocolErrorException(org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException) SchemaManager(org.apache.directory.api.ldap.model.schema.SchemaManager) DefaultSchemaManager(org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager) MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule) Test(org.junit.Test)

Example 12 with LdapProtocolErrorException

use of org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException in project directory-ldap-api by apache.

the class DefaultSchemaManager method delete.

/**
 * {@inheritDoc}
 */
@Override
public boolean delete(SchemaObject schemaObject) throws LdapException {
    // First, clear the errors
    errors.clear();
    if (registries.isRelaxed()) {
        // Apply the addition right away
        registries.delete(errors, schemaObject);
        return errors.isEmpty();
    } else {
        // The new schemaObject's OID must exist
        if (!checkOidExist(schemaObject)) {
            Throwable error = new LdapProtocolErrorException(I18n.err(I18n.ERR_16039_OID_DOES_NOT_EXIST, schemaObject.getOid()));
            errors.add(error);
            return false;
        }
        // Get the SchemaObject to delete if it's not a LoadableSchemaObject
        SchemaObject toDelete = getSchemaObject(schemaObject);
        // First check that this SchemaObject does not have any referencing SchemaObjects
        Set<SchemaObjectWrapper> referencing = registries.getReferencing(toDelete);
        if ((referencing != null) && !referencing.isEmpty()) {
            String msg = I18n.err(I18n.ERR_16040_CANNOT_REMOVE_FROM_REGISTRY, schemaObject.getOid(), Strings.setToString(referencing));
            Throwable error = new LdapProtocolErrorException(msg);
            errors.add(error);
            return false;
        }
        String schemaName = getSchemaName(toDelete);
        // At this point, the deleted AttributeType may be referenced, it will be checked
        // there, if the schema and the AttributeType are both enabled.
        Schema schema = getLoadedSchema(schemaName);
        if (schema == null) {
            // The SchemaObject must be associated with an existing schema
            String msg = I18n.err(I18n.ERR_16041_CANNOT_DELETE_SCHEMA_OBJECT, schemaObject.getOid());
            LOG.info(msg);
            Throwable error = new LdapProtocolErrorException(msg);
            errors.add(error);
            return false;
        }
        if (schema.isEnabled() && schemaObject.isEnabled()) {
            // As we may break the registries, work on a cloned registries
            Registries clonedRegistries = null;
            try {
                clonedRegistries = registries.clone();
            } catch (CloneNotSupportedException cnse) {
                throw new LdapOtherException(cnse.getMessage(), cnse);
            }
            // Delete the SchemaObject from the cloned registries
            clonedRegistries.delete(errors, toDelete);
            // Remove the cloned registries
            clonedRegistries.clear();
            // If we didn't get any error, apply the deletion to the real retistries
            if (errors.isEmpty()) {
                // Apply the deletion to the real registries
                registries.delete(errors, toDelete);
                LOG.debug(I18n.msg(I18n.MSG_16022_REMOVED_FROM_ENABLED_SCHEMA, toDelete.getName(), schemaName));
                return true;
            } else {
                // We have some error : reject the deletion and get out
                LOG.info(I18n.msg(I18n.MSG_16023_CANNOT_DELETE_SCHEMAOBJECT, schemaObject.getOid(), Strings.listToString(errors)));
                return false;
            }
        } else {
            // At least, we register the OID in the globalOidRegistry, and associates it with the
            // schema
            registries.associateWithSchema(errors, schemaObject);
            LOG.debug(I18n.msg(I18n.MSG_16024_REMOVED_FROM_DISABLED_SCHEMA, schemaObject.getName(), schemaName));
            return errors.isEmpty();
        }
    }
}
Also used : SchemaObject(org.apache.directory.api.ldap.model.schema.SchemaObject) LoadableSchemaObject(org.apache.directory.api.ldap.model.schema.LoadableSchemaObject) LdapProtocolErrorException(org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException) Schema(org.apache.directory.api.ldap.model.schema.registries.Schema) Registries(org.apache.directory.api.ldap.model.schema.registries.Registries) SchemaObjectWrapper(org.apache.directory.api.ldap.model.schema.SchemaObjectWrapper) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException)

Example 13 with LdapProtocolErrorException

use of org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException in project directory-ldap-api by apache.

the class DefaultSchemaManager method load.

/**
 * Load the schema in the registries. We will load everything accordingly to the two flags :
 * - isRelaxed
 * - disabledAccepted
 */
private boolean load(Registries registries, Schema schema) throws LdapException {
    if (schema == null) {
        LOG.info(I18n.msg(I18n.MSG_16013_SCHEMA_IS_NULL));
        return false;
    }
    // First avoid loading twice the same schema
    if (registries.isSchemaLoaded(schema.getSchemaName())) {
        return true;
    }
    if (schema.isDisabled()) {
        if (registries.isDisabledAccepted()) {
            LOG.info(I18n.msg(I18n.MSG_16014_LOADING_DISABLED_SCHEMA, schema.getSchemaName(), schema));
            registries.schemaLoaded(schema);
            addSchemaObjects(schema, registries);
        } else {
            return false;
        }
    } else {
        LOG.info(I18n.msg(I18n.MSG_16015_LOADING_ENABLED_SCHEMA, schema.getSchemaName(), schema));
        // Check that the dependencies, if any, are correct
        if (schema.getDependencies() != null) {
            for (String dependency : schema.getDependencies()) {
                Schema dependencySchema = schemaMap.get(dependency);
                if (dependencySchema == null) {
                    // The dependency has not been loaded.
                    String msg = I18n.err(I18n.ERR_16035_CANNOT_LOAD_SCHEMA, schema.getSchemaName());
                    LOG.info(msg);
                    Throwable error = new LdapProtocolErrorException(msg);
                    errors.add(error);
                    return false;
                }
                // If the dependency is disabled, then enable it
                if (dependencySchema.isDisabled()) {
                    dependencySchema.enable();
                    if (!load(registries, dependencySchema)) {
                        dependencySchema.disable();
                        return false;
                    }
                }
            }
        }
        registries.schemaLoaded(schema);
        addSchemaObjects(schema, registries);
    }
    return true;
}
Also used : LdapProtocolErrorException(org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException) Schema(org.apache.directory.api.ldap.model.schema.registries.Schema)

Example 14 with LdapProtocolErrorException

use of org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException in project directory-ldap-api by apache.

the class DefaultSchemaManager method add.

// -----------------------------------------------------------------------------------
// SchemaObject operations
// -----------------------------------------------------------------------------------
/**
 * {@inheritDoc}
 */
@Override
public boolean add(SchemaObject schemaObject) throws LdapException {
    // First, clear the errors
    errors.clear();
    // Clone the schemaObject
    SchemaObject copy = copy(schemaObject);
    if (copy == null) {
        return false;
    }
    if (registries.isRelaxed()) {
        // Apply the addition right away
        registries.add(errors, copy, true);
        return errors.isEmpty();
    } else {
        // The new schemaObject's OID must not already exist
        if (checkOidExist(copy)) {
            LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.OID_ALREADY_REGISTERED, I18n.err(I18n.ERR_16036_OID_NOT_UNIQUE, schemaObject.getOid()));
            ldapSchemaException.setSourceObject(schemaObject);
            errors.add(ldapSchemaException);
            return false;
        }
        // Build the new AttributeType from the given entry
        String schemaName = getSchemaName(copy);
        if (schemaName == null) {
            // The schema associated with the SchemaaObject does not exist. This is not valid.
            LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.NONEXISTENT_SCHEMA, I18n.err(I18n.ERR_16037_NON_EXISTING_SCHEMA, schemaObject.getOid(), copy.getSchemaName()));
            ldapSchemaException.setSourceObject(schemaObject);
            ldapSchemaException.setRelatedId(copy.getSchemaName());
            errors.add(ldapSchemaException);
            return false;
        }
        // At this point, the constructed AttributeType has not been checked against the
        // existing Registries. It may be broken (missing SUP, or such), it will be checked
        // there, if the schema and the AttributeType are both enabled.
        Schema schema = getLoadedSchema(schemaName);
        if (schema == null) {
            // The SchemaObject must be associated with an existing schema
            String msg = I18n.err(I18n.ERR_16038_NOT_ASSOCIATED_TO_A_SCHEMA, copy.getOid());
            LOG.info(msg);
            Throwable error = new LdapProtocolErrorException(msg);
            errors.add(error);
            return false;
        }
        if (schema.isEnabled() && copy.isEnabled()) {
            // As we may break the registries, work on a cloned registries
            Registries clonedRegistries = null;
            try {
                clonedRegistries = registries.clone();
            } catch (CloneNotSupportedException cnse) {
                throw new LdapOtherException(cnse.getMessage(), cnse);
            }
            // Inject the new SchemaObject in the cloned registries
            clonedRegistries.add(errors, copy, true);
            // Remove the cloned registries
            clonedRegistries.clear();
            // If we didn't get any error, apply the addition to the real retistries
            if (errors.isEmpty()) {
                // Copy again as the clonedRegistries clear has removed the previous copy
                copy = copy(schemaObject);
                // Apply the addition to the real registries
                registries.add(errors, copy, true);
                LOG.debug(I18n.msg(I18n.MSG_16019_ENABLED_SCHEMA_ADDED, copy.getName(), schemaName));
                return true;
            } else {
                // We have some error : reject the addition and get out
                LOG.info(I18n.msg(I18n.MSG_16020_CANNOT_LOAD_SCHEMAOBJECT, copy.getOid(), Strings.listToString(errors)));
                return false;
            }
        } else {
            // At least, we register the OID in the globalOidRegistry, and associates it with the
            // schema
            registries.associateWithSchema(errors, copy);
            LOG.debug(I18n.msg(I18n.MSG_16021_ADDED_INTO_DISABLED_SCHEMA, copy.getName(), schemaName));
            return errors.isEmpty();
        }
    }
}
Also used : SchemaObject(org.apache.directory.api.ldap.model.schema.SchemaObject) LoadableSchemaObject(org.apache.directory.api.ldap.model.schema.LoadableSchemaObject) LdapProtocolErrorException(org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException) Schema(org.apache.directory.api.ldap.model.schema.registries.Schema) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException) Registries(org.apache.directory.api.ldap.model.schema.registries.Registries) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException)

Aggregations

LdapProtocolErrorException (org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException)14 SchemaManager (org.apache.directory.api.ldap.model.schema.SchemaManager)10 DefaultSchemaManager (org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager)10 Test (org.junit.Test)10 MatchingRule (org.apache.directory.api.ldap.model.schema.MatchingRule)4 LdapSyntax (org.apache.directory.api.ldap.model.schema.LdapSyntax)3 Schema (org.apache.directory.api.ldap.model.schema.registries.Schema)3 LdapOtherException (org.apache.directory.api.ldap.model.exception.LdapOtherException)2 LoadableSchemaObject (org.apache.directory.api.ldap.model.schema.LoadableSchemaObject)2 MutableAttributeType (org.apache.directory.api.ldap.model.schema.MutableAttributeType)2 Normalizer (org.apache.directory.api.ldap.model.schema.Normalizer)2 SchemaObject (org.apache.directory.api.ldap.model.schema.SchemaObject)2 SyntaxChecker (org.apache.directory.api.ldap.model.schema.SyntaxChecker)2 BooleanNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.BooleanNormalizer)2 Registries (org.apache.directory.api.ldap.model.schema.registries.Registries)2 BooleanSyntaxChecker (org.apache.directory.api.ldap.model.schema.syntaxCheckers.BooleanSyntaxChecker)2 AuthenticationException (javax.naming.AuthenticationException)1 AuthenticationNotSupportedException (javax.naming.AuthenticationNotSupportedException)1 CommunicationException (javax.naming.CommunicationException)1 ContextNotEmptyException (javax.naming.ContextNotEmptyException)1