Search in sources :

Example 21 with LdapException

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

the class SchemaEntityFactory method getNormalizer.

/**
 * {@inheritDoc}
 */
@Override
public Normalizer getNormalizer(SchemaManager schemaManager, Entry entry, Registries targetRegistries, String schemaName) throws LdapException {
    checkEntry(entry, SchemaConstants.NORMALIZER);
    // The Normalizer OID
    String oid = getOid(entry, SchemaConstants.NORMALIZER, schemaManager.isStrict());
    // Get the schema
    if (!schemaManager.isSchemaLoaded(schemaName)) {
        // The schema is not loaded. We can't create the requested Normalizer
        String msg = I18n.err(I18n.ERR_16024_CANNOT_ADD_NORMALIZER, 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_16025_CANNOT_ADD_NORMALIZER_IN_REGISTRY, entry.getDn().getName(), schemaName);
        LOG.info(msg);
        schema = schemaManager.getLoadedSchema(schemaName);
    }
    // The FQCN
    String className = getFqcn(entry, SchemaConstants.NORMALIZER);
    // The ByteCode
    Attribute byteCode = entry.get(MetaSchemaConstants.M_BYTECODE_AT);
    try {
        // Class load the Normalizer
        Normalizer normalizer = classLoadNormalizer(schemaManager, oid, className, byteCode);
        // Update the common fields
        setSchemaObjectProperties(normalizer, entry, schema);
        // return the resulting Normalizer
        return normalizer;
    } catch (Exception e) {
        throw new LdapUnwillingToPerformException(ResultCodeEnum.UNWILLING_TO_PERFORM, e.getMessage(), e);
    }
}
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) Normalizer(org.apache.directory.api.ldap.model.schema.Normalizer) DefaultSchema(org.apache.directory.api.ldap.model.schema.registries.DefaultSchema) Schema(org.apache.directory.api.ldap.model.schema.registries.Schema) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) InvocationTargetException(java.lang.reflect.InvocationTargetException) LdapUnwillingToPerformException(org.apache.directory.api.ldap.model.exception.LdapUnwillingToPerformException) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 22 with LdapException

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

the class DefaultSchemaManager method verify.

/**
 * {@inheritDoc}
 */
@Override
public boolean verify(Schema... schemas) throws LdapException {
    // Work on a cloned registries
    Registries clonedRegistries = cloneRegistries();
    // Loop on all the schemas
    for (Schema schema : schemas) {
        try {
            // Inject the schema
            boolean loaded = load(clonedRegistries, schema);
            if (!loaded) {
                // We got an error : exit
                clonedRegistries.clear();
                return false;
            }
            // Now, check the registries
            List<Throwable> errorList = clonedRegistries.checkRefInteg();
            if (!errorList.isEmpty()) {
                // We got an error : exit
                clonedRegistries.clear();
                return false;
            }
        } catch (Exception e) {
            // We got an error : exit
            clonedRegistries.clear();
            return false;
        }
    }
    // We can now delete the cloned registries before exiting
    clonedRegistries.clear();
    return true;
}
Also used : Schema(org.apache.directory.api.ldap.model.schema.registries.Schema) Registries(org.apache.directory.api.ldap.model.schema.registries.Registries) LdapProtocolErrorException(org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException) LdapUnwillingToPerformException(org.apache.directory.api.ldap.model.exception.LdapUnwillingToPerformException) IOException(java.io.IOException) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 23 with LdapException

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

the class DefaultSchemaLdifExtractor method copyFile.

/**
 * Copies a file line by line from the source file argument to the
 * destination file argument.
 *
 * @param source the source file to copy
 * @param destination the destination to copy the source to
 * @throws IOException if there are IO errors or the source does not exist
 */
private void copyFile(File source, File destination) throws IOException {
    LOG.debug(I18n.msg(I18n.MSG_16003_COPYFILE, source, destination));
    if (!destination.getParentFile().exists() && !destination.getParentFile().mkdirs()) {
        throw new IOException(I18n.err(I18n.ERR_16006_DIRECTORY_CREATION_FAILED, destination.getParentFile().getAbsolutePath()));
    }
    if (!source.getParentFile().exists()) {
        throw new FileNotFoundException(I18n.err(I18n.ERR_16001_CANNOT_COPY_NON_EXISTENT, source.getAbsolutePath()));
    }
    try (Writer out = new OutputStreamWriter(Files.newOutputStream(Paths.get(destination.getPath())), Charset.defaultCharset());
        LdifReader ldifReader = new LdifReader(source)) {
        boolean first = true;
        LdifEntry ldifEntry = null;
        while (ldifReader.hasNext()) {
            if (first) {
                ldifEntry = ldifReader.next();
                if (ldifEntry.get(SchemaConstants.ENTRY_UUID_AT) == null) {
                    // No UUID, let's create one
                    UUID entryUuid = UUID.randomUUID();
                    ldifEntry.addAttribute(SchemaConstants.ENTRY_UUID_AT, entryUuid.toString());
                }
                first = false;
            } else {
                // throw an exception : we should not have more than one entry per schema ldif file
                String msg = I18n.err(I18n.ERR_16002_MORE_THAN_ONE_ENTRY, source);
                LOG.error(msg);
                throw new InvalidObjectException(msg);
            }
        }
        // Add the version at the first line, to avoid a warning
        String ldifString;
        if (ldifEntry != null) {
            ldifString = "version: 1\n" + ldifEntry.toString();
        } else {
            ldifString = "version: 1\n";
        }
        out.write(ldifString);
        out.flush();
    } catch (LdapException le) {
        String msg = I18n.err(I18n.ERR_16003_ERROR_PARSING_LDIF, source, le.getLocalizedMessage());
        LOG.error(msg);
        throw new InvalidObjectException(msg);
    }
}
Also used : LdifReader(org.apache.directory.api.ldap.model.ldif.LdifReader) FileNotFoundException(java.io.FileNotFoundException) OutputStreamWriter(java.io.OutputStreamWriter) InvalidObjectException(java.io.InvalidObjectException) IOException(java.io.IOException) UUID(java.util.UUID) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) LdifEntry(org.apache.directory.api.ldap.model.ldif.LdifEntry)

Example 24 with LdapException

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

the class JarLdifSchemaLoader method initializeSchemas.

/**
 * Scans for LDIF files just describing the various schema contained in
 * the schema repository.
 *
 * @throws LdapException
 */
private void initializeSchemas() throws IOException, LdapException {
    if (IS_DEBUG) {
        LOG.debug(I18n.msg(I18n.MSG_16006_INITIALIZING_SCHEMA));
    }
    Pattern pat = Pattern.compile("schema" + SEPARATOR_PATTERN + "ou=schema" + SEPARATOR_PATTERN + "cn=[a-z0-9-_]*\\." + LDIF_EXT);
    for (String file : RESOURCE_MAP.keySet()) {
        if (pat.matcher(file).matches()) {
            URL resource = getResource(file, "schema LDIF file");
            InputStream in = resource.openStream();
            try {
                LdifReader reader = new LdifReader(in);
                LdifEntry entry = reader.next();
                reader.close();
                Schema schema = getSchema(entry.getEntry());
                schemaMap.put(schema.getSchemaName(), schema);
                if (IS_DEBUG) {
                    LOG.debug(I18n.msg(I18n.MSG_16007_SCHEMA_INITIALIZED, schema));
                }
            } catch (LdapException le) {
                LOG.error(I18n.err(I18n.ERR_16009_LDIF_LOAD_FAIL, file), le);
                throw le;
            } finally {
                in.close();
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) LdifReader(org.apache.directory.api.ldap.model.ldif.LdifReader) InputStream(java.io.InputStream) Schema(org.apache.directory.api.ldap.model.schema.registries.Schema) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) URL(java.net.URL) LdifEntry(org.apache.directory.api.ldap.model.ldif.LdifEntry)

Example 25 with LdapException

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

the class ModifyRequestTest method testRequestWith1ModificationBase64Value.

/**
 * Test parsing of a request with a Modification element with Base64 Value
 * @throws NamingException
 */
@Test
public void testRequestWith1ModificationBase64Value() throws LdapException {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(ModifyRequestTest.class.getResource("request_with_1_modification_base64_value.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    ModifyRequest modifyRequest = (ModifyRequest) parser.getBatchRequest().getCurrentRequest();
    Collection<Modification> modifications = modifyRequest.getModifications();
    assertEquals(1, modifications.size());
    Modification modification = modifications.iterator().next();
    Attribute attribute = modification.getAttribute();
    assertEquals(ModificationOperation.ADD_ATTRIBUTE, modification.getOperation());
    assertEquals("directreport", attribute.getId());
    String expected = new String(new byte[] { 'c', 'n', '=', 'E', 'm', 'm', 'a', 'n', 'u', 'e', 'l', ' ', 'L', (byte) 0xc3, (byte) 0xa9, 'c', 'h', 'a', 'r', 'n', 'y', ',', ' ', 'o', 'u', '=', 'p', 'e', 'o', 'p', 'l', 'e', ',', ' ', 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', ' ', 'd', 'c', '=', 'c', 'o', 'm' }, StandardCharsets.UTF_8);
    assertEquals(expected, attribute.get().getValue());
}
Also used : Modification(org.apache.directory.api.ldap.model.entry.Modification) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Aggregations

LdapException (org.apache.directory.api.ldap.model.exception.LdapException)329 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)159 ArrayList (java.util.ArrayList)93 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)91 FinderException (org.apache.directory.fortress.core.FinderException)73 Entry (org.apache.directory.api.ldap.model.entry.Entry)63 Modification (org.apache.directory.api.ldap.model.entry.Modification)63 IOException (java.io.IOException)54 SearchCursor (org.apache.directory.api.ldap.model.cursor.SearchCursor)51 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)50 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)49 UpdateException (org.apache.directory.fortress.core.UpdateException)41 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)35 Dn (org.apache.directory.api.ldap.model.name.Dn)34 LdapAuthenticationException (org.apache.directory.api.ldap.model.exception.LdapAuthenticationException)25 AttributeType (org.apache.directory.api.ldap.model.schema.AttributeType)25 DecoderException (org.apache.directory.api.asn1.DecoderException)22 LdapNoPermissionException (org.apache.directory.api.ldap.model.exception.LdapNoPermissionException)22 LdapOtherException (org.apache.directory.api.ldap.model.exception.LdapOtherException)22 ConnectException (java.net.ConnectException)21