Search in sources :

Example 6 with EntryValidator

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);
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Schema(com.unboundid.ldap.sdk.schema.Schema) ArrayList(java.util.ArrayList) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) IOException(java.io.IOException) TreeMap(java.util.TreeMap) EntryValidator(com.unboundid.ldap.sdk.schema.EntryValidator) LDIFException(com.unboundid.ldif.LDIFException) ArgumentException(com.unboundid.util.args.ArgumentException) LDAPException(com.unboundid.ldap.sdk.LDAPException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) Entry(com.unboundid.ldap.sdk.Entry) LDAPException(com.unboundid.ldap.sdk.LDAPException) LDIFReader(com.unboundid.ldif.LDIFReader) LDIFException(com.unboundid.ldif.LDIFException) LDIFWriter(com.unboundid.ldif.LDIFWriter) File(java.io.File) ResultCode(com.unboundid.ldap.sdk.ResultCode) NotNull(com.unboundid.util.NotNull)

Aggregations

EntryValidator (com.unboundid.ldap.sdk.schema.EntryValidator)6 Entry (com.unboundid.ldap.sdk.Entry)5 LDAPException (com.unboundid.ldap.sdk.LDAPException)5 Schema (com.unboundid.ldap.sdk.schema.Schema)5 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)4 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)3 LDAPMessage (com.unboundid.ldap.protocol.LDAPMessage)3 Attribute (com.unboundid.ldap.sdk.Attribute)3 ChangeLogEntry (com.unboundid.ldap.sdk.ChangeLogEntry)3 Control (com.unboundid.ldap.sdk.Control)3 DN (com.unboundid.ldap.sdk.DN)3 RDN (com.unboundid.ldap.sdk.RDN)3 ReadOnlyEntry (com.unboundid.ldap.sdk.ReadOnlyEntry)3 AssertionRequestControl (com.unboundid.ldap.sdk.controls.AssertionRequestControl)3 AuthorizationIdentityRequestControl (com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl)3 AuthorizationIdentityResponseControl (com.unboundid.ldap.sdk.controls.AuthorizationIdentityResponseControl)3 DontUseCopyRequestControl (com.unboundid.ldap.sdk.controls.DontUseCopyRequestControl)3 DraftLDUPSubentriesRequestControl (com.unboundid.ldap.sdk.controls.DraftLDUPSubentriesRequestControl)3 ManageDsaITRequestControl (com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl)3 PermissiveModifyRequestControl (com.unboundid.ldap.sdk.controls.PermissiveModifyRequestControl)3