Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle no entity body requests in when processing connector response #2309

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ public enum ENDPOINT_TIMEOUT_TYPE { ENDPOINT_TIMEOUT, GLOBAL_TIMEOUT, HTTP_CONNE
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 BEFORE_INVOKE_TEMPLATE = "BEFORE_INVOKE_TEMPLATE";

public static final String DEFAULT_ERROR_TYPE = "ANY";
public static final String ERROR_STATS_REPORTED = "ERROR_STATS_REPORTED";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public Object getPayload() {

@Override
public void setHeaders(Map<String, Object> headers) {
this.headers = headers;
if (headers != null) {
this.headers = headers;
}
}

@Override
Expand All @@ -49,7 +51,9 @@ public Map<String, Object> getHeaders() {

@Override
public void setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
if (attributes != null) {
this.attributes = attributes;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public class CallMediator extends AbstractMediator implements ManagedLifecycle {
public static final String IS_TARGET_AVAILABLE = "IS_TARGET_AVAILABLE";
public static final String ORIGINAL_TRANSPORT_HEADERS = "_ORIGINAL_TRANSPORT_HEADERS";
public static final String ORIGINAL_CONTENT_TYPE = "_ORIGINAL_CONTENT_TYPE";
public static final String ORIGINAL_NO_ENTITY_BODY = "_ORIGINAL_NO_ENTITY_BODY";

public static final String JSON_TYPE = "application/json";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.apache.synapse.mediators.template;

import com.google.gson.JsonObject;
import org.apache.axis2.transport.TransportUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.synapse.ContinuationState;
import org.apache.synapse.ManagedLifecycle;
import org.apache.synapse.Mediator;
Expand All @@ -45,6 +47,7 @@
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.transport.passthru.PassThroughConstants;
import org.apache.synapse.util.MediatorEnrichUtil;
import org.apache.synapse.util.synapse.expression.constants.ExpressionConstants;

Expand All @@ -56,7 +59,9 @@
import java.util.TreeMap;

import static org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS;
import static org.apache.synapse.mediators.builtin.CallMediator.ORIGINAL_CONTENT_TYPE;
import static org.apache.synapse.mediators.builtin.CallMediator.ORIGINAL_MESSAGE_TYPE;
import static org.apache.synapse.mediators.builtin.CallMediator.ORIGINAL_NO_ENTITY_BODY;
import static org.apache.synapse.mediators.builtin.CallMediator.ORIGINAL_TRANSPORT_HEADERS;

/**
Expand Down Expand Up @@ -220,6 +225,10 @@ public boolean isContentAware() {
while (parameterNames.hasNext()) {
String parameterName = parameterNames.next();
if (!"".equals(parameterName)) {
if (SynapseConstants.RESPONSE_VARIABLE.equals(parameterName) ||
SynapseConstants.OVERWRITE_BODY.equals(parameterName)) {
return true;
}
Value parameter = pName2ExpressionMap.get(parameterName);
SynapsePath expression = parameter.getExpression();
if (expression != null && expression.isContentAware()) {
Expand Down Expand Up @@ -310,18 +319,14 @@ 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)) {
if (pName2ExpressionMap.containsKey(SynapseConstants.RESPONSE_VARIABLE)) {
Value responseVariable = pName2ExpressionMap.get(SynapseConstants.RESPONSE_VARIABLE);
Value overwriteBody = pName2ExpressionMap.get(SynapseConstants.OVERWRITE_BODY);
if (responseVariable != null && overwriteBody != null) {
if (responseVariable != 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) {
if (StringUtils.isNotEmpty(responseVariableValue)) {
return true;
} else {
if (log.isDebugEnabled()) {
Expand All @@ -341,24 +346,34 @@ 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));
Value overwriteBodyValue = pName2ExpressionMap.get(SynapseConstants.OVERWRITE_BODY);
boolean overwriteBody = false;
if (overwriteBodyValue != null) {
overwriteBody = Boolean.parseBoolean(overwriteBodyValue.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);
org.apache.axis2.context.MessageContext axis2MessageContext =
((Axis2MessageContext) synCtx).getAxis2MessageContext();
Object messageType = axis2MessageContext.getProperty(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE);
Object contentType = axis2MessageContext.getProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE);
Object headers = axis2MessageContext.getProperty(TRANSPORT_HEADERS);
Map transportHeadersMap = (Map) headers;
// Create a clone of the original transport headers
transportHeadersMap = new TreeMap(transportHeadersMap);
synCtx.setProperty(ORIGINAL_MESSAGE_TYPE + "_" + SynapseConstants.BEFORE_INVOKE_TEMPLATE, messageType);
synCtx.setProperty(ORIGINAL_CONTENT_TYPE + "_" + SynapseConstants.BEFORE_INVOKE_TEMPLATE, contentType);
synCtx.setProperty(ORIGINAL_TRANSPORT_HEADERS + "_" +
SynapseConstants.BEFORE_INVOKE_TEMPLATE, transportHeadersMap);
Object noEntityBody = axis2MessageContext.getProperty(PassThroughConstants.NO_ENTITY_BODY);
synCtx.setProperty(ORIGINAL_NO_ENTITY_BODY + "_" +
SynapseConstants.BEFORE_INVOKE_TEMPLATE, noEntityBody);
if (!Boolean.TRUE.equals(noEntityBody)) {
Source source = MediatorEnrichUtil.createSourceWithBody();
String targetPropertyName = SynapseConstants.ORIGINAL_PAYLOAD + "_" +
SynapseConstants.BEFORE_INVOKE_TEMPLATE;
Target target = MediatorEnrichUtil.createTargetWithProperty(targetPropertyName);
doEnrich(synCtx, source, target);
}
}
}

Expand All @@ -367,23 +382,43 @@ private void postMediate(MessageContext synCtx) {
if (!storeResponseInVariableEnabled(synCtx)) {
return;
}
processConnectorResponse(synCtx);
boolean overwriteBody = Boolean.parseBoolean(pName2ExpressionMap.get(
SynapseConstants.OVERWRITE_BODY).evaluateValue(synCtx));
processConnectorResponse(synCtx, overwriteBody);
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);
(Map) synCtx.getProperty(ORIGINAL_TRANSPORT_HEADERS + "_" +
SynapseConstants.BEFORE_INVOKE_TEMPLATE);
org.apache.axis2.context.MessageContext axis2MsgCtx =
((Axis2MessageContext) synCtx).getAxis2MessageContext();
axis2MsgCtx.setProperty(TRANSPORT_HEADERS, originalTransportHeaders);
axis2MsgCtx.setProperty(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE,
synCtx.getProperty(ORIGINAL_MESSAGE_TYPE + "_" + SynapseConstants.BEFORE_INVOKE_TEMPLATE));
axis2MsgCtx.setProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE,
synCtx.getProperty(ORIGINAL_CONTENT_TYPE + "_" + SynapseConstants.BEFORE_INVOKE_TEMPLATE));
Object noEntityBody = synCtx.getProperty(ORIGINAL_NO_ENTITY_BODY + "_" +
SynapseConstants.BEFORE_INVOKE_TEMPLATE);
if (!Boolean.TRUE.equals(noEntityBody)) {
Source sourceForResponsePayload = MediatorEnrichUtil.createSourceWithProperty(
SynapseConstants.ORIGINAL_PAYLOAD + "_" + SynapseConstants.BEFORE_INVOKE_TEMPLATE);
Target targetForResponsePayload = MediatorEnrichUtil.createTargetWithBody();
doEnrich(synCtx, sourceForResponsePayload, targetForResponsePayload);
} else {
axis2MsgCtx.setProperty(PassThroughConstants.NO_ENTITY_BODY, Boolean.TRUE);
TransportUtils.createSOAPEnvelope(null);
}

// Remove the temporary properties
synCtx.getPropertyKeySet().remove(SynapseConstants.ORIGINAL_PAYLOAD + "_" +
SynapseConstants.BEFORE_INVOKE_TEMPLATE);
synCtx.getPropertyKeySet().remove(ORIGINAL_MESSAGE_TYPE + "_" + SynapseConstants.BEFORE_INVOKE_TEMPLATE);
synCtx.getPropertyKeySet().remove(ORIGINAL_CONTENT_TYPE + "_" + SynapseConstants.BEFORE_INVOKE_TEMPLATE);
synCtx.getPropertyKeySet().remove(ORIGINAL_NO_ENTITY_BODY + "_" +
SynapseConstants.BEFORE_INVOKE_TEMPLATE);
synCtx.getPropertyKeySet().remove(ORIGINAL_TRANSPORT_HEADERS + "_" +
SynapseConstants.BEFORE_INVOKE_TEMPLATE);
}

}

public String getTargetTemplate() {
Expand Down Expand Up @@ -458,21 +493,25 @@ public void init(SynapseEnvironment se) {
}
}

private void processConnectorResponse(MessageContext synCtx) {
private void processConnectorResponse(MessageContext synCtx, boolean overwriteBody) {

String responseVariableName = pName2ExpressionMap.get(SynapseConstants.RESPONSE_VARIABLE).evaluateValue(synCtx);
ConnectorResponse connectorResponse = (ConnectorResponse) synCtx.getVariable(responseVariableName);
Map<String, Object> 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);
doEnrich(synCtx, sourceForResponsePayload, targetForResponsePayload);
if (overwriteBody) {
// If overwrite body is enabled, no need to store the payload in the variable
Map response = (Map) synCtx.getVariable(responseVariableName);
response.remove(ExpressionConstants.PAYLOAD);
}
} else {
Object payload = connectorResponse.getPayload();
if (payload != null) {
if (!overwriteBody && payload != null) {
responseMap.put(ExpressionConstants.PAYLOAD, payload);
}
Map<String, Object> headers = connectorResponse.getHeaders();
Expand All @@ -487,6 +526,19 @@ private void processConnectorResponse(MessageContext synCtx) {
}
}

private void doEnrich(MessageContext synCtx, Source source, Target target) {

// If the message builder is not invoked, build the message
if (!Boolean.TRUE.equals(synCtx.getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED))) {
MediatorEnrichUtil.buildMessage(synCtx);
}
EnrichMediator enrichMediator = new EnrichMediator();
enrichMediator.setSource(source);
enrichMediator.setTarget(target);
enrichMediator.setNativeJsonSupportEnabled(true);
enrichMediator.mediate(synCtx);
}

private JsonObject convertMapToJson(Map<String, Object> map) {

JsonObject jsonObject = new JsonObject();
Expand Down
Loading