From 56fde8d0db213a7c4e1f1fe0cb0a5f995e6461d4 Mon Sep 17 00:00:00 2001 From: GDLMadushanka Date: Thu, 5 Oct 2023 11:49:28 +0530 Subject: [PATCH] Avoid cloning the message envelope when iterating In iterate mediator json message flow, we don't need to clone the entire envelope since we are replacing it with iterated message later. Adding an empty envelope instead improves the performance. Fixes wso2/api-manager/issues/2196 --- .../eip/splitter/IterateMediator.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/eip/splitter/IterateMediator.java b/modules/core/src/main/java/org/apache/synapse/mediators/eip/splitter/IterateMediator.java index 642833086f..ae76b3dda1 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/eip/splitter/IterateMediator.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/eip/splitter/IterateMediator.java @@ -23,9 +23,12 @@ import com.google.gson.JsonElement; import com.google.gson.JsonParser; import com.jayway.jsonpath.JsonPath; +import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMNode; +import org.apache.axiom.soap.SOAP11Constants; import org.apache.axiom.soap.SOAPEnvelope; +import org.apache.axiom.soap.SOAPFactory; import org.apache.axis2.AxisFault; import org.apache.axis2.Constants; import org.apache.axis2.context.OperationContext; @@ -372,10 +375,17 @@ private MessageContext getIteratedMessage(MessageContext synCtx, int msgNumber, throws AxisFault, JaxenException { // clone the message for the mediation in iteration - MessageContext newCtx = MessageHelper.cloneMessageContext(synCtx); - - //Remove the original jsonstream from the context - JsonUtil.removeJsonPayload(((Axis2MessageContext) newCtx).getAxis2MessageContext()); + MessageContext newCtx = MessageHelper.cloneMessageContext(synCtx, false, false); + // Adding an empty envelope since JsonUtil.getNewJsonPayload requires an envelope + SOAPFactory fac; + if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI + .equals(synCtx.getEnvelope().getBody().getNamespace().getNamespaceURI())) { + fac = OMAbstractFactory.getSOAP11Factory(); + } else { + fac = OMAbstractFactory.getSOAP12Factory(); + } + SOAPEnvelope newEnvelope = fac.getDefaultEnvelope(); + newCtx.setEnvelope(newEnvelope); if (id != null) { // set the parent correlation details to the cloned MC -