Remove Soap Envelope from the $body before using Send mediator in WSO2 - json

My API is receiving a XML with a field that repeats itself a couple of times, looking like this:
<Root>
<Element>
<Request>username=username&password=password&field3=field3&field4=field4</Request>
<Request>username=username&password=password&field3=field3&field4=field4</Request>
</Element>
</Root>
Using an IterateMediator I am extracting those Request fields and make separate calls with each of them to an URL, with the help of an AggregatorMediator and aSendMediator.
The API I'm calling will only accept only one line at a time, along with the following metadata:
HTTP method: POST
Content-Type: application/x-www-form-urlencoded
Payload: username=username&password=password&field3=field3&field4=field4
The problem is that, although I extract the payload properly from the IterateMediator, the value of the $body before reaching the SendMediator is surrounded with Soap Envelope tags. I've tried using a PayloadFactoryMediator to change the value of the $body, but I can only make it xml or json, and not text/plain.
Do you have any idea how can I make the body something else than xml or json?

Could you show your sequence?
"..but I can only make it xml or json, and not text/plain.."
Did you try media-type="text" ?
<payloadFactory media-type="text" xmlns="http://ws.apache.org/ns/synapse">
<format>$1</format>
</payloadFactory>

Related

wso2 Transform XML to Json inside sequence

I receive from an rabbitmq inbound an XML message and I have to send it to mongodb with restheart.
I must modify the content of the Json generate by the formatter before sending it to the mongo rest api, because I must modify a datetime format send in string to add "ISOdate ()" for mongodb.
My incomming message can be different from other messages.
For exemple, I have this from inbound :
<CONTENT><CODE_USER>100</CODE_USER><DATE>2017-12-12</DATE></CONTENT>
and I get this for the endpoint,
{"CONTENT" : {"CODE_USER":100","DATE":"2017-12-12"}}
So I want to modify the Json format before sending to the endpoint to modify the date string.
Is there a way to trigger the esb Json formatter and get the result before send it to the endpoint ?
Thanks,
Nicolas
I find an solution,
I use json-eval($.*.) function and an script mediator to modify the date format before sending to the endpoint, like this :
<property expression="json-eval($.*.)" name="location" scope="default" type="STRING"/>
<script language="js"><![CDATA[var message = new String(mc.getProperty('location'));
var reg = /\"((\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)\.(\d{1,3})Z)\"/g;
var mess=message.replace (reg,'{\"$date\" : \"\$1\"}');
mess=mess.substring (1,mess.length-1);
mc.setPayloadJSON(mess);]]></script>
<property name="ContentType" scope="axis2" type="STRING" value="application/json"/>
Yes that is possible. You can add a mediator to the in-sequence such that the transformation happens before releasing the response to mongo-db.
Please check the available mediators and if there is no any suitable one for your requirement, you can simply write your own mediator by extending org.apache.synapse.mediators.AbstractMediator
For the details on how to write a custom mediator, you can refer the blog: https://medium.com/#nirothipanram/esb-few-tips-in-writing-a-custom-class-mediator-b9a322f4eaa8

Converting SOAP Response XML to JSON using XSJS

I'm having problems to convert an XML response file to JSON using XSJS, Any help will be appreciated. Thanks!
It is actually pretty simple. While sending the request using XSJS to the server...your URL path should contain the format specifier.
http://myhost:8000/exampleapp/resource.xsjs/some/more/options?$format=JSON
or if you do not want to append to the URL then include the following line your request header
accept: application/json

Converting map payoad to json payload in mule

I have output from my data weaver is a map like below
[{"LNUMBER":9192428,"INUMBER":005RPS}]
But while sending this payload to api I want to have a json payload without [] like below
{"LNUMBER":9192428,"INUMBER":005RPS}
What is the best way I can achieve this in mule.
Assuming you cannot do it in your dataweave trasformation you can simply put in payload the first element of the map and than use object to json transformer:
<set-payload value="#[payload[0]]" doc:name="Set Payload"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
Off course this will work only for the case you have a map containing always only one element.
I would aniway suggest you to see if you can do it directly in dataweave it will probally make more sense.
hi please add application/json in the palce of application/java it will convert all your data into json paylaod
inside transform message %dw 1.0
%output application/java to json

stWSO2ESB OutSequence Processing

I'm transforming XML request to SOAP via XSLT in WSO2ESB, just wondering is it possible to make request parameter available to be used in response?
E.g.
<request>
<test>123</test>
<param1>testing</param1>
</request>
-> converted to SOAP
<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">#S:Body><ns2:testrequest xmlns:ns2="http://xml.testing.com/test"><teststring>testing</teststring></ns2:testrequest></S:Body></S:Envelope></soapenv:Body></soapenv:Envelope>
In the response
<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:testresponse xmlns:ns2="http://xml.testing.com/test"><responsestring>success</responsestring></ns2:testresponse></S:Body></S:Envelope></soapenv:Body></soapenv:Envelope>
I want to return in XML
<responsestring>
<test>123</test>
<return1>success</return1>
</responsestring>
As you see, 123 isn't send to the server and has not received from the server. However, client is sending this parameter and i would like to just use this parameter in request and send back in response, is this possible? By how? I'm very new to synapse and very new to WSO2ESB, could anyone please enlighten me?
Thanks.
Yes it is possible. You can use property mediator in the Insequence to set the required value as a property and then add it in the outsequence to response using enrich mediator.
Got it working now.
Simply by adding the property mediator in both insequence and outsequence together with the XSLT, where the xslt is trying to get the value from test property. That's it!
Insequence
<property xmlns:ns="http://org.apache.synapse/xsd" name="TEST" expression="//request/*[local-name()=test]" scope="default"/>
outsequence
<xslt key="xxxx.xslt">
<property name="test" expression="get-property('TEST')"/>
</xslt>

Create and link XSD to a WADL

I am creating some services using JAX-RS that need to take in arbitrarily complex objects as arguments, not just primitives like integers and strings. A discussion on the CXF mailing list says to just use a wrapper object as a single parameter in this case.
My concern is how to document the input format to the service? If creating a service that looks something like the following:
#POST
#Produces("application/json")
#Consumes("application/json")
#Path("oneParam")
public ComplexObject2 myServiceMethod(ComplexObject1 obj) {
Foo f = obj.foo
Bar b = obj.bar
...
}
the auto-generated WADL that CXF produces will only produce the following:
<resource path="/oneParam">
<method name="POST">
<request>
<representation mediaType="application/json"/>
</request>
<response>
<representation mediaType="application/json"/>
</response>
</method>
</resource>
This contains no information on what the request or response actually contains. Sergey on the CXF mailing list said it was possible to link a schema to the representation, but how am I supposed to do that? And how do I create the XSD?
(P.S. Using POST for idempotent resources might not be RESTful, but it's not important here as we are in essence doing RPC using Json. This is more or less a 1:1 clone of an existing SOAP based api.)
It is possible to link an XSD file into a WADL file and then to reference an XML element in the representation for requests and responses. However, as it is XML schema it doesn't apply to JSON representations.
To link an XSD into a WADL file, create a grammars element at the top of the file before the main resources element.
<grammars>
<include href="myapp.xsd"/>
</grammars>
Then add a reference to an XML element as follows (using a modified version of your example):
<resource path="/oneParam">
<method name="POST">
<request>
<representation mediaType="application/xml" element="myapp:oneParamRequest" />
</request>
<response>
<representation mediaType="application/xml" element="myapp:oneParamResponse" />
</response>
</method>
</resource>
The prefix myapp is defined in the XSD and can be used in the WADL file as well.
I don't know to to configure CXF to do this automatically. My experience with Jersey is similar and we use the generated WADL as a starting point for hand-editing later.