use of com.unboundid.util.SubtreeDeleter in project ldapsdk by pingidentity.
the class LDAPDelete method doToolProcessing.
/**
* {@inheritDoc}
*/
@Override()
@NotNull()
public ResultCode doToolProcessing() {
// Get the controls that should be included in search and delete requests.
searchControls = getSearchControls();
deleteControls = getDeleteControls();
// barrier.
if (ratePerSecond.isPresent()) {
deleteRateLimiter = new FixedRateBarrier(1000L, ratePerSecond.getValue());
}
// Create a subtree deleter instance if appropriate.
if (clientSideSubtreeDelete.isPresent()) {
subtreeDeleter = new SubtreeDeleter();
subtreeDeleter.setAdditionalSearchControls(searchControls);
subtreeDeleter.setAdditionalSearchControls(deleteControls);
subtreeDeleter.setDeleteRateLimiter(deleteRateLimiter);
if (searchPageSize.isPresent()) {
subtreeDeleter.setSimplePagedResultsPageSize(searchPageSize.getValue());
}
}
// If the encryptionPassphraseFile argument was provided, then read that
// passphrase.
final char[] encryptionPassphrase;
if (encryptionPassphraseFile.isPresent()) {
try {
encryptionPassphrase = getPasswordFileReader().readPassword(encryptionPassphraseFile.getValue());
} catch (final LDAPException e) {
Debug.debugException(e);
commentToErr(e.getMessage());
return e.getResultCode();
} catch (final Exception e) {
Debug.debugException(e);
commentToErr(ERR_LDAPDELETE_CANNOT_READ_ENCRYPTION_PW_FILE.get(encryptionPassphraseFile.getValue().getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
return ResultCode.LOCAL_ERROR;
}
} else {
encryptionPassphrase = null;
}
// If the character set argument was specified, then make sure it's valid.
final Charset charset;
try {
charset = Charset.forName(characterSet.getValue());
} catch (final Exception e) {
Debug.debugException(e);
commentToErr(ERR_LDAPDELETE_UNSUPPORTED_CHARSET.get(characterSet.getValue()));
return ResultCode.PARAM_ERROR;
}
// Get the connection pool.
final StartAdministrativeSessionPostConnectProcessor p;
if (useAdministrativeSession.isPresent()) {
p = new StartAdministrativeSessionPostConnectProcessor(new StartAdministrativeSessionExtendedRequest(getToolName(), true));
} else {
p = null;
}
try {
connectionPool = getConnectionPool(1, 2, 0, p, null, true, new ReportBindResultLDAPConnectionPoolHealthCheck(this, true, verbose.isPresent()));
connectionPool.setRetryFailedOperationsDueToInvalidConnections((!neverRetry.isPresent()));
} catch (final LDAPException e) {
Debug.debugException(e);
// If the failure was something else, then display that failure result.
if (e.getResultCode() != ResultCode.INVALID_CREDENTIALS) {
for (final String line : ResultUtils.formatResult(e, true, 0, WRAP_COLUMN)) {
err(line);
}
}
return e.getResultCode();
}
// Figure out the method that we'll identify the entries to delete and
// take the appropriate action.
final AtomicReference<ResultCode> returnCode = new AtomicReference<>();
if (entryDN.isPresent()) {
deleteFromEntryDNArgument(returnCode);
} else if (dnFile.isPresent()) {
deleteFromDNFile(returnCode, charset, encryptionPassphrase);
} else if (deleteEntriesMatchingFilter.isPresent()) {
deleteFromFilters(returnCode);
} else if (deleteEntriesMatchingFiltersFromFile.isPresent()) {
deleteFromFilterFile(returnCode, charset, encryptionPassphrase);
} else if (!parser.getTrailingArguments().isEmpty()) {
deleteFromTrailingArguments(returnCode);
} else {
deleteFromStandardInput(returnCode, charset, encryptionPassphrase);
}
// Close the reject writer.
final LDIFWriter rw = rejectWriter.get();
if (rw != null) {
try {
rw.close();
} catch (final Exception e) {
Debug.debugException(e);
commentToErr(ERR_LDAPDELETE_ERROR_CLOSING_REJECT_WRITER.get(rejectFile.getValue().getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
returnCode.compareAndSet(null, ResultCode.LOCAL_ERROR);
}
}
// Close the connection pool.
connectionPool.close();
returnCode.compareAndSet(null, ResultCode.SUCCESS);
return returnCode.get();
}
use of com.unboundid.util.SubtreeDeleter in project sugoi-api by InseeFr.
the class LdapWriterStore method deleteApplication.
/**
* Delete application branch
*/
@Override
public ProviderResponse deleteApplication(String applicationName, ProviderRequest providerRequest) {
ldapReaderStore.getApplication(applicationName).orElseThrow(() -> new ApplicationNotFoundException(config.get(LdapConfigKeys.REALM_NAME), applicationName));
try {
(new SubtreeDeleter()).delete(ldapPoolConnection, getApplicationDN(applicationName));
} catch (LDAPException e) {
throw new StoreException("Failed to delete application " + applicationName, e);
}
ProviderResponse response = new ProviderResponse();
response.setStatus(ProviderResponseStatus.OK);
response.setEntityId(applicationName);
return response;
}
use of com.unboundid.util.SubtreeDeleter in project ldapsdk by pingidentity.
the class LDAPModify method doClientSideSubtreeDelete.
/**
* Performs the appropriate processing for an LDIF delete change record.
*
* @param changeRecord The LDIF delete 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 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 doClientSideSubtreeDelete(@NotNull final LDIFChangeRecord changeRecord, @NotNull final List<Control> controls, @NotNull final LDAPConnectionPool pool, @Nullable final LDIFWriter rejectWriter) throws LDAPException {
// Create the subtree deleter with the provided set of controls. Make sure
// to include any controls in the delete change record itself.
final List<Control> additionalControls;
if (changeRecord.getControls().isEmpty()) {
additionalControls = controls;
} else {
additionalControls = new ArrayList<>(controls.size() + changeRecord.getControls().size());
additionalControls.addAll(changeRecord.getControls());
additionalControls.addAll(controls);
}
final SubtreeDeleter subtreeDeleter = new SubtreeDeleter();
subtreeDeleter.setAdditionalDeleteControls(additionalControls);
// Perform the subtree delete.
commentToOut(INFO_LDAPMODIFY_CLIENT_SIDE_DELETING_SUBTREE.get(changeRecord.getDN()));
final SubtreeDeleterResult subtreeDeleterResult = subtreeDeleter.delete(pool, changeRecord.getDN());
// Evaluate the result of the subtree delete.
final LDAPResult finalResult;
if (subtreeDeleterResult.completelySuccessful()) {
final long entriesDeleted = subtreeDeleterResult.getEntriesDeleted();
if (entriesDeleted == 0L) {
// This means that the base entry did not exist. Even though the
// subtree deleter returned a successful result, we'll use a final
// result of "no such object".
finalResult = new LDAPResult(-1, ResultCode.NO_SUCH_OBJECT, ERR_LDAPMODIFY_CLIENT_SIDE_SUB_DEL_SUCCEEDED_WITH_0_ENTRIES.get(changeRecord.getDN()), null, StaticUtils.NO_STRINGS, StaticUtils.NO_CONTROLS);
} else if (entriesDeleted == 1L) {
// This means the base entry existed (and we deleted it successfully),
// but did not have any subordinates.
finalResult = new LDAPResult(-1, ResultCode.SUCCESS, INFO_LDAPMODIFY_CLIENT_SIDE_SUB_DEL_SUCCEEDED_WITH_1_ENTRY.get(changeRecord.getDN()), null, StaticUtils.NO_STRINGS, StaticUtils.NO_CONTROLS);
} else {
// This means that the base entry existed and had subordinates, and we
// deleted all of them successfully.
finalResult = new LDAPResult(-1, ResultCode.SUCCESS, INFO_LDAPMODIFY_CLIENT_SIDE_SUB_DEL_SUCCEEDED_WITH_ENTRIES.get(subtreeDeleterResult.getEntriesDeleted(), changeRecord.getDN()), null, StaticUtils.NO_STRINGS, StaticUtils.NO_CONTROLS);
}
} else {
// If there was a search error, then display information about it.
final SearchResult searchError = subtreeDeleterResult.getSearchError();
if (searchError != null) {
commentToErr(ERR_LDAPMODIFY_CLIENT_SIDE_SUB_DEL_SEARCH_ERROR.get());
displayResult(searchError, false);
err("#");
}
final SortedMap<DN, LDAPResult> deleteErrors = subtreeDeleterResult.getDeleteErrorsDescendingMap();
for (final Map.Entry<DN, LDAPResult> deleteError : deleteErrors.entrySet()) {
commentToErr(ERR_LDAPMODIFY_CLIENT_SIDE_SUB_DEL_ERROR.get(String.valueOf(deleteError.getKey())));
displayResult(deleteError.getValue(), false);
err("#");
}
ResultCode resultCode = ResultCode.OTHER;
final StringBuilder buffer = new StringBuilder();
buffer.append(ERR_LDAPMODIFY_CLIENT_SIDE_SUB_DEL_FINAL_ERR_BASE.get());
if (searchError != null) {
resultCode = searchError.getResultCode();
buffer.append(" ");
buffer.append(ERR_LDAPMODIFY_CLIENT_SIDE_SUB_DEL_FINAL_SEARCH_ERR.get());
}
if (!deleteErrors.isEmpty()) {
resultCode = deleteErrors.values().iterator().next().getResultCode();
buffer.append(" ");
final int numDeleteErrors = deleteErrors.size();
if (numDeleteErrors == 1) {
buffer.append(ERR_LDAPMODIFY_CLIENT_SIDE_SUB_DEL_FINAL_DEL_ERR_COUNT_1.get());
} else {
buffer.append(ERR_LDAPMODIFY_CLIENT_SIDE_SUB_DEL_FINAL_DEL_ERR_COUNT.get(numDeleteErrors));
}
}
buffer.append(" ");
final long deletedCount = subtreeDeleterResult.getEntriesDeleted();
if (deletedCount == 1L) {
buffer.append(ERR_LDAPMODIFY_CLIENT_SIDE_SUB_DEL_FINAL_DEL_COUNT_1.get());
} else {
buffer.append(ERR_LDAPMODIFY_CLIENT_SIDE_SUB_DEL_FINAL_DEL_COUNT.get(deletedCount));
}
finalResult = new LDAPResult(-1, resultCode, buffer.toString(), null, StaticUtils.NO_STRINGS, StaticUtils.NO_CONTROLS);
}
// Display information about the final result.
displayResult(finalResult, useTransaction.isPresent());
// should end all processing, then throw an exception.
switch(finalResult.getResultCode().intValue()) {
case ResultCode.SUCCESS_INT_VALUE:
case ResultCode.NO_OPERATION_INT_VALUE:
break;
default:
writeRejectedChange(rejectWriter, null, changeRecord, finalResult);
if (!continueOnError.isPresent()) {
throw new LDAPException(finalResult);
}
break;
}
return finalResult.getResultCode();
}
Aggregations