WSO2 Call Template Mediator how to get a variable - wso2

I'm trying to use Call Template Mediator in WSO2 and I'm trying to get a dynamic value inside function, but I don't find the way to get it.
For example:
<property expression="$ctx:variable" name="test" type="STRING"/>
<call-template target="HelloWorld_Logger">
<with-param name="message" value="VARIABLE: " expression="$ctx:test" />
</call-template>
I'm not able to get the property "test", if i remove value field the IDE reports an error.
There is some way to get a property inside <call-template> function?

The Call Template Mediator doesn't have a parameter called expression to pass dynamic values. You must pass the XPath expression within {} to the value parameter itself. In your case, the call-template will be as follows,
<call-template target="HelloWorld_Logger">
<with-param name="message" value="{$ctx:test}"/>
</call-template>
Please refer to the Call Template Mediator for more information.

Related

wso2 dataservice filter null for a dataservice field

in my Dataservice ,in a select statement one of the field has a null value.
It is returned like this
<ROLLNUMBER xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
I want to write a filter and do some logic based on if the value is null or it has some value.
How can do that in WSO2 ESB?
I tried a few XSL expression nothing works
You need to use a filter mediator wherein you need to use xpath expression as //*[local-name()='ROLL_NUMBER']/text(), what this will do is for the element ROLL_NUMBER if there is a value only then the filter condition is satisfied and it goes to then condition if not then it will go to else condition
Try the following
<filter source="boolean(get-property('yourProperty'))" regex="false">
<then> <!-- NULL OR NON EXIST --> </then>
<else> <!-- EXIST --> </else>
</filter>

replace function with get-property in wso2esb

I am trying the below option and i m getting error, can anyone suggest me the solution. I have added synapse.xpath.dom.failover.enabled=true
Code:trying to replace ~TOKEN~ with the property value
<property name="verificationLink" expression="fn:replace($ctx:reqVerifyUrl , '~TOKEN~', get-property('verification_code'))" scope="default" type="STRING" xmlns:fn="http://www.w3.org/2005/xpath-functions" />
Error:
TID: [-1234] [] [2017-02-15 00:14:19,318] ERROR {org.apache.synapse.util.xpath.SynapseXPath} - Evaluation of the XPath expression fn:replace($ctx:reqVerifyUrl , '~TOKEN~', get-property('verification_code')) resulted in an error {org.apache.synapse.util.xpath.SynapseXPath} net.sf.saxon.trans.XPathException: Unknown system function get-property()
get-property is not a standard xpath function and you must say to the xpath engine that this function comes from synapse :
<property name="verificationLink" expression="fn:replace($ctx:reqVerifyUrl , '~TOKEN~', syn:get-property('verification_code'))" scope="default" type="STRING" xmlns:fn="http://www.w3.org/2005/xpath-functions" />
You do not need to add the definition of 'syn' namespace, it is well known in your mediation (xmlns:syn="http://ws.apache.org/ns/synapse")
Two things need to be done:
A. Review this and ensure your Synapse config is set correctly. Is there any replace function in wso2esb?
B. Then you need to add prefix fn & syn if you need to use methods like get-property() with XPath 2.0. Also note, you must use (include) the following namespaces values for ‘syn’ and ‘fn’.
xmlns:syn=”http://ws.apache.org/commons/ns/payload”
xmlns:fn=”http://www.w3.org/2005/xpath-functions”
Here is a sample property mediator using replace.
<property xmlns:syn="http://ws.apache.org/commons/ns/payload" xmlns:fn="http://www.w3.org/2005/xpath-functions" name="xmlValue" expression="fn:replace(syn:get-property('textValue'), 'xmlData=', '')></property>;

How to create Property array in wso2 ESB?

I have my request body as:
<tns:InputRequest xmlns:tns="http://tempuri.org/">
<tns:ID>ID_001</tns:ID>
<tns:ID>ID_002</tns:ID>
<tns:Description>Description for ID_001</tns:Description>
<tns:Description>Description for ID_002</tns:Description>
</tns:InputRequest>
and to get the value of ID and Description, i Have created property as:
<property xmlns:tns="http://tempuri.org/" name="ID" expression="//tns:ID" scope="default" type="STRING"/>
<property xmlns:tns="http://tempuri.org/" name="Description" expression="//tns:Description" scope="default" type="STRING"/>
But this gets me only one value. How can i make a property array so that i can store multiple values of ID and description in it and how to retreive from this array property?Looking forward to your reply.Thanks in advance
You should be able to extract those values using XPATH (//node/child::node()) and then set to property.
Below thread will help you to extract required nodes and set to property. You need to set the type as 'OM' to preserve XML as it is.
how to catch an array of nodes to a property

How to pass parameters to XSLT from JBoss Actions Pipeline

Say I have a given action:
<service category="MyService" name="MyFirstService">
<actions mep="RequestResponse">
<action class="actions.CXFListenerAction" name="CXFServiceListener"/>
<action class="org.jboss.soa.esb.actions.transformation.xslt.XsltAction" name="Transform XML">
<property name="templateFile" value="/stylesheets/transform_response.xslt"/>
<property name="failOnWarning" value="true"/>
</action>
</actions>
I am trying to figure out how to add a property name or parameter that I could then access from within the XSLT. I've tried add additional property names,
<property name="param1" value="Hey!"/>
but I'm not 100% sure if this is correct for adding parameters accessible by the XSLT.
Thanks.
The properties defined for the XsltAction class are properties specific to that action class and are not related to parameters in the template file.
So in short, it's not possible to pass parameters to the xslt from the JBoss ESB action pipeline. However, it would be possible to create a custom action that decorates your ESB message with data you define as a property in your jboss-esb.xml file and insert that before your XsltAction. That may be what you're looking for.

How can I pass in multiple data sources into an XSLT mediator in WSO2 ESB

I have tried to pass in multiple nodesets into an XSLT in the XSLT mediator in WSO2 ESB through parameters to the stylesheet. However when trying to refer to the parameters passed in the XSLT processor says that the values are string types.
The XSLT mediator config itself is structured like this:
<xslt key="conf:/Test/TestMapping.xsl">
<property xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd" name="originalMsg" expression="get-property('OriginalMsg')" />
</xslt>
In the XSLT itself when the passed parameter is used to try to refer to an element in this data, e.g.
<xsl:value-of select="$originalMsg/ns1:Node/ns1:OtherNode"/>
the error reports that "/" operation cannot be applied to a string when it expects a nodeset.
I have tried to pass on the nodeset which is the mesage body from a previous step in the flow, which I have tried various medthods to save:
Through Enrich mediator:
<enrich>
<source clone="true" type="body" xpath="" property="" />
<target action="replace" type="property" xpath="" property="OriginalMsg" />
</enrich>
Through Property mediator:
<property xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd" name="OriginalMsg" expression="$body" scope="default" type="OM" />
But this has not worked.
Alternatively, is there a way to use document() in the ESB function to refer to message context data or some other approach?
I worked around this issue by adding the additional XML data hierarchies that I needed to include as siblings to the current root element within the message body, with the Enrich mediator.
This allowed all the different data to be available in the XSLT.
Then in XSLT mediator configuration I set the source path to $body (i.e. the message body) rather than the default value (which is the first child of message body) and added a SOAP Body element as root element in the XSLT itself. In this way all the children of the message body were replaced with the single target XML structure.
This is because in the XSLT mediator code:
private void applyProperties(Transformer transformer, MessageContext synCtx, SynapseLog synLog) {
for (MediatorProperty prop : properties) {
if (prop != null) {
String value;
if (prop.getValue() != null) {
value = prop.getValue();
} else {
value = prop.getExpression().stringValueOf(synCtx);
}
if (synLog.isTraceOrDebugEnabled()) {
if (value == null) {
synLog.traceOrDebug("Not setting parameter '" + prop.getName() + "'");
} else {
synLog.traceOrDebug("Setting parameter '" + prop.getName() + "' to '"
+ value + "'");
}
}
if (value != null) {
transformer.setParameter(prop.getName(), value);
}
}
}
You can see that value is always a String. I propose to add for the properties the type of the property before injecting in the transformer.
What do you think?