use of com.unboundid.ldif.LDIFRecord 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;
}
use of com.unboundid.ldif.LDIFRecord in project ldapsdk by pingidentity.
the class TransformLDIFTestCase method testExcludeRecordsWithoutChangeType.
/**
* Tests the ability to exclude LDIF records without change types.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testExcludeRecordsWithoutChangeType() throws Exception {
// Create the LDIF file to process.
final File sourceLDIFFile = createTempFile("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example", "", "dn: ou=People,dc=example,dc=com", "changetype: add", "objectClass: top", "objectClass: organizationalUnit", "ou: People", "", "dn: ou=People,dc=example,dc=com", "changetype: modify", "add: description", "description: foo", "", "dn: ou=People,dc=example,dc=com", "changetype: moddn", "newrdn: ou=Users", "deleteoldrdn: 1", "", "dn: ou=Users,dc=example,dc=com", "changetype: delete");
final File outputFile = runTool("--sourceLDIF", sourceLDIFFile.getAbsolutePath(), "--excludeRecordsWithoutChangeType");
final LDIFReader reader = new LDIFReader(outputFile);
LDIFRecord r = reader.readLDIFRecord();
assertNotNull(r);
assertTrue(r instanceof LDIFAddChangeRecord);
r = reader.readLDIFRecord();
assertNotNull(r);
assertTrue(r instanceof LDIFModifyChangeRecord);
r = reader.readLDIFRecord();
assertNotNull(r);
assertTrue(r instanceof LDIFModifyDNChangeRecord);
r = reader.readLDIFRecord();
assertNotNull(r);
assertTrue(r instanceof LDIFDeleteChangeRecord);
assertNull(reader.readEntry());
reader.close();
}
use of com.unboundid.ldif.LDIFRecord in project ldapsdk by pingidentity.
the class TransformLDIFTestCase method testExcludeChangeType.
/**
* Tests the ability to exclude LDIF records with a specified set of change
* types.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testExcludeChangeType() throws Exception {
// Create the LDIF file to process.
final File sourceLDIFFile = createTempFile("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example", "", "dn: ou=People,dc=example,dc=com", "changetype: add", "objectClass: top", "objectClass: organizationalUnit", "ou: People", "", "dn: ou=People,dc=example,dc=com", "changetype: modify", "add: description", "description: foo", "", "dn: ou=People,dc=example,dc=com", "changetype: moddn", "newrdn: ou=Users", "deleteoldrdn: 1", "", "dn: ou=Users,dc=example,dc=com", "changetype: delete");
final File outputFile = runTool("--sourceLDIF", sourceLDIFFile.getAbsolutePath(), "--excludeChangeType", "add", "--excludeChangeType", "delete");
final LDIFReader reader = new LDIFReader(outputFile);
LDIFRecord r = reader.readLDIFRecord();
assertNotNull(r);
assertTrue(r instanceof Entry);
r = reader.readLDIFRecord();
assertNotNull(r);
assertTrue(r instanceof LDIFModifyChangeRecord);
r = reader.readLDIFRecord();
assertNotNull(r);
assertTrue(r instanceof LDIFModifyDNChangeRecord);
assertNull(reader.readEntry());
reader.close();
}
use of com.unboundid.ldif.LDIFRecord in project ldapsdk by pingidentity.
the class GenerateSchemaFromSource method doToolProcessing.
/**
* {@inheritDoc}
*/
@Override()
@NotNull()
public ResultCode doToolProcessing() {
// Load the specified Java class.
final String className = classNameArg.getValue();
final Class<?> targetClass;
try {
targetClass = Class.forName(className);
} catch (final Exception e) {
Debug.debugException(e);
err(ERR_GEN_SCHEMA_CANNOT_LOAD_CLASS.get(className));
return ResultCode.PARAM_ERROR;
}
// Create an LDAP persister for the class and use it to ensure that the
// class is valid.
final LDAPPersister<?> persister;
try {
persister = LDAPPersister.getInstance(targetClass);
} catch (final Exception e) {
Debug.debugException(e);
err(ERR_GEN_SCHEMA_INVALID_CLASS.get(className, StaticUtils.getExceptionMessage(e)));
return ResultCode.LOCAL_ERROR;
}
// Use the persister to generate the attribute type and object class
// definitions.
final List<AttributeTypeDefinition> attrTypes;
try {
attrTypes = persister.constructAttributeTypes();
} catch (final Exception e) {
Debug.debugException(e);
err(ERR_GEN_SCHEMA_ERROR_CONSTRUCTING_ATTRS.get(className, StaticUtils.getExceptionMessage(e)));
return ResultCode.LOCAL_ERROR;
}
final List<ObjectClassDefinition> objectClasses;
try {
objectClasses = persister.constructObjectClasses();
} catch (final Exception e) {
Debug.debugException(e);
err(ERR_GEN_SCHEMA_ERROR_CONSTRUCTING_OCS.get(className, StaticUtils.getExceptionMessage(e)));
return ResultCode.LOCAL_ERROR;
}
// Convert the attribute type and object class definitions into their
// appropriate string representations.
int i = 0;
final ASN1OctetString[] attrTypeValues = new ASN1OctetString[attrTypes.size()];
for (final AttributeTypeDefinition d : attrTypes) {
attrTypeValues[i++] = new ASN1OctetString(d.toString());
}
i = 0;
final ASN1OctetString[] ocValues = new ASN1OctetString[objectClasses.size()];
for (final ObjectClassDefinition d : objectClasses) {
ocValues[i++] = new ASN1OctetString(d.toString());
}
// Construct the LDIF record to be written.
final LDIFRecord schemaRecord;
if (modifyFormatArg.isPresent()) {
schemaRecord = new LDIFModifyChangeRecord("cn=schema", new Modification(ModificationType.ADD, "attributeTypes", attrTypeValues), new Modification(ModificationType.ADD, "objectClasses", ocValues));
} else {
schemaRecord = new Entry("cn=schema", new Attribute("objectClass", "top", "ldapSubentry", "subschema"), new Attribute("cn", "schema"), new Attribute("attributeTypes", attrTypeValues), new Attribute("objectClasses", ocValues));
}
// Write the schema entry to the specified file.
final File outputFile = outputFileArg.getValue();
try {
final LDIFWriter ldifWriter = new LDIFWriter(outputFile);
ldifWriter.writeLDIFRecord(schemaRecord);
ldifWriter.close();
} catch (final Exception e) {
Debug.debugException(e);
err(ERR_GEN_SCHEMA_CANNOT_WRITE_SCHEMA.get(outputFile.getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
return ResultCode.LOCAL_ERROR;
}
return ResultCode.SUCCESS;
}
Aggregations