WSO2 ESB can not response json message - json

I have a service response is :
<Response>
<BackUrl>www.hao123.com</url>
<resultCode>0</resultCode>
</Response>
I set messageType:"application/json". The response is:
{Response:{"BackUrl":"coship.com","resultCode":"0";}}.
But I need transform this message to a json string like:
{"BackUrl":"http://www.coship.com","resultCode":"0"}
How ro remove node "Response". I tried to use script mc.setPayloadJSON in ESB 4.5.1. But errors happened.
https://wso2.org/jira/browse/ESBJAVA-1618?page=com.atlassian.jira.plugin.system.issuetabpanels%3Aall-tabpanel#issue-tabs
Anyone can help me? Thank you very much.

There is an issue reported as mentioned, and it is not resolved yet. But, you can set messgae type as
<property name="messageType" value="application/json" scope="axis2"/>
and check?
here is a post which may useful to you

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

How to host and transform SOAP message in WSO2

I am relative new in WSO2 to create services, and i have to do a project in the enterprise where i work. First i had to take a message in, for example, json and send xml. I was able to do so, with this code:
<?xml version="1.0" encoding="UTF-8"?>
<api context="/Test" name="JsonToXmlApi" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST" uri-template="/xml/">
<inSequence>
<property name="messageType" scope="axis2" type="STRING" value="text/xml"/>
<header action="remove" name="To" scope="default"/>
<property name="RESPONSE" scope="default" type="STRING" value="true"/>
<send/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
Now i need to do the following, i have to send a message in SOAP, XML format, consume it in SOAP and receive back with JSON or XML format.
Someone could help me? I am asking because i am searching for tutorials, for beginners, about it for more than a week, and wasn't able to solve my problem.
Another question, would be possible to do this in REST API project?
If someone could help me, with a detailed tutorial or good video teaching that, i would be grateful.
Thank you.
Your post is a bit unclear but I will try to summarise the typical common combinations of input interface, wso2 component and backend services when you are developing web services.
Web services are services you can invoke over http or https - you might have three types of interfaces for these and you might use two types of WSO2 ESB components - proxy and API - to develop these.
1) Input/Output: JSON
WSO2 component: API
Backend: Any (JSon/XML/SOAP)
2) Input/Output: Plain XML
WSO2 component: API
Backend: Any (JSon/XML/SOAP)
3) Input/Output: SOAP
WSO2 component: Proxy
Backend: Any (JSon/XML/SOAP)
One of the key components of any SOAP service is the WSDL - the WSO2 proxy allows you to publish a WSDL so that your caller knows what sort of data and operations your service provides.
See more here: https://docs.wso2.com/display/EI611/Working+with+Proxy+Services
Adding this note only because you asked:
Technically, you might be able to receive a SOAP message in an API component because SOAP is just an XML document - but it would be really pointless. The purpose of the API component is to expose resources and allow you to perform HTTP operations (GET, POST etc) on those resources (this model can be called REST).
The purpose of a SOAP based web service is to expose data and operations as defined in a WSDL and the WSO2 proxy component is what allows you to do that.

How to JsonPost to a WebService target in Nlog

With Nlog 4.4.5 am trying to use protocol JsonPost of WebService target to post logs to slack. Slack webhooks need Json data over https post (https://api.slack.com/incoming-webhooks)
<target xsi:type="WebService"
name="slack"
url="https://hooks.slack.com/services/T*****/B4T****/7F********XDv9h/"
protocol="JsonPost"
encoding="utf-8"
>
<parameter name="text" type="System.String" layout="${date:format=yyyy-MM-dd HH\:mm\:ss.fff}: ${level} - ${message}"/>
</target>
It does not post anything to my slack channel created for the webhook whereas I see the other targets in my Nlog.config are called and log files are populated with new data.
Any idea where I should look into to sort it out?
The problem was with the ending "/" in the Slack webhook url. Removed and it started posting logs to my slack channel.
Maybe something like this answer:
https://github.com/NLog/NLog/issues/1958
While waiting for this issue to be resolved (Using JSON-Layout directly):
https://github.com/NLog/NLog/issues/1905

JSONProvider transform elements to attributes (cxf)

We are building a REST service with CXF where we send and receive data in both JSON and XML
In the JSON version we dont want to bother our clients with using the '#' notation for attributes and using the "attributesToElements" parameter on the JsonProvider (org.apache.cxf.jaxrs.provider.json.JSONProvider) ensures we dont send these attributes to the client that way.
However when receiving the data back (same format) the JsonProvider is unable to UnMarshall the received data correctly because it doesnt realize the attribute values are attributes (and treats them as elements instead).
In issue 3475 of the cxf bug tracker this problem is acknowledged and marked resolved because its a a problem to handle on the receiving end. there is being referred to using the transformation feature of the Jsonprovider, however i cant seem to get it to work.
i tried to configure the transform in beans.xml with several variations like:
<property name="inTransformElements">
<map>
<entry key="fieldname" value="#fieldname"/>
<entry key="fieldname" value="{#}fieldname"/>
</map>
</property>
but with no succes.
Is it possible with the cxf JsonProvider to transform elements back to attributes?
And if so how?
JUst to understand the question,
XML shown above is the output
<property name="inTransformElements">
<map>
<entry key="fieldname" value="#fieldname"/>
<entry key="fieldname" value="{#}fieldname"/>
</map>
</property>
Did you try with latest version of CXF?

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>