How to convert the JSON in WSO2 ESB - json

I am trying to pass my JSON as a object to a WSO2 ESB.
I am getting JSON like this:
'{FIELDNAME":"NAME",FIELDVALUE:"KISHORE"}'
This JSON I need to pass my ESB as a dynamic column. But Its accepting like JSON {"NAME":"KISHORE"}. How can I convert from above JSON to below one in WSO2 ESB. If I get above one I am unable to pass to proxy. If I get below one I will pass like this (//name/child::text()) then I will get value as "KISHORE" I was tried with ENRICH mediator but its not working.
<proxy xmlns="http://ws.apache.org/ns/synapse" name="test_dynamic" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<enrich>
<source type="body" clone="true"/>
<target type="property" property="RM"/>
</enrich>
<property name="RM" expression="//fieldname/child::text()" scope="default" type="STRING"/>
<log level="custom">
<property name="r_no" expression="get-property('R_no')"/>
<property name="r_value" expression="get-property('R_value')"/>
<property name="emp_d" expression="get-property('emp')"/>
<property name="RM" expression="get-property('RM')"/>
</log>
<log level="full"/>
</inSequence>
</target>
<description></description>
</proxy>
ANS: request, Envelope:
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body><fieldname>e_no</fieldname><fieldvalue>5</fieldvalue></soapenv:Body>
</soapenv:Envelope>

I think you should send the JSON message like this:
'{"RECORD":{FIELDNAME":"NAME",FIELDVALUE:"KISHORE"}}'
then get the value by the "Property" mediator in WSO2 ESB, like
<property name="FIELDNAME" expression="//FIELDNAME/text()" scope="default" type="STRING"/>
<property name="FIELDVALUE" expression="//FIELDVALUE/text()" scope="default" type="STRING"/>

The link provided below by WSO2 explains the exact usecase which you are interested in solving
Steps:
Convert JSON to XML using the XSLT mediator
Use the Enrich mediator to replace the Body of the SOAP message
Follow this link for detailed info and actual proxy configuration
https://docs.wso2.com/display/ESB480/Sample+440%3A+Converting+JSON+to+XML+Using+XSLT

Related

Spring 4.0.5 -> 4.2.2 XML no longer valid - Endpoint no longer returning JSON

Scenario: Using gradle to update from Spring 4.0.5.RELEASE to 4.2.2.RELEASE causes an endpoint to return XML instead of JSON.
I have an endpoint on my server that returns a list of objects. Previously, I could hit this endpoint in my browser and view the JSON object in a nice format due to my JSONView extension. After upgrading Spring, however, that same endpoint now displays XML in the browser. However, when using js to make a request to the same endpoint, I am given JSON.
Is there something I need to change to revert to the old behavior while still maintaining the 4.2.2 release?
It seems to be stemming from this issue, which Spring's reaction to changed between the two versions: https://github.com/strongloop/strong-remoting/issues/118.
The following is my spring-api-servlet.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.XXX.spring" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="jacksonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter" />
</list>
</property>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000" />
</bean>
Not sure of the details but this is probably a content type negotiation thing. spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc has a high level description. In spring increments, spring will sometimes fiddle with or re-order message converters, meaning the one that "wins" is non-specific if the request accept header is broad.

How to convert SOAP response with xsi values to json in WSO2esb

I'm using wso2 esb 4.8.1 version for SOAP to rest conversion with as API. I have a soap request with xsi values. After I generate the proper soap request with script mediator and um getting an expected response. But I have an issue with response. Because I need t convert the soap response into json. When I tried with following out sequence um not getting the proper json response with axis2. How can I convert this soap response into json properly?
This it the soap response.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:easyDownloadResponse xmlns:ns1="http://usermanage.ivas.huawei.com" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<easyDownloadReturn href="#id0"/>
</ns1:easyDownloadResponse>
<multiRef xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://response.usermanage.ivas.huawei.com" id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:EasyDownloadResp">
<eventClassName xsi:type="xsd:string" xsi:nil="true"/>
<failedResources xsi:type="soapenc:Array" xsi:nil="true"/>
<operationID xsi:type="xsd:long">0</operationID>
<resultCode xsi:type="xsd:int">0</resultCode>
<resultInfo xsi:type="xsd:string" xsi:nil="true"/>
<returnCode xsi:type="xsd:string">000000</returnCode>
<toneTransactionID xsi:type="soapenc:Array" xsi:nil="true"/>
<transactionID xsi:type="xsd:string" xsi:nil="true"/>
</multiRef>
</soapenv:Body>
</soapenv:Envelope>
This is the response which i'm getting
{"easyDownloadResponse":{"#encodingStyle":"http://schemas.xmlsoap.org/soap/encoding/","easyDownloadReturn":{"#href":"#id0"}}}
This is my out sequence
<outSequence xmlns="http://ws.apache.org/ns/synapse">
<property name="messageType" value="application/json" scope="axis2" type="STRING"></property>
<send></send>
</outSequence>
All your answers are highly welcome.
Finally I found the solution for the issue. In the normal case we use
<property name="messageType" value="application/json" scope="axis2" type="STRING"></property>
But this axis2 property cannot convert the complex soap response into json such as with xsi.
For that need to use the following axis2 property. Then it converts the entire soap response into Json as we expected.
<property name="messageType" value="application/json/badgerfish" scope="axis2" type="STRING"></property>
This is my full outsequence.
<outSequence xmlns="http://ws.apache.org/ns/synapse">
<property name="messageType" value="application/json/badgerfish" scope="axis2" type="STRING"></property>
<send></send>
</outSequence>
You can try payload mediator to get exact json format 1. But still you may have to uncomment the following lines in
$ESB_HOME/repository/conf/axis2/axis2.xml
<!--messageFormatter contentType="application/json"
class="org.apache.axis2.json.JSONStreamFormatter"/-->
<!--messageBuilder contentType="application/json"
class="org.apache.axis2.json.JSONStreamBuilder"/-->
By default, JSON messages are converted to XML when they are received by the PayloadFactor mediator. However, if you enable the JSON stream formatter and builder, incoming JSON messages are left in JSON format.
And also you can use script mediator again (In outsequence) to modify your json response. Refer this sample

WSO2 ESB 4.5.1: SOAP to JSON transformation issues

So I´m trying to build a proxy based on a wsdl. This proxy will listen on hhtp/https and transform the incoming SOAP message into JSON sending it over JMS to the end-point. And of course the way back that is from JSON to SOAP needs to be considered as well.
Can somebody please help me how to do that. I´ve tried sample 440 on ESB 4.5.1 just switching the "in_transform" with "out_transform", also some other recipes found here in stackoverflow but none of them seems to do the job. In all cases I get an empty JMS message at the end-point.
Here´s a copy of my proxy
<proxy xmlns="http://ws.apache.org/ns/synapse" name="Tarjetas" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<log level="full"/>
<property name="messageType" value="application/json" scope="axis2"/>
</inSequence>
<outSequence>
<log level="full"/>
<xslt key="out_transform"/>
<property name="messageType" value="text/xml" scope="axis2"/>
<send/>
</outSequence>
<endpoint>
<address uri="jms:/FORTALEZA.BRIDGE.MOBILE.QUEUE?transport.jms.ConnectionFactoryJNDIName=QueueConnectionFactory&java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory&java.naming.provider.url=tcp://localhost:61616&transport.jms.DestinationType=queue"/>
</endpoint>
</target>
<publishWSDL uri="file:repository/wsdl/Debito.wsdl"/>
<description></description>
</proxy>
and here´s my localEntry for out_transform
<localEntry key="out_transform">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
xmlns:m0="http://services.samples"
version="2.0"
exclude-result-prefixes="m0 fn">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="*">
<xsl:element name="{local-name()}" namespace="http://services.samples">
<xsl:copy-of select="attribute::*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
</localEntry>
and I´m using the following within axis2.xml
<messageFormatter contentType="application/json"
class="org.apache.axis2.json.JSONStreamFormatter"/>
<messageBuilder contentType="application/json"
class="org.apache.axis2.json.JSONStreamBuilder"/>
I really appreciate all the help I can get.
You need to use the following message formatter and message builder in the axis2.xml
<messageFormatter contentType="application/json"
class="org.apache.axis2.json.JSONMessageFormatter"/>
<messageBuilder contentType="application/json"
class="org.apache.axis2.json.JSONBuilder"/>
You can use the StreamFormatter when you are building the JSON message using a script mediator. Otherwise the above formatter/builder would work for your requirement. You need to comment out the StreamFormatters which you have uncommented.

How can we convert from <a>fai</a><b>3</b> to <fai>3</fai> using xpath in WSO2 ESB

I am getting JSON like this
{"request":""AttributesCriteriaList":[
{"FieldName":"PartyBranchId","OperationType":1,"FieldValue":"-1500000000","JoinType":2},
{"FieldName":"AssetTypeId","OperationType":1,"FieldValue":"-1500000001","JoinType":2},
{"FieldName":"PeriodFrom","OperationType":10,"FieldValue":"1356998400","JoinType":2},
{"FieldName":"PeriodTo","OperationType":11,"FieldValue":"1358208000","JoinType":2},
{"FieldName":"Status","OperationType":1,"FieldValue":"4","JoinType":2}
]
}
How can I convert in to
{"assetid":"150000001","partybranchid":"152555222"}
in WSO2 ESB?
The easiest way is to translate the given message using XSLT (If you aware of writing XSLT this seems pretty simple)
Then you just required to write a a proxy (in WSO2 ESB),
<proxy name="JSONTOJSONTranformProxy" transports="https,http">
<target>
<inSequence>
<xslt key="translate.xslt"/>
<property name="messageType" value="application/json" scope="axis2"/>
<send>
<address ="[SOME_ADDRESS]"/>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
</target>
</proxy>

Does WSO2 ESB support REST to REST with JSON format?

I want use WSO2 ESB as a gateway. I'm using version 4.0.3 I have some existing RESTful services with JSON message. I know ESB now has REST API supported. But I still can't find solution for WSO2 ESB REST to REST. I mean all backend services are RESTful with JSON format. Can anyone help me?
Yes we do support REST -REST services, which means its categorize under protocol switching, with WSO2 ESB it has REST API which which enables you to handle incoming REST or any other format to and do mediation and pass them to the back end (its doesn't matter which protocol is)
following proxy allows you to transfer incomming REST message to back end REST service
basically if you need to access incomming form data you may have to use
<messageFormatter contentType="application/x-www-form-urlencoded"
class="org.apache.axis2.transport.http.XFormURLEncodedFormatter"/>
<messageBuilder contentType="application/x-www-form-urlencoded" class="org.apache.synapse.commons.builders.XFormURLEncodedBuilder"/>
which allows you to extract incomming REST submit details and do any mediation as you prefer
REST TO REST VIA REST API
<api name="studentSecureAPI" context="/SecureStudentRequest">
<resource methods="POST" uri-template="/student/{name}">
<inSequence>
<property name="REST_URI" expression="fn:substring($axis2:REST_URL_POSTFIX,16,fn:string-length($axis2:REST_URL_POSTFIX))"/>
<property name="AGE" expression="//xformValues//age"/>
<property name="STUDENT" expression="get-property('uri.var.name')"/>
<property name="DISABLE_CHUNKING" value="true" scope="axis2" type="STRING"/>
<property name="HTTP_METHOD" value="POST" scope="axis2" type="STRING"/>
<property name="ContentType" value="application/x-www-form-urlencoded" scope="axis2" type="STRING"/>
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
<property name="REST_URL_POSTFIX" expression="$ctx:REST_URI" scope="axis2"/>
<payloadFactory>
<format>
<POST>
<age>$1</age>
</POST>
</format>
<args>
<arg expression="$ctx:AGE"/>
</args>
</payloadFactory>
<send>
<endpoint>
<address uri="http://localhost:9764/as/services/RestService"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
</resource>
REST TO REST VIA SIMPLE PROXY :
<proxy name="StudentRequestProxy" transports="https http" startOnLoad="true" trace="disable">
<target>
<inSequence>
<property name="DISABLE_CHUNKING" value="true" scope="axis2" type="STRING"/>
<property name="ContentType" value="text/xml" scope="axis2" type="STRING"/>
<property xmlns:ns3="http://org.apache.synapse/xsd" name="Lang" expression="get-property('transport', 'Accept')" scope="default" type="STRING"/>
<log level="custom">
<property name="HTTP_METHOD IS###########" expression="$axis2:HTTP_METHOD"/>
</log>
<switch source="$axis2:HTTP_METHOD">
<case regex="GET">
<property name="HTTP_METHOD" value="GET" scope="axis2" type="STRING"/>
</case>
<case regex="POST">
<property name="HTTP_METHOD" value="POST" scope="axis2" type="STRING"/>
</case>
<default/>
</switch>
<send>
<endpoint>
<address uri="http://localhost:9764/as/services/RestService"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
</target>
</proxy>
WSO2 ESB provides excellent support for receiving, processing and intermediating REST calls. Please find sample at [1] and [2] for further to your information.
Latest ESB version (4.8.1) is perfectly working with JSON REST calls.
try following payloadFactory and property mediators,
<payloadFactory media-type="json">
<format>
{
"name":"$1",
"age":$2
}
</format>
<args>
<arg evaluator="json" expression="$ctx:name"/>
<arg evaluator="json" expression="$ctx:age"/>
</args>
</payloadFactory>
<property name="messageType" value="application/json" scope="axis2"/>
More information can be found at here
If you are not fixed on using any particular ESB, you could check out the UltraESB - here is a sample for all REST methods [http://docs.adroitlogic.org/display/esb/Restful+Proxy+Services] and there is great support for JSON too
Disclaimer - I am the founder and CTO of AdroitLogic