How to implement sequential back-end calls in WSO2 EI/ESB? - wso2

I want to implement sequential back-end calls in WSO2 Integration Studio.
After the first back-end call, I want to send a few fields from the response from the first back-end call to the second back-end call.
I tried using payload factory mediator inside a for-each loop after the http back-end call, but it is giving only last object.
Any other way to get it done?

You do not need a foreach loop here. You can implement a Service Chain pattern. You can create a Payload using the PayloadFactory mediator to use in second call which uses the values from the first call's response. Have a look at the following sample.
<api xmlns="http://ws.apache.org/ns/synapse" name="ServiceChaining" context="/servicechain">
<resource methods="GET">
<inSequence>
<call>
<endpoint>
<http uri-template="http://www.mocky.io/v2/5eb1cf1d320000749428f99e"/>
</endpoint>
</call>
<payloadFactory media-type="json">
<format>{"Hello" : {"test1" : "$1", "test3": "$2"}}</format>
<args>
<arg evaluator="json" expression="$.key1"/>
<arg evaluator="json" expression="$.key3"/>
</args>
</payloadFactory>
<call>
<endpoint>
<http method="POST" uri-template="http://www.mocky.io/v2/5185415ba171ea3a00704eed"/>
</endpoint>
</call>
<respond/>
</inSequence>
</resource>
</api>

Related

wso2 micro-integrator 7.0.0 service chaining advanced

I'm trying to implement service chaining using Integration-studio 7.0.0
For.eg
Service 1(GET) - getCityById/{cityId}
response=> {"cityName":"Mumbai"}
Service 2(POST) - getStateByCityName
(it accepts above cityName inside body)
response=> {"stateName":"Maharashtra"}
Service 3(GET) - getCountryByStateName/{stateName}
response=> {"countryName":"India"}
Service 1 returns me response as {"cityName":"Mumbai"}.
I set that response inside the payload and call service 2.
But service 2 doesn't get called.
Kindly let me know,how should i proceed further
Thanks for ur help in advance!
Below is the code
<?xml version="1.0" encoding="UTF-8"?>
<api context="/country1" name="CountryDetails" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET">
<inSequence>
<call>
<endpoint key="CITY"/>
</call>
<payloadFactory media-type="json">
<format>{"cityName":"$1"}</format>
<args>
<arg evaluator="json" expression="$.cityName"/>
</args>
</payloadFactory>
<call>
<endpoint key="StateAPI"/>
</call>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>

Webservice response as HTML instead of SOAP message

I am working on a wso2 scenario in which I send a SOAP message to a webservice and then receive and manipulate the answer. For this purpose I have TWO Webservices. One is placed in a test environment (1) and the another is the live one (2).
I have no access to the webservice code, I'm just a user.
The problem is:
Via SOAPUI
I'm able to use the service from Webservices (1) and (2). No error, everything works fine.
Via WSO2
I'm able to use the service from webservice (1) but when I point to the webservice (2) I get a HTML 403 Forbidden response instead of a SOAP/XML message.
Am I missing something or this could be a misconfiguration of the webservice?
Following my in/out requests:
Sequence:
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="pricing" trace="enable" xmlns="http://ws.apache.org/ns/synapse">
<smooks config-key="smooks-csv.xml">
<input type="text"/>
<output type="xml"/>
</smooks>
<iterate expression="//product" id="iterateXML" sequential="true"
xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd">
<target>
<sequence>
<payloadFactory media-type="xml">
<format>
<xyz xmlns="http://tempuri.org/">
<safeKey>$6</safeKey>
<storeId>$5</storeId>
<articleId>$1</articleId>
<barcode/>
<sku>$2</sku>
<price>$3</price>
<discount>$4</discount>
</xyz>
</format>
<args>
<arg evaluator="xml" expression="//articleId"/>
<arg evaluator="xml" expression="//sku"/>
<arg evaluator="xml" expression="//price"/>
<arg evaluator="xml" expression="//discount"/>
<arg evaluator="xml" expression="//storeId"/>
<arg evaluator="xml" expression="//key"/>
</args>
</payloadFactory>
<in>
<header name="Action" scope="default" value="http://tempuri.org/xyz"/>
<send>
<endpoint>
<recipientlist>
<endpoint key="PS_PRICING"/>
<endpoint key="fileSave"/>
</recipientlist>
</endpoint>
</send>
<drop/>
</in>
<out>
<send/>
</out>
<log level="full" separator=";">
<property expression="*" name="ResponsePriceUpdate"/>
</log>
</sequence>
</target>
</iterate>
</sequence>
UPDATE:
After testing using TCPMon and TCPTrace I got the following:
Via SOAPUi
If I send the message SOAPUi -> Live Webservice - Works fine! (Picture a)
If I send the message SOAPUi -> TCPMon/TCPTrace -> Live Webservice - 403 Forbidden! (Picture b)
It doesn't make sense at all to me. I'm running out of ideas.
(a)
(b)
For the people who has found this question relevant:
The problem was in the server-side. Server was rejecting some "unknown" sources of incoming traffic.
Maybe add ?wsdl as postfix to the url: http://sample.com/webservice.asmx?wsdl

Wso2 esb: Edit the response of one rest API and send that response to another rest api

We have 2 rest APIs and have to edit and send the one rest API response as a request message to the another rest API.
Ex: First api response is
<?xml version="1.0"?>
<api>
<attribute_set_id>4</attribute_set_id>
<type_id>simple</type_id>
<sku>test_dress</sku>
</api>
Now we have to edit that response as
<?xml version="1.0"?>
<api>
<id>4</id>
<product_id>simple</product_id>
<sku>test_dress</sku>
</api>
and send that response as a request message to the another rest api.
Is it possible through WSO2 ESB and please provide any sample to work for above scenario.
Please let me know if I need to give more details.
Thanks,
Geetha Gupta
Little sample you must customize to meet your needs :
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="MyProxy"
transports="http"
startOnLoad="true"
trace="disable">
<description/>
<target>
<inSequence>
<!-- send current message to rest api 1 (sample with a dynamic value inside rest url) -->
<property name="uri.var.MyPath" value="mypath"/>
<call>
<endpoint>
<http method="POST" uri-template="http://myhost/{uri.var.MyPath}/restapi1"/>
</endpoint>
</call>
<!-- here come the response from rest api 1 : transform this response -->
<payloadFactory media-type="xml">
<format>
<api>
<id>$1</id>
<product_id>$2</product_id>
<sku>$3</sku>
</api>
</format>
<args>
<arg evaluator="xml" expression="//api/attribute_set_id/text()"/>
<arg evaluator="xml" expression="//api/type_id/text()"/>
<arg evaluator="xml" expression="//api/sku/text()"/>
</args>
</payloadFactory>
<!-- call rest api 2 -->
<call>
<endpoint>
<http method="POST" uri-template="http://myhost/{uri.var.MyPath}/restapi2"/>
</endpoint>
</call>
<!-- send back response from restapi2 -->
<send/>
</inSequence>
<outSequence/>
</target>
</proxy>
What you need is the XSLT mediator (https://docs.wso2.com/display/ESB460/XSLT+Mediator).
Refer http://architects.dzone.com/articles/xslt-mediator-tutorial for an example for this.
You can do it through following steps
Read response and assign the response parameters to some variables by using <proprty>
Set dynamic payload with these parameters by using <payloadFactory>
After you can call another rest using <send> mediator

WSO2 ESB - How to get and set cookies in WSDL calls in

I use WSO2 ESB and want to connect 2 web services together - on a timer pull data from one service and push it to another.
The problem is that one of the services authenticates callers with cookies. You first need to call a GetSession method with the username and password. The response to this call sets a cookie. Then with this cookie you make other calls.
I couldn't find anywhere in the documentation, how can I get a cookie from the result of one call and set it for a subsequent call. Is it at all achievable? If so - how?
Here is my sequence code:
<sequence xmlns="http://ws.apache.org/ns/synapse" name="SampleName" trace="enable">
<payloadFactory media-type="xml">
<format>
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
<GetSessionWithCredentials xmlns="blabla">
<User>bla</User>
<Password>bla</Password>
</GetSessionWithCredentials>
</Body>
</Envelope>
</format>
</payloadFactory>
<call>
<endpoint key="conf:/Tracker"></endpoint>
</call>
<payloadFactory media-type="xml">
<format>
<GetTrackingList xmlns="blabla"></GetTrackingList>
</format>
</payloadFactory>
<property xmlns:ns="http://org.apache.synapse/xsd" name="Cookie" expression="$trp:Cookie"></property>
<call>
<endpoint key="conf:/Tracker"></endpoint>
</call>
<log level="full"></log>
</sequence>
Thanks a lot
get Cookie header : <property name="Cookie" expression="$trp:Cookie"/>
If you want to get one cookie and it's value, use xpath expression with 'substring' for exemple
set cookie header with value JSESSIONID=1 : <property name="Cookie" value="JSESSIONID=1" scope="transport"/>

how to integrate wso2 DSS server and wso2 ESB

My current scenario is that i have a web service exposed from data service which returns me email address of the user when i give it the name of the user. Now i want to use this web service in ESB and get the email id from this web service in a property and show it in console using LOG mediator.
What should i do now and how?
Sorry for this silly question but i am newest member of wso2 esb. So please help me on this.
Now ihave a response like:
<brs:getRecipientKeyResponse xmlns:brs="http://brs.carbon.wso2.org">
<brs:MailRecipient xsi:type="ax2338:MailRecipient" xmlns:ax2338="http://email.samples/xsd" xmlns:ax2337="http://email.samples/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ax2337:recipient>kevin</ax2337:recipient>
</brs:MailRecipient>
</brs:getRecipientKeyResponse>
Ihave to get the recipient element from this response and put this in payload. My complete sequence for this is:
<sequence xmlns="http://ws.apache.org/ns/synapse" name="receiveSeq">
<log>
<property name="getRecipient" value="------------Trying to get data Fom BRS Response----------------------------"/>
<property xmlns:ns="http://org.apache.synapse/xsd" xmlns:ax2337="http://email.samples/xsd" name="Recipient" expression="//ax2337:recipient"/>
</log>
<payloadFactory>
<format>
<p:GetEmailDetails xmlns:p="http://ws.wso2.org/dataservice">
<xs:name xmlns:xs="http://ws.wso2.org/dataservice">$1</xs:name>
</p:GetEmailDetails>
</format>
<args>
<arg xmlns:ns="http://org.apache.synapse/xsd" xmlns:ax2337="http://email.samples/xsd" expression="//ax2337:recipient"/>
</args>
</payloadFactory>
<log>
<property xmlns:ns="http://org.apache.synapse/xsd" name="getName" expression="get-property('Recipient')"/>
</log>
<send receive="DBSeq">
<endpoint key="emailServiceEP"/>
</send>
</sequence>
<!--this part is not able to get data --->
<property xmlns:ns="http://org.apache.synapse/xsd" name="getName" expression="get-property('Recipient')"/>
u just use the the your wso2dss tryit service in that request side code copy into the payloadfactory insted of " ? " keep the $1 ,$2 ..like this and pass the argument below as per ur above order order is play a vital role for this response i think it will help for u
<payloadFactory>
<format>
<p:insert_emp_operation xmlns:p="http://ws.wso2.org/dataservice">
<xs:eno xmlns:xs="http://ws.wso2.org/dataservice">$1</xs:eno>
<xs:ename xmlns:xs="http://ws.wso2.org/dataservice">$2</xs:ename>
<xs:esal xmlns:xs="http://ws.wso2.org/dataservice">$3</xs:esal>
</p:insert_emp_operation>
</format>
<args>
<arg expression="get-property('eno')"/>
<arg expression="get-property('ename')"/>
<arg expression="get-property('esal')"/>
</args>
</payloadFactory>
<send receive="Error_Seq">
<endpoint>
<address uri="http://localhost:9764/services/emp_DataService/" format="soap11"/>
</endpoint>
</send>
Since you have the Dataservice is implemented, give that as endpoint url to your proxy which can be created in wso2esb. When you send request to your proxy,in the outsequence, you will receive the response of your dataservice. Just do a log with "level=full" you will see the full response. Use the property mediator and do an xpath to pick the value which you needed.
Sample conf:
<proxy name="StockQuoteProxy">
<target>
<endpoint>
<address uri="DS endpoint"/>
</endpoint>
<outSequence>
<log level="full">
<property name="email" expression="xpath from the email attribute in the rseponse"/>
</log>
<send/>
</outSequence>
</target>
</proxy>
Here is esb sample guide on how to create proxies;
http://docs.wso2.org/wiki/display/ESB460/Proxy+Service+Samples