use of com.unboundid.ldif.LDIFModifyDNChangeRecord in project ldapsdk by pingidentity.
the class ExcludeChangeTypeTransformationTestCase method testExcludeModifyDNChangeType.
/**
* Tests the behavior when the set of change types to exclude contains only
* the modify DN change type.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testExcludeModifyDNChangeType() throws Exception {
final ExcludeChangeTypeTransformation t = new ExcludeChangeTypeTransformation(ChangeType.MODIFY_DN);
final LDIFAddChangeRecord addChangeRecord = new LDIFAddChangeRecord(new Entry("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example"));
assertNotNull(t.transformChangeRecord(addChangeRecord));
assertEquals(t.transformChangeRecord(addChangeRecord), addChangeRecord);
assertNotNull(t.translate(addChangeRecord, 0));
assertEquals(t.translate(addChangeRecord, 0), addChangeRecord);
assertNotNull(t.translateChangeRecordToWrite(addChangeRecord));
assertEquals(t.translateChangeRecordToWrite(addChangeRecord), addChangeRecord);
final LDIFDeleteChangeRecord deleteChangeRecord = new LDIFDeleteChangeRecord("dc=example,dc=com");
assertNotNull(t.transformChangeRecord(deleteChangeRecord));
assertEquals(t.transformChangeRecord(deleteChangeRecord), deleteChangeRecord);
assertNotNull(t.translate(deleteChangeRecord, 0));
assertEquals(t.translate(deleteChangeRecord, 0), deleteChangeRecord);
assertNotNull(t.translateChangeRecordToWrite(deleteChangeRecord));
assertEquals(t.translateChangeRecordToWrite(deleteChangeRecord), deleteChangeRecord);
final LDIFModifyChangeRecord modifyChangeRecord = new LDIFModifyChangeRecord(new ModifyRequest("dn: dc=example,dc=com", "changetype: modify", "replace: description", "description: foo"));
assertNotNull(t.transformChangeRecord(modifyChangeRecord));
assertEquals(t.transformChangeRecord(modifyChangeRecord), modifyChangeRecord);
assertNotNull(t.translate(modifyChangeRecord, 0));
assertEquals(t.translate(modifyChangeRecord, 0), modifyChangeRecord);
assertNotNull(t.translateChangeRecordToWrite(modifyChangeRecord));
assertEquals(t.translateChangeRecordToWrite(modifyChangeRecord), modifyChangeRecord);
final LDIFModifyDNChangeRecord modifyDNChangeRecord = new LDIFModifyDNChangeRecord("ou=People,dc=example,dc=com", "ou=Users", true, null);
assertNull(t.transformChangeRecord(modifyDNChangeRecord));
assertNull(t.translate(modifyDNChangeRecord, 0));
assertNull(t.translateChangeRecordToWrite(modifyDNChangeRecord));
}
use of com.unboundid.ldif.LDIFModifyDNChangeRecord in project ldapsdk by pingidentity.
the class ExcludeChangeTypeTransformationTestCase method testExcludeDeleteChangeType.
/**
* Tests the behavior when the set of change types to exclude contains only
* the delete change type.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testExcludeDeleteChangeType() throws Exception {
final ExcludeChangeTypeTransformation t = new ExcludeChangeTypeTransformation(ChangeType.DELETE);
final LDIFAddChangeRecord addChangeRecord = new LDIFAddChangeRecord(new Entry("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example"));
assertNotNull(t.transformChangeRecord(addChangeRecord));
assertEquals(t.transformChangeRecord(addChangeRecord), addChangeRecord);
assertNotNull(t.translate(addChangeRecord, 0));
assertEquals(t.translate(addChangeRecord, 0), addChangeRecord);
assertNotNull(t.translateChangeRecordToWrite(addChangeRecord));
assertEquals(t.translateChangeRecordToWrite(addChangeRecord), addChangeRecord);
final LDIFDeleteChangeRecord deleteChangeRecord = new LDIFDeleteChangeRecord("dc=example,dc=com");
assertNull(t.transformChangeRecord(deleteChangeRecord));
assertNull(t.translate(deleteChangeRecord, 0));
assertNull(t.translateChangeRecordToWrite(deleteChangeRecord));
final LDIFModifyChangeRecord modifyChangeRecord = new LDIFModifyChangeRecord(new ModifyRequest("dn: dc=example,dc=com", "changetype: modify", "replace: description", "description: foo"));
assertNotNull(t.transformChangeRecord(modifyChangeRecord));
assertEquals(t.transformChangeRecord(modifyChangeRecord), modifyChangeRecord);
assertNotNull(t.translate(modifyChangeRecord, 0));
assertEquals(t.translate(modifyChangeRecord, 0), modifyChangeRecord);
assertNotNull(t.translateChangeRecordToWrite(modifyChangeRecord));
assertEquals(t.translateChangeRecordToWrite(modifyChangeRecord), modifyChangeRecord);
final LDIFModifyDNChangeRecord modifyDNChangeRecord = new LDIFModifyDNChangeRecord("ou=People,dc=example,dc=com", "ou=Users", true, null);
assertNotNull(t.transformChangeRecord(modifyDNChangeRecord));
assertEquals(t.transformChangeRecord(modifyDNChangeRecord), modifyDNChangeRecord);
assertNotNull(t.translate(modifyDNChangeRecord, 0));
assertEquals(t.translate(modifyDNChangeRecord, 0), modifyDNChangeRecord);
assertNotNull(t.translateChangeRecordToWrite(modifyDNChangeRecord));
assertEquals(t.translateChangeRecordToWrite(modifyDNChangeRecord), modifyDNChangeRecord);
}
use of com.unboundid.ldif.LDIFModifyDNChangeRecord in project ldapsdk by pingidentity.
the class InMemoryRequestHandler method addChangeLogEntry.
/**
* Creates a changelog entry from the information in the provided modify DN
* request and adds it to the server changelog.
*
* @param modifyDNRequest The modify DN request to use to construct the
* changelog entry.
* @param authzDN The authorization DN for the change.
*/
private void addChangeLogEntry(@NotNull final ModifyDNRequestProtocolOp modifyDNRequest, @NotNull final DN authzDN) {
// If the changelog is disabled, then don't do anything.
if (maxChangelogEntries <= 0) {
return;
}
final long changeNumber = lastChangeNumber.incrementAndGet();
final LDIFModifyDNChangeRecord changeRecord = new LDIFModifyDNChangeRecord(modifyDNRequest.getDN(), modifyDNRequest.getNewRDN(), modifyDNRequest.deleteOldRDN(), modifyDNRequest.getNewSuperiorDN());
try {
addChangeLogEntry(ChangeLogEntry.constructChangeLogEntry(changeNumber, changeRecord), authzDN);
} catch (final LDAPException le) {
// This should not happen.
Debug.debugException(le);
}
}
use of com.unboundid.ldif.LDIFModifyDNChangeRecord in project ldapsdk by pingidentity.
the class LDAPModify method doToolProcessing.
/**
* {@inheritDoc}
*/
@Override()
@NotNull()
public ResultCode doToolProcessing() {
// Examine the arguments to determine the sets of controls to use for each
// type of request.
final ArrayList<Control> addControls = new ArrayList<>(10);
final ArrayList<Control> deleteControls = new ArrayList<>(10);
final ArrayList<Control> modifyControls = new ArrayList<>(10);
final ArrayList<Control> modifyDNControls = new ArrayList<>(10);
final ArrayList<Control> searchControls = new ArrayList<>(10);
try {
createRequestControls(addControls, deleteControls, modifyControls, modifyDNControls, searchControls);
} catch (final LDAPException le) {
Debug.debugException(le);
for (final String line : ResultUtils.formatResult(le, true, 0, WRAP_COLUMN)) {
err(line);
}
return le.getResultCode();
}
// If an encryption passphrase file was specified, then read its value.
String encryptionPassphrase = null;
if (encryptionPassphraseFile.isPresent()) {
try {
encryptionPassphrase = ToolUtils.readEncryptionPassphraseFromFile(encryptionPassphraseFile.getValue());
} catch (final LDAPException e) {
Debug.debugException(e);
wrapErr(0, WRAP_COLUMN, e.getMessage());
return e.getResultCode();
}
}
LDAPConnectionPool connectionPool = null;
LDIFReader ldifReader = null;
LDIFWriter rejectWriter = null;
try {
// before performing the bind.
try {
final StartAdministrativeSessionPostConnectProcessor p;
if (useAdministrativeSession.isPresent()) {
p = new StartAdministrativeSessionPostConnectProcessor(new StartAdministrativeSessionExtendedRequest(getToolName(), true));
} else {
p = null;
}
if (!dryRun.isPresent()) {
connectionPool = getConnectionPool(1, 2, 0, p, null, true, new ReportBindResultLDAPConnectionPoolHealthCheck(this, true, verbose.isPresent()));
}
} catch (final LDAPException le) {
Debug.debugException(le);
// If the failure was something else, then display that failure result.
if (le.getResultCode() != ResultCode.INVALID_CREDENTIALS) {
for (final String line : ResultUtils.formatResult(le, true, 0, WRAP_COLUMN)) {
err(line);
}
}
return le.getResultCode();
}
if (connectionPool != null) {
connectionPool.setRetryFailedOperationsDueToInvalidConnections((!neverRetry.isPresent()));
}
// Report that the connection was successfully established.
if (connectionPool != null) {
try {
final LDAPConnection connection = connectionPool.getConnection();
final String hostPort = connection.getHostPort();
connectionPool.releaseConnection(connection);
commentToOut(INFO_LDAPMODIFY_CONNECTION_ESTABLISHED.get(hostPort));
out();
} catch (final LDAPException le) {
Debug.debugException(le);
// This should never happen.
}
}
// If we should process the operations in a transaction, then start that
// now.
final ASN1OctetString txnID;
if (useTransaction.isPresent()) {
final Control[] startTxnControls;
if (proxyAs.isPresent()) {
// In a transaction, the proxied authorization control must only be
// used in the start transaction request and not in any of the
// subsequent operation requests.
startTxnControls = new Control[] { new ProxiedAuthorizationV2RequestControl(proxyAs.getValue()) };
} else if (proxyV1As.isPresent()) {
// In a transaction, the proxied authorization control must only be
// used in the start transaction request and not in any of the
// subsequent operation requests.
startTxnControls = new Control[] { new ProxiedAuthorizationV1RequestControl(proxyV1As.getValue()) };
} else {
startTxnControls = StaticUtils.NO_CONTROLS;
}
try {
final StartTransactionExtendedResult startTxnResult = (StartTransactionExtendedResult) connectionPool.processExtendedOperation(new StartTransactionExtendedRequest(startTxnControls));
if (startTxnResult.getResultCode() == ResultCode.SUCCESS) {
txnID = startTxnResult.getTransactionID();
final TransactionSpecificationRequestControl c = new TransactionSpecificationRequestControl(txnID);
addControls.add(c);
deleteControls.add(c);
modifyControls.add(c);
modifyDNControls.add(c);
final String txnIDString;
if (StaticUtils.isPrintableString(txnID.getValue())) {
txnIDString = txnID.stringValue();
} else {
final StringBuilder hexBuffer = new StringBuilder();
StaticUtils.toHex(txnID.getValue(), ":", hexBuffer);
txnIDString = hexBuffer.toString();
}
commentToOut(INFO_LDAPMODIFY_STARTED_TXN.get(txnIDString));
} else {
commentToErr(ERR_LDAPMODIFY_CANNOT_START_TXN.get(startTxnResult.getResultString()));
return startTxnResult.getResultCode();
}
} catch (final LDAPException le) {
Debug.debugException(le);
commentToErr(ERR_LDAPMODIFY_CANNOT_START_TXN.get(StaticUtils.getExceptionMessage(le)));
return le.getResultCode();
}
} else {
txnID = null;
}
// Create an LDIF reader that will be used to read the changes to process.
try {
final InputStream ldifInputStream;
if (ldifFile.isPresent()) {
ldifInputStream = ToolUtils.getInputStreamForLDIFFiles(ldifFile.getValues(), encryptionPassphrase, getOut(), getErr()).getFirst();
} else {
ldifInputStream = in;
}
ldifReader = new LDIFReader(ldifInputStream, 0, null, null, characterSet.getValue());
} catch (final Exception e) {
commentToErr(ERR_LDAPMODIFY_CANNOT_CREATE_LDIF_READER.get(StaticUtils.getExceptionMessage(e)));
return ResultCode.LOCAL_ERROR;
}
if (stripTrailingSpaces.isPresent()) {
ldifReader.setTrailingSpaceBehavior(TrailingSpaceBehavior.STRIP);
}
// If appropriate, create a reject writer.
if (rejectFile.isPresent()) {
try {
rejectWriter = new LDIFWriter(rejectFile.getValue());
// Set the maximum allowed wrap column. This is better than setting a
// wrap column of zero because it will ensure that comments don't get
// wrapped either.
rejectWriter.setWrapColumn(Integer.MAX_VALUE);
} catch (final Exception e) {
Debug.debugException(e);
commentToErr(ERR_LDAPMODIFY_CANNOT_CREATE_REJECT_WRITER.get(rejectFile.getValue().getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
return ResultCode.LOCAL_ERROR;
}
}
// If appropriate, create a rate limiter.
final FixedRateBarrier rateLimiter;
if (ratePerSecond.isPresent()) {
rateLimiter = new FixedRateBarrier(1000L, ratePerSecond.getValue());
} else {
rateLimiter = null;
}
// Iterate through the set of changes to process.
boolean commitTransaction = true;
ResultCode resultCode = null;
final ArrayList<LDAPRequest> multiUpdateRequests = new ArrayList<>(10);
final boolean isBulkModify = modifyEntriesMatchingFilter.isPresent() || modifyEntriesMatchingFiltersFromFile.isPresent() || modifyEntryWithDN.isPresent() || modifyEntriesWithDNsFromFile.isPresent();
readChangeRecordLoop: while (true) {
// If there is a rate limiter, then use it to sleep if necessary.
if ((rateLimiter != null) && (!isBulkModify)) {
rateLimiter.await();
}
// Read the next LDIF change record. If we get an error then handle it
// and abort if appropriate.
final LDIFChangeRecord changeRecord;
try {
changeRecord = ldifReader.readChangeRecord(defaultAdd.isPresent());
} catch (final IOException ioe) {
Debug.debugException(ioe);
final String message = ERR_LDAPMODIFY_IO_ERROR_READING_CHANGE.get(StaticUtils.getExceptionMessage(ioe));
commentToErr(message);
writeRejectedChange(rejectWriter, message, null);
commitTransaction = false;
resultCode = ResultCode.LOCAL_ERROR;
break;
} catch (final LDIFException le) {
Debug.debugException(le);
final StringBuilder buffer = new StringBuilder();
if (le.mayContinueReading() && (!useTransaction.isPresent())) {
buffer.append(ERR_LDAPMODIFY_RECOVERABLE_LDIF_ERROR_READING_CHANGE.get(le.getLineNumber(), StaticUtils.getExceptionMessage(le)));
} else {
buffer.append(ERR_LDAPMODIFY_UNRECOVERABLE_LDIF_ERROR_READING_CHANGE.get(le.getLineNumber(), StaticUtils.getExceptionMessage(le)));
}
if ((resultCode == null) || (resultCode == ResultCode.SUCCESS)) {
resultCode = ResultCode.LOCAL_ERROR;
}
if ((le.getDataLines() != null) && (!le.getDataLines().isEmpty())) {
buffer.append(StaticUtils.EOL);
buffer.append(StaticUtils.EOL);
buffer.append(ERR_LDAPMODIFY_INVALID_LINES.get());
buffer.append(StaticUtils.EOL);
for (final String s : le.getDataLines()) {
buffer.append(s);
buffer.append(StaticUtils.EOL);
}
}
final String message = buffer.toString();
commentToErr(message);
writeRejectedChange(rejectWriter, message, null);
if (le.mayContinueReading() && (!useTransaction.isPresent())) {
continue;
} else {
commitTransaction = false;
resultCode = ResultCode.LOCAL_ERROR;
break;
}
}
// type.
if (changeRecord == null) {
break;
}
// the change record into a set of modifications.
if (modifyEntriesMatchingFilter.isPresent()) {
for (final Filter filter : modifyEntriesMatchingFilter.getValues()) {
final ResultCode rc = handleModifyMatchingFilter(connectionPool, changeRecord, modifyEntriesMatchingFilter.getIdentifierString(), filter, searchControls, modifyControls, rateLimiter, rejectWriter);
if (rc != ResultCode.SUCCESS) {
if ((resultCode == null) || (resultCode == ResultCode.SUCCESS) || (resultCode == ResultCode.NO_OPERATION)) {
resultCode = rc;
}
}
}
}
if (modifyEntriesMatchingFiltersFromFile.isPresent()) {
for (final File f : modifyEntriesMatchingFiltersFromFile.getValues()) {
final FilterFileReader filterReader;
try {
filterReader = new FilterFileReader(f);
} catch (final Exception e) {
Debug.debugException(e);
commentToErr(ERR_LDAPMODIFY_ERROR_OPENING_FILTER_FILE.get(f.getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
return ResultCode.LOCAL_ERROR;
}
try {
while (true) {
final Filter filter;
try {
filter = filterReader.readFilter();
} catch (final IOException ioe) {
Debug.debugException(ioe);
commentToErr(ERR_LDAPMODIFY_IO_ERROR_READING_FILTER_FILE.get(f.getAbsolutePath(), StaticUtils.getExceptionMessage(ioe)));
return ResultCode.LOCAL_ERROR;
} catch (final LDAPException le) {
Debug.debugException(le);
commentToErr(le.getMessage());
if (continueOnError.isPresent()) {
if ((resultCode == null) || (resultCode == ResultCode.SUCCESS) || (resultCode == ResultCode.NO_OPERATION)) {
resultCode = le.getResultCode();
}
continue;
} else {
return le.getResultCode();
}
}
if (filter == null) {
break;
}
final ResultCode rc = handleModifyMatchingFilter(connectionPool, changeRecord, modifyEntriesMatchingFiltersFromFile.getIdentifierString(), filter, searchControls, modifyControls, rateLimiter, rejectWriter);
if (rc != ResultCode.SUCCESS) {
if ((resultCode == null) || (resultCode == ResultCode.SUCCESS) || (resultCode == ResultCode.NO_OPERATION)) {
resultCode = rc;
}
}
}
} finally {
try {
filterReader.close();
} catch (final Exception e) {
Debug.debugException(e);
}
}
}
}
if (modifyEntryWithDN.isPresent()) {
for (final DN dn : modifyEntryWithDN.getValues()) {
final ResultCode rc = handleModifyWithDN(connectionPool, changeRecord, modifyEntryWithDN.getIdentifierString(), dn, modifyControls, rateLimiter, rejectWriter);
if (rc != ResultCode.SUCCESS) {
if ((resultCode == null) || (resultCode == ResultCode.SUCCESS) || (resultCode == ResultCode.NO_OPERATION)) {
resultCode = rc;
}
}
}
}
if (modifyEntriesWithDNsFromFile.isPresent()) {
for (final File f : modifyEntriesWithDNsFromFile.getValues()) {
final DNFileReader dnReader;
try {
dnReader = new DNFileReader(f);
} catch (final Exception e) {
Debug.debugException(e);
commentToErr(ERR_LDAPMODIFY_ERROR_OPENING_DN_FILE.get(f.getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
return ResultCode.LOCAL_ERROR;
}
try {
while (true) {
final DN dn;
try {
dn = dnReader.readDN();
} catch (final IOException ioe) {
Debug.debugException(ioe);
commentToErr(ERR_LDAPMODIFY_IO_ERROR_READING_DN_FILE.get(f.getAbsolutePath(), StaticUtils.getExceptionMessage(ioe)));
return ResultCode.LOCAL_ERROR;
} catch (final LDAPException le) {
Debug.debugException(le);
commentToErr(le.getMessage());
if (continueOnError.isPresent()) {
if ((resultCode == null) || (resultCode == ResultCode.SUCCESS) || (resultCode == ResultCode.NO_OPERATION)) {
resultCode = le.getResultCode();
}
continue;
} else {
return le.getResultCode();
}
}
if (dn == null) {
break;
}
final ResultCode rc = handleModifyWithDN(connectionPool, changeRecord, modifyEntriesWithDNsFromFile.getIdentifierString(), dn, modifyControls, rateLimiter, rejectWriter);
if (rc != ResultCode.SUCCESS) {
if ((resultCode == null) || (resultCode == ResultCode.SUCCESS) || (resultCode == ResultCode.NO_OPERATION)) {
resultCode = rc;
}
}
}
} finally {
try {
dnReader.close();
} catch (final Exception e) {
Debug.debugException(e);
}
}
}
}
if (isBulkModify) {
continue;
}
try {
final ResultCode rc;
if (changeRecord instanceof LDIFAddChangeRecord) {
rc = doAdd((LDIFAddChangeRecord) changeRecord, addControls, connectionPool, multiUpdateRequests, rejectWriter);
} else if (changeRecord instanceof LDIFDeleteChangeRecord) {
rc = doDelete((LDIFDeleteChangeRecord) changeRecord, deleteControls, connectionPool, multiUpdateRequests, rejectWriter);
} else if (changeRecord instanceof LDIFModifyChangeRecord) {
rc = doModify((LDIFModifyChangeRecord) changeRecord, modifyControls, connectionPool, multiUpdateRequests, rejectWriter);
} else if (changeRecord instanceof LDIFModifyDNChangeRecord) {
rc = doModifyDN((LDIFModifyDNChangeRecord) changeRecord, modifyDNControls, connectionPool, multiUpdateRequests, rejectWriter);
} else {
// This should never happen.
commentToErr(ERR_LDAPMODIFY_UNSUPPORTED_CHANGE_RECORD_HEADER.get());
for (final String line : changeRecord.toLDIF()) {
err("# " + line);
}
throw new LDAPException(ResultCode.PARAM_ERROR, ERR_LDAPMODIFY_UNSUPPORTED_CHANGE_RECORD_HEADER.get() + changeRecord.toString());
}
if ((resultCode == null) && (rc != ResultCode.SUCCESS)) {
resultCode = rc;
}
} catch (final LDAPException le) {
Debug.debugException(le);
commitTransaction = false;
if (continueOnError.isPresent()) {
if ((resultCode == null) || (resultCode == ResultCode.SUCCESS) || (resultCode == ResultCode.NO_OPERATION)) {
resultCode = le.getResultCode();
}
} else {
resultCode = le.getResultCode();
break;
}
}
}
// operation, then process that now.
if (useTransaction.isPresent()) {
LDAPResult endTxnResult;
final EndTransactionExtendedRequest endTxnRequest = new EndTransactionExtendedRequest(txnID, commitTransaction);
try {
endTxnResult = connectionPool.processExtendedOperation(endTxnRequest);
} catch (final LDAPException le) {
endTxnResult = le.toLDAPResult();
}
displayResult(endTxnResult, false);
if (((resultCode == null) || (resultCode == ResultCode.SUCCESS)) && (endTxnResult.getResultCode() != ResultCode.SUCCESS)) {
resultCode = endTxnResult.getResultCode();
}
} else if (multiUpdateErrorBehavior.isPresent()) {
final MultiUpdateErrorBehavior errorBehavior;
if (multiUpdateErrorBehavior.getValue().equalsIgnoreCase("atomic")) {
errorBehavior = MultiUpdateErrorBehavior.ATOMIC;
} else if (multiUpdateErrorBehavior.getValue().equalsIgnoreCase("abort-on-error")) {
errorBehavior = MultiUpdateErrorBehavior.ABORT_ON_ERROR;
} else {
errorBehavior = MultiUpdateErrorBehavior.CONTINUE_ON_ERROR;
}
final Control[] multiUpdateControls;
if (proxyAs.isPresent()) {
multiUpdateControls = new Control[] { new ProxiedAuthorizationV2RequestControl(proxyAs.getValue()) };
} else if (proxyV1As.isPresent()) {
multiUpdateControls = new Control[] { new ProxiedAuthorizationV1RequestControl(proxyV1As.getValue()) };
} else {
multiUpdateControls = StaticUtils.NO_CONTROLS;
}
ExtendedResult multiUpdateResult;
try {
commentToOut(INFO_LDAPMODIFY_SENDING_MULTI_UPDATE_REQUEST.get());
final MultiUpdateExtendedRequest multiUpdateRequest = new MultiUpdateExtendedRequest(errorBehavior, multiUpdateRequests, multiUpdateControls);
multiUpdateResult = connectionPool.processExtendedOperation(multiUpdateRequest);
} catch (final LDAPException le) {
multiUpdateResult = new ExtendedResult(le);
}
displayResult(multiUpdateResult, false);
resultCode = multiUpdateResult.getResultCode();
}
if (resultCode == null) {
return ResultCode.SUCCESS;
} else {
return resultCode;
}
} finally {
if (rejectWriter != null) {
try {
rejectWriter.close();
} catch (final Exception e) {
Debug.debugException(e);
}
}
if (ldifReader != null) {
try {
ldifReader.close();
} catch (final Exception e) {
Debug.debugException(e);
}
}
if (connectionPool != null) {
try {
connectionPool.close();
} catch (final Exception e) {
Debug.debugException(e);
}
}
}
}
use of com.unboundid.ldif.LDIFModifyDNChangeRecord in project ldapsdk by pingidentity.
the class ModifyDNAuditLogMessage method getRevertChangeRecords.
/**
* {@inheritDoc}
*/
@Override()
@NotNull()
public List<LDIFChangeRecord> getRevertChangeRecords() throws AuditLogException {
// attribute modifications.
if (attributeModifications == null) {
throw new AuditLogException(getLogMessageLines(), ERR_MODIFY_DN_NOT_REVERTIBLE.get(modifyDNChangeRecord.getDN()));
}
// Get the DN of the entry after the modify DN operation was processed,
// along with parsed versions of the original DN, new RDN, and new superior
// DN.
final DN newDN;
final DN newSuperiorDN;
final DN originalDN;
final RDN newRDN;
try {
newDN = modifyDNChangeRecord.getNewDN();
originalDN = modifyDNChangeRecord.getParsedDN();
newSuperiorDN = modifyDNChangeRecord.getParsedNewSuperiorDN();
newRDN = modifyDNChangeRecord.getParsedNewRDN();
} catch (final Exception e) {
Debug.debugException(e);
if (modifyDNChangeRecord.getNewSuperiorDN() == null) {
throw new AuditLogException(getLogMessageLines(), ERR_MODIFY_DN_CANNOT_GET_NEW_DN_WITHOUT_NEW_SUPERIOR.get(modifyDNChangeRecord.getDN(), modifyDNChangeRecord.getNewRDN()), e);
} else {
throw new AuditLogException(getLogMessageLines(), ERR_MODIFY_DN_CANNOT_GET_NEW_DN_WITH_NEW_SUPERIOR.get(modifyDNChangeRecord.getDN(), modifyDNChangeRecord.getNewRDN(), modifyDNChangeRecord.getNewSuperiorDN()), e);
}
}
// If the original DN is the null DN, then fail.
if (originalDN.isNullDN()) {
throw new AuditLogException(getLogMessageLines(), ERR_MODIFY_DN_CANNOT_REVERT_NULL_DN.get());
}
// be false or the new RDN must equal the old RDN.
if (attributeModifications.isEmpty()) {
if (modifyDNChangeRecord.deleteOldRDN() && (!newRDN.equals(originalDN.getRDN()))) {
throw new AuditLogException(getLogMessageLines(), ERR_MODIFY_DN_CANNOT_REVERT_WITHOUT_NECESSARY_MODS.get(modifyDNChangeRecord.getDN()));
}
}
// Construct the DN, new RDN, and new superior DN values for the change
// needed to revert the modify DN operation.
final String revertedDN = newDN.toString();
final String revertedNewRDN = originalDN.getRDNString();
final String revertedNewSuperiorDN;
if (newSuperiorDN == null) {
revertedNewSuperiorDN = null;
} else {
revertedNewSuperiorDN = originalDN.getParentString();
}
// in the entry.
if (attributeModifications.isEmpty()) {
return Collections.<LDIFChangeRecord>singletonList(new LDIFModifyDNChangeRecord(revertedDN, revertedNewRDN, false, revertedNewSuperiorDN));
}
// Iterate through the modifications to see which new RDN attributes were
// added to the entry. If they were all added, then we need to use a
// deleteOldRDN value of true. If none of them were added, then we need to
// use a deleteOldRDN value of false. If some of them were added but some
// were not, then we need to use a deleteOldRDN value o false and have a
// second modification to delete those values that were added.
//
// Also, collect any additional modifications that don't involve new RDN
// attribute values.
final int numNewRDNs = newRDN.getAttributeNames().length;
final Set<ObjectPair<String, byte[]>> addedNewRDNValues = new HashSet<>(StaticUtils.computeMapCapacity(numNewRDNs));
final RDN originalRDN = originalDN.getRDN();
final List<Modification> additionalModifications = new ArrayList<>(attributeModifications.size());
final int numModifications = attributeModifications.size();
for (int i = numModifications - 1; i >= 0; i--) {
final Modification m = attributeModifications.get(i);
if (m.getModificationType() == ModificationType.ADD) {
final Attribute a = m.getAttribute();
final ArrayList<byte[]> retainedValues = new ArrayList<>(a.size());
for (final ASN1OctetString value : a.getRawValues()) {
final byte[] valueBytes = value.getValue();
if (newRDN.hasAttributeValue(a.getName(), valueBytes)) {
addedNewRDNValues.add(new ObjectPair<>(a.getName(), valueBytes));
} else {
retainedValues.add(valueBytes);
}
}
if (retainedValues.size() == a.size()) {
additionalModifications.add(new Modification(ModificationType.DELETE, a.getName(), a.getRawValues()));
} else if (!retainedValues.isEmpty()) {
additionalModifications.add(new Modification(ModificationType.DELETE, a.getName(), StaticUtils.toArray(retainedValues, byte[].class)));
}
} else if (m.getModificationType() == ModificationType.DELETE) {
final Attribute a = m.getAttribute();
final ArrayList<byte[]> retainedValues = new ArrayList<>(a.size());
for (final ASN1OctetString value : a.getRawValues()) {
final byte[] valueBytes = value.getValue();
if (!originalRDN.hasAttributeValue(a.getName(), valueBytes)) {
retainedValues.add(valueBytes);
}
}
if (retainedValues.size() == a.size()) {
additionalModifications.add(new Modification(ModificationType.ADD, a.getName(), a.getRawValues()));
} else if (!retainedValues.isEmpty()) {
additionalModifications.add(new Modification(ModificationType.ADD, a.getName(), StaticUtils.toArray(retainedValues, byte[].class)));
}
} else {
final Modification revertModification = ModifyAuditLogMessage.getRevertModification(m);
if (revertModification == null) {
throw new AuditLogException(getLogMessageLines(), ERR_MODIFY_DN_MOD_NOT_REVERTIBLE.get(modifyDNChangeRecord.getDN(), m.getModificationType().getName(), m.getAttributeName()));
} else {
additionalModifications.add(revertModification);
}
}
}
final boolean revertedDeleteOldRDN;
if (addedNewRDNValues.size() == numNewRDNs) {
revertedDeleteOldRDN = true;
} else {
revertedDeleteOldRDN = false;
if (!addedNewRDNValues.isEmpty()) {
for (final ObjectPair<String, byte[]> p : addedNewRDNValues) {
additionalModifications.add(0, new Modification(ModificationType.DELETE, p.getFirst(), p.getSecond()));
}
}
}
final List<LDIFChangeRecord> changeRecords = new ArrayList<>(2);
changeRecords.add(new LDIFModifyDNChangeRecord(revertedDN, revertedNewRDN, revertedDeleteOldRDN, revertedNewSuperiorDN));
if (!additionalModifications.isEmpty()) {
changeRecords.add(new LDIFModifyChangeRecord(originalDN.toString(), additionalModifications));
}
return Collections.unmodifiableList(changeRecords);
}
Aggregations