use of com.unboundid.ldif.AggregateLDIFReaderEntryTranslator 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;
}
Aggregations