Search in sources :

Example 11 with EndTransactionExtendedResult

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

the class TransactionExtendedOperationHandlerTestCase method testFailedTransaction.

/**
 * Provides a test case for a transaction in which one of the operations
 * fails after other operations that should have succeeded.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testFailedTransaction() throws Exception {
    final TestUnsolicitedNotificationHandler unsolicitedNotificationHandler = new TestUnsolicitedNotificationHandler();
    final LDAPConnectionOptions connectionOptions = new LDAPConnectionOptions();
    connectionOptions.setUnsolicitedNotificationHandler(unsolicitedNotificationHandler);
    final InMemoryDirectoryServer ds = getTestDS(true, true);
    final LDAPConnection conn = ds.getConnection(connectionOptions);
    assertEquals(unsolicitedNotificationHandler.getNotificationCount(), 0);
    ds.assertEntryExists("dc=example,dc=com");
    ds.assertEntryExists("ou=People,dc=example,dc=com");
    ds.assertEntryExists("uid=test.user,ou=People,dc=example,dc=com");
    ds.assertEntryMissing("ou=test,dc=example,dc=com");
    ds.assertEntryMissing("uid=test.user,ou=test,dc=example,dc=com");
    final StartTransactionExtendedResult startTxnResult = (StartTransactionExtendedResult) conn.processExtendedOperation(new StartTransactionExtendedRequest());
    assertResultCodeEquals(startTxnResult, ResultCode.SUCCESS);
    final ASN1OctetString txnID = startTxnResult.getTransactionID();
    assertNotNull(txnID);
    final TransactionSpecificationRequestControl txnControl = new TransactionSpecificationRequestControl(txnID);
    ds.assertEntryExists("dc=example,dc=com");
    ds.assertEntryExists("ou=People,dc=example,dc=com");
    ds.assertEntryExists("uid=test.user,ou=People,dc=example,dc=com");
    ds.assertEntryMissing("ou=test,dc=example,dc=com");
    ds.assertEntryMissing("uid=test.user,ou=test,dc=example,dc=com");
    final AddRequest addRequest = new AddRequest("dn: ou=test,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: test");
    addRequest.setControls(txnControl);
    assertResultCodeEquals(conn, addRequest, ResultCode.SUCCESS);
    ds.assertEntryExists("dc=example,dc=com");
    ds.assertEntryExists("ou=People,dc=example,dc=com");
    ds.assertEntryExists("uid=test.user,ou=People,dc=example,dc=com");
    ds.assertEntryMissing("ou=test,dc=example,dc=com");
    ds.assertEntryMissing("uid=test.user,ou=test,dc=example,dc=com");
    final ModifyRequest modifyRequest = new ModifyRequest("dn: uid=test.user,ou=People,dc=example,dc=com", "changeType: modify", "replace: description", "description: foo");
    modifyRequest.setControls(txnControl);
    assertResultCodeEquals(conn, modifyRequest, ResultCode.SUCCESS);
    ds.assertEntryExists("dc=example,dc=com");
    ds.assertEntryExists("ou=People,dc=example,dc=com");
    ds.assertEntryExists("uid=test.user,ou=People,dc=example,dc=com");
    ds.assertEntryMissing("ou=test,dc=example,dc=com");
    ds.assertEntryMissing("uid=test.user,ou=test,dc=example,dc=com");
    final ModifyDNRequest modifyDNRequest = new ModifyDNRequest("uid=test.user,ou=People,dc=example,dc=com", "uid=test.user", false, "ou=test,dc=example,dc=com");
    modifyDNRequest.setControls(txnControl);
    assertResultCodeEquals(conn, modifyDNRequest, ResultCode.SUCCESS);
    ds.assertEntryExists("dc=example,dc=com");
    ds.assertEntryExists("ou=People,dc=example,dc=com");
    ds.assertEntryExists("uid=test.user,ou=People,dc=example,dc=com");
    ds.assertEntryMissing("ou=test,dc=example,dc=com");
    ds.assertEntryMissing("uid=test.user,ou=test,dc=example,dc=com");
    final DeleteRequest deleteRequest = new DeleteRequest("ou=missing,dc=example,dc=com");
    deleteRequest.setControls(txnControl);
    assertResultCodeEquals(conn, deleteRequest, ResultCode.SUCCESS);
    ds.assertEntryExists("dc=example,dc=com");
    ds.assertEntryExists("ou=People,dc=example,dc=com");
    ds.assertEntryExists("uid=test.user,ou=People,dc=example,dc=com");
    ds.assertEntryMissing("ou=test,dc=example,dc=com");
    ds.assertEntryMissing("uid=test.user,ou=test,dc=example,dc=com");
    final EndTransactionExtendedResult endTxnResult = (EndTransactionExtendedResult) conn.processExtendedOperation(new EndTransactionExtendedRequest(txnID, true));
    assertResultCodeEquals(endTxnResult, ResultCode.NO_SUCH_OBJECT);
    assertTrue(endTxnResult.getFailedOpMessageID() > 0);
    assertEquals(endTxnResult.getFailedOpMessageID(), deleteRequest.getLastMessageID());
    assertNotNull(endTxnResult.getOperationResponseControls());
    assertTrue(endTxnResult.getOperationResponseControls().isEmpty());
    ds.assertEntryExists("dc=example,dc=com");
    ds.assertEntryExists("ou=People,dc=example,dc=com");
    ds.assertEntryExists("uid=test.user,ou=People,dc=example,dc=com");
    ds.assertEntryMissing("ou=test,dc=example,dc=com");
    ds.assertEntryMissing("uid=test.user,ou=test,dc=example,dc=com");
    conn.close();
    assertEquals(unsolicitedNotificationHandler.getNotificationCount(), 0);
}
Also used : LDAPConnectionOptions(com.unboundid.ldap.sdk.LDAPConnectionOptions) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) EndTransactionExtendedRequest(com.unboundid.ldap.sdk.extensions.EndTransactionExtendedRequest) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) ModifyRequest(com.unboundid.ldap.sdk.ModifyRequest) AddRequest(com.unboundid.ldap.sdk.AddRequest) ModifyDNRequest(com.unboundid.ldap.sdk.ModifyDNRequest) StartTransactionExtendedResult(com.unboundid.ldap.sdk.extensions.StartTransactionExtendedResult) StartTransactionExtendedRequest(com.unboundid.ldap.sdk.extensions.StartTransactionExtendedRequest) DeleteRequest(com.unboundid.ldap.sdk.DeleteRequest) EndTransactionExtendedResult(com.unboundid.ldap.sdk.extensions.EndTransactionExtendedResult) TestUnsolicitedNotificationHandler(com.unboundid.ldap.sdk.TestUnsolicitedNotificationHandler) TransactionSpecificationRequestControl(com.unboundid.ldap.sdk.controls.TransactionSpecificationRequestControl) Test(org.testng.annotations.Test)

Example 12 with EndTransactionExtendedResult

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

the class TransactionExtendedOperationHandlerTestCase method testFailedModifyDNInTransaction.

/**
 * Provides a test case for a transaction in which a modify DN operation fails
 * after other operations that should have succeeded.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testFailedModifyDNInTransaction() throws Exception {
    final TestUnsolicitedNotificationHandler unsolicitedNotificationHandler = new TestUnsolicitedNotificationHandler();
    final LDAPConnectionOptions connectionOptions = new LDAPConnectionOptions();
    connectionOptions.setUnsolicitedNotificationHandler(unsolicitedNotificationHandler);
    final InMemoryDirectoryServer ds = getTestDS(true, true);
    final LDAPConnection conn = ds.getConnection(connectionOptions);
    assertEquals(unsolicitedNotificationHandler.getNotificationCount(), 0);
    ds.assertEntryExists("dc=example,dc=com");
    ds.assertEntryExists("ou=People,dc=example,dc=com");
    ds.assertEntryExists("uid=test.user,ou=People,dc=example,dc=com");
    ds.assertEntryMissing("ou=test,dc=example,dc=com");
    ds.assertEntryMissing("uid=test.user,ou=test,dc=example,dc=com");
    final StartTransactionExtendedResult startTxnResult = (StartTransactionExtendedResult) conn.processExtendedOperation(new StartTransactionExtendedRequest());
    assertResultCodeEquals(startTxnResult, ResultCode.SUCCESS);
    final ASN1OctetString txnID = startTxnResult.getTransactionID();
    assertNotNull(txnID);
    final TransactionSpecificationRequestControl txnControl = new TransactionSpecificationRequestControl(txnID);
    final AddRequest addRequest = new AddRequest("dn: ou=test,dc=example,dc=com", "objectClass: top", "objectClass: organizationalUnit", "ou: test");
    addRequest.setControls(txnControl);
    assertResultCodeEquals(conn, addRequest, ResultCode.SUCCESS);
    final ModifyDNRequest modifyDNRequest = new ModifyDNRequest("ou=Missing,dc=example,dc=com", "ou=Still Missing", true);
    modifyDNRequest.setControls(txnControl);
    assertResultCodeEquals(conn, modifyDNRequest, ResultCode.SUCCESS);
    final EndTransactionExtendedResult endTxnResult = (EndTransactionExtendedResult) conn.processExtendedOperation(new EndTransactionExtendedRequest(txnID, true));
    assertResultCodeEquals(endTxnResult, ResultCode.NO_SUCH_OBJECT);
    assertTrue(endTxnResult.getFailedOpMessageID() > 0);
    assertEquals(endTxnResult.getFailedOpMessageID(), modifyDNRequest.getLastMessageID());
    assertNotNull(endTxnResult.getOperationResponseControls());
    assertTrue(endTxnResult.getOperationResponseControls().isEmpty());
    ds.assertEntryExists("dc=example,dc=com");
    ds.assertEntryExists("ou=People,dc=example,dc=com");
    ds.assertEntryExists("uid=test.user,ou=People,dc=example,dc=com");
    ds.assertEntryMissing("ou=test,dc=example,dc=com");
    ds.assertEntryMissing("uid=test.user,ou=test,dc=example,dc=com");
    conn.close();
    assertEquals(unsolicitedNotificationHandler.getNotificationCount(), 0);
}
Also used : LDAPConnectionOptions(com.unboundid.ldap.sdk.LDAPConnectionOptions) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) AddRequest(com.unboundid.ldap.sdk.AddRequest) ModifyDNRequest(com.unboundid.ldap.sdk.ModifyDNRequest) StartTransactionExtendedResult(com.unboundid.ldap.sdk.extensions.StartTransactionExtendedResult) StartTransactionExtendedRequest(com.unboundid.ldap.sdk.extensions.StartTransactionExtendedRequest) EndTransactionExtendedRequest(com.unboundid.ldap.sdk.extensions.EndTransactionExtendedRequest) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) EndTransactionExtendedResult(com.unboundid.ldap.sdk.extensions.EndTransactionExtendedResult) TestUnsolicitedNotificationHandler(com.unboundid.ldap.sdk.TestUnsolicitedNotificationHandler) TransactionSpecificationRequestControl(com.unboundid.ldap.sdk.controls.TransactionSpecificationRequestControl) Test(org.testng.annotations.Test)

Example 13 with EndTransactionExtendedResult

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

the class ResultUtilsTestCase method getFormatResultData.

/**
 * Retrieves a set of data for testing the {@code formatResult} method.
 *
 * @return  The test data.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@DataProvider(name = "formatResultData")
public Iterator<Object[]> getFormatResultData() throws Exception {
    final LinkedList<Object[]> resultList = new LinkedList<Object[]>();
    // A simple success result.
    final String txnNoteLine = "#      NOTE:  No changes will actually be " + "applied to the server until the transaction is committed.";
    resultList.add(new Object[] { new LDAPResult(-1, ResultCode.SUCCESS), Arrays.asList("#      Result Code:  0 (success)", txnNoteLine) });
    // A failure result with a single referral URL and a single control .
    final String[] singleReferralURL = { "ldap://ds.example.com:389/dc=example,dc=com" };
    final Control[] singleResponseControl = { new Control("1.2.3.4") };
    resultList.add(new Object[] { new LDAPResult(-1, ResultCode.OTHER, "Something went wrong", "dc=example,dc=com", singleReferralURL, singleResponseControl), Arrays.asList("#      Result Code:  80 (other)", "#      Diagnostic Message:  Something went wrong", "#      Matched DN:  dc=example,dc=com", "#      Referral URL:  " + "ldap://ds.example.com:389/dc=example,dc=com", "#      Response Control:", "#           OID:  1.2.3.4", "#           Is Critical:  false") });
    // A failure result with multiple referral URLs and multiple controls.
    final String[] multipleReferralURLs = { "ldap://ds1.example.com:389/dc=example,dc=com", "ldap://ds2.example.com:389/dc=example,dc=com" };
    final Control[] multipleResponseControls = { new Control("1.2.3.4"), new Control("1.2.3.5", true, new ASN1OctetString("control value")) };
    resultList.add(new Object[] { new LDAPResult(-1, ResultCode.OTHER, "Something went wrong", "dc=example,dc=com", multipleReferralURLs, multipleResponseControls), Arrays.asList("#      Result Code:  80 (other)", "#      Diagnostic Message:  Something went wrong", "#      Matched DN:  dc=example,dc=com", "#      Referral URL:  " + "ldap://ds1.example.com:389/dc=example,dc=com", "#      Referral URL:  " + "ldap://ds2.example.com:389/dc=example,dc=com", "#      Response Control:", "#           OID:  1.2.3.4", "#           Is Critical:  false", "#      Response Control:", "#           OID:  1.2.3.5", "#           Is Critical:  true", "#           Raw Value:", "#                63 6f 6e 74 72 6f 6c 20 76 61 6c 75 " + "65            control value") });
    // A successful start transaction extended result with a printable
    // transaction ID.
    resultList.add(new Object[] { new StartTransactionExtendedResult(2, ResultCode.SUCCESS, null, null, null, new ASN1OctetString("txnID"), null), Arrays.asList("#      Result Code:  0 (success)", txnNoteLine, "#      Start Transaction Extended Result Transaction ID:  " + "txnID") });
    // A successful start transaction extended result with a non-printable
    // transaction ID.
    resultList.add(new Object[] { new StartTransactionExtendedResult(2, ResultCode.SUCCESS, null, null, null, new ASN1OctetString(new byte[] { 0x01, 0x23, 0x45 }), null), Arrays.asList("#      Result Code:  0 (success)", txnNoteLine, "#      Start Transaction Extended Result Transaction ID:  " + "0x012345") });
    // A successful end transaction extended result.
    final LinkedHashMap<Integer, Control[]> opResponseControls = new LinkedHashMap<Integer, Control[]>(2);
    opResponseControls.put(3, new Control[] { new Control("1.2.3.4") });
    opResponseControls.put(4, new Control[] { new Control("1.2.3.4"), new Control("1.2.3.5", true, new ASN1OctetString("control value")) });
    resultList.add(new Object[] { new EndTransactionExtendedResult(5, ResultCode.SUCCESS, null, null, null, null, opResponseControls, null), Arrays.asList("#      Result Code:  0 (success)", txnNoteLine, "#      End Transaction Extended Result Response Control for " + "Message ID 3:", "#           Response Control:", "#                OID:  1.2.3.4", "#                Is Critical:  false", "#      End Transaction Extended Result Response Control for " + "Message ID 4:", "#           Response Control:", "#                OID:  1.2.3.4", "#                Is Critical:  false", "#      End Transaction Extended Result Response Control for " + "Message ID 4:", "#           Response Control:", "#                OID:  1.2.3.5", "#                Is Critical:  true", "#                Raw Value:", "#                     63 6f 6e 74 72 6f 6c 20 76 61 6c 75 " + "65            control value") });
    // A failed end transaction extended result.
    resultList.add(new Object[] { new EndTransactionExtendedResult(5, ResultCode.OTHER, "One of the operations failed", null, null, 3, null, null), Arrays.asList("#      Result Code:  80 (other)", "#      Diagnostic Message:  One of the operations failed", "#      End Transaction Extended Result Failed Operation " + "Message ID:  3") });
    // A successful multi-update extended result.
    final ArrayList<ObjectPair<OperationType, LDAPResult>> opResults = new ArrayList<ObjectPair<OperationType, LDAPResult>>(2);
    opResults.add(new ObjectPair<OperationType, LDAPResult>(OperationType.ADD, new LDAPResult(3, ResultCode.SUCCESS)));
    opResults.add(new ObjectPair<OperationType, LDAPResult>(OperationType.DELETE, new LDAPResult(4, ResultCode.SUCCESS)));
    opResults.add(new ObjectPair<OperationType, LDAPResult>(OperationType.MODIFY, new LDAPResult(5, ResultCode.SUCCESS)));
    opResults.add(new ObjectPair<OperationType, LDAPResult>(OperationType.MODIFY_DN, new LDAPResult(6, ResultCode.SUCCESS)));
    resultList.add(new Object[] { new MultiUpdateExtendedResult(7, ResultCode.SUCCESS, null, null, null, MultiUpdateChangesApplied.ALL, opResults), Arrays.asList("#      Result Code:  0 (success)", txnNoteLine, "#      Multi-Update Changes Applied:  ALL", "#      Multi-Update ADD Operation Result:", "#           Result Code:  0 (success)", "#      Multi-Update DELETE Operation Result:", "#           Result Code:  0 (success)", "#      Multi-Update MODIFY Operation Result:", "#           Result Code:  0 (success)", "#      Multi-Update MODIFY_DN Operation Result:", "#           Result Code:  0 (success)") });
    // A successful password modify extended result.
    resultList.add(new Object[] { new PasswordModifyExtendedResult(1, ResultCode.SUCCESS, null, null, null, new ASN1OctetString("newPassword"), null), Arrays.asList("#      Result Code:  0 (success)", txnNoteLine, "#      Password Modify Extended Result Generated Password:  " + "newPassword") });
    // A successful generic extended result.
    resultList.add(new Object[] { new ExtendedResult(1, ResultCode.SUCCESS, null, null, null, "1.2.3.4", new ASN1OctetString("extended operation value"), null), Arrays.asList("#      Result Code:  0 (success)", txnNoteLine, "#      Extended Result OID:  1.2.3.4", "#      Extended Result Raw Value:", "#           65 78 74 65 6e 64 65 64 20 6f 70 65 72 61 74 " + "69   extended operati", "#           6f 6e 20 76 61 6c 75 " + "65                           on value") });
    return resultList.iterator();
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) LDAPResult(com.unboundid.ldap.sdk.LDAPResult) ArrayList(java.util.ArrayList) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) MultiUpdateExtendedResult(com.unboundid.ldap.sdk.unboundidds.extensions.MultiUpdateExtendedResult) IntermediateClientResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.IntermediateClientResponseControl) TransactionSettingsResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.TransactionSettingsResponseControl) Control(com.unboundid.ldap.sdk.Control) UniquenessResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.UniquenessResponseControl) SimplePagedResultsControl(com.unboundid.ldap.sdk.controls.SimplePagedResultsControl) PasswordValidationDetailsResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.PasswordValidationDetailsResponseControl) VirtualListViewResponseControl(com.unboundid.ldap.sdk.controls.VirtualListViewResponseControl) GetPasswordPolicyStateIssuesResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.GetPasswordPolicyStateIssuesResponseControl) AccountUsableResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.AccountUsableResponseControl) GetServerIDResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.GetServerIDResponseControl) SoftDeleteResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.SoftDeleteResponseControl) PasswordExpiredControl(com.unboundid.ldap.sdk.controls.PasswordExpiredControl) PasswordExpiringControl(com.unboundid.ldap.sdk.controls.PasswordExpiringControl) PostReadResponseControl(com.unboundid.ldap.sdk.controls.PostReadResponseControl) GetAuthorizationEntryResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.GetAuthorizationEntryResponseControl) ServerSideSortResponseControl(com.unboundid.ldap.sdk.controls.ServerSideSortResponseControl) MatchingEntryCountResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.MatchingEntryCountResponseControl) GeneratePasswordResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.GeneratePasswordResponseControl) EntryChangeNotificationControl(com.unboundid.ldap.sdk.controls.EntryChangeNotificationControl) GetUserResourceLimitsResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.GetUserResourceLimitsResponseControl) AssuredReplicationResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.AssuredReplicationResponseControl) ContentSyncStateControl(com.unboundid.ldap.sdk.controls.ContentSyncStateControl) GetRecentLoginHistoryResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.GetRecentLoginHistoryResponseControl) JoinResultControl(com.unboundid.ldap.sdk.unboundidds.controls.JoinResultControl) GetBackendSetIDResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.GetBackendSetIDResponseControl) PasswordPolicyResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.PasswordPolicyResponseControl) PreReadResponseControl(com.unboundid.ldap.sdk.controls.PreReadResponseControl) AuthorizationIdentityResponseControl(com.unboundid.ldap.sdk.controls.AuthorizationIdentityResponseControl) ContentSyncDoneControl(com.unboundid.ldap.sdk.controls.ContentSyncDoneControl) PasswordModifyExtendedResult(com.unboundid.ldap.sdk.extensions.PasswordModifyExtendedResult) StartTransactionExtendedResult(com.unboundid.ldap.sdk.extensions.StartTransactionExtendedResult) ExtendedResult(com.unboundid.ldap.sdk.ExtendedResult) StartTransactionExtendedResult(com.unboundid.ldap.sdk.extensions.StartTransactionExtendedResult) NoticeOfDisconnectionExtendedResult(com.unboundid.ldap.sdk.extensions.NoticeOfDisconnectionExtendedResult) MultiUpdateExtendedResult(com.unboundid.ldap.sdk.unboundidds.extensions.MultiUpdateExtendedResult) AbortedTransactionExtendedResult(com.unboundid.ldap.sdk.extensions.AbortedTransactionExtendedResult) EndTransactionExtendedResult(com.unboundid.ldap.sdk.extensions.EndTransactionExtendedResult) PasswordModifyExtendedResult(com.unboundid.ldap.sdk.extensions.PasswordModifyExtendedResult) OperationType(com.unboundid.ldap.sdk.OperationType) EndTransactionExtendedResult(com.unboundid.ldap.sdk.extensions.EndTransactionExtendedResult) ObjectPair(com.unboundid.util.ObjectPair) DataProvider(org.testng.annotations.DataProvider)

Example 14 with EndTransactionExtendedResult

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

the class TransactionExtendedOperationHandler method handleEndTransaction.

/**
 * Performs the appropriate processing for an end transaction extended
 * request.
 *
 * @param  handler    The in-memory request handler that received the request.
 * @param  messageID  The message ID for the associated request.
 * @param  request    The extended request that was received.
 *
 * @return  The result for the extended operation processing.
 */
@NotNull()
private static EndTransactionExtendedResult handleEndTransaction(@NotNull final InMemoryRequestHandler handler, final int messageID, @NotNull final ExtendedRequest request) {
    // Get information about any transaction currently in progress on the
    // connection.  If there isn't one, then fail.
    final Map<String, Object> connectionState = handler.getConnectionState();
    final ObjectPair<?, ?> txnInfo = (ObjectPair<?, ?>) connectionState.remove(STATE_VARIABLE_TXN_INFO);
    if (txnInfo == null) {
        return new EndTransactionExtendedResult(messageID, ResultCode.CONSTRAINT_VIOLATION, ERR_TXN_EXTOP_END_NO_ACTIVE_TXN.get(), null, null, null, null, null);
    }
    // Make sure that we can decode the end transaction request.
    final ASN1OctetString existingTxnID = (ASN1OctetString) txnInfo.getFirst();
    final EndTransactionExtendedRequest endTxnRequest;
    try {
        endTxnRequest = new EndTransactionExtendedRequest(request);
    } catch (final LDAPException le) {
        Debug.debugException(le);
        try {
            handler.getClientConnection().sendUnsolicitedNotification(new AbortedTransactionExtendedResult(existingTxnID, ResultCode.PROTOCOL_ERROR, ERR_TXN_EXTOP_ABORTED_BY_MALFORMED_END_TXN.get(existingTxnID.stringValue()), null, null, null));
        } catch (final LDAPException le2) {
            Debug.debugException(le2);
        }
        return new EndTransactionExtendedResult(messageID, ResultCode.PROTOCOL_ERROR, le.getMessage(), null, null, null, null, null);
    }
    // Make sure that the transaction ID of the existing transaction matches the
    // transaction ID from the end transaction request.
    final ASN1OctetString targetTxnID = endTxnRequest.getTransactionID();
    if (!existingTxnID.stringValue().equals(targetTxnID.stringValue())) {
        // transaction has been aborted.
        try {
            handler.getClientConnection().sendUnsolicitedNotification(new AbortedTransactionExtendedResult(existingTxnID, ResultCode.CONSTRAINT_VIOLATION, ERR_TXN_EXTOP_ABORTED_BY_WRONG_END_TXN.get(existingTxnID.stringValue(), targetTxnID.stringValue()), null, null, null));
        } catch (final LDAPException le) {
            Debug.debugException(le);
            return new EndTransactionExtendedResult(messageID, le.getResultCode(), le.getMessage(), le.getMatchedDN(), le.getReferralURLs(), null, null, le.getResponseControls());
        }
        return new EndTransactionExtendedResult(messageID, ResultCode.CONSTRAINT_VIOLATION, ERR_TXN_EXTOP_END_WRONG_TXN.get(targetTxnID.stringValue(), existingTxnID.stringValue()), null, null, null, null, null);
    }
    // If the transaction should be aborted, then we can just send the response.
    if (!endTxnRequest.commit()) {
        return new EndTransactionExtendedResult(messageID, ResultCode.SUCCESS, INFO_TXN_EXTOP_END_TXN_ABORTED.get(existingTxnID.stringValue()), null, null, null, null, null);
    }
    // If we've gotten here, then we'll try to commit the transaction.  First,
    // get a snapshot of the current state so that we can roll back to it if
    // necessary.
    final InMemoryDirectoryServerSnapshot snapshot = handler.createSnapshot();
    boolean rollBack = true;
    try {
        // Create a map to hold information about response controls from
        // operations processed as part of the transaction.
        final List<?> requestMessages = (List<?>) txnInfo.getSecond();
        final Map<Integer, Control[]> opResponseControls = new LinkedHashMap<>(StaticUtils.computeMapCapacity(requestMessages.size()));
        // Iterate through the requests that have been submitted as part of the
        // transaction and attempt to process them.
        ResultCode resultCode = ResultCode.SUCCESS;
        String diagnosticMessage = null;
        String failedOpType = null;
        Integer failedOpMessageID = null;
        txnOpLoop: for (final Object o : requestMessages) {
            final LDAPMessage m = (LDAPMessage) o;
            switch(m.getProtocolOpType()) {
                case LDAPMessage.PROTOCOL_OP_TYPE_ADD_REQUEST:
                    final LDAPMessage addResponseMessage = handler.processAddRequest(m.getMessageID(), m.getAddRequestProtocolOp(), m.getControls());
                    final AddResponseProtocolOp addResponseOp = addResponseMessage.getAddResponseProtocolOp();
                    final List<Control> addControls = addResponseMessage.getControls();
                    if ((addControls != null) && (!addControls.isEmpty())) {
                        final Control[] controls = new Control[addControls.size()];
                        addControls.toArray(controls);
                        opResponseControls.put(m.getMessageID(), controls);
                    }
                    if (addResponseOp.getResultCode() != ResultCode.SUCCESS_INT_VALUE) {
                        resultCode = ResultCode.valueOf(addResponseOp.getResultCode());
                        diagnosticMessage = addResponseOp.getDiagnosticMessage();
                        failedOpType = INFO_TXN_EXTOP_OP_TYPE_ADD.get();
                        failedOpMessageID = m.getMessageID();
                        break txnOpLoop;
                    }
                    break;
                case LDAPMessage.PROTOCOL_OP_TYPE_DELETE_REQUEST:
                    final LDAPMessage deleteResponseMessage = handler.processDeleteRequest(m.getMessageID(), m.getDeleteRequestProtocolOp(), m.getControls());
                    final DeleteResponseProtocolOp deleteResponseOp = deleteResponseMessage.getDeleteResponseProtocolOp();
                    final List<Control> deleteControls = deleteResponseMessage.getControls();
                    if ((deleteControls != null) && (!deleteControls.isEmpty())) {
                        final Control[] controls = new Control[deleteControls.size()];
                        deleteControls.toArray(controls);
                        opResponseControls.put(m.getMessageID(), controls);
                    }
                    if (deleteResponseOp.getResultCode() != ResultCode.SUCCESS_INT_VALUE) {
                        resultCode = ResultCode.valueOf(deleteResponseOp.getResultCode());
                        diagnosticMessage = deleteResponseOp.getDiagnosticMessage();
                        failedOpType = INFO_TXN_EXTOP_OP_TYPE_DELETE.get();
                        failedOpMessageID = m.getMessageID();
                        break txnOpLoop;
                    }
                    break;
                case LDAPMessage.PROTOCOL_OP_TYPE_MODIFY_REQUEST:
                    final LDAPMessage modifyResponseMessage = handler.processModifyRequest(m.getMessageID(), m.getModifyRequestProtocolOp(), m.getControls());
                    final ModifyResponseProtocolOp modifyResponseOp = modifyResponseMessage.getModifyResponseProtocolOp();
                    final List<Control> modifyControls = modifyResponseMessage.getControls();
                    if ((modifyControls != null) && (!modifyControls.isEmpty())) {
                        final Control[] controls = new Control[modifyControls.size()];
                        modifyControls.toArray(controls);
                        opResponseControls.put(m.getMessageID(), controls);
                    }
                    if (modifyResponseOp.getResultCode() != ResultCode.SUCCESS_INT_VALUE) {
                        resultCode = ResultCode.valueOf(modifyResponseOp.getResultCode());
                        diagnosticMessage = modifyResponseOp.getDiagnosticMessage();
                        failedOpType = INFO_TXN_EXTOP_OP_TYPE_MODIFY.get();
                        failedOpMessageID = m.getMessageID();
                        break txnOpLoop;
                    }
                    break;
                case LDAPMessage.PROTOCOL_OP_TYPE_MODIFY_DN_REQUEST:
                    final LDAPMessage modifyDNResponseMessage = handler.processModifyDNRequest(m.getMessageID(), m.getModifyDNRequestProtocolOp(), m.getControls());
                    final ModifyDNResponseProtocolOp modifyDNResponseOp = modifyDNResponseMessage.getModifyDNResponseProtocolOp();
                    final List<Control> modifyDNControls = modifyDNResponseMessage.getControls();
                    if ((modifyDNControls != null) && (!modifyDNControls.isEmpty())) {
                        final Control[] controls = new Control[modifyDNControls.size()];
                        modifyDNControls.toArray(controls);
                        opResponseControls.put(m.getMessageID(), controls);
                    }
                    if (modifyDNResponseOp.getResultCode() != ResultCode.SUCCESS_INT_VALUE) {
                        resultCode = ResultCode.valueOf(modifyDNResponseOp.getResultCode());
                        diagnosticMessage = modifyDNResponseOp.getDiagnosticMessage();
                        failedOpType = INFO_TXN_EXTOP_OP_TYPE_MODIFY_DN.get();
                        failedOpMessageID = m.getMessageID();
                        break txnOpLoop;
                    }
                    break;
            }
        }
        if (resultCode == ResultCode.SUCCESS) {
            diagnosticMessage = INFO_TXN_EXTOP_COMMITTED.get(existingTxnID.stringValue());
            rollBack = false;
        } else {
            diagnosticMessage = ERR_TXN_EXTOP_COMMIT_FAILED.get(existingTxnID.stringValue(), failedOpType, failedOpMessageID, diagnosticMessage);
        }
        return new EndTransactionExtendedResult(messageID, resultCode, diagnosticMessage, null, null, failedOpMessageID, opResponseControls, null);
    } finally {
        if (rollBack) {
            handler.restoreSnapshot(snapshot);
        }
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) DeleteResponseProtocolOp(com.unboundid.ldap.protocol.DeleteResponseProtocolOp) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) EndTransactionExtendedRequest(com.unboundid.ldap.sdk.extensions.EndTransactionExtendedRequest) AddResponseProtocolOp(com.unboundid.ldap.protocol.AddResponseProtocolOp) ModifyDNResponseProtocolOp(com.unboundid.ldap.protocol.ModifyDNResponseProtocolOp) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) LinkedHashMap(java.util.LinkedHashMap) LDAPException(com.unboundid.ldap.sdk.LDAPException) ModifyResponseProtocolOp(com.unboundid.ldap.protocol.ModifyResponseProtocolOp) ArrayList(java.util.ArrayList) List(java.util.List) EndTransactionExtendedResult(com.unboundid.ldap.sdk.extensions.EndTransactionExtendedResult) ResultCode(com.unboundid.ldap.sdk.ResultCode) ObjectPair(com.unboundid.util.ObjectPair) AbortedTransactionExtendedResult(com.unboundid.ldap.sdk.extensions.AbortedTransactionExtendedResult) NotNull(com.unboundid.util.NotNull)

Example 15 with EndTransactionExtendedResult

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

the class ResultUtils method formatResult.

/**
 * Adds a multi-line string representation of the provided result to the
 * given list.
 *
 * @param  lines     The list to which the lines should be added.
 * @param  result    The result to be formatted.
 * @param  inTxn     Indicates whether the operation is part of an active
 *                   transaction.
 * @param  prefix    The prefix to use for each line.
 * @param  maxWidth  The maximum length of each line in characters, including
 *                   the comment prefix and indent.
 */
private static void formatResult(@NotNull final List<String> lines, @NotNull final LDAPResult result, final boolean inTxn, @NotNull final String prefix, final int maxWidth) {
    // Format the result code.  If it's a success result but the operation was
    // part of a transaction, then indicate that no change has actually been
    // made yet.
    final ResultCode resultCode = result.getResultCode();
    wrap(lines, INFO_RESULT_UTILS_RESULT_CODE.get(String.valueOf(resultCode)), prefix, maxWidth);
    if (inTxn && (resultCode == ResultCode.SUCCESS)) {
        wrap(lines, INFO_RESULT_UTILS_SUCCESS_WITH_TXN.get(), prefix, maxWidth);
    }
    // Format the diagnostic message, if there is one.
    final String diagnosticMessage = result.getDiagnosticMessage();
    if (diagnosticMessage != null) {
        wrap(lines, INFO_RESULT_UTILS_DIAGNOSTIC_MESSAGE.get(diagnosticMessage), prefix, maxWidth);
    }
    // Format the matched DN, if there is one.
    final String matchedDN = result.getMatchedDN();
    if (matchedDN != null) {
        wrap(lines, INFO_RESULT_UTILS_MATCHED_DN.get(matchedDN), prefix, maxWidth);
    }
    // If there are any referral URLs, then display them.
    final String[] referralURLs = result.getReferralURLs();
    if (referralURLs != null) {
        for (final String referralURL : referralURLs) {
            wrap(lines, INFO_RESULT_UTILS_REFERRAL_URL.get(referralURL), prefix, maxWidth);
        }
    }
    if (result instanceof SearchResult) {
        final SearchResult searchResult = (SearchResult) result;
        // We'll always display the search entry count if we know it.
        final int numEntries = searchResult.getEntryCount();
        if (numEntries >= 0) {
            wrap(lines, INFO_RESULT_UTILS_NUM_SEARCH_ENTRIES.get(numEntries), prefix, maxWidth);
        }
        // We'll only display the search reference count if it's greater than
        // zero.
        final int numReferences = searchResult.getReferenceCount();
        if (numReferences > 0) {
            wrap(lines, INFO_RESULT_UTILS_NUM_SEARCH_REFERENCES.get(numReferences), prefix, maxWidth);
        }
    } else if (result instanceof StartTransactionExtendedResult) {
        final StartTransactionExtendedResult startTxnResult = (StartTransactionExtendedResult) result;
        final ASN1OctetString txnID = startTxnResult.getTransactionID();
        if (txnID != null) {
            if (StaticUtils.isPrintableString(txnID.getValue())) {
                wrap(lines, INFO_RESULT_UTILS_START_TXN_RESULT_TXN_ID.get(txnID.stringValue()), prefix, maxWidth);
            } else {
                wrap(lines, INFO_RESULT_UTILS_START_TXN_RESULT_TXN_ID.get("0x" + StaticUtils.toHex(txnID.getValue())), prefix, maxWidth);
            }
        }
    } else if (result instanceof EndTransactionExtendedResult) {
        final EndTransactionExtendedResult endTxnResult = (EndTransactionExtendedResult) result;
        final int failedOpMessageID = endTxnResult.getFailedOpMessageID();
        if (failedOpMessageID > 0) {
            wrap(lines, INFO_RESULT_UTILS_END_TXN_RESULT_FAILED_MSG_ID.get(failedOpMessageID), prefix, maxWidth);
        }
        final Map<Integer, Control[]> controls = endTxnResult.getOperationResponseControls();
        if (controls != null) {
            for (final Map.Entry<Integer, Control[]> e : controls.entrySet()) {
                for (final Control c : e.getValue()) {
                    wrap(lines, INFO_RESULT_UTILS_END_TXN_RESULT_OP_CONTROL.get(e.getKey()), prefix, maxWidth);
                    formatResponseControl(lines, c, prefix + "     ", maxWidth);
                }
            }
        }
    } else if (result instanceof MultiUpdateExtendedResult) {
        final MultiUpdateExtendedResult multiUpdateResult = (MultiUpdateExtendedResult) result;
        final MultiUpdateChangesApplied changesApplied = multiUpdateResult.getChangesApplied();
        if (changesApplied != null) {
            wrap(lines, INFO_RESULT_UTILS_MULTI_UPDATE_CHANGES_APPLIED.get(changesApplied.name()), prefix, maxWidth);
        }
        final List<ObjectPair<OperationType, LDAPResult>> multiUpdateResults = multiUpdateResult.getResults();
        if (multiUpdateResults != null) {
            for (final ObjectPair<OperationType, LDAPResult> p : multiUpdateResults) {
                wrap(lines, INFO_RESULT_UTILS_MULTI_UPDATE_RESULT_HEADER.get(p.getFirst().name()), prefix, maxWidth);
                formatResult(lines, p.getSecond(), false, prefix + "     ", maxWidth);
            }
        }
    } else if (result instanceof PasswordModifyExtendedResult) {
        final PasswordModifyExtendedResult passwordModifyResult = (PasswordModifyExtendedResult) result;
        final String generatedPassword = passwordModifyResult.getGeneratedPassword();
        if (generatedPassword != null) {
            wrap(lines, INFO_RESULT_UTILS_PASSWORD_MODIFY_RESULT_GENERATED_PW.get(generatedPassword), prefix, maxWidth);
        }
    } else if (result instanceof ExtendedResult) {
        final ExtendedResult extendedResult = (ExtendedResult) result;
        final String oid = ((ExtendedResult) result).getOID();
        if (oid != null) {
            wrap(lines, INFO_RESULT_UTILS_RESPONSE_EXTOP_OID.get(oid), prefix, maxWidth);
        }
        final ASN1OctetString value = extendedResult.getValue();
        if ((value != null) && (value.getValueLength() > 0)) {
            wrap(lines, INFO_RESULT_UTILS_RESPONSE_EXTOP_RAW_VALUE_HEADER.get(), prefix, maxWidth);
            // We'll ignore the maximum width for this portion of the output.
            for (final String line : StaticUtils.stringToLines(StaticUtils.toHexPlusASCII(value.getValue(), 0))) {
                lines.add(prefix + "     " + line);
            }
        }
    }
    // If there are any controls, then display them.  We'll interpret any
    // controls that we can, but will fall back to a general display for any
    // that we don't recognize or can't parse.
    final Control[] controls = result.getResponseControls();
    if (controls != null) {
        for (final Control c : controls) {
            formatResponseControl(lines, c, prefix, maxWidth);
        }
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) LDAPResult(com.unboundid.ldap.sdk.LDAPResult) SearchResult(com.unboundid.ldap.sdk.SearchResult) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) MultiUpdateExtendedResult(com.unboundid.ldap.sdk.unboundidds.extensions.MultiUpdateExtendedResult) IntermediateClientResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.IntermediateClientResponseControl) TransactionSettingsResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.TransactionSettingsResponseControl) Control(com.unboundid.ldap.sdk.Control) UniquenessResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.UniquenessResponseControl) SimplePagedResultsControl(com.unboundid.ldap.sdk.controls.SimplePagedResultsControl) PasswordValidationDetailsResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.PasswordValidationDetailsResponseControl) VirtualListViewResponseControl(com.unboundid.ldap.sdk.controls.VirtualListViewResponseControl) GetPasswordPolicyStateIssuesResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.GetPasswordPolicyStateIssuesResponseControl) AccountUsableResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.AccountUsableResponseControl) GetServerIDResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.GetServerIDResponseControl) SoftDeleteResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.SoftDeleteResponseControl) PasswordExpiredControl(com.unboundid.ldap.sdk.controls.PasswordExpiredControl) PasswordExpiringControl(com.unboundid.ldap.sdk.controls.PasswordExpiringControl) PostReadResponseControl(com.unboundid.ldap.sdk.controls.PostReadResponseControl) GetAuthorizationEntryResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.GetAuthorizationEntryResponseControl) ServerSideSortResponseControl(com.unboundid.ldap.sdk.controls.ServerSideSortResponseControl) MatchingEntryCountResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.MatchingEntryCountResponseControl) GeneratePasswordResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.GeneratePasswordResponseControl) EntryChangeNotificationControl(com.unboundid.ldap.sdk.controls.EntryChangeNotificationControl) GetUserResourceLimitsResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.GetUserResourceLimitsResponseControl) AssuredReplicationResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.AssuredReplicationResponseControl) ContentSyncStateControl(com.unboundid.ldap.sdk.controls.ContentSyncStateControl) GetRecentLoginHistoryResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.GetRecentLoginHistoryResponseControl) JoinResultControl(com.unboundid.ldap.sdk.unboundidds.controls.JoinResultControl) GetBackendSetIDResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.GetBackendSetIDResponseControl) PasswordPolicyResponseControl(com.unboundid.ldap.sdk.unboundidds.controls.PasswordPolicyResponseControl) PreReadResponseControl(com.unboundid.ldap.sdk.controls.PreReadResponseControl) AuthorizationIdentityResponseControl(com.unboundid.ldap.sdk.controls.AuthorizationIdentityResponseControl) ContentSyncDoneControl(com.unboundid.ldap.sdk.controls.ContentSyncDoneControl) MultiUpdateChangesApplied(com.unboundid.ldap.sdk.unboundidds.extensions.MultiUpdateChangesApplied) PasswordModifyExtendedResult(com.unboundid.ldap.sdk.extensions.PasswordModifyExtendedResult) StartTransactionExtendedResult(com.unboundid.ldap.sdk.extensions.StartTransactionExtendedResult) ExtendedResult(com.unboundid.ldap.sdk.ExtendedResult) StartTransactionExtendedResult(com.unboundid.ldap.sdk.extensions.StartTransactionExtendedResult) NoticeOfDisconnectionExtendedResult(com.unboundid.ldap.sdk.extensions.NoticeOfDisconnectionExtendedResult) MultiUpdateExtendedResult(com.unboundid.ldap.sdk.unboundidds.extensions.MultiUpdateExtendedResult) AbortedTransactionExtendedResult(com.unboundid.ldap.sdk.extensions.AbortedTransactionExtendedResult) EndTransactionExtendedResult(com.unboundid.ldap.sdk.extensions.EndTransactionExtendedResult) PasswordModifyExtendedResult(com.unboundid.ldap.sdk.extensions.PasswordModifyExtendedResult) List(java.util.List) ArrayList(java.util.ArrayList) OperationType(com.unboundid.ldap.sdk.OperationType) EndTransactionExtendedResult(com.unboundid.ldap.sdk.extensions.EndTransactionExtendedResult) Map(java.util.Map) AssuredReplicationServerResultCode(com.unboundid.ldap.sdk.unboundidds.controls.AssuredReplicationServerResultCode) ResultCode(com.unboundid.ldap.sdk.ResultCode) ObjectPair(com.unboundid.util.ObjectPair)

Aggregations

ASN1OctetString (com.unboundid.asn1.ASN1OctetString)15 EndTransactionExtendedResult (com.unboundid.ldap.sdk.extensions.EndTransactionExtendedResult)15 StartTransactionExtendedResult (com.unboundid.ldap.sdk.extensions.StartTransactionExtendedResult)14 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)12 LDAPConnectionOptions (com.unboundid.ldap.sdk.LDAPConnectionOptions)12 TestUnsolicitedNotificationHandler (com.unboundid.ldap.sdk.TestUnsolicitedNotificationHandler)12 EndTransactionExtendedRequest (com.unboundid.ldap.sdk.extensions.EndTransactionExtendedRequest)12 StartTransactionExtendedRequest (com.unboundid.ldap.sdk.extensions.StartTransactionExtendedRequest)12 Test (org.testng.annotations.Test)12 TransactionSpecificationRequestControl (com.unboundid.ldap.sdk.controls.TransactionSpecificationRequestControl)9 AddRequest (com.unboundid.ldap.sdk.AddRequest)8 ModifyDNRequest (com.unboundid.ldap.sdk.ModifyDNRequest)5 Control (com.unboundid.ldap.sdk.Control)4 DeleteRequest (com.unboundid.ldap.sdk.DeleteRequest)4 ModifyRequest (com.unboundid.ldap.sdk.ModifyRequest)4 PostReadResponseControl (com.unboundid.ldap.sdk.controls.PostReadResponseControl)4 PreReadResponseControl (com.unboundid.ldap.sdk.controls.PreReadResponseControl)4 AbortedTransactionExtendedResult (com.unboundid.ldap.sdk.extensions.AbortedTransactionExtendedResult)3 ObjectPair (com.unboundid.util.ObjectPair)3 ArrayList (java.util.ArrayList)3