diff --git a/modules/core/src/main/java/org/apache/synapse/SynapseConstants.java b/modules/core/src/main/java/org/apache/synapse/SynapseConstants.java index ef86c8b246..f07a417c20 100644 --- a/modules/core/src/main/java/org/apache/synapse/SynapseConstants.java +++ b/modules/core/src/main/java/org/apache/synapse/SynapseConstants.java @@ -726,6 +726,9 @@ public enum ENDPOINT_TIMEOUT_TYPE { ENDPOINT_TIMEOUT, GLOBAL_TIMEOUT, HTTP_CONNE public static final String OAUTH_TAG = "oauth"; public static final String SCATTER_MESSAGES = "SCATTER_MESSAGES"; public static final String CONTINUE_FLOW_TRIGGERED_FROM_MEDIATOR_WORKER = "CONTINUE_FLOW_TRIGGERED_FROM_MEDIATOR_WORKER"; + public static final String RESPONSE_VARIABLE = "responseVariable"; + public static final String OVERWRITE_BODY = "overwriteBody"; + public static final String ORIGINAL_PAYLOAD = "ORIGINAL_PAYLOAD"; public static final String DEFAULT_ERROR_TYPE = "ANY"; public static final String ERROR_STATS_REPORTED = "ERROR_STATS_REPORTED"; diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/CallMediatorFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/CallMediatorFactory.java index a01b2f94eb..9578843052 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/CallMediatorFactory.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/CallMediatorFactory.java @@ -30,7 +30,7 @@ import org.apache.synapse.mediators.elementary.EnrichMediator; import org.apache.synapse.mediators.elementary.Source; import org.apache.synapse.mediators.elementary.Target; -import org.apache.synapse.util.CallMediatorEnrichUtil; +import org.apache.synapse.util.MediatorEnrichUtil; import org.jaxen.JaxenException; import javax.xml.namespace.QName; @@ -110,7 +110,7 @@ public Mediator createSpecificMediator(OMElement elem, Properties properties) { OMElement targetEle = elem.getFirstChildWithName(TARGET_Q); if (targetEle != null) { if (sourceEle == null) { - Source source = CallMediatorEnrichUtil.createSourceWithBody(); + Source source = MediatorEnrichUtil.createSourceWithBody(); callMediator.setSourceAvailable(true); callMediator.setSourceForOutboundPayload(source); } @@ -168,7 +168,7 @@ public Mediator createSpecificMediator(OMElement elem, Properties properties) { private void populateSource(CallMediator callMediator, Source source, OMElement sourceEle) { OMAttribute typeAttr = sourceEle.getAttribute(ATT_TYPE); if (typeAttr != null && typeAttr.getAttributeValue() != null) { - source.setSourceType(CallMediatorEnrichUtil.convertTypeToInt(typeAttr.getAttributeValue())); + source.setSourceType(MediatorEnrichUtil.convertTypeToInt(typeAttr.getAttributeValue())); } OMAttribute contentTypeAtt = sourceEle.getAttribute(CONTENT_TYPE); @@ -220,7 +220,7 @@ private void populateTarget(CallMediator callMediator, Target target, OMElement OMAttribute typeAttr = sourceEle.getAttribute(ATT_TYPE); target.setAction("replace"); if (typeAttr != null && typeAttr.getAttributeValue() != null) { - int type = CallMediatorEnrichUtil.convertTypeToInt(typeAttr.getAttributeValue()); + int type = MediatorEnrichUtil.convertTypeToInt(typeAttr.getAttributeValue()); if (type >= 0) { target.setTargetType(type); } else { diff --git a/modules/core/src/main/java/org/apache/synapse/core/axis2/Axis2SynapseEnvironment.java b/modules/core/src/main/java/org/apache/synapse/core/axis2/Axis2SynapseEnvironment.java index 400aa2c871..1955628e5d 100644 --- a/modules/core/src/main/java/org/apache/synapse/core/axis2/Axis2SynapseEnvironment.java +++ b/modules/core/src/main/java/org/apache/synapse/core/axis2/Axis2SynapseEnvironment.java @@ -71,7 +71,7 @@ import org.apache.synapse.transport.util.MessageHandlerProvider; import org.apache.synapse.transport.customlogsetter.CustomLogSetter; import org.apache.synapse.unittest.UnitTestingExecutor; -import org.apache.synapse.util.CallMediatorEnrichUtil; +import org.apache.synapse.util.MediatorEnrichUtil; import org.apache.synapse.util.concurrent.InboundThreadPool; import org.apache.synapse.util.concurrent.SynapseThreadPool; import org.apache.synapse.util.logging.LoggingUtils; @@ -879,26 +879,26 @@ private void callMediatorPostMediate(MessageContext response) { Target targetForResponsePayload; if (isTargetAvailable) { - CallMediatorEnrichUtil.buildMessage(response); + MediatorEnrichUtil.buildMessage(response); } if (isTargetAvailable && isSourceAvailable) { - sourceForResponsePayload = CallMediatorEnrichUtil.createSourceWithBody(); - sourceForOriginalPayload = CallMediatorEnrichUtil.createSourceWithProperty(INTERMEDIATE_ORIGINAL_BODY); - targetForResponsePayload = CallMediatorEnrichUtil.createTargetWithBody(); - CallMediatorEnrichUtil + sourceForResponsePayload = MediatorEnrichUtil.createSourceWithBody(); + sourceForOriginalPayload = MediatorEnrichUtil.createSourceWithProperty(INTERMEDIATE_ORIGINAL_BODY); + targetForResponsePayload = MediatorEnrichUtil.createTargetWithBody(); + MediatorEnrichUtil .doEnrich(response, sourceForResponsePayload, targetForInboundPayload, sourceMessageType); - CallMediatorEnrichUtil + MediatorEnrichUtil .doEnrich(response, sourceForOriginalPayload, targetForResponsePayload, originalMessageType); - CallMediatorEnrichUtil.preservetransportHeaders(response, originalTransportHeaders); + MediatorEnrichUtil.preservetransportHeaders(response, originalTransportHeaders); if (!sourceMessageType.equalsIgnoreCase(originalMessageType)) { - CallMediatorEnrichUtil.setContentType(response, originalMessageType, originalContentType); + MediatorEnrichUtil.setContentType(response, originalMessageType, originalContentType); if (sourceMessageType.equalsIgnoreCase(JSON_TYPE)) { JsonUtil.removeJsonStream(((Axis2MessageContext) response).getAxis2MessageContext()); } } } else if (isTargetAvailable) { - sourceForResponsePayload = CallMediatorEnrichUtil.createSourceWithBody(); - CallMediatorEnrichUtil + sourceForResponsePayload = MediatorEnrichUtil.createSourceWithBody(); + MediatorEnrichUtil .doEnrich(response, sourceForResponsePayload, targetForInboundPayload, sourceMessageType); } response.setProperty(IS_SOURCE_AVAILABLE, false); diff --git a/modules/core/src/main/java/org/apache/synapse/data/connector/ConnectorResponse.java b/modules/core/src/main/java/org/apache/synapse/data/connector/ConnectorResponse.java new file mode 100644 index 0000000000..cef99fba14 --- /dev/null +++ b/modules/core/src/main/java/org/apache/synapse/data/connector/ConnectorResponse.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.synapse.data.connector; + +import java.util.Map; + +public interface ConnectorResponse { + + void setPayload(Object payload); + + Object getPayload(); + + void setHeaders(Map headers); + + Map getHeaders(); + + void setAttributes(Map attributes); + + Map getAttributes(); + + void addAttribute(String key, Object value); + + void removeAttribute(String key); + + void addHeader(String key, String value); + + void removeHeader(String key); +} diff --git a/modules/core/src/main/java/org/apache/synapse/data/connector/DefaultConnectorResponse.java b/modules/core/src/main/java/org/apache/synapse/data/connector/DefaultConnectorResponse.java new file mode 100644 index 0000000000..fe348d1d38 --- /dev/null +++ b/modules/core/src/main/java/org/apache/synapse/data/connector/DefaultConnectorResponse.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.synapse.data.connector; + +import java.util.HashMap; +import java.util.Map; + +public class DefaultConnectorResponse implements ConnectorResponse { + + private Object payload; + private Map headers = new HashMap<>(); + private Map attributes = new HashMap<>(); + + @Override + public void setPayload(Object payload) { + this.payload = payload; + } + + @Override + public Object getPayload() { + return payload; + } + + @Override + public void setHeaders(Map headers) { + this.headers = headers; + } + + @Override + public Map getHeaders() { + return headers; + } + + @Override + public void setAttributes(Map attributes) { + this.attributes = attributes; + } + + @Override + public Map getAttributes() { + return attributes; + } + + @Override + public void addAttribute(String key, Object value) { + attributes.put(key, value); + } + + @Override + public void removeAttribute(String key) { + attributes.remove(key); + } + + @Override + public void addHeader(String key, String value) { + headers.put(key, value); + } + + @Override + public void removeHeader(String key) { + headers.remove(key); + } +} diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/CallMediator.java b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/CallMediator.java index 20be8fd817..b64e174474 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/CallMediator.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/CallMediator.java @@ -48,7 +48,7 @@ import org.apache.synapse.mediators.elementary.Source; import org.apache.synapse.mediators.elementary.Target; import org.apache.synapse.message.senders.blocking.BlockingMsgSender; -import org.apache.synapse.util.CallMediatorEnrichUtil; +import org.apache.synapse.util.MediatorEnrichUtil; import org.apache.synapse.util.MessageHelper; import java.io.IOException; @@ -158,33 +158,33 @@ public boolean mediate(MessageContext synInCtx) { sourceMessageType = originalMessageType; } if (isSourceAvailable) { - CallMediatorEnrichUtil.buildMessage(synInCtx); + MediatorEnrichUtil.buildMessage(synInCtx); } if (isSourceAvailable && isTargetAvailable) { - Source sourceForInboundPayload = CallMediatorEnrichUtil.createSourceWithBody(); + Source sourceForInboundPayload = MediatorEnrichUtil.createSourceWithBody(); Target targetForOriginalPayload = - CallMediatorEnrichUtil.createTargetWithProperty(INTERMEDIATE_ORIGINAL_BODY); - Target targetForOutboundPayload = CallMediatorEnrichUtil.createTargetWithBody(); + MediatorEnrichUtil.createTargetWithProperty(INTERMEDIATE_ORIGINAL_BODY); + Target targetForOutboundPayload = MediatorEnrichUtil.createTargetWithBody(); sourceForInboundPayload.setClone(true); - CallMediatorEnrichUtil + MediatorEnrichUtil .doEnrich(synInCtx, sourceForInboundPayload, targetForOriginalPayload, originalMessageType); if (!(EnrichMediator.BODY == sourceForOutboundPayload.getSourceType() && EnrichMediator.BODY == targetForOutboundPayload.getTargetType())) { - CallMediatorEnrichUtil + MediatorEnrichUtil .doEnrich(synInCtx, sourceForOutboundPayload, targetForOutboundPayload, getSourceMessageType()); } if (!sourceMessageType.equalsIgnoreCase(originalMessageType)) { - CallMediatorEnrichUtil.setContentType(synInCtx, sourceMessageType, sourceMessageType); + MediatorEnrichUtil.setContentType(synInCtx, sourceMessageType, sourceMessageType); if (originalMessageType.equalsIgnoreCase(JSON_TYPE)) { JsonUtil.removeJsonStream(axis2MessageContext); } } } else if (isSourceAvailable) { - Target targetForOutboundPayload = CallMediatorEnrichUtil.createTargetWithBody(); - CallMediatorEnrichUtil + Target targetForOutboundPayload = MediatorEnrichUtil.createTargetWithBody(); + MediatorEnrichUtil .doEnrich(synInCtx, sourceForOutboundPayload, targetForOutboundPayload, getSourceMessageType()); if (!sourceMessageType.equalsIgnoreCase(originalMessageType)) { - CallMediatorEnrichUtil.setContentType(synInCtx, sourceMessageType, sourceMessageType); + MediatorEnrichUtil.setContentType(synInCtx, sourceMessageType, sourceMessageType); if (originalMessageType.equalsIgnoreCase(JSON_TYPE)) { JsonUtil.removeJsonStream(axis2MessageContext); } @@ -280,27 +280,27 @@ private boolean handleBlockingCall(MessageContext synInCtx, String originalMessa public void postMediate(MessageContext response, String originalMessageType, String originalContentType, Map originalTransportHeaders) { if (isTargetAvailable()) { - CallMediatorEnrichUtil.buildMessage(response); + MediatorEnrichUtil.buildMessage(response); } if (isTargetAvailable && isSourceAvailable) { - Source sourceForResponsePayload = CallMediatorEnrichUtil.createSourceWithBody(); + Source sourceForResponsePayload = MediatorEnrichUtil.createSourceWithBody(); Source sourceForOriginalPayload = - CallMediatorEnrichUtil.createSourceWithProperty(INTERMEDIATE_ORIGINAL_BODY); - Target targetForResponsePayload = CallMediatorEnrichUtil.createTargetWithBody(); - CallMediatorEnrichUtil.doEnrich(response, sourceForResponsePayload, targetForInboundPayload, + MediatorEnrichUtil.createSourceWithProperty(INTERMEDIATE_ORIGINAL_BODY); + Target targetForResponsePayload = MediatorEnrichUtil.createTargetWithBody(); + MediatorEnrichUtil.doEnrich(response, sourceForResponsePayload, targetForInboundPayload, getSourceMessageType()); - CallMediatorEnrichUtil.doEnrich(response, sourceForOriginalPayload, targetForResponsePayload, + MediatorEnrichUtil.doEnrich(response, sourceForOriginalPayload, targetForResponsePayload, originalMessageType); - CallMediatorEnrichUtil.preservetransportHeaders(response, originalTransportHeaders); + MediatorEnrichUtil.preservetransportHeaders(response, originalTransportHeaders); if (!sourceMessageType.equalsIgnoreCase(originalMessageType)) { - CallMediatorEnrichUtil.setContentType(response, originalMessageType, originalContentType); + MediatorEnrichUtil.setContentType(response, originalMessageType, originalContentType); if (sourceMessageType.equalsIgnoreCase(JSON_TYPE)) { JsonUtil.removeJsonStream(((Axis2MessageContext) response).getAxis2MessageContext()); } } } else if (isTargetAvailable) { - Source sourceForResponsePayload = CallMediatorEnrichUtil.createSourceWithBody(); - CallMediatorEnrichUtil.doEnrich(response, sourceForResponsePayload, targetForInboundPayload, + Source sourceForResponsePayload = MediatorEnrichUtil.createSourceWithBody(); + MediatorEnrichUtil.doEnrich(response, sourceForResponsePayload, targetForInboundPayload, getSourceMessageType()); } } diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/elementary/Target.java b/modules/core/src/main/java/org/apache/synapse/mediators/elementary/Target.java index c1d7946652..dc858d8371 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/elementary/Target.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/elementary/Target.java @@ -42,7 +42,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.synapse.MessageContext; -import org.apache.synapse.SynapseConstants; import org.apache.synapse.SynapseException; import org.apache.synapse.SynapseLog; import org.apache.synapse.commons.json.Constants; @@ -52,7 +51,7 @@ import org.apache.synapse.core.axis2.Axis2MessageContext; import org.apache.synapse.mediators.Value; import org.apache.synapse.mediators.eip.EIPUtils; -import org.apache.synapse.util.CallMediatorEnrichUtil; +import org.apache.synapse.util.MediatorEnrichUtil; import org.apache.synapse.util.InlineExpressionUtil; import org.apache.synapse.util.synapse.expression.constants.ExpressionConstants; import org.apache.synapse.util.xpath.SynapseJsonPath; @@ -253,7 +252,7 @@ public void insert(MessageContext synContext, .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS); JsonObject headers = EIPUtils.convertMapToJsonObj(transportHeaders); result.put(ExpressionConstants.HEADERS, headers); - result.put(ExpressionConstants.ATTRIBUTES, CallMediatorEnrichUtil.populateTransportAttributes(synContext)); + result.put(ExpressionConstants.ATTRIBUTES, MediatorEnrichUtil.populateTransportAttributes(synContext)); synContext.setVariable(key, result); } else { synLog.error("Action " + action + " is not supported when enriching variables"); @@ -508,7 +507,7 @@ public void insertJson(MessageContext synCtx, Object sourceJsonElement, SynapseL .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS); JsonObject headers = EIPUtils.convertMapToJsonObj(transportHeaders); result.put(ExpressionConstants.HEADERS, headers); - result.put(ExpressionConstants.ATTRIBUTES, CallMediatorEnrichUtil.populateTransportAttributes(synCtx)); + result.put(ExpressionConstants.ATTRIBUTES, MediatorEnrichUtil.populateTransportAttributes(synCtx)); synCtx.setVariable(key, result); } break; diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/template/InvokeMediator.java b/modules/core/src/main/java/org/apache/synapse/mediators/template/InvokeMediator.java index 5e93d02abb..6dd0bf5209 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/template/InvokeMediator.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/template/InvokeMediator.java @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -18,11 +18,12 @@ */ package org.apache.synapse.mediators.template; +import com.google.gson.JsonObject; import org.apache.synapse.ContinuationState; import org.apache.synapse.ManagedLifecycle; import org.apache.synapse.Mediator; import org.apache.synapse.MessageContext; -import org.apache.synapse.SynapseException; +import org.apache.synapse.SynapseConstants; import org.apache.synapse.SynapseLog; import org.apache.synapse.aspects.AspectConfiguration; import org.apache.synapse.aspects.ComponentType; @@ -33,17 +34,30 @@ import org.apache.synapse.continuation.ContinuationStackManager; import org.apache.synapse.continuation.ReliantContinuationState; import org.apache.synapse.core.SynapseEnvironment; +import org.apache.synapse.core.axis2.Axis2MessageContext; +import org.apache.synapse.data.connector.ConnectorResponse; import org.apache.synapse.mediators.AbstractMediator; import org.apache.synapse.mediators.FlowContinuableMediator; import org.apache.synapse.mediators.MediatorFaultHandler; import org.apache.synapse.mediators.Value; import org.apache.synapse.mediators.eip.EIPUtils; +import org.apache.synapse.mediators.elementary.EnrichMediator; +import org.apache.synapse.mediators.elementary.Source; +import org.apache.synapse.mediators.elementary.Target; import org.apache.synapse.transport.customlogsetter.CustomLogSetter; +import org.apache.synapse.util.MediatorEnrichUtil; +import org.apache.synapse.util.synapse.expression.constants.ExpressionConstants; +import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Stack; +import java.util.TreeMap; + +import static org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS; +import static org.apache.synapse.mediators.builtin.CallMediator.ORIGINAL_MESSAGE_TYPE; +import static org.apache.synapse.mediators.builtin.CallMediator.ORIGINAL_TRANSPORT_HEADERS; /** * This class handles invocation of a synapse function template. (); } - public boolean mediate(MessageContext synCtx) { return mediate(synCtx, true); } @@ -177,10 +190,12 @@ private boolean mediate(MessageContext synCtx, boolean executePreFetchingSequenc } } + prepareForMediation(synCtx); boolean result = mediator.mediate(synCtx); if (result && executePreFetchingSequence) { ContinuationStackManager.removeReliantContinuationState(synCtx); + postMediate(synCtx); } if (errorHandlerMediator != null) { @@ -230,6 +245,7 @@ public boolean mediate(MessageContext synCtx, ContinuationState continuationStat TemplateMediator templateMediator = (TemplateMediator) synCtx.getSequenceTemplate(targetTemplate); if (!continuationState.hasChild()) { result = templateMediator.mediate(synCtx, continuationState.getPosition() + 1); + postMediate(synCtx); if (result) { templateMediator.popFuncContextFrom(synCtx); } @@ -238,6 +254,7 @@ public boolean mediate(MessageContext synCtx, ContinuationState continuationStat (FlowContinuableMediator) templateMediator.getChild(continuationState.getPosition()); result = mediator.mediate(synCtx, continuationState.getChildContState()); + postMediate(synCtx); if (isStatisticsEnabled) { ((Mediator) mediator).reportCloseStatistics(synCtx, null); @@ -253,6 +270,7 @@ public boolean mediate(MessageContext synCtx, ContinuationState continuationStat ContinuationState childContinuationState = continuationState.getChildContState(); result = prefetchInvoke.mediate(synCtx, childContinuationState); + postMediate(synCtx); if (result && !childContinuationState.hasChild()) { // Pre fetching invoke mediator flow completed. @@ -290,6 +308,84 @@ private void populateParameters(MessageContext synCtx, String templateQualifiedN } } + private boolean storeResponseInVariableEnabled(MessageContext synCtx) { + + if (pName2ExpressionMap.keySet().contains(SynapseConstants.OVERWRITE_BODY) && + pName2ExpressionMap.keySet().contains(SynapseConstants.RESPONSE_VARIABLE)) { + Value responseVariable = pName2ExpressionMap.get(SynapseConstants.RESPONSE_VARIABLE); + Value overwriteBody = pName2ExpressionMap.get(SynapseConstants.OVERWRITE_BODY); + if (responseVariable != null && overwriteBody != null) { + String responseVariableValue = responseVariable.evaluateValue(synCtx); + String overwriteBodyValue = overwriteBody.evaluateValue(synCtx); + if (log.isDebugEnabled()) { + log.debug("Response variable value: " + responseVariableValue); + log.debug("Overwrite body value: " + overwriteBodyValue); + } + if (responseVariableValue != null && overwriteBodyValue != null) { + return true; + } else { + if (log.isDebugEnabled()) { + log.debug("Response variable value or overwrite body value is null"); + } + } + } + } + if (log.isDebugEnabled()) { + log.debug("Response does not needs to be stored in a variable"); + } + return false; + } + + private void prepareForMediation(MessageContext synCtx) { + + if (!storeResponseInVariableEnabled(synCtx)) { + return; + } + + org.apache.axis2.context.MessageContext axis2MessageContext = + ((Axis2MessageContext) synCtx).getAxis2MessageContext(); + Object messageType = axis2MessageContext.getProperty(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE); + Object headers = axis2MessageContext.getProperty(TRANSPORT_HEADERS); + Map transportHeadersMap = (Map) headers; + // Create a clone of the original transport headers + transportHeadersMap = new TreeMap(transportHeadersMap); + String originalMessageType = (String) messageType; + synCtx.setProperty(ORIGINAL_MESSAGE_TYPE + "_" + synCtx.getMessageID(), originalMessageType); + synCtx.setProperty(ORIGINAL_TRANSPORT_HEADERS + "_" + synCtx.getMessageID(), transportHeadersMap); + boolean overwriteBody = Boolean.parseBoolean(pName2ExpressionMap.get( + SynapseConstants.OVERWRITE_BODY).evaluateValue(synCtx)); + if (!overwriteBody) { + Source source = MediatorEnrichUtil.createSourceWithBody(); + String targetPropertyName = SynapseConstants.ORIGINAL_PAYLOAD + "_" + synCtx.getMessageID(); + Target target = MediatorEnrichUtil.createTargetWithProperty(targetPropertyName); + MediatorEnrichUtil.doEnrich(synCtx, source, target, originalMessageType); + } + } + + private void postMediate(MessageContext synCtx) { + + if (!storeResponseInVariableEnabled(synCtx)) { + return; + } + processConnectorResponse(synCtx); + boolean overwriteBody = Boolean.parseBoolean(pName2ExpressionMap.get( + SynapseConstants.OVERWRITE_BODY).evaluateValue(synCtx)); + if (!overwriteBody) { + String originalMessageType = + (String) synCtx.getProperty(ORIGINAL_MESSAGE_TYPE + "_" + synCtx.getMessageID()); + Map originalTransportHeaders = + (Map) synCtx.getProperty(ORIGINAL_TRANSPORT_HEADERS + "_" + synCtx.getMessageID()); + Source sourceForResponseProperty = MediatorEnrichUtil.createSourceWithProperty( + SynapseConstants.ORIGINAL_PAYLOAD + "_" + synCtx.getMessageID()); + Target targetForResponseProperty = MediatorEnrichUtil.createTargetWithBody(); + MediatorEnrichUtil.doEnrich( + synCtx, sourceForResponseProperty, targetForResponseProperty, originalMessageType); + org.apache.axis2.context.MessageContext axis2MsgCtx = + ((Axis2MessageContext) synCtx).getAxis2MessageContext(); + axis2MsgCtx.setProperty(TRANSPORT_HEADERS, originalTransportHeaders); + } + } + public String getTargetTemplate() { return targetTemplate; } @@ -362,6 +458,50 @@ public void init(SynapseEnvironment se) { } } + private void processConnectorResponse(MessageContext synCtx) { + + String responseVariableName = pName2ExpressionMap.get(SynapseConstants.RESPONSE_VARIABLE).evaluateValue(synCtx); + ConnectorResponse connectorResponse = (ConnectorResponse) synCtx.getVariable(responseVariableName); + Map responseMap = new HashMap<>(); + if (connectorResponse == null) { + String messageType = (String) synCtx.getProperty(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE); + Source sourceForResponsePayload = MediatorEnrichUtil.createSourceWithBody(); + Target targetForResponsePayload = new Target(); + targetForResponsePayload.setTargetType(EnrichMediator.VARIABLE); + targetForResponsePayload.setVariable(pName2ExpressionMap.get(SynapseConstants.RESPONSE_VARIABLE)); + MediatorEnrichUtil.doEnrich(synCtx, sourceForResponsePayload, targetForResponsePayload, messageType); + } else { + Object payload = connectorResponse.getPayload(); + if (payload != null) { + responseMap.put(ExpressionConstants.PAYLOAD, payload); + } + Map headers = connectorResponse.getHeaders(); + if (!headers.isEmpty()) { + responseMap.put(ExpressionConstants.HEADERS, convertMapToJson(headers)); + } + Map attributes = connectorResponse.getAttributes(); + if (!attributes.isEmpty()) { + responseMap.put(ExpressionConstants.ATTRIBUTES, convertMapToJson(attributes)); + } + synCtx.setVariable(responseVariableName, responseMap); + } + } + + private JsonObject convertMapToJson(Map map) { + + JsonObject jsonObject = new JsonObject(); + map.forEach((key, value) -> { + if (value instanceof Number) { + jsonObject.addProperty(key, (Number) value); + } else if (value instanceof Boolean) { + jsonObject.addProperty(key, (Boolean) value); + } else { + jsonObject.addProperty(key, value.toString()); + } + }); + return jsonObject; + } + public void destroy() { TemplateMediator templateMediator = synapseEnv.getSynapseConfiguration().getSequenceTemplate(targetTemplate); diff --git a/modules/core/src/main/java/org/apache/synapse/util/CallMediatorEnrichUtil.java b/modules/core/src/main/java/org/apache/synapse/util/MediatorEnrichUtil.java similarity index 96% rename from modules/core/src/main/java/org/apache/synapse/util/CallMediatorEnrichUtil.java rename to modules/core/src/main/java/org/apache/synapse/util/MediatorEnrichUtil.java index 1968f37d77..d8ff2d8677 100644 --- a/modules/core/src/main/java/org/apache/synapse/util/CallMediatorEnrichUtil.java +++ b/modules/core/src/main/java/org/apache/synapse/util/MediatorEnrichUtil.java @@ -52,7 +52,7 @@ import java.util.ArrayList; import java.util.Map; -public class CallMediatorEnrichUtil { +public class MediatorEnrichUtil { public static final String CUSTOM = "custom"; public static final String PROPERTY = "property"; @@ -63,7 +63,7 @@ public class CallMediatorEnrichUtil { public static final String JSON_TYPE = "application/json"; public static final String TEXT_TYPE = "text/plain"; - public static final Log log = LogFactory.getLog(CallMediatorEnrichUtil.class); + public static final Log log = LogFactory.getLog(MediatorEnrichUtil.class); private final static QName TEXT_ELEMENT = new QName("http://ws.apache.org/commons/ns/payload", "text"); public static int convertTypeToInt(String type) { @@ -253,7 +253,7 @@ public static void buildMessage(MessageContext synCtx) { public static Source createSourceWithProperty(String propertyName) { Source source = new Source(); - source.setSourceType(CallMediatorEnrichUtil.convertTypeToInt("property")); + source.setSourceType(MediatorEnrichUtil.convertTypeToInt("property")); source.setProperty(propertyName); source.setClone(false); return source; @@ -262,13 +262,13 @@ public static Source createSourceWithProperty(String propertyName) { public static Source createSourceWithBody() { Source source = new Source(); source.setClone(false); - source.setSourceType(CallMediatorEnrichUtil.convertTypeToInt("body")); + source.setSourceType(MediatorEnrichUtil.convertTypeToInt("body")); return source; } public static Target createTargetWithProperty(String propertyName) { Target target = new Target(); - target.setTargetType(CallMediatorEnrichUtil.convertTypeToInt("property")); + target.setTargetType(MediatorEnrichUtil.convertTypeToInt("property")); target.setProperty(propertyName); target.setAction("replace"); return target; @@ -277,7 +277,7 @@ public static Target createTargetWithProperty(String propertyName) { public static Target createTargetWithBody() { Target target = new Target(); target.setAction("replace"); - target.setTargetType(CallMediatorEnrichUtil.convertTypeToInt("body")); + target.setTargetType(MediatorEnrichUtil.convertTypeToInt("body")); return target; } @@ -308,6 +308,7 @@ public static SynapseLog getLog(MessageContext synCtx) { * @return The JSON object containing the transport attributes. */ public static JsonObject populateTransportAttributes(MessageContext synCtx) { + JsonObject attributes = new JsonObject(); Object httpStatusCodeObj = ((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty( SynapseConstants.HTTP_SC); @@ -320,6 +321,5 @@ public static JsonObject populateTransportAttributes(MessageContext synCtx) { } return attributes; } - }