use of com.unboundid.ldap.sdk.ModifyDNRequest in project ldapsdk by pingidentity.
the class InMemoryDirectoryServerReferentialIntegrityTestCase method testModifyDNWithReferentialIntegrity.
/**
* Tests the behavior for modify DN operations with referential integrity
* enabled.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testModifyDNWithReferentialIntegrity() throws Exception {
// Create a directory server instance and configure it for use with
// referential integrity.
final InMemoryDirectoryServerConfig cfg = new InMemoryDirectoryServerConfig("dc=example,dc=com");
cfg.setReferentialIntegrityAttributes("member", "uniqueMember");
final InMemoryDirectoryServer ds = new InMemoryDirectoryServer(cfg);
ds.startListening();
// Populate the server with a set of test data.
ds.addEntries(generateDomainEntry("example", "dc=com"), generateOrgUnitEntry("People", "dc=example,dc=com"), generateOrgUnitEntry("Groups", "dc=example,dc=com"), generateUserEntry("none", "ou=People,dc=example,dc=com", "No", "Memberships", "password"), generateUserEntry("single", "ou=People,dc=example,dc=com", "Single", "Membership", "password"), generateUserEntry("multiple", "ou=People,dc=example,dc=com", "Multiple", "Memberships", "password"), generateGroupOfNamesEntry("group1", "ou=Groups,dc=example,dc=com", "uid=single,ou=People,dc=example,dc=com", "uid=multiple,ou=People,dc=example,dc=com", "uid=nonexistent,ou=People,dc=example,dc=com"), generateGroupOfNamesEntry("group2", "ou=Groups,dc=example,dc=com", "uid=multiple,ou=People,dc=example,dc=com", "uid=nonexistent,ou=People,dc=example,dc=com"), generateGroupOfUniqueNamesEntry("group3", "ou=Groups,dc=example,dc=com", "uid=multiple,ou=People,dc=example,dc=com"), generateGroupOfUniqueNamesEntry("group4", "ou=Groups,dc=example,dc=com", "uid=nonexistent,ou=People,dc=example,dc=com"));
ds.assertValueExists("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=single,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=multiple,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=nonexistent,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group2,ou=Groups,dc=example,dc=com", "member", "uid=multiple,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group2,ou=Groups,dc=example,dc=com", "member", "uid=nonexistent,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group3,ou=Groups,dc=example,dc=com", "uniqueMember", "uid=multiple,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group4,ou=Groups,dc=example,dc=com", "uniqueMember", "uid=nonexistent,ou=People,dc=example,dc=com");
// Rename a user that is a member of only one group, and ensure that the
// reference to it is updated.
final LDAPConnection conn = ds.getConnection();
final ModifyDNRequest modifyDNRequest = new ModifyDNRequest("uid=single,ou=People,dc=example,dc=com", "uid=one", true);
assertResultCodeEquals(conn, modifyDNRequest, ResultCode.SUCCESS);
ds.assertValueMissing("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=single,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=one,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=multiple,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=nonexistent,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group2,ou=Groups,dc=example,dc=com", "member", "uid=multiple,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group2,ou=Groups,dc=example,dc=com", "member", "uid=nonexistent,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group3,ou=Groups,dc=example,dc=com", "uniqueMember", "uid=multiple,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group4,ou=Groups,dc=example,dc=com", "uniqueMember", "uid=nonexistent,ou=People,dc=example,dc=com");
// Rename a user that is not member of any groups, and ensure that no
// changes are made.
modifyDNRequest.setDN("uid=none,ou=People,dc=example,dc=com");
modifyDNRequest.setNewRDN("uid=zero");
assertResultCodeEquals(conn, modifyDNRequest, ResultCode.SUCCESS);
ds.assertValueMissing("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=single,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=one,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=multiple,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=nonexistent,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group2,ou=Groups,dc=example,dc=com", "member", "uid=multiple,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group2,ou=Groups,dc=example,dc=com", "member", "uid=nonexistent,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group3,ou=Groups,dc=example,dc=com", "uniqueMember", "uid=multiple,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group4,ou=Groups,dc=example,dc=com", "uniqueMember", "uid=nonexistent,ou=People,dc=example,dc=com");
// Rename a user that is a member of multiple groups, and ensure that all
// appropriate changes are made.
modifyDNRequest.setDN("uid=multiple,ou=People,dc=example,dc=com");
modifyDNRequest.setNewRDN("uid=three");
assertResultCodeEquals(conn, modifyDNRequest, ResultCode.SUCCESS);
ds.assertValueMissing("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=single,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=one,ou=People,dc=example,dc=com");
ds.assertValueMissing("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=multiple,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=three,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=nonexistent,ou=People,dc=example,dc=com");
ds.assertValueMissing("cn=group2,ou=Groups,dc=example,dc=com", "member", "uid=multiple,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group2,ou=Groups,dc=example,dc=com", "member", "uid=three,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group2,ou=Groups,dc=example,dc=com", "member", "uid=nonexistent,ou=People,dc=example,dc=com");
ds.assertValueMissing("cn=group3,ou=Groups,dc=example,dc=com", "uniqueMember", "uid=multiple,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group3,ou=Groups,dc=example,dc=com", "uniqueMember", "uid=three,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group4,ou=Groups,dc=example,dc=com", "uniqueMember", "uid=nonexistent,ou=People,dc=example,dc=com");
// Rename the entire "ou=People" subtree and ensure that all references to
// the subordinate entries are updated.
modifyDNRequest.setDN("ou=People,dc=example,dc=com");
modifyDNRequest.setNewRDN("ou=Users");
assertResultCodeEquals(conn, modifyDNRequest, ResultCode.SUCCESS);
ds.assertValueMissing("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=single,ou=People,dc=example,dc=com");
ds.assertValueMissing("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=one,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=one,ou=Users,dc=example,dc=com");
ds.assertValueMissing("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=multiple,ou=People,dc=example,dc=com");
ds.assertValueMissing("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=three,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=three,ou=Users,dc=example,dc=com");
ds.assertValueExists("cn=group1,ou=Groups,dc=example,dc=com", "member", "uid=nonexistent,ou=People,dc=example,dc=com");
ds.assertValueMissing("cn=group2,ou=Groups,dc=example,dc=com", "member", "uid=multiple,ou=People,dc=example,dc=com");
ds.assertValueMissing("cn=group2,ou=Groups,dc=example,dc=com", "member", "uid=three,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group2,ou=Groups,dc=example,dc=com", "member", "uid=three,ou=Users,dc=example,dc=com");
ds.assertValueExists("cn=group2,ou=Groups,dc=example,dc=com", "member", "uid=nonexistent,ou=People,dc=example,dc=com");
ds.assertValueMissing("cn=group3,ou=Groups,dc=example,dc=com", "uniqueMember", "uid=multiple,ou=People,dc=example,dc=com");
ds.assertValueMissing("cn=group3,ou=Groups,dc=example,dc=com", "uniqueMember", "uid=three,ou=People,dc=example,dc=com");
ds.assertValueExists("cn=group3,ou=Groups,dc=example,dc=com", "uniqueMember", "uid=three,ou=Users,dc=example,dc=com");
ds.assertValueExists("cn=group4,ou=Groups,dc=example,dc=com", "uniqueMember", "uid=nonexistent,ou=People,dc=example,dc=com");
// Close the connection and shut down the server.
conn.close();
ds.shutDown(true);
}
use of com.unboundid.ldap.sdk.ModifyDNRequest in project ldapsdk by pingidentity.
the class LDAPConnection method rename.
/**
* Alters the DN of an entry in the directory.
*
* @param dn The DN of the entry to modify.
* @param newRDN The new RDN to use for the entry.
* @param newParentDN The DN of the new parent, or {@code null} if it
* should not be moved below a new parent.
* @param deleteOldRDN Indicates whether to remove the old RDN value(s).
* @param constraints The constraints to use for the modify operation.
*
* @throws LDAPException If a problem occurs while processing the delete.
*/
public void rename(@NotNull final String dn, @NotNull final String newRDN, @Nullable final String newParentDN, final boolean deleteOldRDN, @Nullable final LDAPConstraints constraints) throws LDAPException {
final ModifyDNRequest modifyDNRequest = new ModifyDNRequest(dn, newRDN, deleteOldRDN, newParentDN);
update(modifyDNRequest, constraints);
try {
final LDAPResult result = conn.modifyDN(modifyDNRequest);
setResponseControls(result);
} catch (final com.unboundid.ldap.sdk.LDAPException le) {
Debug.debugException(le);
setResponseControls(le);
throw new LDAPException(le);
}
}
use of com.unboundid.ldap.sdk.ModifyDNRequest in project ldapsdk by pingidentity.
the class LDAPModify method doModifyDN.
/**
* Performs the appropriate processing for an LDIF modify DN change record.
*
* @param changeRecord The LDIF modify DN change record to process.
* @param controls The set of controls to include in the request.
* @param pool The connection pool to use to communicate with
* the directory server.
* @param multiUpdateRequests The list to which the request should be added
* if it is to be processed as part of a
* multi-update operation. It may be
* {@code null} if the operation should not be
* processed via the multi-update operation.
* @param rejectWriter The LDIF writer to use for recording
* information about rejected changes. It may be
* {@code null} if no reject writer is
* configured.
*
* @return The result code obtained from processing.
*
* @throws LDAPException If the operation did not complete successfully
* and processing should not continue.
*/
@NotNull()
private ResultCode doModifyDN(@NotNull final LDIFModifyDNChangeRecord changeRecord, @NotNull final List<Control> controls, @NotNull final LDAPConnectionPool pool, @Nullable final List<LDAPRequest> multiUpdateRequests, @Nullable final LDIFWriter rejectWriter) throws LDAPException {
// Create the modify DN request to process.
final ModifyDNRequest modifyDNRequest = changeRecord.toModifyDNRequest(true);
for (final Control c : controls) {
modifyDNRequest.addControl(c);
}
// just add the request to the list and return without doing anything else.
if (multiUpdateErrorBehavior.isPresent()) {
multiUpdateRequests.add(modifyDNRequest);
commentToOut(INFO_LDAPMODIFY_MODIFY_DN_ADDED_TO_MULTI_UPDATE.get(modifyDNRequest.getDN()));
return ResultCode.SUCCESS;
}
// Try to determine the new DN that the entry will have after the operation.
DN newDN = null;
try {
newDN = changeRecord.getNewDN();
} catch (final Exception e) {
Debug.debugException(e);
// This should only happen if the provided DN, new RDN, or new superior DN
// was malformed. Although we could reject the operation now, we'll go
// ahead and send the request to the server in case it has some special
// handling for the DN.
}
// If the --dryRun argument was provided, then we'll stop here.
if (dryRun.isPresent()) {
if (modifyDNRequest.getNewSuperiorDN() == null) {
if (newDN == null) {
commentToOut(INFO_LDAPMODIFY_DRY_RUN_RENAME.get(modifyDNRequest.getDN(), dryRun.getIdentifierString()));
} else {
commentToOut(INFO_LDAPMODIFY_DRY_RUN_RENAME_TO.get(modifyDNRequest.getDN(), newDN.toString(), dryRun.getIdentifierString()));
}
} else {
if (newDN == null) {
commentToOut(INFO_LDAPMODIFY_DRY_RUN_MOVE.get(modifyDNRequest.getDN(), dryRun.getIdentifierString()));
} else {
commentToOut(INFO_LDAPMODIFY_DRY_RUN_MOVE_TO.get(modifyDNRequest.getDN(), newDN.toString(), dryRun.getIdentifierString()));
}
}
return ResultCode.SUCCESS;
}
// Process the modify DN operation and get the result.
final String currentDN = modifyDNRequest.getDN();
if (modifyDNRequest.getNewSuperiorDN() == null) {
if (newDN == null) {
commentToOut(INFO_LDAPMODIFY_MOVING_ENTRY.get(currentDN));
} else {
commentToOut(INFO_LDAPMODIFY_MOVING_ENTRY_TO.get(currentDN, newDN.toString()));
}
} else {
if (newDN == null) {
commentToOut(INFO_LDAPMODIFY_RENAMING_ENTRY.get(currentDN));
} else {
commentToOut(INFO_LDAPMODIFY_RENAMING_ENTRY_TO.get(currentDN, newDN.toString()));
}
}
if (verbose.isPresent()) {
for (final String ldifLine : modifyDNRequest.toLDIFChangeRecord().toLDIF(WRAP_COLUMN)) {
out(ldifLine);
}
out();
}
LDAPResult modifyDNResult;
try {
modifyDNResult = pool.modifyDN(modifyDNRequest);
} catch (final LDAPException le) {
Debug.debugException(le);
modifyDNResult = le.toLDAPResult();
}
// Display information about the result.
displayResult(modifyDNResult, useTransaction.isPresent());
// should end all processing, then throw an exception.
switch(modifyDNResult.getResultCode().intValue()) {
case ResultCode.SUCCESS_INT_VALUE:
case ResultCode.NO_OPERATION_INT_VALUE:
break;
case ResultCode.ASSERTION_FAILED_INT_VALUE:
writeRejectedChange(rejectWriter, INFO_LDAPMODIFY_ASSERTION_FAILED.get(modifyDNRequest.getDN(), String.valueOf(assertionFilter.getValue())), modifyDNRequest.toLDIFChangeRecord(), modifyDNResult);
throw new LDAPException(modifyDNResult);
default:
writeRejectedChange(rejectWriter, null, modifyDNRequest.toLDIFChangeRecord(), modifyDNResult);
if (useTransaction.isPresent() || (!continueOnError.isPresent())) {
throw new LDAPException(modifyDNResult);
}
break;
}
return modifyDNResult.getResultCode();
}
use of com.unboundid.ldap.sdk.ModifyDNRequest in project ldapsdk by pingidentity.
the class ParallelUpdateOperationThread method run.
/**
* Operates in a loop, retrieving changes from the operation queue and
* processing them.
*/
@Override()
public void run() {
LDIFChangeRecord r = opQueue.getChangeRecord();
// Various controls that might be present on the requests.
final Control undeleteRequestControl = new UndeleteRequestControl();
while (r != null) {
if (rateLimiter != null) {
rateLimiter.await();
}
DN parsedDN = null;
DN parsedNewDN = null;
final long startTime = System.currentTimeMillis();
try {
parsedDN = r.getParsedDN();
if (r instanceof LDIFAddChangeRecord) {
final AddRequest addRequest = ((LDIFAddChangeRecord) r).toAddRequest();
addRequest.addControls(addControls);
if (allowUndelete && addRequest.hasAttribute(ATTR_UNDELETE_FROM_DN)) {
addRequest.addControl(undeleteRequestControl);
}
connectionPool.add(addRequest);
parallelUpdate.opCompletedSuccessfully(r, (System.currentTimeMillis() - startTime));
} else if (r instanceof LDIFDeleteChangeRecord) {
final DeleteRequest deleteRequest = ((LDIFDeleteChangeRecord) r).toDeleteRequest();
deleteRequest.addControls(deleteControls);
connectionPool.delete(deleteRequest);
parallelUpdate.opCompletedSuccessfully(r, (System.currentTimeMillis() - startTime));
} else if (r instanceof LDIFModifyChangeRecord) {
final ModifyRequest modifyRequest = ((LDIFModifyChangeRecord) r).toModifyRequest();
modifyRequest.addControls(modifyControls);
connectionPool.modify(modifyRequest);
parallelUpdate.opCompletedSuccessfully(r, (System.currentTimeMillis() - startTime));
} else if (r instanceof LDIFModifyDNChangeRecord) {
final LDIFModifyDNChangeRecord modifyDNChangeRecord = (LDIFModifyDNChangeRecord) r;
parsedNewDN = modifyDNChangeRecord.getNewDN();
final ModifyDNRequest modifyDNRequest = modifyDNChangeRecord.toModifyDNRequest();
modifyDNRequest.addControls(modifyDNControls);
connectionPool.modifyDN(modifyDNRequest);
parallelUpdate.opCompletedSuccessfully(r, (System.currentTimeMillis() - startTime));
} else {
// This should never happen.
r.processChange(connectionPool);
parallelUpdate.opCompletedSuccessfully(r, (System.currentTimeMillis() - startTime));
}
} catch (final LDAPException e) {
Debug.debugException(e);
parallelUpdate.opFailed(r, e, (System.currentTimeMillis() - startTime));
}
if (parsedNewDN == null) {
r = opQueue.getChangeRecord(parsedDN);
} else {
r = opQueue.getChangeRecord(parsedDN, parsedNewDN);
}
}
}
Aggregations