Search in sources :

Example 16 with TransactionSpecificationRequestControl

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

the class InMemoryRequestHandler method processTransactionRequest.

/**
 * Determines whether the provided set of controls includes a transaction
 * specification request control.  If so, then it will verify that it
 * references a valid transaction for the client.  If the request is part of a
 * valid transaction, then the transaction specification request control will
 * be removed and the request will be stashed in the client connection state
 * so that it can be retrieved and processed when the transaction is
 * committed.
 *
 * @param  messageID  The message ID for the request to be processed.
 * @param  request    The protocol op for the request to be processed.
 * @param  controls   The set of controls for the request to be processed.
 *
 * @return  The transaction ID for the associated transaction, or {@code null}
 *          if the request is not part of any transaction.
 *
 * @throws  LDAPException  If the transaction specification request control is
 *                         present but does not refer to a valid transaction
 *                         for the associated client connection.
 */
@SuppressWarnings("unchecked")
@Nullable()
private ASN1OctetString processTransactionRequest(final int messageID, @NotNull final ProtocolOp request, @NotNull final Map<String, Control> controls) throws LDAPException {
    final TransactionSpecificationRequestControl txnControl = (TransactionSpecificationRequestControl) controls.remove(TransactionSpecificationRequestControl.TRANSACTION_SPECIFICATION_REQUEST_OID);
    if (txnControl == null) {
        return null;
    }
    // See if the client has an active transaction.  If not, then fail.
    final ASN1OctetString txnID = txnControl.getTransactionID();
    final ObjectPair<ASN1OctetString, List<LDAPMessage>> txnInfo = (ObjectPair<ASN1OctetString, List<LDAPMessage>>) connectionState.get(TransactionExtendedOperationHandler.STATE_VARIABLE_TXN_INFO);
    if (txnInfo == null) {
        throw new LDAPException(ResultCode.UNAVAILABLE_CRITICAL_EXTENSION, ERR_MEM_HANDLER_TXN_CONTROL_WITHOUT_TXN.get(txnID.stringValue()));
    }
    // Make sure that the active transaction has a transaction ID that matches
    // the transaction ID from the control.  If not, then abort the existing
    // transaction and fail.
    final ASN1OctetString existingTxnID = txnInfo.getFirst();
    if (!txnID.stringValue().equals(existingTxnID.stringValue())) {
        connectionState.remove(TransactionExtendedOperationHandler.STATE_VARIABLE_TXN_INFO);
        connection.sendUnsolicitedNotification(new AbortedTransactionExtendedResult(existingTxnID, ResultCode.CONSTRAINT_VIOLATION, ERR_MEM_HANDLER_TXN_ABORTED_BY_CONTROL_TXN_ID_MISMATCH.get(existingTxnID.stringValue(), txnID.stringValue()), null, null, null));
        throw new LDAPException(ResultCode.UNAVAILABLE_CRITICAL_EXTENSION, ERR_MEM_HANDLER_TXN_CONTROL_ID_MISMATCH.get(txnID.stringValue(), existingTxnID.stringValue()));
    }
    // Stash the request in the transaction state information so that it will
    // be processed when the transaction is committed.
    txnInfo.getSecond().add(new LDAPMessage(messageID, request, new ArrayList<>(controls.values())));
    return txnID;
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) LDAPException(com.unboundid.ldap.sdk.LDAPException) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TransactionSpecificationRequestControl(com.unboundid.ldap.sdk.controls.TransactionSpecificationRequestControl) ObjectPair(com.unboundid.util.ObjectPair) AbortedTransactionExtendedResult(com.unboundid.ldap.sdk.extensions.AbortedTransactionExtendedResult) Nullable(com.unboundid.util.Nullable)

Aggregations

ASN1OctetString (com.unboundid.asn1.ASN1OctetString)16 TransactionSpecificationRequestControl (com.unboundid.ldap.sdk.controls.TransactionSpecificationRequestControl)16 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)14 Test (org.testng.annotations.Test)14 AddRequest (com.unboundid.ldap.sdk.AddRequest)12 LDAPConnectionOptions (com.unboundid.ldap.sdk.LDAPConnectionOptions)10 TestUnsolicitedNotificationHandler (com.unboundid.ldap.sdk.TestUnsolicitedNotificationHandler)10 StartTransactionExtendedRequest (com.unboundid.ldap.sdk.extensions.StartTransactionExtendedRequest)10 StartTransactionExtendedResult (com.unboundid.ldap.sdk.extensions.StartTransactionExtendedResult)10 ModifyRequest (com.unboundid.ldap.sdk.ModifyRequest)9 EndTransactionExtendedRequest (com.unboundid.ldap.sdk.extensions.EndTransactionExtendedRequest)9 EndTransactionExtendedResult (com.unboundid.ldap.sdk.extensions.EndTransactionExtendedResult)8 DeleteRequest (com.unboundid.ldap.sdk.DeleteRequest)6 ModifyDNRequest (com.unboundid.ldap.sdk.ModifyDNRequest)6 Control (com.unboundid.ldap.sdk.Control)5 PostReadRequestControl (com.unboundid.ldap.sdk.controls.PostReadRequestControl)5 ExtendedResult (com.unboundid.ldap.sdk.ExtendedResult)3 LDAPException (com.unboundid.ldap.sdk.LDAPException)3 Modification (com.unboundid.ldap.sdk.Modification)3 PreReadRequestControl (com.unboundid.ldap.sdk.controls.PreReadRequestControl)3