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 "";
}
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;
}
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);
}
}
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);
}
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;
}
Aggregations