Search in sources :

Example 1 with Target

use of org.apache.synapse.mediators.elementary.Target 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);
    }
}
Also used : Target(org.apache.synapse.mediators.elementary.Target) Map(java.util.Map) Source(org.apache.synapse.mediators.elementary.Source) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 2 with Target

use of org.apache.synapse.mediators.elementary.Target 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);
}
Also used : Target(org.apache.synapse.mediators.elementary.Target) Map(java.util.Map) HashMap(java.util.HashMap) Source(org.apache.synapse.mediators.elementary.Source)

Example 3 with Target

use of org.apache.synapse.mediators.elementary.Target in project wso2-synapse by wso2.

the class CallMediatorEnrichUtil method createTargetWithBody.

public static Target createTargetWithBody() {
    Target target = new Target();
    target.setAction("replace");
    target.setTargetType(CallMediatorEnrichUtil.convertTypeToInt("body"));
    return target;
}
Also used : Target(org.apache.synapse.mediators.elementary.Target)

Example 4 with Target

use of org.apache.synapse.mediators.elementary.Target in project wso2-synapse by wso2.

the class CallMediatorEnrichUtil method createTargetWithProperty.

public static Target createTargetWithProperty(String propertyName) {
    Target target = new Target();
    target.setTargetType(CallMediatorEnrichUtil.convertTypeToInt("property"));
    target.setProperty(propertyName);
    target.setAction("replace");
    return target;
}
Also used : Target(org.apache.synapse.mediators.elementary.Target)

Example 5 with Target

use of org.apache.synapse.mediators.elementary.Target 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;
}
Also used : EnrichMediator(org.apache.synapse.mediators.elementary.EnrichMediator) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) OMElement(org.apache.axiom.om.OMElement) Source(org.apache.synapse.mediators.elementary.Source) JsonArray(com.google.gson.JsonArray) Target(org.apache.synapse.mediators.elementary.Target) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) OMText(org.apache.axiom.om.OMText) JsonParser(com.google.gson.JsonParser)

Aggregations

Target (org.apache.synapse.mediators.elementary.Target)7 Source (org.apache.synapse.mediators.elementary.Source)5 Map (java.util.Map)2 OMElement (org.apache.axiom.om.OMElement)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 File (java.io.File)1 HashMap (java.util.HashMap)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 OMText (org.apache.axiom.om.OMText)1 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)1 Endpoint (org.apache.synapse.endpoints.Endpoint)1 CallMediator (org.apache.synapse.mediators.builtin.CallMediator)1 EnrichMediator (org.apache.synapse.mediators.elementary.EnrichMediator)1