Search in sources :

Example 1 with ScriptMediator

use of org.apache.synapse.mediators.bsf.ScriptMediator in project wso2-synapse by wso2.

the class JavaScriptMediatorTest method testInlineMediator.

public void testInlineMediator() throws Exception {
    ScriptMediator mediator = new ScriptMediator("js", "mc.getPayloadXML().b == 'petra';", null);
    MessageContext mc = TestUtils.getTestContext("<a><b>petra</b></a>", null);
    assertTrue(mediator.mediate(mc));
    mc = TestUtils.getTestContext("<a><b>sue</b></a>", null);
    assertFalse(mediator.mediate(mc));
    mc = TestUtils.getTestContext("<a><b>petra</b></a>", null);
    assertTrue(mediator.mediate(mc));
}
Also used : ScriptMediator(org.apache.synapse.mediators.bsf.ScriptMediator) MessageContext(org.apache.synapse.MessageContext)

Example 2 with ScriptMediator

use of org.apache.synapse.mediators.bsf.ScriptMediator in project wso2-synapse by wso2.

the class JavaScriptMediatorTest method testInlineMediator2.

public void testInlineMediator2() throws Exception {
    ScriptMediator mediator = new ScriptMediator("js", "mc.getPayloadXML().b == 'petra';", null);
    MessageContext mc = TestUtils.getTestContext("<a><b>petra</b></a>", null);
    assertTrue(mediator.mediate(mc));
    mc = TestUtils.getTestContext("<a><b>sue</b></a>", null);
    assertFalse(mediator.mediate(mc));
    mc = TestUtils.getTestContext("<a><b>petra</b></a>", null);
    assertTrue(mediator.mediate(mc));
}
Also used : ScriptMediator(org.apache.synapse.mediators.bsf.ScriptMediator) MessageContext(org.apache.synapse.MessageContext)

Example 3 with ScriptMediator

use of org.apache.synapse.mediators.bsf.ScriptMediator 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 4 with ScriptMediator

use of org.apache.synapse.mediators.bsf.ScriptMediator in project wso2-synapse by wso2.

the class NashornJavaScriptMediatorTest method testInlineMediatorOnNashornEngine.

/**
 * Test functionality of mediate with inline script in nashornJS.
 *
 * @throws Exception
 */
public void testInlineMediatorOnNashornEngine() throws Exception {
    MessageContext mc = TestUtils.getTestContext("<foo/>", null);
    ScriptMediator mediator = new ScriptMediator("nashornJs", INLINE_SCRIPT, null);
    boolean responese = mediator.mediate(mc);
    assertTrue(responese);
}
Also used : ScriptMediator(org.apache.synapse.mediators.bsf.ScriptMediator) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Aggregations

MessageContext (org.apache.synapse.MessageContext)4 ScriptMediator (org.apache.synapse.mediators.bsf.ScriptMediator)4 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)2 DataHandler (javax.activation.DataHandler)1 DataSource (javax.activation.DataSource)1 ByteArrayDataSource (org.apache.axiom.attachments.ByteArrayDataSource)1 OMText (org.apache.axiom.om.OMText)1 Entry (org.apache.synapse.config.Entry)1 Value (org.apache.synapse.mediators.Value)1