From 070716e0c0d510f223c7baed0ccb5ad384a37d9b Mon Sep 17 00:00:00 2001 From: Isuru Wijesiri Date: Tue, 9 Jul 2024 12:03:08 +0530 Subject: [PATCH] Fix failing of requeue on rollback if auth endpoint gives 401 --- .../OAuthConfiguredHTTPEndpoint.java | 4 ++-- .../senders/blocking/BlockingMsgSender.java | 20 ++++++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/endpoints/OAuthConfiguredHTTPEndpoint.java b/modules/core/src/main/java/org/apache/synapse/endpoints/OAuthConfiguredHTTPEndpoint.java index 2e218b2983..ad7a165661 100644 --- a/modules/core/src/main/java/org/apache/synapse/endpoints/OAuthConfiguredHTTPEndpoint.java +++ b/modules/core/src/main/java/org/apache/synapse/endpoints/OAuthConfiguredHTTPEndpoint.java @@ -57,8 +57,8 @@ public void send(MessageContext synCtx) { } // Clone the original MessageContext and save it to do a retry after a token refresh - MessageContext cloneMessageContext = MessageHelper.cloneMessageContext(synCtx); - MessageCache.getInstance().addMessageContext(synCtx.getMessageID(), cloneMessageContext); + MessageContext clonedMessageContext = MessageHelper.cloneMessageContext(synCtx); + MessageCache.getInstance().addMessageContext(synCtx.getMessageID(), clonedMessageContext); super.send(synCtx); diff --git a/modules/core/src/main/java/org/apache/synapse/message/senders/blocking/BlockingMsgSender.java b/modules/core/src/main/java/org/apache/synapse/message/senders/blocking/BlockingMsgSender.java index dd7b582e2e..82e5df42d0 100644 --- a/modules/core/src/main/java/org/apache/synapse/message/senders/blocking/BlockingMsgSender.java +++ b/modules/core/src/main/java/org/apache/synapse/message/senders/blocking/BlockingMsgSender.java @@ -475,9 +475,10 @@ public void send(EndpointDefinition endpointDefinition, MessageContext synapseIn if (originalMC != null && OAuthUtils.retryOnOAuthFailure(httpEndpoint, synapseInMsgCtx, synapseInMsgCtx)) { - MessageContext messageContext = httpEndpoint.retryCallWithNewToken(originalMC); - ((Axis2MessageContext) synapseInMsgCtx).setAxis2MessageContext( - ((Axis2MessageContext) messageContext).getAxis2MessageContext()); + originalMC.getEnvelope().build(); + + resetMessageContext((Axis2MessageContext) synapseInMsgCtx, (Axis2MessageContext) originalMC); + httpEndpoint.retryCallWithNewToken(synapseInMsgCtx); } } @@ -489,6 +490,19 @@ public void send(EndpointDefinition endpointDefinition, MessageContext synapseIn } } + private static void resetMessageContext(Axis2MessageContext synapseInMsgCtx, Axis2MessageContext originalMC) throws AxisFault { + // Replace envelope and headers with original envelope and headers before retrying + org.apache.axis2.context.MessageContext originalAxisMC = + originalMC.getAxis2MessageContext(); + + org.apache.axis2.context.MessageContext synapseInAxisMC = + synapseInMsgCtx.getAxis2MessageContext(); + + synapseInAxisMC.setEnvelope(originalAxisMC.getEnvelope()); + synapseInAxisMC.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, + originalAxisMC.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)); + } + private void sendRobust(org.apache.axis2.context.MessageContext axisOutMsgCtx, Options clientOptions, AxisService anonymousService, ServiceContext serviceCtx, MessageContext synapseInMsgCtx) throws AxisFault {