Search in sources :

Example 41 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class SynapseJsonPath method stringValueOf.

public String stringValueOf(MessageContext synCtx) {
    org.apache.axis2.context.MessageContext amc = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
    InputStream stream;
    if (!JsonUtil.hasAJsonPayload(amc) || "true".equals(enableStreamingJsonPath)) {
        try {
            if (null == amc.getEnvelope().getBody().getFirstElement()) {
                // Get message from PT Pipe.
                stream = getMessageInputStreamPT(amc);
                if (stream == null) {
                    stream = JsonUtil.getJsonPayload(amc);
                } else {
                    JsonUtil.getNewJsonPayload(amc, stream, true, true);
                }
            } else {
                // Message Already built.
                stream = JsonUtil.toJsonStream(amc.getEnvelope().getBody().getFirstElement());
            }
            if (stream != null) {
                return stringValueOf(stream);
            } else {
                log.warn("Json Payload is empty.");
                return "";
            }
        } catch (IOException e) {
            handleException("Could not find JSON Stream in PassThrough Pipe during JSON path evaluation.", e);
        }
    } else {
        stream = JsonUtil.getJsonPayload(amc);
        return stringValueOf(stream);
    }
    return "";
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 42 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class TracingDataCollectionHelper method extractTransportProperties.

/**
 * Extract transport headers from the synapse message context.
 *
 * @param synCtx synapse message context
 * @return transport headers map
 */
public static Map<String, Object> extractTransportProperties(MessageContext synCtx) {
    Map<String, Object> transportPropertyMap = new TreeMap<>();
    Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
    org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
    Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    if (headers != null && headers instanceof Map) {
        Map headersMap = (Map) headers;
        Set<String> axis2PropertySet = headersMap.keySet();
        for (String entry : axis2PropertySet) {
            transportPropertyMap.put(entry, headersMap.get(entry));
        }
    }
    // Adding important transport related properties
    if (axis2MessageCtx.getTo() != null) {
        transportPropertyMap.put(SynapseConstants.HEADER_TO, axis2MessageCtx.getTo().getAddress());
    }
    if (axis2MessageCtx.getFrom() != null) {
        transportPropertyMap.put(SynapseConstants.HEADER_FROM, axis2MessageCtx.getFrom().getAddress());
    }
    if (axis2MessageCtx.getWSAAction() != null) {
        transportPropertyMap.put("WSAction", axis2MessageCtx.getWSAAction());
    }
    if (axis2MessageCtx.getSoapAction() != null) {
        transportPropertyMap.put("SOAPAction", axis2MessageCtx.getSoapAction());
    }
    if (axis2MessageCtx.getReplyTo() != null) {
        transportPropertyMap.put(SynapseConstants.HEADER_REPLY_TO, axis2MessageCtx.getReplyTo().getAddress());
    }
    if (axis2MessageCtx.getMessageID() != null) {
        transportPropertyMap.put(SynapseConstants.HEADER_MESSAGE_ID, axis2MessageCtx.getMessageID());
    }
    // Remove unnecessary properties
    if (transportPropertyMap.get("Cookie") != null) {
        transportPropertyMap.remove("Cookie");
    }
    return transportPropertyMap;
}
Also used : TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 43 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class ScriptMediator method processJSONPayload.

private void processJSONPayload(MessageContext synCtx, ScriptMessageContext scriptMC) throws ScriptException {
    if (!(synCtx instanceof Axis2MessageContext)) {
        return;
    }
    org.apache.axis2.context.MessageContext messageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
    String jsonString = (String) messageContext.getProperty("JSON_STRING");
    Object jsonObject = null;
    prepareForJSON(scriptMC);
    if (JsonUtil.hasAJsonPayload(messageContext)) {
        try {
            String jsonPayload = JsonUtil.jsonPayloadToString(messageContext);
            if (NASHORN_JAVA_SCRIPT.equals(language)) {
                jsonObject = jsonSerializer.callMember("parse", jsonPayload);
            } else {
                String scriptWithJsonParser = "JSON.parse(JSON.stringify(" + jsonPayload + "))";
                jsonObject = this.jsEngine.eval('(' + scriptWithJsonParser + ')');
            }
        } catch (ScriptException e) {
            throw new ScriptException("Invalid JSON payload", e.getFileName(), e.getLineNumber(), e.getColumnNumber());
        }
    } else if (jsonString != null) {
        String jsonPayload = jsonParser.parse(jsonString).toString();
        jsonObject = this.jsEngine.eval('(' + jsonPayload + ')');
    }
    if (jsonObject != null) {
        scriptMC.setJsonObject(synCtx, jsonObject);
    }
}
Also used : ScriptException(javax.script.ScriptException) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 44 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class NashornJavaScriptMediatorTest method testExternalScriptWithCommentsOnNashornEngine.

/**
 * Test functionality of mediate with external script in nashornJS.
 *
 * @throws Exception
 */
public void testExternalScriptWithCommentsOnNashornEngine() throws Exception {
    String request = "{\n" + "    \"results\": [\n" + "        {\n" + "            \"geometry\": {\n" + "                \"location\": {\n" + "                    \"lat\": -33.86726,\n" + "                    \"lng\": 151.195813\n" + "                }\n" + "            },\n" + "            \"icon\": \"bar-71.png\",\n" + "            \"id\": \"7eaf7\",\n" + "            \"name\": \"Biaggio Cafe\",\n" + "            \"opening_hours\": {\n" + "                \"open_now\": true\n" + "            },\n" + "            \"photos\": [\n" + "                {\n" + "                    \"height\": 600,\n" + "                    \"html_attributions\": [],\n" + "                    \"photo_reference\": \"CoQBegAAAI\",\n" + "                    \"width\": 900\n" + "                }\n" + "            ],\n" + "            \"price_level\": 1,\n" + "            \"reference\": \"CnRqAAAAtz\",\n" + "            \"types\": [\n" + "                \"bar\",\n" + "                \"restaurant\",\n" + "                \"food\",\n" + "                \"establishment\"\n" + "            ],\n" + "            \"vicinity\": \"48 Pirrama Road, Pyrmont\"\n" + "        },\n" + "        {\n" + "            \"geometry\": {\n" + "                \"location\": {\n" + "                    \"lat\": -33.866804,\n" + "                    \"lng\": 151.195579\n" + "                }\n" + "            },\n" + "            \"icon\": \"generic_business-71.png\",\n" + "            \"id\": \"3ef98\",\n" + "            \"name\": \"Doltone House\",\n" + "            \"photos\": [\n" + "                {\n" + "                    \"height\": 600,\n" + "                    \"html_attributions\": [],\n" + "                    \"photo_reference\": \"CqQBmgAAAL\",\n" + "                    \"width\": 900\n" + "                }\n" + "            ],\n" + "            \"reference\": \"CnRrAAAAV\",\n" + "            \"types\": [\n" + "                \"food\",\n" + "                \"establishment\"\n" + "            ],\n" + "            \"vicinity\": \"48 Pirrama Road, Pyrmont\"\n" + "        }\n" + "    ],\n" + "    \"status\": \"OK\"\n" + "}";
    MessageContext mc = TestUtils.getTestContextJson(request, null);
    String scriptSrc = "function transform(mc) {\n" + "    payload = mc.getPayloadJSON();\n" + "    results = payload.results;\n" + "    var response = new Array();\n" + "    for (i = 0; i < results.length; ++i) {\n" + "        // this is a comment\n" + "        location_object = results[i];\n" + "        l = new Object();\n" + "        l.name = location_object.name;\n" + "        l.tags = location_object.types;\n" + "        l.id = \"ID:\" + (location_object.id);\n" + "        response[i] = l;\n" + "    }\n" + "    mc.setPayloadJSON(response);\n" + "}";
    String scriptSrcKey = "conf:/repository/esb/transform.js";
    Entry e = new Entry();
    DataSource dataSource = new ByteArrayDataSource(scriptSrc.getBytes());
    DataHandler dataHandler = new DataHandler(dataSource);
    OMText text = OMAbstractFactory.getOMFactory().createOMText(dataHandler, true);
    e.setKey(scriptSrcKey);
    e.setValue(text);
    mc.getConfiguration().addEntry(scriptSrcKey, e);
    Value v = new Value(scriptSrcKey);
    ScriptMediator mediator = new ScriptMediator("nashornJs", new LinkedHashMap<Value, Object>(), v, "transform", null);
    boolean result = mediator.mediate(mc);
    String response = JsonUtil.jsonPayloadToString(((Axis2MessageContext) mc).getAxis2MessageContext());
    String expectedResponse = "[{\"name\":\"Biaggio Cafe\",\"tags\":[\"bar\",\"restaurant\",\"food\"," + "\"establishment\"],\"id\":\"ID:7eaf7\"},{\"name\":\"Doltone House\",\"tags\":[\"food\"," + "\"establishment\"],\"id\":\"ID:3ef98\"}]";
    assertEquals(expectedResponse, response);
    assertEquals(true, result);
}
Also used : Entry(org.apache.synapse.config.Entry) ScriptMediator(org.apache.synapse.mediators.bsf.ScriptMediator) OMText(org.apache.axiom.om.OMText) Value(org.apache.synapse.mediators.Value) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource) ByteArrayDataSource(org.apache.axiom.attachments.ByteArrayDataSource) DataSource(javax.activation.DataSource)

Example 45 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class CloneMediator method getClonedMessageContext.

/**
 * clone the provided message context as a new message, and mark as the messageSequence'th
 * message context of a total of messageCount messages
 *
 * @param synCtx          - MessageContext which is subjected to the cloning
 * @param messageSequence - the position of this message of the cloned set
 * @param messageCount    - total of cloned copies
 *
 * @return MessageContext the cloned message context
 */
private MessageContext getClonedMessageContext(MessageContext synCtx, int messageSequence, int messageCount) {
    MessageContext newCtx = null;
    try {
        newCtx = MessageHelper.cloneMessageContext(synCtx);
        // Set isServerSide property in the cloned message context
        ((Axis2MessageContext) newCtx).getAxis2MessageContext().setServerSide(((Axis2MessageContext) synCtx).getAxis2MessageContext().isServerSide());
        if (id != null) {
            // set the parent correlation details to the cloned MC -
            // for the use of aggregation like tasks
            newCtx.setProperty(EIPConstants.AGGREGATE_CORRELATION + "." + id, synCtx.getMessageID());
            // set the property MESSAGE_SEQUENCE to the MC for aggregation purposes
            newCtx.setProperty(EIPConstants.MESSAGE_SEQUENCE + "." + id, String.valueOf(messageSequence) + EIPConstants.MESSAGE_SEQUENCE_DELEMITER + messageCount);
        } else {
            newCtx.setProperty(EIPConstants.MESSAGE_SEQUENCE, String.valueOf(messageSequence) + EIPConstants.MESSAGE_SEQUENCE_DELEMITER + messageCount);
        }
    } catch (AxisFault axisFault) {
        handleException("Error cloning the message context", axisFault, synCtx);
    }
    return newCtx;
}
Also used : AxisFault(org.apache.axis2.AxisFault) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Aggregations

Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)92 MessageContext (org.apache.synapse.MessageContext)50 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)24 Map (java.util.Map)23 HashMap (java.util.HashMap)20 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)19 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)18 Test (org.junit.Test)16 OMElement (org.apache.axiom.om.OMElement)15 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)14 SynapseException (org.apache.synapse.SynapseException)14 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)13 ArrayList (java.util.ArrayList)12 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)11 EndpointReference (org.apache.axis2.addressing.EndpointReference)10 SynapseLog (org.apache.synapse.SynapseLog)9 Endpoint (org.apache.synapse.endpoints.Endpoint)9 OperationContext (org.apache.axis2.context.OperationContext)8 AxisFault (org.apache.axis2.AxisFault)7 Entry (org.apache.synapse.config.Entry)7