PayloadFactory - Enrich with Response - wso2

I make a callout that response is like this:
<ns:return>
<ax2446:contract>
<ax2446:array>variable1</ax2446:array>
<ax2446:array>value1</ax2446:array>
</ax2446:contract>
<ax2446:contract>
<ax2446:array>variable2</ax2446:array>
<ax2446:array>value2</ax2446:array>
</ax2446:contract>
<ax2446:contract>
<ax2446:array>variable3</ax2446:array>
<ax2446:array>value3</ax2446:array>
</ax2446:contract>
<ax2446:documents>
<ax2446:array>attachement1</ax2446:array>
<ax2446:array>D:\AUTO\filename-1.txt</ax2446:array>
</ax2446:documents>
<ax2446:documents>
<ax2446:array>attachment2</ax2446:array>
<ax2446:array>D:\AUTO\filename-2.txt</ax2446:array>
</ax2446:documents>
<ax2446:process>TEST_PROCESS</ax2446:modulo>
</ns:return>
i want to create a new payload with contracts values (the number can change)
<p:instantiateProcess xmlns:p="http://carbon.sample">
<xs:process xmlns:xs="http://carbon.sample">TEST_PROCESS</xs:process>
<ns:contract xmlns:ns="http://carbon.sample">
<xs:array xmlns:xs="http://carbon.sample">variable1</xs:array>
<xs:array xmlns:xs="http://carbon.sample">value1</xs:array>
</ns:contract>
<ns:contract xmlns:ns="http://carbon.sample">
<xs:array xmlns:xs="http://carbon.sample">variable2</xs:array>
<xs:array xmlns:xs="http://carbon.sample">value2</xs:array>
</ns:contract>
<ns:contract xmlns:ns="http://carbon.sample">
<xs:array xmlns:xs="http://carbon.sample">variable3</xs:array>
<xs:array xmlns:xs="http://carbon.sample">value3</xs:array>
</ns:contract>
</p:instantiateProcess>ยด
i get all the contract by:
<property
expression="//ns:return/ax2431:contract"
name="contract" scope="default" type="STRING"
xmlns:ax2431="http://vo.carbon.sample/xsd" xmlns:ns="http://carbon.sample"/>
I try with payloadfactory, by i dont know how to enrinch (if it is correct to use this mediator) the message to make a call to a new web service. or if i have to make the payload with an iterator...
can anybody help me? thanks

You appear to have a few options:
Use a Payload factory
Use XSLT
Use Script meditator
I suspect all 3 could get the result you are after. It is a question of which you feel most comfortable with. Personally, I would head down the Script mediator route, mapping between two XML payloads, here is an example: http://nimbleapi.com/2016/05/javascript-mapping-between-xml-payloads/

Once you get the contract list you can use iterate mediator to iterate the list and inside the iterator you can add payloadFactory mediator to create payload, then, same time you can send the modified payload to a new web service.
Please find the following links which explain in detail how you use iterate mediator.
http://sparkletechthoughts.blogspot.com/2012/08/how-to-use-iterator-mediator-to-iterate.html

In addition to XSLT or Script meditators, ForEach mediator is also an option.
https://docs.wso2.com/display/ESB490/ForEach+Mediator

Related

Store Value returned from Java Class in Class Mediator

In my WSO2 ESB, I have a created Java class, which produces a string value as output. Now I am calling my Java class with "Class Mediator". How can I store the value retured from my Java class in my WSO2 ESB and show that as log in Log mediator?
You can save values in property in class mediator as below,
public boolean mediate(MessageContext context) {
context.setProperty("testKey","Value in class mediator");
return true;
}
and access the stored values as below,
<log>
<property name="test log" expression="$ctx:testKey"/>
</log>
You need to set the property in your java class for example context.setProperty("propertyName","PropertyValue")
Now as the property is set in your java class then you can use it anywhere in your sequence by using the expression $ctx:propertyName or try using get-property(propertyName) both would fetch the value but $ctx is performance is faster when compared to get-property

Wanted to remove property name while printing the log in WSO2

Here I have one doubt
I am creating log mediator in WSO2 and setting one property msg and it gives me output as follows
[2017-08-04 18:13:10,041] [] INFO - LogMediator Msg = Msg Coming************************
Here I just want the output like Msg Coming************************ I dont want to print property name...
How I will able to do it..
Example
<property name="*******************" value="hello" />
or
<property name="*******************" expression="translate('some string to log', 'abcdefg.....z', '****************************')" />
In first case you will get unknown property, in second unkond property with unknown value. Why you need such logging

Dynamic custom mediator parametres in wso2 ESB

I built custom mediator using articles http://wso2.com/library/2898/ and http://wso2.com/library/2936/ .
It looks like this:
<inSequence>
<property expression="local-name($body/*[1])" name="method" scope="default" type="STRING"/>
<MyMediator>
<header>SomeHeader<header/>
<topic>SomeTopic</topic>
</MyMediator>
</inSequence>
It works well, but one issue I didn't solve, how I can dynamically change value of header and/or topic mediators parameter?
When I tried to put something like get-property('method') instead of SomeHeader. It was delivered into mediator code like string, ebs didn't calculate it.
My question is how to change value of my custom mediators parameter to property value?
You can choose for exemple to use {} braces to say that your string is an XPath request. In your mediator, if you find {} in first and last position, use org.apache.synapse.util.xpath.SynapseXPath : create a new SynapseXPath with your xpath (the string between {}) and use stringValueOf(theMessageContext)
You can't pass any dynamic value to a custom mediator, but you can read any property within custom mediators.
public boolean mediate(MessageContext mc) {
String value = (String) mc.getProperty("SomeHeader");
}
Current implementation doesn't support for expressions as class mediator properties. Please refer Jira for more details.

Create sub-tags in Matlab Soap request

I am developing a SOAP client in Matlab for connection a Web Service. What I am doing is the following script:
createClassFromWsdl('http://192.168.107.239/WSDL/v4.0/iLON100.wsdl')
obj = iLON100
methods(obj)
With the next result:
Methods for class iLON100:
Clear Get List Set display
Delete InvokeCmd Read Write iLON100
Then, I am editing for example the method List in order to request the list of Items for the service. The dot m file is:
% Build up the argument lists.
values = { '','//Item[#xsi:type="Dp_Cfg"]'};
names = { 'iLonItem','xSelect'};
types = {};
% Create the message, make the call, and convert the response into a variable.
soapMessage = createSoapMessage('http://wsdl.echelon.com/web_services_ns/ilon100/v4.0/message/',
'List', values, names, types, 'document');
I have also a SOAP tester from the vendor of the device. Then, if I compare both XML requests, they differ as you can see in the next example (firstly the original request and secondly the Matlab one):
<SOAP-ENV:Body>
<List xmlns="http://wsdl.echelon.com/web_services_ns/ilon100/v4.0/message/">
<iLonItem>
<xSelect>
//Item[#xsi:type="Dp_Cfg"]
</xSelect>
</iLonItem>
</List>
</SOAP-ENV:Body>
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<List xmlns="http://wsdl.echelon.com/web_services_ns/ilon100/v4.0/message/">
<iLonItem/>
<xSelect>Item</xSelect>
</List>
</soap:Body>
As you can observe, the tags are not included as sub-tags. I would like to know how to do it and generate the same structure of XML for sending the SOAP request correctly.
Thank you so much,
At the end, I have solved it by creating a structure as follows:
myStruct = struct('iLonItem',struct('xSelect','//Item[#xsi:type="Dp_Cfg"]'))
This structure is inserted into "values" and the method's name "iLONItem".

Mule - Pass Parameters to datamapper and access them in xpath conditions

How to pass parameters to datamapper in mule and access them. (In XSLT, I pass them as context parameters, receive them in param and access using $ symbol). I need to do the same thing in datamapper. Any suggestions/links/example are appreciated.
Approach1:
We are using invokeTransformer method in datamapper
output.abc= invokeTransformer("MyTransformer",input.abcdef);
This MyTransformer is a java component which has this default method overridden.
#Override
public String transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
System.out.println("Inside transformer" +message.getProperty ("sessionVariable1",PropertyScope.SESSION));
return message.getProperty("sessionVariable1",PropertyScope.SESSION);
But, the problem is I am not calling this transformer from mule flow. But, invoking it from datamapper. Hence the argument 'message' does not get passed. So, Unable to retrive that session variable to return to datamapper. Is there a way to send this argument(MuleMessage from datamapper)?
You can use input arguments with DataMapper and then refer to them in the output:
<set-variable variableName="testvar" value="value of testvar"/>
<data-mapper:transform config-ref="new_mapping_grf"">
<data-mapper:input-arguments>
<data-mapper:input-argument key="testvar">#[flowVars['testvar']]</data-mapper:input-argument>
</data-mapper:input-arguments>
</data-mapper:transform>
and
output.myField = invokeTransformer("MyTransformer",inputArguments.testvar);
or
output.myField = inputArguments.testvar;
Adding input arguments available in the DataMapper GUI through the input side green plus icon.