Search in sources :

Example 1 with LDAPExtendedOperationException

use of com.unboundid.ldap.sdk.LDAPExtendedOperationException in project ldapsdk by pingidentity.

the class LDAPPasswordModify method followPasswordModifyReferral.

/**
 * Attempts to follow a referral that was returned in response to a password
 * modify extended request.
 *
 * @param  request               The extended request that was sent.
 * @param  result                The extended result that was received,
 *                               including the referral details.
 * @param  receivedOnConnection  The LDAP connection on which the referral
 *                               result was received.
 * @param  referralCount         The number of referrals that have been
 *                               returned so far.  If this is too high, then
 *                               subsequent referrals will not be followed.
 *
 * @return  A result code that indicates whether the password update was
 *          successful.
 */
@NotNull()
private ResultCode followPasswordModifyReferral(@NotNull final PasswordModifyExtendedRequest request, @NotNull final PasswordModifyExtendedResult result, @NotNull final LDAPConnection receivedOnConnection, final int referralCount) {
    final List<LDAPURL> referralURLs = new ArrayList<>();
    for (final String urlString : result.getReferralURLs()) {
        try {
            referralURLs.add(new LDAPURL(urlString));
        } catch (final LDAPException e) {
            Debug.debugException(e);
        }
    }
    if (referralURLs.isEmpty()) {
        logCompletionMessage(true, ERR_PWMOD_EXTOP_NO_VALID_REFERRAL_URLS.get(String.valueOf(result)));
        return ResultCode.REFERRAL;
    }
    LDAPException firstException = null;
    for (final LDAPURL url : referralURLs) {
        try (LDAPConnection referralConnection = receivedOnConnection.getReferralConnection(url, receivedOnConnection)) {
            final String referredUserIdentity;
            if (url.getBaseDN().isNullDN()) {
                referredUserIdentity = request.getUserIdentity();
            } else {
                referredUserIdentity = url.getBaseDN().toString();
            }
            final PasswordModifyExtendedRequest referralRequest = new PasswordModifyExtendedRequest(referredUserIdentity, request.getOldPassword(), request.getNewPassword(), request.getControls());
            final PasswordModifyExtendedResult referralResult = (PasswordModifyExtendedResult) referralConnection.processExtendedOperation(referralRequest);
            out();
            out(INFO_PWMOD_EXTOP_RESULT_HEADER.get());
            for (final String line : ResultUtils.formatResult(referralResult, true, 0, WRAP_COLUMN)) {
                out(line);
            }
            out();
            final String generatedPassword = referralResult.getGeneratedPassword();
            if (referralResult.getResultCode() == ResultCode.SUCCESS) {
                logCompletionMessage(false, INFO_PWMOD_EXTOP_SUCCESSFUL.get());
                if (generatedPassword != null) {
                    out();
                    wrapOut(0, WRAP_COLUMN, INFO_PWMOD_SERVER_GENERATED_PW.get(generatedPassword));
                }
                return ResultCode.SUCCESS;
            } else if (referralResult.getResultCode() == ResultCode.NO_OPERATION) {
                logCompletionMessage(false, INFO_PWMOD_EXTOP_NO_OP.get());
                if (generatedPassword != null) {
                    out();
                    wrapOut(0, WRAP_COLUMN, INFO_PWMOD_SERVER_GENERATED_PW.get(generatedPassword));
                }
                return ResultCode.SUCCESS;
            } else if (referralResult.getResultCode() == ResultCode.REFERRAL) {
                final int maxReferralCount = receivedOnConnection.getConnectionOptions().getReferralHopLimit();
                if (referralCount > maxReferralCount) {
                    logCompletionMessage(true, ERR_PWMOD_TOO_MANY_REFERRALS.get());
                    return ResultCode.REFERRAL_LIMIT_EXCEEDED;
                } else {
                    return followPasswordModifyReferral(referralRequest, referralResult, referralConnection, (referralCount + 1));
                }
            } else {
                if (firstException == null) {
                    firstException = new LDAPExtendedOperationException(referralResult);
                }
            }
        } catch (final LDAPException e) {
            Debug.debugException(e);
            if (firstException == null) {
                firstException = e;
            }
        }
    }
    logCompletionMessage(true, ERR_PWMOD_FOLLOW_REFERRAL_FAILED.get(String.valueOf(firstException.getResultCode()), firstException.getDiagnosticMessage()));
    return firstException.getResultCode();
}
Also used : LDAPURL(com.unboundid.ldap.sdk.LDAPURL) LDAPException(com.unboundid.ldap.sdk.LDAPException) PasswordModifyExtendedResult(com.unboundid.ldap.sdk.extensions.PasswordModifyExtendedResult) ArrayList(java.util.ArrayList) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) PasswordModifyExtendedRequest(com.unboundid.ldap.sdk.extensions.PasswordModifyExtendedRequest) LDAPExtendedOperationException(com.unboundid.ldap.sdk.LDAPExtendedOperationException) NotNull(com.unboundid.util.NotNull)

Example 2 with LDAPExtendedOperationException

use of com.unboundid.ldap.sdk.LDAPExtendedOperationException in project ldapsdk by pingidentity.

the class StartAdministrativeSessionPostConnectProcessorTestCase method testFailedSession.

/**
 * Tests the behavior of the post-connect processor when the extended
 * operation should return an error result.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testFailedSession() throws Exception {
    final InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=example,dc=com");
    config.addExtendedOperationHandler(new StartAdministrativeSessionInMemoryExtendedOperationHandler(new ExtendedResult(1, ResultCode.UNWILLING_TO_PERFORM, "Not gonna do it", null, null, null, null, null)));
    final InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
    ds.startListening();
    final SingleServerSet serverSet = new SingleServerSet("127.0.0.1", ds.getListenPort());
    final LDAPConnectionPool pool = new LDAPConnectionPool(serverSet, null, 0, 1, new StartAdministrativeSessionPostConnectProcessor(new StartAdministrativeSessionExtendedRequest("testSuccessfulSession", true)));
    try {
        pool.getRootDSE();
        fail("Expected an exception from the post-connect processor");
    } catch (final Exception e) {
        assertTrue(e instanceof LDAPExtendedOperationException);
        assertEquals(((LDAPExtendedOperationException) e).getResultCode(), ResultCode.UNWILLING_TO_PERFORM);
    }
    pool.close();
    ds.shutDown(true);
}
Also used : LDAPConnectionPool(com.unboundid.ldap.sdk.LDAPConnectionPool) SingleServerSet(com.unboundid.ldap.sdk.SingleServerSet) InMemoryDirectoryServer(com.unboundid.ldap.listener.InMemoryDirectoryServer) InMemoryDirectoryServerConfig(com.unboundid.ldap.listener.InMemoryDirectoryServerConfig) ExtendedResult(com.unboundid.ldap.sdk.ExtendedResult) LDAPExtendedOperationException(com.unboundid.ldap.sdk.LDAPExtendedOperationException) LDAPExtendedOperationException(com.unboundid.ldap.sdk.LDAPExtendedOperationException) Test(org.testng.annotations.Test)

Example 3 with LDAPExtendedOperationException

use of com.unboundid.ldap.sdk.LDAPExtendedOperationException in project ldapsdk by pingidentity.

the class StartTLSExtendedRequest method process.

/**
 * Sends this StartTLS request to the server and performs the necessary
 * client-side security processing if the operation is processed successfully.
 * That this method is guaranteed to throw an {@code LDAPException} if the
 * server returns a non-success result.
 *
 * @param  connection  The connection to use to communicate with the directory
 *                     server.
 * @param  depth       The current referral depth for this request.  It should
 *                     always be zero for the initial request, and should only
 *                     be incremented when following referrals.
 *
 * @return The extended result received from the server if StartTLS processing
 *         was completed successfully.
 *
 * @throws  LDAPException  If the server returned a non-success result, or if
 *                         a problem was encountered while performing
 *                         client-side security processing.
 */
@Override()
@NotNull()
public ExtendedResult process(@NotNull final LDAPConnection connection, final int depth) throws LDAPException {
    // Set an SO_TIMEOUT on the connection if it's not operating in synchronous
    // mode to make it more responsive during the negotiation phase.
    InternalSDKHelper.setSoTimeout(connection, 50);
    final ExtendedResult result = super.process(connection, depth);
    if (result.getResultCode() == ResultCode.SUCCESS) {
        InternalSDKHelper.convertToTLS(connection, sslSocketFactory);
    } else {
        throw new LDAPExtendedOperationException(result);
    }
    return result;
}
Also used : ExtendedResult(com.unboundid.ldap.sdk.ExtendedResult) LDAPExtendedOperationException(com.unboundid.ldap.sdk.LDAPExtendedOperationException) NotNull(com.unboundid.util.NotNull)

Aggregations

LDAPExtendedOperationException (com.unboundid.ldap.sdk.LDAPExtendedOperationException)3 ExtendedResult (com.unboundid.ldap.sdk.ExtendedResult)2 NotNull (com.unboundid.util.NotNull)2 InMemoryDirectoryServer (com.unboundid.ldap.listener.InMemoryDirectoryServer)1 InMemoryDirectoryServerConfig (com.unboundid.ldap.listener.InMemoryDirectoryServerConfig)1 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)1 LDAPConnectionPool (com.unboundid.ldap.sdk.LDAPConnectionPool)1 LDAPException (com.unboundid.ldap.sdk.LDAPException)1 LDAPURL (com.unboundid.ldap.sdk.LDAPURL)1 SingleServerSet (com.unboundid.ldap.sdk.SingleServerSet)1 PasswordModifyExtendedRequest (com.unboundid.ldap.sdk.extensions.PasswordModifyExtendedRequest)1 PasswordModifyExtendedResult (com.unboundid.ldap.sdk.extensions.PasswordModifyExtendedResult)1 ArrayList (java.util.ArrayList)1 Test (org.testng.annotations.Test)1