WSO2 ESB custom properties for Smooks mediator - wso2

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

Related

wso2esb: How to pass the parameters to the callout mediator?

Data service SOAP operation has one parameter.
How do I pass this parameter to the callout mediator?
Callout source code:
<?xml version="1.0" encoding="UTF-8"?>
<callout action="urn:getVendorsOperation"
serviceURL="http://...:8280/services/myService/" xmlns="http://ws.apache.org/ns/synapse">
<source type="envelope"/>
<target key="response"/>
</callout>
the parameter:
<?xml version="1.0" encoding="UTF-8"?>
<property expression="get-property('uri.var.filterQuery')"
name="filterQuery" scope="default" type="STRING" xmlns="http://ws.apache.org/ns/synapse"/>
UPD:
WSDL FILE: http://...:8280/services/myService?wsdl
https://drive.google.com/file/d/1JLpIhHO_Jbk2bSWNodudnyG_X8fXUuCy/view?usp=sharing
When you have Data Services - it creates webservice for which you should create appropriate request. In body / payload you use that parameter to get data back from request. What should the request look like? You can see by using wsdl. http://...:8280/services/myService?wsdl and there can be handy tool like SoapUI for generate body of that request. If you are using WSO2EI i recommend use DSS in local transport, it has better performance and not exposing it through http.
The documentation is not recommend using calloutMediator and it say is better to use callMediator.

WSO2 EI - How to parametrize OAuth2 default mediator

I am using the OAuth2 mediator in WSO2 EI 6.6 and I would like to parametrize this mediator parameteres: user, pass, url. I fail to set this values from properties.
Here is the xml of this mediator.
<property name="ISurl" value="https://localhost:9445/services/"/>
<property name="ISuser" value="admin"/>
<property name="ISpass" value="admin"/>
<oauthService remoteServiceUrl="$ctx:ISurl/" username="$ISuser" password="$ctx:ISpass"/>
The error shown is
Caused by: org.apache.axis2.AxisFault: The system cannot infer the transport information from the $ctx:ISurl/OAuth2TokenValidationService URL.
at org.apache.axis2.description.ClientUtils.inferOutTransport(ClientUtils.java:86)
Is there a way to parametrize this parameters to avoid options like code customizations at compiling time?
Since this is a class mediator implementation, it should have expression support[1]. Hence you should be able to parameterize this parameters using runtime properties. Something like below.
<class name="org.soasecurity.oauth2.scope.validator.OAuth2ScopeMediator">
<property expression="$ctx:username" value="user"></property>
</class>
[1] - https://docs.wso2.com/display/EI660/Class+Mediator

How to use WSO2 ESB to mediate fixed length text data

I have the needs to use WSO2 ESB to mediate the incoming fixed length text data as received via MQ, one line of text per message, into XML format and then send the transformed data onto an SOAP endpoint via HTTP.
I understand that I could use WSO2 ESB admin console to configure an InSequence to do the data parsing and mapping with substring function and then set up a proxy service to include this configured sequenece.
I would need helps about how to do all these in details in steps. Greatly appreciate if someone could provide some examples or links to some webpages about how-to.
Thanks!
You should have a look to smooks, I think this is the best solution :
CSV : http://wso2.com/library/blog-post/2013/09/csv-to-xml-transformation-with-wso2-esb-smooks-mediator/
Fixed Lengh text : http://vvratha.blogspot.fr/2014/05/processing-large-text-file-using-smooks.html
An other solution would be to write your own messageBuilder, search "org.apache.axis2.format.PlainTextBuilder" to find the source code...
I agree with Jean-Michel, that smooks would be a good solution. But, this is also possible to do within a single simple proxy service. Set up a simple pass-through proxy to your endpoint. Then, open it up in source view (or the wizard) and configure the insequence to add a PayloadMediator.
Here is an example of how to use Payload Mediator [1]
Here is an excerpted example of what that would look like with a few xpath expressions to extract fixed-length fields from your input:
<payloadFactory media-type="xml">
<format>
<m:body xmlns:m="http://services.samples">
<m:field1>$1</m:field1>
<m:field2>$2</m:field2>
</m:body>
</format>
<args>
<arg expression="substring(//*,0,10)"/>
<arg expression="substring(//*,10,10)"/>
</args>
</payloadFactory>
You may also need to use the content type property in your sequence, because you're changing the content type to xml:
<property name="ContentType" value="text/xml" scope="axis2"/>
Best of luck!
[1] https://docs.wso2.com/pages/viewpage.action?pageId=33136018
For those who are interested in a working solution, here is my smooks configuration:
<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:fl="http://www.milyn.org/xsd/smooks/fixed-length-1.3.xsd">
<fl:reader fields="price[5]?trim,quantity[5]?trim,symbol[5]?trim.upper_case,comment[10]?trim" recordElementName="order">
<fl:listBinding beanId="order" class="test.Order" />
</fl:reader>
</smooks-resource-list>
Also, need to add the jar file of test.Order to the classpath of WSO2 ESB.

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

Add XML node to WSO2 DSS and ESB response

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.