How to access a row into foreach in wso2ESB (Developer Studio) - wso2

I'm integrating wso2dss 3.5 and wso2esb 4.9. In DSS, I created a DataService to get data from a database. Following the WSO2 Dashboard into DSS, I used the endpoint in my eclipse project (Developer Studio). The xml gerated by DSS is:
<elements xmlns="http://ws.wso2.org/dataservice">
<row>
<name>nome1</name>
<address>rua xalala das xalalas</address>
</row>
<row>
<name>nome2</name>
<address>ruas rms did sa</address>
</row>
<row>
<name>nome3</name>
<address>aldoas daso dasodsa</address>
</row>
</elements>
So, in my ESB config project, there is this generated code:
<foreach expression="//ds:elements/ds:row" xmlns:ds="http://ws.wso2.org/dataservice">
<sequence>
<payloadFactory media-type="xml">
<format>
<p:Insert xmlns:p="http://ws.wso2.org/dataservice">
<xs:nome xmlns:xs="http://ws.wso2.org/dataservice">$1</xs:nome>
<xs:endereco xmlns:xs="http://ws.wso2.org/dataservice">$2</xs:endereco>
</p:Insert>
</format>
<args>
<arg evaluator="xml" expression="/ds:row/name"/>
<arg evaluator="xml" expression="/ds:row/address"/>
</args>
</payloadFactory>
<log>
<property expression="/ds:row/name" name="nome"/>
<property expression="/ds:row/address" name="endereco"/>
</log>
</sequence>
</foreach>
I'm trying to get the value of first and second element at each row (name and address). How can I do this?

try add the xmlns:ds to args tag, like this:
<foreach expression="//ds:elements/ds:row" xmlns:ds="http://ws.wso2.org/dataservice">
<sequence>
<payloadFactory media-type="xml">
<format>
<p:Insert xmlns:p="http://ws.wso2.org/dataservice">
<xs:nome xmlns:xs="http://ws.wso2.org/dataservice">$1</xs:nome>
<xs:endereco xmlns:xs="http://ws.wso2.org/dataservice">$2</xs:endereco>
</p:Insert>
</format>
<args xmlns:ds="http://ws.wso2.org/dataservice">>
<arg evaluator="xml" expression="//ds:name"/>
<arg evaluator="xml" expression="//ds:address"/>
</args>
</payloadFactory>
<log>
<property expression="/ds:row/name" name="nome"/>
<property expression="/ds:row/address" name="endereco"/>
</log>
</sequence>
</foreach>

Related

WSO2 Api Manager 3.2.0 How Expose a SOAP Service as a REST API

I Use this document but this is Don't work:
https://apim.docs.wso2.com/en/3.2.0/learn/tutorials/expose-a-soap-service-as-a-rest-api/
for example automatic create this mediator for input and it's not work :
<header description="SOAPAction" name="SOAPAction" scope="transport" value="http://tempuri.org/ILivePublisher/Candles"/>
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
<property expression="json-eval($.Candles.instrumentIsin)" name="req.var.Candles.instrumentIsin"/>
<property expression="json-eval($.Candles.interval.Interval)" name="req.var.Candles.interval.Interval"/>
<property expression="json-eval($.Candles.since)" name="req.var.Candles.since"/>
<property expression="json-eval($.Candles.to)" name="req.var.Candles.to"/>
<payloadFactory description="transform" media-type="xml">
<format>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<web:Candles xmlns:web="http://tempuri.org/">
<web:instrumentIsin>$1</web:instrumentIsin>
<web:interval>
<web:Interval>$2</web:Interval>
</web:interval>
<web:since>$3</web:since>
<web:to>$4</web:to>
</web:Candles>
</soapenv:Body>
</soapenv:Envelope>
</format>
<args>
<arg evaluator="xml" expression="get-property('req.var.Candles.instrumentIsin')"/>
<arg evaluator="xml" expression="get-property('req.var.Candles.interval.Interval')"/>
<arg evaluator="xml" expression="get-property('req.var.Candles.since')"/>
<arg evaluator="xml" expression="get-property('req.var.Candles.to')"/>
</args>
</payloadFactory>
<property description="messageProperty" name="messageType" scope="axis2" type="STRING" value="application/soap+xml"/>

WSO2 EI 6.6.0 Class Mediator not being able to use SOAP call return content

I have the following API in EI 6.6.0:
<?xml version='1.0' encoding='UTF-8'?>
<api xmlns="http://ws.apache.org/ns/synapse" name="sample" context="/sample">
<resource methods="POST">
<inSequence>
<payloadFactory media-type="xml">
<format>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<P xmlns="http://tempuri.org/">
<P1>$1</P1>
<P2>$2</P2>
<P3>$3</P3>
</P>
</soap:Body>
</soap:Envelope>
</format>
<args>
<arg evaluator="json" expression="$.p1" />
<arg evaluator="json" expression="$.p2" />
<arg evaluator="json" expression="$.p3" />
</args>
</payloadFactory>
<log level="full" />
<property name="Content-Type" value="text/xml;charset=UTF-8" scope="axis2"/>
<header name="Accept" scope="transport" value="text/xml"/>
<call>
<endpoint>
<wsdl Action="name_of_the_action" service="name_of_the_service" port="name_of_soap_port" uri="http://<ip>/path?WSDL" />
</endpoint>
</call>
<class name="my_mediator_package"></class>
<log level="full" />
<payloadFactory media-type="xml">
<format>
<retorno xmlns="">
<msg>$1</msg>
</retorno>
</format>
<args>
<arg evaluator="xml" expression="get-property('property_set_on_mediator')" />
</args>
</payloadFactory>
<property name="messageType" value="application/xml" scope="axis2" type="STRING" />
<respond />
</inSequence>
<outSequence>
</outSequence>
<faultSequence>
<property name="text" value="An unexpected error occured"/>
<property name="message" expression="get-property('ERROR_MESSAGE')"/>
<payloadFactory media-type="xml">
<format>
<error xmlns="">
<msg>$1</msg>
</error>
</format>
<args>
<arg evaluator="xml" expression="get-property('ERROR_MESSAGE')"/>
</args>
</payloadFactory>
<property name="messageType" value="application/json" scope="axis2" type="STRING"/>
<respond/>
</faultSequence>
</resource>
</api>
my mediate method content:
public boolean mediate(MessageContext synCtx) {
org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx)
.getAxis2MessageContext();
try {
// Getting the json payload to string
String jsonPayloadToString = JsonUtil.jsonPayloadToString(((Axis2MessageContext) synCtx)
.getAxis2MessageContext());
System.out.println("original payload : \n" + jsonPayloadToString + "\n");
I'm not being able to use the return from my SOAP call in my mediator so I can work on it.
When I run the API I get the following from my mediator code:
original payload:
{}
Is there a way so I can obtain the SOAP call returned envelope and use it in my mediator?
The JSON payload is coming as empty because you are calling a SOAP backend and getting a SOAP payload. You can use synCtx.getEnvelope() in your mediator to get the SOAPEnvelope from the response.
1- make sure the json payload is there. So, log the json properties inside your inSequence.
2- I'm not being able to use the return from my SOAP call in my mediator so I can work on it. you can see the response payload in your outSequence which currently is doing nothing.
3- According to your scenario which is simply calling a SOAP webservice, you do not need a class mediator. In other words, when you do not need manipulating the initial payload and then pass it to the destination service, logically implementing your own class mediator benefits you nothing.
Also, there are quite number of samples in https://docs.wso2.com/display/EI611 which will help you.
Please let me know if your problem is solved.

Loop Foreach into WSO2ESB doesn't working

I'm integrating wso2dss 3.5 and wso2esb 4.9. In DSS, I created a DataService to get data from a database. Following the WSO2 Dashboard into DSS, I used the endpoint in my eclipse project (Developer Studio). The xml gerated by DSS is:
<elements xmlns="http://ws.wso2.org/dataservice">
<row>
<name>nome1</name>
<address>rua xalala das xalalas</address>
</row>
<row>
<name>nome2</name>
<address>ruas rms did sa</address>
</row>
<row>
<name>nome3</name>
<address>aldoas daso dasodsa</address>
</row>
</elements>
So, in my ESB project I have this gerated code:
<foreach expression="//elements/row">
<sequence>
<payloadFactory media-type="xml">
<format>
<p:Insert xmlns:p="http://ws.wso2.org/dataservice">
<xs:nome xmlns:xs="http://ws.wso2.org/dataservice">$1</xs:nome>
<xs:endereco xmlns:xs="http://ws.wso2.org/dataservice">$2</xs:endereco>
</p:Insert>
</format>
<args>
<arg evaluator="xml" expression="/row/name"/>
<arg evaluator="xml" expression="/row/address"/>
</args>
</payloadFactory>
<log>
<property name="xalela" value="xalxalxlalxal"/>
</log>
</sequence>
</foreach>
The problem is that it isn't loggin into foreach, in other words, it isn't accessing inside the foreach mediator. What's wrong?
xpath //elements/row return nothing because node "elements" belong to namespace http://ws.wso2.org/dataservice
try with //ds:element/ds:row and define ds as xmlns:ds="http://ws.wso2.org/dataservice"

WSO2ESB for each mediator, FOR EACH RESPONSE having /n/t extra with each element

I have this below mentioned soap request, I am using WSO2ESB 4.9.
Soap request :
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><jsonArray><jsonElement><a><s>as</s></a><b>Type1</b><c>C1</c><d><t>A1</t></d><e>e1</e></jsonElement><jsonElement><a><s>as</s></a><b>Type2</b><c>C2</c><d><t>A1</t></d><e>e1</e></jsonElement></jsonArray></soapenv:Body></soapenv:Envelope>
I want to iterator over jsonElement/b and get json list : ["Type1", "Type2"].But I am getting below list :
[
"\n\t\t\t\t\t\t\t\t\t\t\tType1\n\t\t\t\t\t\t\t\t\t",
"\n\t\t\t\t\t\t\t\t\t\t\tType2\n\t\t\t\t\t\t\t\t\t"
]
Please any way to get the required list.For each mediator :
<foreach expression="//jsonArray/jsonElement">
<sequence>
<payloadFactory media-type="xml">
<format>
<jsonElement>
$1
</jsonElement>
</format>
<args>
<arg expression="//b" evaluator="xml" />
</args>
</payloadFactory>
</sequence>
</foreach>
Got it working, there is space and newline in foreach <jsonElement>.
Working code:
<foreach expression="//jsonArray/jsonElement">
<sequence>
<payloadFactory media-type="xml">
<format>
<jsonElement xmlns="">$1</jsonElement>
</format>
<args>
<arg expression="//b" evaluator="xml" />
</args>
</payloadFactory>
</sequence>
</foreach>

How to create Payload Dynamically in wso2 ESB

I am invoking a wso2 DSS service which returns me data like:
<IdentifierCollection xmlns="http://tempuri.org/">
<Identifier>
<One>1</One>
<Two>2</Two>
<Three>3</Three>
</Identifier>
</IdentifierCollection>
To invoke Dss my esb code is:
<payloadFactory>
<format>
<p:GetIdentifier xmlns:p="http://tempuri.org/">
<xs:ID xmlns:xs="http://tempuri.org/">$1</xs:ID>
</p:GetIdentifier >
</format>
<args>
<arg expression="get-property('ID')"/>
</args>
</payloadFactory>
<send receive="ResponseOfGetIdentifier">
<endpoint key="IdentifierEP"/>
</send>
Now in my ResponseOfGetIdentifier Sequence i am capturing all data in properties
<sequence xmlns="http://ws.apache.org/ns/synapse" name="ResponseOfGetIdentifier">
<iterate xmlns:ns="http://org.apache.synapse/xsd" xmlns:p="http://tempuri.org/" preservePayload="true" attachPath="//p:EntriesCollection" expression="//p:EntriesCollection/p:Entries" id="IterateForResponse">
<target>
<sequence>
<property xmlns:ns="http://org.apache.synapse/xsd" xmlns:p="http://tempuri.org" name="ResponseOne" expression="//p:Identifier/p:one" scope="default" type="STRING"/>
<property xmlns:ns="http://org.apache.synapse/xsd" xmlns:p="http://tempuri.org" name="ResponseTwo" expression="//p:Identifier/p:Two" scope="default" type="STRING"/>
<property xmlns:ns="http://org.apache.synapse/xsd" xmlns:p="http://tempuri.org" name="ResponseThree" expression="//p:Identifier/p:Three" scope="default" type="STRING"/>
<payloadFactory>
<format>
<tns:ProductIdentifier xmlns:tns="http://globalArther.products.com">
<IdentifierProducts>
<Product1>$1</Product1>
<Product2>$2</Product>
<Product3>$3</Product3>
</IdentifierProducts>
</tns:ProductIdentifier>
</format>
<args>
<arg expression="get-property('ResponseOne')"/>
<arg expression="get-property(''ResponseTwo)"/>
<arg expression="get-property('ResponseThree')"/>
</args>
</payloadFactory>
</sequence>
</target>
</iterate>
</sequence>
Since my dss response contains only one iteration of Identifier node hence above code is working but when my identifier node count is more than one i.e
<IdentifierCollection xmlns="http://tempuri.org/">
<Identifier>
<One>1</One>
<Two>2</Two>
<Three>3</Three>
</Identifier>
<Identifier>
<One>a</One>
<Two>b</Two>
<Three>c</Three>
</Identifier>
</IdentifierCollection>
, since my payload can take only one data it takes the second iteration data and shows me only single result as:
<tns:ProductIdentifier xmlns:tns="http://globalArther.products.com">
<IdentifierProducts>
<Product1>a</Product1>
<Product2>b</Product>
<Product3>c</Product3>
</IdentifierProducts>
</tns:ProductIdentifier>
But i want my response as :
<tns:ProductIdentifier xmlns:tns="http://globalArther.products.com">
<IdentifierProducts>
<Product1>1</Product1>
<Product2>2</Product>
<Product3>3</Product3>
</IdentifierProducts>
<IdentifierProducts>
<Product1>a</Product1>
<Product2>b</Product>
<Product3>c</Product3>
</IdentifierProducts>
</tns:ProductIdentifier>
So now my question is how can i do that i.e how can i make my payload dynamic i.e. i want to add every iteration in my payload.
Looking forward to your answers.Thanks in advance
Payload is kind of static. You need to use XSLT mediator to achieve this .That is, you have to write XSLT script to make the iteration logic.