use of org.apache.synapse.mediators.transform.PayloadFactoryMediator in project wso2-synapse by wso2.
the class PayloadFactoryMediatorSerializerTest method testSerializeSpecificMediator2.
/**
* Test SerializeSpecificMediator method with format added for payloadFactory mediator
* and assert the format added in OmElement.
*/
@Test
public void testSerializeSpecificMediator2() {
PayloadFactoryMediatorSerializer serializer = new PayloadFactoryMediatorSerializer();
PayloadFactoryMediator payloadFactoryMediator = new PayloadFactoryMediator();
payloadFactoryMediator.setFormat(format);
OMElement element = serializer.serializeSpecificMediator(payloadFactoryMediator);
Assert.assertNotNull(element);
Assert.assertTrue("Format set for Mediator is not serialized", element.getChildren().next().toString().contains("<p:echoInt xmlns:p=\"" + "http://echo.services.core.carbon.wso2.org\">"));
}
use of org.apache.synapse.mediators.transform.PayloadFactoryMediator in project wso2-synapse by wso2.
the class PayloadFactoryMediatorSerializerTest method testSerializeSpecificMediator4.
/**
* Test SerializeSpecificMediator method with PathArgument added for payloadFactory mediator
* and assert that argument is added.
*/
@Test
public void testSerializeSpecificMediator4() throws JaxenException {
PayloadFactoryMediatorSerializer serializer = new PayloadFactoryMediatorSerializer();
PayloadFactoryMediator payloadFactoryMediator = new PayloadFactoryMediator();
Argument argument = new Argument();
argument.setValue("TestArgument1");
payloadFactoryMediator.addPathArgument(argument);
payloadFactoryMediator.setFormat(format);
OMElement element = serializer.serializeSpecificMediator(payloadFactoryMediator);
MediatorFactory mediatorFactory = new PayloadFactoryMediatorFactory();
Mediator mediator = mediatorFactory.createMediator(element, null);
Assert.assertNotNull(element);
Assert.assertEquals("Path argument added is not serialized", "TestArgument1", ((PayloadFactoryMediator) mediator).getPathArgumentList().get(0).getValue());
}
use of org.apache.synapse.mediators.transform.PayloadFactoryMediator in project wso2-synapse by wso2.
the class PayloadFactoryMediatorSerializerTest method testSerializeSpecificMediator3.
/**
* Test SerializeSpecificMediator method with Dynamic format added for payloadFactory mediator
* and assert that dynamic format is set.
*/
@Test
public void testSerializeSpecificMediator3() {
PayloadFactoryMediatorSerializer serializer = new PayloadFactoryMediatorSerializer();
PayloadFactoryMediator payloadFactoryMediator = new PayloadFactoryMediator();
payloadFactoryMediator.setFormat(format);
payloadFactoryMediator.setFormatDynamic(true);
payloadFactoryMediator.setFormatKey(new Value("testKey"));
OMElement element = serializer.serializeSpecificMediator(payloadFactoryMediator);
Assert.assertNotNull(element);
MediatorFactory mediatorFactory = new PayloadFactoryMediatorFactory();
Mediator mediator = mediatorFactory.createMediator(element, null);
Assert.assertTrue("Dynamic Format set for Mediator is not serialized", ((PayloadFactoryMediator) mediator).isFormatDynamic());
}
use of org.apache.synapse.mediators.transform.PayloadFactoryMediator in project wso2-synapse by wso2.
the class PayloadFactoryMediatorSerializer method serializeSpecificMediator.
public OMElement serializeSpecificMediator(Mediator m) {
if (!(m instanceof PayloadFactoryMediator)) {
handleException("Unsupported mediator passed in for serialization : " + m.getType());
return null;
}
PayloadFactoryMediator mediator = (PayloadFactoryMediator) m;
OMElement payloadFactoryElem = fac.createOMElement(PAYLOAD_FACTORY, synNS);
if (mediator.getType() != null) {
payloadFactoryElem.addAttribute(fac.createOMAttribute(MEDIA_TYPE, null, mediator.getType()));
}
saveTracingState(payloadFactoryElem, mediator);
if (!mediator.isFormatDynamic()) {
if (mediator.getFormat() != null) {
try {
OMElement formatElem = fac.createOMElement(FORMAT, synNS);
String type = mediator.getType();
if (type != null && (type.contains(JSON_TYPE) || type.contains(TEXT))) {
formatElem.setText(mediator.getFormat());
} else {
formatElem.addChild(AXIOMUtil.stringToOM(mediator.getFormat()));
}
payloadFactoryElem.addChild(formatElem);
} catch (XMLStreamException e) {
handleException("Error while serializing payloadFactory mediator", e);
}
} else {
handleException("Invalid payloadFactory mediator, format is required");
}
} else {
// Serialize Value using ValueSerializer
OMElement formatElem = fac.createOMElement(FORMAT, synNS);
formatElem.addAttribute(fac.createOMAttribute("key", nullNS, mediator.getFormatKey().getKeyValue()));
ValueSerializer keySerializer = new ValueSerializer();
keySerializer.serializeValue(mediator.getFormatKey(), XMLConfigConstants.KEY, formatElem);
payloadFactoryElem.addChild(formatElem);
}
OMElement argumentsElem = fac.createOMElement(ARGS, synNS);
List<Argument> pathArgList = mediator.getPathArgumentList();
if (null != pathArgList && pathArgList.size() > 0) {
for (Argument arg : pathArgList) {
if (arg.getExpression() == null && arg.getValue() == null) {
continue;
}
OMElement argElem = fac.createOMElement(ARG, synNS);
if (arg.isLiteral()) {
// add literal attribute only it is true. (because default value is false)
argElem.addAttribute(fac.createOMAttribute(LITERAL, nullNS, "true"));
}
if (null != arg.getExpression() && null != arg.getExpression().getPathType()) {
argElem.addAttribute(fac.createOMAttribute(EVALUATOR, nullNS, getEvaluator(arg.getExpression().getPathType())));
} else if (null == arg.getExpression() && arg.getValue() != null) {
argElem.addAttribute(fac.createOMAttribute(VALUE, nullNS, arg.getValue()));
} else {
argElem.addAttribute(fac.createOMAttribute(EVALUATOR, nullNS, getEvaluator(SynapsePath.X_PATH)));
}
if (null != arg.getExpression()) {
SynapsePathSerializer.serializePath(arg.getExpression(), argElem, EXPRESSION);
// We don't want the "json-eval(" prefix in PayloadFactory, for backward compatibility and since
// PF has evaluator attribute.
QName EXPR_Q = new QName(EXPRESSION);
String strExpr = argElem.getAttribute(EXPR_Q).getAttributeValue();
if (strExpr.startsWith("json-eval(")) {
argElem.getAttribute(EXPR_Q).setAttributeValue(strExpr.substring(10, strExpr.length() - 1));
}
}
argumentsElem.addChild(argElem);
}
}
payloadFactoryElem.addChild(argumentsElem);
return payloadFactoryElem;
}
use of org.apache.synapse.mediators.transform.PayloadFactoryMediator in project wso2-synapse by wso2.
the class PayloadFactoryMediatorFactory method createSpecificMediator.
public Mediator createSpecificMediator(OMElement elem, Properties properties) {
PayloadFactoryMediator payloadFactoryMediator = new PayloadFactoryMediator();
processAuditStatus(payloadFactoryMediator, elem);
String mediaTypeValue = elem.getAttributeValue(TYPE_Q);
// for the backward compatibility.
if (mediaTypeValue != null) {
// set the mediaType for the PF
payloadFactoryMediator.setType(mediaTypeValue);
} else {
payloadFactoryMediator.setType(XML_TYPE);
}
OMElement formatElem = elem.getFirstChildWithName(FORMAT_Q);
if (formatElem != null) {
OMAttribute n = formatElem.getAttribute(ATT_KEY);
if (n == null) {
// OMElement copy = formatElem.getFirstElement().cloneOMElement();
OMElement copy = formatElem.cloneOMElement();
removeIndentations(copy);
if (mediaTypeValue != null && (mediaTypeValue.contains(JSON_TYPE) || mediaTypeValue.contains(TEXT_TYPE))) {
payloadFactoryMediator.setFormat(copy.getText());
} else {
payloadFactoryMediator.setFormat(copy.getFirstElement().toString());
}
} else {
ValueFactory keyFac = new ValueFactory();
Value generatedKey = keyFac.createValue(XMLConfigConstants.KEY, formatElem);
payloadFactoryMediator.setFormatKey(generatedKey);
payloadFactoryMediator.setFormatDynamic(true);
}
} else {
handleException("format element of payloadFactoryMediator is required");
}
OMElement argumentsElem = elem.getFirstChildWithName(ARGS_Q);
if (argumentsElem != null) {
Iterator itr = argumentsElem.getChildElements();
while (itr.hasNext()) {
OMElement argElem = (OMElement) itr.next();
Argument arg = new Argument();
String value;
boolean isLiteral = false;
String isLiteralString = argElem.getAttributeValue(ATT_LITERAL);
if (isLiteralString != null) {
// if literal is 'true' then set literal to true. defaults to false.
isLiteral = Boolean.parseBoolean(isLiteralString);
}
arg.setLiteral(isLiteral);
if ((value = argElem.getAttributeValue(ATT_VALUE)) != null) {
arg.setValue(value);
arg.setExpression(null);
payloadFactoryMediator.addPathArgument(arg);
} else if ((value = argElem.getAttributeValue(ATT_EXPRN)) != null) {
if (value.trim().length() == 0) {
handleException("Attribute value for xpath cannot be empty");
} else {
try {
// set the evaluator
String evaluator = argElem.getAttributeValue(ATT_EVAL);
if (evaluator != null && evaluator.equals(JSON_TYPE)) {
if (value.startsWith("json-eval(")) {
value = value.substring(10, value.length() - 1);
}
arg.setExpression(SynapseJsonPathFactory.getSynapseJsonPath(value));
// we have to explicitly define the path type since we are not going to mark
// JSON Path's with prefix "json-eval(".
arg.getExpression().setPathType(SynapsePath.JSON_PATH);
payloadFactoryMediator.addPathArgument(arg);
} else {
SynapseXPath sxp = SynapseXPathFactory.getSynapseXPath(argElem, ATT_EXPRN);
// we need to disable stream Xpath forcefully
sxp.setForceDisableStreamXpath(Boolean.TRUE);
arg.setExpression(sxp);
arg.getExpression().setPathType(SynapsePath.X_PATH);
payloadFactoryMediator.addPathArgument(arg);
}
} catch (JaxenException e) {
handleException("Invalid XPath expression for attribute expression : " + value, e);
}
}
} else {
handleException("Unsupported arg type. value or expression attribute required");
}
}
}
return payloadFactoryMediator;
}
Aggregations