Add XML node to WSO2 DSS and ESB response - wso2

I have WSO2 DSS set up to return data like this:
<Products>
<Product>
<SKU>12345678910123</SKU>
<Item>123456</Item>
<ItemName>My Product Name</ItemName>
</Product>
</Products>
If there is no data to return I simply get the following:
<Products xmlns="http://data.mydomain.com/wso2/Products/getSKUinfo"/>
I have WSO2 ESB Pass Through Proxy set up to connect to the above service. When I hit the service in a URL like esbhost.domain.com/services/Products/SKU/12345678910123 I get the above. What I'd like to do is add a "Status" node (or whatever it's called) to essentially return this:
<Products>
<Status>1</Status>
<Product>
<SKU>12345678910123</SKU>
<Item>123456</Item>
<ItemName>My Product Name</ItemName>
</Product>
</Products>
If there isn't a match/no data to return I'd like the response to be:
<Products>
<Status>0</Status>
</Products>
Is this possible to do in WSO2 ESB? Or do I need to add something in WSO2 DSS service?
Please pardon me if I'm not using the right terminology.
Thanks,
Jared

Yes it can be done in the ESB. Basically in the "outSequence" of that proxy service, you can simply use the filter mediator, to check payload using XPath to check whether there isn't a match or no data is there. So from the filter mediator, in their separate paths, you can build up a message using the enrich mediator and add the necessary elements you need, like the 0 and so on. So basically in the filter mediator "true" path, you can save the "Product" element in a property, and add it later to the last created element in the message body using enrich. You may want to check out the ESB samples to get a feeling on how these mediators work.
Cheers,
Anjana.

Related

wso2 esb: Construct one message from multiple web service calls

I have a number of web services each returning a list of user ids as follows:
<application name="abc">
<users>
<id>123</id>
<id>456</id>
<id>789</id>
</users>
</application>
I need to be able to
Call a proxy service with a specific id (for example 123);
Call each webservice and search for the ID;
Create a response for each webservice and finally
Aggregate all responses in one message which is sent to the client as follows:
<response>
<id>123</id>
<application name="abc">
found
</application>
<application name="lmn">
not found
</application>
<application name="xyz">
found
</application>
</response>
Its probably a mix of service chaining and aggregate, but I cannot figure out how to do it. I tried cloning a request and using send at the end with a receiving sequence which transforms the body using the payload factory. In the Out sequence I then used aggregate to combine the new messages. However it times out and I don't think it's a matter of timing. My main issue is how to create a new message from each webservice response the aggregate mediator can combine them.
Any help is appreciated.
Thanks
You need to follow this pattern, https://docs.wso2.com/display/IntegrationPatterns/Scatter-Gather and you are almost there. When you define receive sequence the response will be forwareded to that sequence and you wouldn't get the response message in outSequence. Use aggregator mediator inside the outSequence and Combine the responses rather than defining a receive sequence.
Once you aggregate the responses, you can use xslt mediator to transform the message.
I managed to solve my issue by creating a proxy service for each web servive. Each proxy service calls the actual web services and uses a filter in the out sequence to create a response likes this:
<application name="abc">
found
</application>
Then I created a REST API which takes the idno as a URI template. I then prepare a payload with this idno and clone the request to the proxy services I mentioned above. Then I aggregate the responses and add the idno in the payload.
If anybody has any questions let me know.

How to split the message and receive element into WSO2 ESB 4.7.0

Hi i am receiving the file from remote server which in .csv format into wso2 ESB proxy service and i am getting the response as
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><text xmlns="http://ws.apache.org/commons/ns/payload">firstname,lastname
a,John
b,Scott
c,Tiger
d,Manager
</text></soapenv:Body></soapenv:Envelope>
but how can i split this message element by element and receive each element into ESB Proxy service.
Guide me how can i receive the split message into ESB and store into database.
I guess, you can use smooks mediator and convert CSV data to XML. Then data can be retrieved using XPath. There is a Stackoverflow question on this, Please go through it. Also you can use db report mediator to persist data in to preferred database. You find doc from here. One you convert data to XML, then you can use XPath to define the data that must be persisted in to database. However, you can even write a custom medaitor for ESB, if you want to do some more things than that. Here it explains how to write a custom mediator

Adding a header to a XML message in Synapse

Using Synapse 2.1, I am trying to transform an XML message with no header into a SOAP message with a header containing credentials to consume a web service. Something like this:
Synapse incoming message:
<SOAP-ENV:Envelope>
<SOAP-ENV:Body>
...TAGS...
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Synapse outgoing message:
<SOAP-ENV:Envelope>
<SOAP-ENV:Header>
<yta:Authentication>
<yta:UserName>srnm</yta:UserName>
<yta:Password>psswrd</yta:Password>
</yta:Authentication>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
...TAGS...
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
How could I configure Synapse to do it? I am successfully using a transform file to update the body of the message, but not to add a header to the output.
I tried using the header and property mediators in the configuration file, but I am not sure what is the way to go. Reading about the header mediator it says "At the moment set header only supports simple valued headers". Could this be the case?
Thanks
For the record, I ended up using a script mediator with an inline javascript script in the configuration file using the addHeader method. See below:
<script language="js">
<![CDATA[
var user = mc.getPayloadXML()..*::UserName.toString();
var psswd = mc.getPayloadXML()..*::Password.toString();
mc.addHeader(false, <yta:Authentication xmlns:yta="yta:namespace url"><yta:UserName>{user}</yta:UserName><yta:Password>{psswd}</yta:Password></yta:Authentication>);
]]>
</script>
You can use XSLT mediator to manipulate it. So add an XSLT transformation with required headers and it would add required headers. Or use Script mediator / Class mediator where you can manipulate message.
Please refer followings which will be useful.
http://wso2.org/forum/thread/10794
http://wso2.org/forum/thread/10843
If this xml structure is not needed.
you can use Http Headers you can use properties as below.
http://blog.thilinamb.com/2011/04/how-to-access-web-service-using-http.html
Looks like you want to secure the service. The easiest then, is to use username-token security. Go to the services dashboard in WSO2 ESB for your proxy service and secure it, using UT. Also see http://docs.wso2.org/wiki/display/ESB460/Sample+200%3A+Using+WS-Security+with+policy+attachments+for+proxy+services for a security sample

WSO2 ESB custom properties for Smooks mediator

I need to use some configuration settings to transform message with Smooks mediator. For example I want to inject a base URL into attribute value of outgoing xml during transformation.
In Java I'd do it by adding beans to ExecutionContext. Looking at SmooksMediator code I do not see this. Can I do it somehow or I should extend and recompile SmooksMediator to supply properties form MessageContext?
For the input as the Smooks mediator we can feed only one stream from the ESB. So if you want to transform a message by injecting a property, you can't achieve it with the smooks mediator.
Use XSLT mediator for this [1]. When configuring the XSLT mediator you can define properties to be passed into the transformation.
ex:
<xslt key="orderTransformer">
<property expression="get-property('name')" name="name"/>
<property expression="get-property('email')" name="email"/>
</xslt>
Then inside the XSLT you can define the two properties as below,
<xsl:param name="email"/>
<xsl:param name="name"/>
and use them appropraitely as $email and $name in templates.
<ns1:email>
<xsl:value-of select="$email"/>
</ns1:email>
<ns1:name>
<xsl:value-of select="$name"/>
</ns1:name>
[1] http://docs.wso2.org/wiki/display/ESB460/XSLT+Mediator
The whole configuration details of Smooks mediator can be found from [1].
Else you can go for a custom mediator to perform your exact task. Detail on custom mediator can be found from [2].
[1]. http://wso2.org/library/tutorials/2011/06/perform-data-mapping-smooks-editor-wso2-carbon-studio
[2]. http://maninda.blogspot.com/2012/11/writing-custom-mediator-for-wso2-esb.html
Thank you,
Dharshana

WSO2 ESB - How to build soapenv:Body for remote service call

I have the WSDL file of the remote web service I need to call from a proxy service in the WSO2 ESB and would like to know if I need to construct the soap:Body's elements manually through XSLT/Enrich or there is a way to generate the soapenv:Body's contains from the WSDL and maybe replace '?' for the values.
For example, if you've used soapUI before you'll know that when you import a WSDL file in a project a soapenv:Envelope gets generated automatically with all the XML elements and question marks for their values. Same goes for the TryIt tool in the WSO2 ESB.
Here is an example of an auto-generated soapenv:Envelope in soapUI after importing WSDL:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:abc="http://abc.com/">
<soapenv:Header/>
<soapenv:Body>
<abc:RegisterCandidate>
<abc:NameFirst>?</abc:NameFirst>
<abc:NameMiddle>?</abc:NameMiddle>
<abc:NameLast>?</abc:NameLast>
<abc:PhoneHome>?</abc:PhoneHome>
<abc:EmailAddress>?</abc:EmailAddress>
<abc:Address1>?</abc:Address1>
<abc:Address2>?</abc:Address2>
<abc:City>?</abc:City>
<abc:State>?</abc:State>
<abc:ZipCode>?</abc:ZipCode>
<abc:Country>?</abc:Country>
</abc:RegisterCandidate>
</soapenv:Body>
</soapenv:Envelope>
Is this possible in the Proxy Service through any of the mediators available to read a WSDL and generate soapenv:Body with its XML tags (in the code above it would be abc:RegisterCandidate with its children)? I've done it with the use of the XSL templates, but it's manual and not very elegant.
I've found a few articles/blogs online about writing proxy services in the WSO2 ESB that call remote web services and what the developers were doing in there was to insert the XML elements needed in the soapenv:Body with the use of XSL templates to have the correct/full SOAP message that is then sent (send mediator) to the remote web service server.
Thank you.
There is no way to generate the soap body from the remote service's wsdl as in your requirement. But there is an easier way than using xslt. That is to use the payload factory mediator. You can define the payload and assign values using xpath as shown in the sample.