use of org.apache.synapse.mediators.elementary.Source in project wso2-synapse by wso2.
the class CallMediator method mediate.
/**
* This will call the send method on the messages with implicit message parameters
* or else if there is an endpoint, with that endpoint parameters
*
* @param synInCtx the current message to be sent
* @return false for in-out invocations as flow should put on to hold after the Call mediator,
* true for out only invocations
*/
public boolean mediate(MessageContext synInCtx) {
if (synInCtx.getEnvironment().isDebuggerEnabled()) {
// this needs to be set only in mediators where outgoing messages are present
MessageHelper.setWireLogHolderProperties(synInCtx, isBreakPoint(), getRegisteredMediationFlowPoint());
if (super.divertMediationRoute(synInCtx)) {
return true;
}
}
org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synInCtx).getAxis2MessageContext();
Object messageType = axis2MessageContext.getProperty(org.apache.axis2.Constants.Configuration.MESSAGE_TYPE);
Object contentType = axis2MessageContext.getProperty(Constants.Configuration.CONTENT_TYPE);
Object headers = axis2MessageContext.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
Map originalTransportHeaders = (Map) headers;
String originalMessageType = (String) messageType;
String originalContentType = (String) contentType;
if (sourceMessageType == null) {
sourceMessageType = originalMessageType;
}
if (isSourceAvailable) {
CallMediatorEnrichUtil.buildMessage(synInCtx);
}
if (isSourceAvailable && isTargetAvailable) {
Source sourceForInboundPayload = CallMediatorEnrichUtil.createSourceWithBody();
Target targetForOriginalPayload = CallMediatorEnrichUtil.createTargetWithProperty(INTERMEDIATE_ORIGINAL_BODY);
Target targetForOutboundPayload = CallMediatorEnrichUtil.createTargetWithBody();
sourceForInboundPayload.setClone(true);
CallMediatorEnrichUtil.doEnrich(synInCtx, sourceForInboundPayload, targetForOriginalPayload, originalMessageType);
CallMediatorEnrichUtil.doEnrich(synInCtx, sourceForOutboundPayload, targetForOutboundPayload, getSourceMessageType());
if (!sourceMessageType.equalsIgnoreCase(originalMessageType)) {
CallMediatorEnrichUtil.setContentType(synInCtx, sourceMessageType, sourceMessageType);
if (originalMessageType.equalsIgnoreCase(JSON_TYPE)) {
JsonUtil.removeJsonStream(axis2MessageContext);
}
}
} else if (isSourceAvailable) {
Target targetForOutboundPayload = CallMediatorEnrichUtil.createTargetWithBody();
CallMediatorEnrichUtil.doEnrich(synInCtx, sourceForOutboundPayload, targetForOutboundPayload, getSourceMessageType());
if (!sourceMessageType.equalsIgnoreCase(originalMessageType)) {
CallMediatorEnrichUtil.setContentType(synInCtx, sourceMessageType, sourceMessageType);
if (originalMessageType.equalsIgnoreCase(JSON_TYPE)) {
JsonUtil.removeJsonStream(axis2MessageContext);
}
}
}
synInCtx.setProperty(TARGET_FOR_INBOUND_PAYLOAD, targetForInboundPayload);
synInCtx.setProperty(SOURCE_MESSAGE_TYPE, sourceMessageType);
synInCtx.setProperty(ORIGINAL_MESSAGE_TYPE, originalMessageType);
synInCtx.setProperty(IS_SOURCE_AVAILABLE, isSourceAvailable);
synInCtx.setProperty(IS_TARGET_AVAILABLE, isTargetAvailable);
synInCtx.setProperty(ORIGINAL_CONTENT_TYPE, originalContentType);
synInCtx.setProperty(ORIGINAL_TRANSPORT_HEADERS, originalTransportHeaders);
if (blocking) {
return handleBlockingCall(synInCtx, originalMessageType, originalContentType, originalTransportHeaders);
} else {
return handleNonBlockingCall(synInCtx);
}
}
use of org.apache.synapse.mediators.elementary.Source in project wso2-synapse by wso2.
the class Axis2SynapseEnvironment method callMediatorPostMediate.
private void callMediatorPostMediate(MessageContext response) {
Target targetForInboundPayload = (Target) response.getProperty(TARGET_FOR_INBOUND_PAYLOAD);
String sourceMessageType = (String) response.getProperty(SOURCE_MESSAGE_TYPE);
String originalMessageType = (String) response.getProperty(ORIGINAL_MESSAGE_TYPE);
boolean isSourceAvailable = (boolean) response.getProperty(IS_SOURCE_AVAILABLE);
boolean isTargetAvailable = (boolean) response.getProperty(IS_TARGET_AVAILABLE);
String originalContentType = (String) response.getProperty(ORIGINAL_CONTENT_TYPE);
Map originalTransportHeaders = (Map) response.getProperty(ORIGINAL_TRANSPORT_HEADERS);
Source sourceForResponsePayload;
Source sourceForOriginalPayload;
Target targetForResponsePayload;
if (isTargetAvailable) {
CallMediatorEnrichUtil.buildMessage(response);
}
if (isTargetAvailable && isSourceAvailable) {
sourceForResponsePayload = CallMediatorEnrichUtil.createSourceWithBody();
sourceForOriginalPayload = CallMediatorEnrichUtil.createSourceWithProperty(INTERMEDIATE_ORIGINAL_BODY);
targetForResponsePayload = CallMediatorEnrichUtil.createTargetWithBody();
CallMediatorEnrichUtil.doEnrich(response, sourceForResponsePayload, targetForInboundPayload, sourceMessageType);
CallMediatorEnrichUtil.doEnrich(response, sourceForOriginalPayload, targetForResponsePayload, originalMessageType);
CallMediatorEnrichUtil.preservetransportHeaders(response, originalTransportHeaders);
if (!sourceMessageType.equalsIgnoreCase(originalMessageType)) {
CallMediatorEnrichUtil.setContentType(response, originalMessageType, originalContentType);
if (sourceMessageType.equalsIgnoreCase(JSON_TYPE)) {
JsonUtil.removeJsonStream(((Axis2MessageContext) response).getAxis2MessageContext());
}
}
} else if (isTargetAvailable) {
sourceForResponsePayload = CallMediatorEnrichUtil.createSourceWithBody();
CallMediatorEnrichUtil.doEnrich(response, sourceForResponsePayload, targetForInboundPayload, sourceMessageType);
}
response.setProperty(IS_SOURCE_AVAILABLE, false);
response.setProperty(IS_TARGET_AVAILABLE, false);
}
use of org.apache.synapse.mediators.elementary.Source in project wso2-synapse by wso2.
the class CallMediatorEnrichUtil method createSourceWithProperty.
public static Source createSourceWithProperty(String propertyName) {
Source source = new Source();
source.setSourceType(CallMediatorEnrichUtil.convertTypeToInt("property"));
source.setProperty(propertyName);
source.setClone(false);
return source;
}
use of org.apache.synapse.mediators.elementary.Source in project wso2-synapse by wso2.
the class CallMediatorEnrichUtil method createSourceWithBody.
public static Source createSourceWithBody() {
Source source = new Source();
source.setClone(false);
source.setSourceType(CallMediatorEnrichUtil.convertTypeToInt("body"));
return source;
}
use of org.apache.synapse.mediators.elementary.Source in project wso2-synapse by wso2.
the class EnrichMediatorFactory method createSpecificMediator.
@Override
protected Mediator createSpecificMediator(OMElement elem, Properties properties) {
if (!XML_Q.equals(elem.getQName())) {
handleException("Unable to create the enrich mediator. " + "Unexpected element as the enrich mediator configuration");
}
EnrichMediator enrich = new EnrichMediator();
processAuditStatus(enrich, elem);
OMElement sourceEle = elem.getFirstChildWithName(SOURCE_Q);
if (sourceEle == null) {
handleException("source element is mandatory");
}
Source source = new Source();
enrich.setSource(source);
OMElement targetEle = elem.getFirstChildWithName(TARGET_Q);
if (targetEle == null) {
handleException("target element is mandatory");
}
Target target = new Target();
enrich.setTarget(target);
validateTypeCombination(sourceEle, targetEle);
populateSource(source, sourceEle);
populateTarget(target, targetEle);
// check whether the inline element of the source is XML
boolean isInlineSourceXML = false;
String inlineString = null;
if (source.getInlineOMNode() != null) {
if (source.getInlineOMNode() instanceof OMText) {
inlineString = ((OMTextImpl) source.getInlineOMNode()).getText();
JsonParser parser = new JsonParser();
try {
JsonElement element = parser.parse(inlineString);
if (!(element instanceof JsonObject || element instanceof JsonArray || element instanceof JsonPrimitive)) {
isInlineSourceXML = true;
}
} catch (JsonSyntaxException ex) {
// Therefore, we will check if it is JSON by checking for {}
if (!(inlineString.trim().startsWith("{") && inlineString.trim().endsWith("}"))) {
// not a JSON. Going ahead with XML
isInlineSourceXML = true;
}
}
} else if (source.getInlineOMNode() instanceof OMElement) {
inlineString = ((OMElement) source.getInlineOMNode()).getText();
isInlineSourceXML = true;
}
if (!StringUtils.isEmpty(inlineString)) {
enrich.setContainsInlineExpressions(InlineExpressionUtil.checkForInlineExpressions(inlineString));
}
}
// Check the enrich mediator configuration to see whether it can support JSON natively
boolean sourceHasCustom = (source.getSourceType() == EnrichMediator.CUSTOM);
boolean targetHasCustom = (target.getTargetType() == EnrichMediator.CUSTOM);
boolean enrichHasCustom = (sourceHasCustom || targetHasCustom);
boolean sourceHasEnvelope = (source.getSourceType() == EnrichMediator.ENVELOPE);
boolean targetHasEnvelope = (target.getTargetType() == EnrichMediator.ENVELOPE);
boolean enrichHasEnvelope = (sourceHasEnvelope || targetHasEnvelope);
boolean sourceHasACustomJsonPath = false;
boolean targetHasACustomJsonPath = false;
if (sourceHasCustom) {
sourceHasACustomJsonPath = SynapsePath.JSON_PATH.equals(source.getXpath().getPathType());
}
if (targetHasCustom) {
targetHasACustomJsonPath = SynapsePath.JSON_PATH.equals(target.getXpath().getPathType());
}
// conditions where native-json-processing is supported
boolean condition1 = (!enrichHasCustom);
boolean condition2 = (sourceHasACustomJsonPath && !targetHasCustom);
boolean condition3 = (!sourceHasCustom && targetHasACustomJsonPath);
boolean condition4 = (sourceHasACustomJsonPath && targetHasACustomJsonPath);
boolean condition5 = !enrichHasEnvelope;
enrich.setNativeJsonSupportEnabled(!isInlineSourceXML && condition5 && (condition1 || condition2 || condition3 || condition4));
addAllCommentChildrenToList(elem, enrich.getCommentsList());
return enrich;
}
Aggregations