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);
}
}
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;
}
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);
}
}
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();
}
}
}
}
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());
}
Aggregations