Search in sources :

Example 1 with LDIFReaderChangeRecordTranslator

use of com.unboundid.ldif.LDIFReaderChangeRecordTranslator in project ldapsdk by pingidentity.

the class TransformLDIF method doToolProcessing.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
public ResultCode doToolProcessing() {
    final Schema schema;
    try {
        schema = getSchema();
    } catch (final LDAPException le) {
        wrapErr(0, MAX_OUTPUT_LINE_LENGTH, le.getMessage());
        return le.getResultCode();
    }
    // If an encryption passphrase file is provided, then get the passphrase
    // from it.
    String encryptionPassphrase = null;
    if (encryptionPassphraseFile.isPresent()) {
        try {
            encryptionPassphrase = ToolUtils.readEncryptionPassphraseFromFile(encryptionPassphraseFile.getValue());
        } catch (final LDAPException e) {
            wrapErr(0, MAX_OUTPUT_LINE_LENGTH, e.getMessage());
            return e.getResultCode();
        }
    }
    // Create the translators to use to apply the transformations.
    final ArrayList<LDIFReaderEntryTranslator> entryTranslators = new ArrayList<>(10);
    final ArrayList<LDIFReaderChangeRecordTranslator> changeRecordTranslators = new ArrayList<>(10);
    final AtomicLong excludedEntryCount = new AtomicLong(0L);
    createTranslators(entryTranslators, changeRecordTranslators, schema, excludedEntryCount);
    final AggregateLDIFReaderEntryTranslator entryTranslator = new AggregateLDIFReaderEntryTranslator(entryTranslators);
    final AggregateLDIFReaderChangeRecordTranslator changeRecordTranslator = new AggregateLDIFReaderChangeRecordTranslator(changeRecordTranslators);
    // Determine the path to the target file to be written.
    final File targetFile;
    if (targetLDIF.isPresent()) {
        targetFile = targetLDIF.getValue();
    } else if (targetToStandardOutput.isPresent()) {
        targetFile = null;
    } else {
        targetFile = new File(sourceLDIF.getValue().getAbsolutePath() + ".scrambled");
    }
    // Create the LDIF reader.
    final LDIFReader ldifReader;
    try {
        final InputStream inputStream;
        if (sourceLDIF.isPresent()) {
            final ObjectPair<InputStream, String> p = ToolUtils.getInputStreamForLDIFFiles(sourceLDIF.getValues(), encryptionPassphrase, getOut(), getErr());
            inputStream = p.getFirst();
            if ((encryptionPassphrase == null) && (p.getSecond() != null)) {
                encryptionPassphrase = p.getSecond();
            }
        } else {
            inputStream = System.in;
        }
        ldifReader = new LDIFReader(inputStream, numThreads.getValue(), entryTranslator, changeRecordTranslator);
        if (schema != null) {
            ldifReader.setSchema(schema);
        }
    } catch (final Exception e) {
        Debug.debugException(e);
        wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ERR_TRANSFORM_LDIF_ERROR_CREATING_LDIF_READER.get(StaticUtils.getExceptionMessage(e)));
        return ResultCode.LOCAL_ERROR;
    }
    ResultCode resultCode = ResultCode.SUCCESS;
    OutputStream outputStream = null;
    processingBlock: try {
        // Create the output stream to use to write the transformed data.
        try {
            if (targetFile == null) {
                outputStream = getOut();
            } else {
                outputStream = new FileOutputStream(targetFile, appendToTargetLDIF.isPresent());
            }
            if (encryptTarget.isPresent()) {
                if (encryptionPassphrase == null) {
                    encryptionPassphrase = ToolUtils.promptForEncryptionPassphrase(false, true, getOut(), getErr());
                }
                outputStream = new PassphraseEncryptedOutputStream(encryptionPassphrase, outputStream);
            }
            if (compressTarget.isPresent()) {
                outputStream = new GZIPOutputStream(outputStream);
            }
        } catch (final Exception e) {
            Debug.debugException(e);
            wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ERR_TRANSFORM_LDIF_ERROR_CREATING_OUTPUT_STREAM.get(targetFile.getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
            resultCode = ResultCode.LOCAL_ERROR;
            break processingBlock;
        }
        // Read the source data one record at a time.  The transformations will
        // automatically be applied by the LDIF reader's translators, and even if
        // there are multiple reader threads, we're guaranteed to get the results
        // in the right order.
        long entriesWritten = 0L;
        while (true) {
            final LDIFRecord ldifRecord;
            try {
                ldifRecord = ldifReader.readLDIFRecord();
            } catch (final LDIFException le) {
                Debug.debugException(le);
                if (le.mayContinueReading()) {
                    wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ERR_TRANSFORM_LDIF_RECOVERABLE_MALFORMED_RECORD.get(StaticUtils.getExceptionMessage(le)));
                    if (resultCode == ResultCode.SUCCESS) {
                        resultCode = ResultCode.PARAM_ERROR;
                    }
                    continue;
                } else {
                    wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ERR_TRANSFORM_LDIF_UNRECOVERABLE_MALFORMED_RECORD.get(StaticUtils.getExceptionMessage(le)));
                    if (resultCode == ResultCode.SUCCESS) {
                        resultCode = ResultCode.PARAM_ERROR;
                    }
                    break processingBlock;
                }
            } catch (final Exception e) {
                Debug.debugException(e);
                wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ERR_TRANSFORM_LDIF_UNEXPECTED_READ_ERROR.get(StaticUtils.getExceptionMessage(e)));
                resultCode = ResultCode.LOCAL_ERROR;
                break processingBlock;
            }
            // done.
            if (ldifRecord == null) {
                break;
            }
            // Write the record to the output stream.
            try {
                if (ldifRecord instanceof PreEncodedLDIFEntry) {
                    outputStream.write(((PreEncodedLDIFEntry) ldifRecord).getLDIFBytes());
                } else {
                    final ByteStringBuffer buffer = getBuffer();
                    if (wrapColumn.isPresent()) {
                        ldifRecord.toLDIF(buffer, wrapColumn.getValue());
                    } else {
                        ldifRecord.toLDIF(buffer, 0);
                    }
                    buffer.append(StaticUtils.EOL_BYTES);
                    buffer.write(outputStream);
                }
            } catch (final Exception e) {
                Debug.debugException(e);
                wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ERR_TRANSFORM_LDIF_WRITE_ERROR.get(targetFile.getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
                resultCode = ResultCode.LOCAL_ERROR;
                break processingBlock;
            }
            // If we've written a multiple of 1000 entries, print a progress
            // message.
            entriesWritten++;
            if ((!targetToStandardOutput.isPresent()) && ((entriesWritten % 1000L) == 0)) {
                final long numExcluded = excludedEntryCount.get();
                if (numExcluded > 0L) {
                    wrapOut(0, MAX_OUTPUT_LINE_LENGTH, INFO_TRANSFORM_LDIF_WROTE_ENTRIES_WITH_EXCLUDED.get(entriesWritten, numExcluded));
                } else {
                    wrapOut(0, MAX_OUTPUT_LINE_LENGTH, INFO_TRANSFORM_LDIF_WROTE_ENTRIES_NONE_EXCLUDED.get(entriesWritten));
                }
            }
        }
        if (!targetToStandardOutput.isPresent()) {
            final long numExcluded = excludedEntryCount.get();
            if (numExcluded > 0L) {
                wrapOut(0, MAX_OUTPUT_LINE_LENGTH, INFO_TRANSFORM_LDIF_COMPLETE_WITH_EXCLUDED.get(entriesWritten, numExcluded));
            } else {
                wrapOut(0, MAX_OUTPUT_LINE_LENGTH, INFO_TRANSFORM_LDIF_COMPLETE_NONE_EXCLUDED.get(entriesWritten));
            }
        }
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (final Exception e) {
                Debug.debugException(e);
                wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ERR_TRANSFORM_LDIF_ERROR_CLOSING_OUTPUT_STREAM.get(targetFile.getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
                if (resultCode == ResultCode.SUCCESS) {
                    resultCode = ResultCode.LOCAL_ERROR;
                }
            }
        }
        try {
            ldifReader.close();
        } catch (final Exception e) {
            Debug.debugException(e);
        // We can ignore this.
        }
    }
    return resultCode;
}
Also used : Schema(com.unboundid.ldap.sdk.schema.Schema) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) AggregateLDIFReaderChangeRecordTranslator(com.unboundid.ldif.AggregateLDIFReaderChangeRecordTranslator) LDIFReaderChangeRecordTranslator(com.unboundid.ldif.LDIFReaderChangeRecordTranslator) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) LDIFReaderEntryTranslator(com.unboundid.ldif.LDIFReaderEntryTranslator) AggregateLDIFReaderEntryTranslator(com.unboundid.ldif.AggregateLDIFReaderEntryTranslator) GZIPOutputStream(java.util.zip.GZIPOutputStream) LDIFRecord(com.unboundid.ldif.LDIFRecord) LDIFException(com.unboundid.ldif.LDIFException) ByteStringBuffer(com.unboundid.util.ByteStringBuffer) InputStream(java.io.InputStream) LDIFException(com.unboundid.ldif.LDIFException) ArgumentException(com.unboundid.util.args.ArgumentException) LDAPException(com.unboundid.ldap.sdk.LDAPException) AtomicLong(java.util.concurrent.atomic.AtomicLong) LDAPException(com.unboundid.ldap.sdk.LDAPException) AggregateLDIFReaderChangeRecordTranslator(com.unboundid.ldif.AggregateLDIFReaderChangeRecordTranslator) LDIFReader(com.unboundid.ldif.LDIFReader) FileOutputStream(java.io.FileOutputStream) AggregateLDIFReaderEntryTranslator(com.unboundid.ldif.AggregateLDIFReaderEntryTranslator) File(java.io.File) ResultCode(com.unboundid.ldap.sdk.ResultCode) NotNull(com.unboundid.util.NotNull)

Aggregations

LDAPException (com.unboundid.ldap.sdk.LDAPException)1 ResultCode (com.unboundid.ldap.sdk.ResultCode)1 Schema (com.unboundid.ldap.sdk.schema.Schema)1 AggregateLDIFReaderChangeRecordTranslator (com.unboundid.ldif.AggregateLDIFReaderChangeRecordTranslator)1 AggregateLDIFReaderEntryTranslator (com.unboundid.ldif.AggregateLDIFReaderEntryTranslator)1 LDIFException (com.unboundid.ldif.LDIFException)1 LDIFReader (com.unboundid.ldif.LDIFReader)1 LDIFReaderChangeRecordTranslator (com.unboundid.ldif.LDIFReaderChangeRecordTranslator)1 LDIFReaderEntryTranslator (com.unboundid.ldif.LDIFReaderEntryTranslator)1 LDIFRecord (com.unboundid.ldif.LDIFRecord)1 ByteStringBuffer (com.unboundid.util.ByteStringBuffer)1 NotNull (com.unboundid.util.NotNull)1 PassphraseEncryptedOutputStream (com.unboundid.util.PassphraseEncryptedOutputStream)1 ArgumentException (com.unboundid.util.args.ArgumentException)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1