use of org.apache.synapse.mediators.Value in project wso2-synapse by wso2.
the class ResourceTest method testFaultSequence.
public void testFaultSequence() throws Exception {
API api = new API("TestAPI", "/test");
Resource resource = new Resource();
resource.setDispatcherHelper(new URITemplateHelper("/~{user}"));
SequenceMediator inSequence = getTestSequence("seq.in", "seq.in.value");
((PropertyMediator) inSequence.getChild(0)).setScope("axis2");
XSLTMediator xsltMediator = new XSLTMediator();
xsltMediator.setXsltKey(new Value("/bogus/key"));
inSequence.addChild(xsltMediator);
resource.setInSequence(inSequence);
SequenceMediator faultSequence = getTestSequence("seq.fault", "seq.fault.value");
((PropertyMediator) faultSequence.getChild(0)).setScope("axis2");
resource.setFaultSequence(faultSequence);
api.addResource(resource);
SynapseConfiguration synapseConfig = new SynapseConfiguration();
synapseConfig.addAPI(api.getName(), api);
synapseConfig.addSequence("main", getTestSequence("main.in", "main.value"));
MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/~foo", "GET");
MessageContextCreatorForAxis2.setSynConfig(synapseConfig);
MessageContextCreatorForAxis2.setSynEnv(synCtx.getEnvironment());
org.apache.axis2.context.MessageContext mc = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
mc.setConfigurationContext(((Axis2SynapseEnvironment) synCtx.getEnvironment()).getAxis2ConfigurationContext());
new SynapseMessageReceiver().receive(mc);
assertEquals("seq.in.value", mc.getProperty("seq.in"));
assertEquals("seq.fault.value", mc.getProperty("seq.fault"));
}
use of org.apache.synapse.mediators.Value in project wso2-synapse by wso2.
the class ScriptMediatorTest method testExternalScriptWithComments.
/**
* Test functionality of mediate with external script in js.
*
* @throws Exception
*/
public void testExternalScriptWithComments() 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("js", 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.mediators.Value in project wso2-synapse by wso2.
the class SendMediatorSerializer method serializeSpecificMediator.
public OMElement serializeSpecificMediator(Mediator m) {
if (!(m instanceof SendMediator)) {
handleException("Unsupported mediator passed in for serialization : " + m.getType());
}
SendMediator mediator = (SendMediator) m;
OMElement send = fac.createOMElement("send", synNS);
saveTracingState(send, mediator);
Endpoint activeEndpoint = mediator.getEndpoint();
if (activeEndpoint != null) {
send.addChild(EndpointSerializer.getElementFromEndpoint(activeEndpoint));
}
Value receive = mediator.getReceivingSequence();
if (receive != null) {
ValueSerializer serializer = new ValueSerializer();
serializer.serializeValue(receive, XMLConfigConstants.RECEIVE, send);
}
if (mediator.isBuildMessage()) {
send.addAttribute(fac.createOMAttribute("buildmessage", nullNS, "true"));
}
return send;
}
use of org.apache.synapse.mediators.Value in project wso2-synapse by wso2.
the class ValidateMediatorSerializer method serializeSpecificMediator.
public OMElement serializeSpecificMediator(Mediator m) {
if (!(m instanceof ValidateMediator)) {
handleException("Unsupported mediator passed in for serialization : " + m.getType());
}
ValidateMediator mediator = (ValidateMediator) m;
OMElement validate = fac.createOMElement("validate", synNS);
saveTracingState(validate, mediator);
if (mediator.getSource() != null) {
SynapsePathSerializer.serializePath(mediator.getSource(), validate, "source");
}
for (Value key : mediator.getSchemaKeys()) {
OMElement schema = fac.createOMElement("schema", synNS, validate);
// Serialize Value using ValueSerializer
ValueSerializer keySerializer = new ValueSerializer();
keySerializer.serializeValue(key, XMLConfigConstants.KEY, schema);
}
ResourceMapSerializer.serializeResourceMap(validate, mediator.getResourceMap());
List<MediatorProperty> features = mediator.getFeatures();
if (!features.isEmpty()) {
for (MediatorProperty mp : features) {
OMElement feature = fac.createOMElement("feature", synNS, validate);
if (mp.getName() != null) {
feature.addAttribute(fac.createOMAttribute("name", nullNS, mp.getName()));
} else {
handleException("The Feature name is missing");
}
if (mp.getValue() != null) {
feature.addAttribute(fac.createOMAttribute("value", nullNS, mp.getValue()));
} else {
handleException("The Feature value is missing");
}
}
}
OMElement onFail = fac.createOMElement("on-fail", synNS, validate);
serializeChildren(onFail, mediator.getList());
OMAttribute cacheSchemaAtt = fac.createOMAttribute("cache-schema", nullNS, String.valueOf(mediator.isCacheSchema()));
validate.addAttribute(cacheSchemaAtt);
return validate;
}
use of org.apache.synapse.mediators.Value in project wso2-synapse by wso2.
the class ValueFactory method createValue.
/**
* Create a key instance
*
* @param elem OMElement
* @return Key
*/
public Value createValue(String name, OMElement elem) {
Value key = null;
OMAttribute attKey = elem.getAttribute(new QName(name));
if (attKey != null) {
String attributeValue = attKey.getAttributeValue();
boolean hasEscape = isEscapedExpression(attributeValue);
if (!hasEscape && isDynamicKey(attributeValue)) {
// Filter json-eval expressions
if (attributeValue.startsWith("{json-eval(")) {
// Get the expression in-between "{}"
attributeValue = attributeValue.substring(1, attributeValue.length() - 1);
SynapseJsonPath synJsonPath = createSynJsonPath(attributeValue);
key = new Value(synJsonPath);
} else {
SynapseXPath synXpath = createSynXpath(elem, attributeValue);
key = new Value(synXpath);
}
} else if (hasEscape) {
/**
* escaped expr
*/
key = new Value(getEscapedExpression(attributeValue));
key.setNamespaces(elem);
} else {
/**
* static key
*/
key = new Value(attributeValue);
}
} else {
handleException("The '" + name + "' attribute is required for the element '" + elem.getLocalName() + "'");
}
return key;
}
Aggregations