Search in sources :

Example 1 with SubtreeDeleter

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();
}
Also used : StartAdministrativeSessionPostConnectProcessor(com.unboundid.ldap.sdk.unboundidds.extensions.StartAdministrativeSessionPostConnectProcessor) Charset(java.nio.charset.Charset) AtomicReference(java.util.concurrent.atomic.AtomicReference) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) GeneralSecurityException(java.security.GeneralSecurityException) ArgumentException(com.unboundid.util.args.ArgumentException) LDAPException(com.unboundid.ldap.sdk.LDAPException) IOException(java.io.IOException) SubtreeDeleter(com.unboundid.util.SubtreeDeleter) StartAdministrativeSessionExtendedRequest(com.unboundid.ldap.sdk.unboundidds.extensions.StartAdministrativeSessionExtendedRequest) LDAPException(com.unboundid.ldap.sdk.LDAPException) FixedRateBarrier(com.unboundid.util.FixedRateBarrier) LDIFWriter(com.unboundid.ldif.LDIFWriter) ResultCode(com.unboundid.ldap.sdk.ResultCode) NotNull(com.unboundid.util.NotNull)

Example 2 with SubtreeDeleter

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;
}
Also used : SubtreeDeleter(com.unboundid.util.SubtreeDeleter) ProviderResponse(fr.insee.sugoi.core.model.ProviderResponse) LDAPException(com.unboundid.ldap.sdk.LDAPException) ApplicationNotFoundException(fr.insee.sugoi.model.exceptions.ApplicationNotFoundException) StoreException(fr.insee.sugoi.model.exceptions.StoreException)

Example 3 with SubtreeDeleter

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();
}
Also used : LDAPResult(com.unboundid.ldap.sdk.LDAPResult) SearchResult(com.unboundid.ldap.sdk.SearchResult) DN(com.unboundid.ldap.sdk.DN) RouteToServerRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.RouteToServerRequestControl) SubtreeDeleteRequestControl(com.unboundid.ldap.sdk.controls.SubtreeDeleteRequestControl) SimplePagedResultsControl(com.unboundid.ldap.sdk.controls.SimplePagedResultsControl) TransactionSpecificationRequestControl(com.unboundid.ldap.sdk.controls.TransactionSpecificationRequestControl) AssuredReplicationRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.AssuredReplicationRequestControl) PostReadRequestControl(com.unboundid.ldap.sdk.controls.PostReadRequestControl) SuppressOperationalAttributeUpdateRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.SuppressOperationalAttributeUpdateRequestControl) NameWithEntryUUIDRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.NameWithEntryUUIDRequestControl) ReplicationRepairRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.ReplicationRepairRequestControl) ProxiedAuthorizationV1RequestControl(com.unboundid.ldap.sdk.controls.ProxiedAuthorizationV1RequestControl) OperationPurposeRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.OperationPurposeRequestControl) PasswordUpdateBehaviorRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.PasswordUpdateBehaviorRequestControl) UndeleteRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.UndeleteRequestControl) GetRecentLoginHistoryRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.GetRecentLoginHistoryRequestControl) PermissiveModifyRequestControl(com.unboundid.ldap.sdk.controls.PermissiveModifyRequestControl) AuthorizationIdentityRequestControl(com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl) Control(com.unboundid.ldap.sdk.Control) GetUserResourceLimitsRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.GetUserResourceLimitsRequestControl) HardDeleteRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.HardDeleteRequestControl) IgnoreNoUserModificationRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.IgnoreNoUserModificationRequestControl) GetBackendSetIDRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.GetBackendSetIDRequestControl) GetAuthorizationEntryRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.GetAuthorizationEntryRequestControl) PurgePasswordRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.PurgePasswordRequestControl) ProxiedAuthorizationV2RequestControl(com.unboundid.ldap.sdk.controls.ProxiedAuthorizationV2RequestControl) SoftDeleteRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.SoftDeleteRequestControl) RetirePasswordRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.RetirePasswordRequestControl) GetServerIDRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.GetServerIDRequestControl) NoOpRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.NoOpRequestControl) PasswordPolicyRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.PasswordPolicyRequestControl) SuppressReferentialIntegrityUpdatesRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.SuppressReferentialIntegrityUpdatesRequestControl) AssertionRequestControl(com.unboundid.ldap.sdk.controls.AssertionRequestControl) RouteToBackendSetRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.RouteToBackendSetRequestControl) ManageDsaITRequestControl(com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl) PasswordValidationDetailsRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.PasswordValidationDetailsRequestControl) UniquenessRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.UniquenessRequestControl) GeneratePasswordRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.GeneratePasswordRequestControl) PreReadRequestControl(com.unboundid.ldap.sdk.controls.PreReadRequestControl) SubtreeDeleter(com.unboundid.util.SubtreeDeleter) LDAPException(com.unboundid.ldap.sdk.LDAPException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) SortedMap(java.util.SortedMap) ResultCode(com.unboundid.ldap.sdk.ResultCode) SubtreeDeleterResult(com.unboundid.util.SubtreeDeleterResult) NotNull(com.unboundid.util.NotNull)

Aggregations

LDAPException (com.unboundid.ldap.sdk.LDAPException)3 SubtreeDeleter (com.unboundid.util.SubtreeDeleter)3 ResultCode (com.unboundid.ldap.sdk.ResultCode)2 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)1 Control (com.unboundid.ldap.sdk.Control)1 DN (com.unboundid.ldap.sdk.DN)1 LDAPResult (com.unboundid.ldap.sdk.LDAPResult)1 SearchResult (com.unboundid.ldap.sdk.SearchResult)1 AssertionRequestControl (com.unboundid.ldap.sdk.controls.AssertionRequestControl)1 AuthorizationIdentityRequestControl (com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl)1 ManageDsaITRequestControl (com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl)1 PermissiveModifyRequestControl (com.unboundid.ldap.sdk.controls.PermissiveModifyRequestControl)1 PostReadRequestControl (com.unboundid.ldap.sdk.controls.PostReadRequestControl)1 PreReadRequestControl (com.unboundid.ldap.sdk.controls.PreReadRequestControl)1 ProxiedAuthorizationV1RequestControl (com.unboundid.ldap.sdk.controls.ProxiedAuthorizationV1RequestControl)1 ProxiedAuthorizationV2RequestControl (com.unboundid.ldap.sdk.controls.ProxiedAuthorizationV2RequestControl)1 SimplePagedResultsControl (com.unboundid.ldap.sdk.controls.SimplePagedResultsControl)1 SubtreeDeleteRequestControl (com.unboundid.ldap.sdk.controls.SubtreeDeleteRequestControl)1 TransactionSpecificationRequestControl (com.unboundid.ldap.sdk.controls.TransactionSpecificationRequestControl)1 AssuredReplicationRequestControl (com.unboundid.ldap.sdk.unboundidds.controls.AssuredReplicationRequestControl)1