use of com.unboundid.ldap.sdk.schema.EntryValidator in project ldapsdk by pingidentity.
the class ValidateLDIF method doToolProcessing.
/**
* Performs the actual processing for this tool. In this case, it gets a
* connection to the directory server and uses it to retrieve the server
* schema. It then reads the LDIF file and validates each entry accordingly.
*
* @return The result code for the processing that was performed.
*/
@Override()
@NotNull()
public ResultCode doToolProcessing() {
// Get the connection to the directory server and use it to read the schema.
final Schema schema;
if (schemaDirectory.isPresent()) {
final File schemaDir = schemaDirectory.getValue();
try {
final TreeMap<String, File> fileMap = new TreeMap<>();
for (final File f : schemaDir.listFiles()) {
final String name = f.getName();
if (f.isFile() && name.endsWith(".ldif")) {
fileMap.put(name, f);
}
}
if (fileMap.isEmpty()) {
err("No LDIF files found in directory " + schemaDir.getAbsolutePath());
return ResultCode.PARAM_ERROR;
}
final ArrayList<File> fileList = new ArrayList<>(fileMap.values());
schema = Schema.getSchema(fileList);
} catch (final Exception e) {
Debug.debugException(e);
err("Unable to read schema from files in directory " + schemaDir.getAbsolutePath() + ": " + StaticUtils.getExceptionMessage(e));
return ResultCode.LOCAL_ERROR;
}
} else {
try {
final LDAPConnection connection = getConnection();
schema = connection.getSchema();
connection.close();
} catch (final LDAPException le) {
Debug.debugException(le);
err("Unable to connect to the directory server and read the schema: ", le.getMessage());
return le.getResultCode();
}
}
// Get the encryption passphrase, if it was provided.
String encryptionPassphrase = null;
if (encryptionPassphraseFile.isPresent()) {
try {
encryptionPassphrase = ToolUtils.readEncryptionPassphraseFromFile(encryptionPassphraseFile.getValue());
} catch (final LDAPException e) {
Debug.debugException(e);
err(e.getMessage());
return e.getResultCode();
}
}
// Create the entry validator and initialize its configuration.
entryValidator = new EntryValidator(schema);
entryValidator.setCheckAttributeSyntax(!ignoreAttributeSyntax.isPresent());
entryValidator.setCheckMalformedDNs(!ignoreMalformedDNs.isPresent());
entryValidator.setCheckEntryMissingRDNValues(!ignoreMissingRDNValues.isPresent());
entryValidator.setCheckMissingAttributes(!ignoreMissingAttributes.isPresent());
entryValidator.setCheckNameForms(!ignoreNameForms.isPresent());
entryValidator.setCheckProhibitedAttributes(!ignoreProhibitedAttributes.isPresent());
entryValidator.setCheckProhibitedObjectClasses(!ignoreProhibitedObjectClasses.isPresent());
entryValidator.setCheckMissingSuperiorObjectClasses(!ignoreMissingSuperiorObjectClasses.isPresent());
entryValidator.setCheckSingleValuedAttributes(!ignoreSingleValuedAttributes.isPresent());
entryValidator.setCheckStructuralObjectClasses(!ignoreStructuralObjectClasses.isPresent());
entryValidator.setCheckUndefinedAttributes(!ignoreUndefinedAttributes.isPresent());
entryValidator.setCheckUndefinedObjectClasses(!ignoreUndefinedObjectClasses.isPresent());
if (ignoreSyntaxViolationsForAttribute.isPresent()) {
entryValidator.setIgnoreSyntaxViolationAttributeTypes(ignoreSyntaxViolationsForAttribute.getValues());
}
// Create an LDIF reader that can be used to read through the LDIF file.
final LDIFReader ldifReader;
rejectWriter = null;
try {
InputStream inputStream = new FileInputStream(ldifFile.getValue());
inputStream = ToolUtils.getPossiblyPassphraseEncryptedInputStream(inputStream, encryptionPassphrase, false, "LDIF file '" + ldifFile.getValue().getPath() + "' is encrypted. Please enter the encryption passphrase:", "ERROR: The provided passphrase was incorrect.", getOut(), getErr()).getFirst();
if (isCompressed.isPresent()) {
inputStream = new GZIPInputStream(inputStream);
} else {
inputStream = ToolUtils.getPossiblyGZIPCompressedInputStream(inputStream);
}
ldifReader = new LDIFReader(inputStream, numThreads.getValue(), this);
} catch (final Exception e) {
Debug.debugException(e);
err("Unable to open the LDIF reader: ", StaticUtils.getExceptionMessage(e));
return ResultCode.LOCAL_ERROR;
}
ldifReader.setSchema(schema);
if (ignoreDuplicateValues.isPresent()) {
ldifReader.setDuplicateValueBehavior(DuplicateValueBehavior.STRIP);
} else {
ldifReader.setDuplicateValueBehavior(DuplicateValueBehavior.REJECT);
}
try {
// rejected entries.
try {
if (rejectFile.isPresent()) {
rejectWriter = new LDIFWriter(rejectFile.getValue());
}
} catch (final Exception e) {
Debug.debugException(e);
err("Unable to create the reject writer: ", StaticUtils.getExceptionMessage(e));
return ResultCode.LOCAL_ERROR;
}
ResultCode resultCode = ResultCode.SUCCESS;
while (true) {
try {
final Entry e = ldifReader.readEntry();
if (e == null) {
// through all of the entries to catch and report on those problems.
break;
}
} catch (final LDIFException le) {
Debug.debugException(le);
malformedEntries.incrementAndGet();
if (resultCode == ResultCode.SUCCESS) {
resultCode = ResultCode.DECODING_ERROR;
}
if (rejectWriter != null) {
try {
rejectWriter.writeComment("Unable to parse an entry read from LDIF:", false, false);
if (le.mayContinueReading()) {
rejectWriter.writeComment(StaticUtils.getExceptionMessage(le), false, true);
} else {
rejectWriter.writeComment(StaticUtils.getExceptionMessage(le), false, false);
rejectWriter.writeComment("Unable to continue LDIF processing.", false, true);
err("Aborting LDIF processing: ", StaticUtils.getExceptionMessage(le));
return ResultCode.LOCAL_ERROR;
}
} catch (final IOException ioe) {
Debug.debugException(ioe);
err("Unable to write to the reject file:", StaticUtils.getExceptionMessage(ioe));
err("LDIF parse failure that triggered the rejection: ", StaticUtils.getExceptionMessage(le));
return ResultCode.LOCAL_ERROR;
}
}
} catch (final IOException ioe) {
Debug.debugException(ioe);
if (rejectWriter != null) {
try {
rejectWriter.writeComment("I/O error reading from LDIF:", false, false);
rejectWriter.writeComment(StaticUtils.getExceptionMessage(ioe), false, true);
return ResultCode.LOCAL_ERROR;
} catch (final Exception ex) {
Debug.debugException(ex);
err("I/O error reading from LDIF:", StaticUtils.getExceptionMessage(ioe));
return ResultCode.LOCAL_ERROR;
}
}
}
}
if (malformedEntries.get() > 0) {
out(malformedEntries.get() + " entries were malformed and could not " + "be read from the LDIF file.");
}
if (entryValidator.getInvalidEntries() > 0) {
if (resultCode == ResultCode.SUCCESS) {
resultCode = ResultCode.OBJECT_CLASS_VIOLATION;
}
for (final String s : entryValidator.getInvalidEntrySummary(true)) {
out(s);
}
} else {
if (malformedEntries.get() == 0) {
out("No errors were encountered.");
}
}
return resultCode;
} finally {
try {
ldifReader.close();
} catch (final Exception e) {
Debug.debugException(e);
}
try {
if (rejectWriter != null) {
rejectWriter.close();
}
} catch (final Exception e) {
Debug.debugException(e);
}
}
}
Aggregations