Does WSO2 ESB support REST to REST with JSON format? - json

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

Related

ESB WSO2 How to send an email from esb

I want to make an api for sending some emails from a SMTP server but I don't a user and a password to put in axis2.xml file.
So my axis2.xml file looks like this:
<transportSender name="mailto" class="org.apache.axis2.transport.mail.MailTransportSender">
<parameter name="mail.smtp.host">email.example.ro</parameter>
<parameter name="mail.smtp.port">25</parameter>
<parameter name="mail.smtp.starttls.enable">true</parameter>
<parameter name="mail.smtp.auth">true</parameter>
<parameter name="mail.smtp.user"></parameter>
<parameter name="mail.smtp.password"></parameter>
<parameter name="mail.smtp.from">email#example.com</parameter>
</transportSender>
I searched on wso2 docs and I found that I have to do a proxy service for sending emails. Is it possible to do it with just an api?
I'm a real newbie with wso2 and my question is what should my proxy service look like and how can I call this service? (should I call it from an api, or what? because I want to send emails based on a value from a database).
#jakub.voves answer is correct. Let me add more details.
Proxy, APIs, Inbound Endpoints etc are a way to bring external messages into the system.(APIs help you to accept Restful invocation, Proxies allow you to consume SOAP messages, JMS Inbound Endpoints allow you to consume JMS messages from queues/topics) Once you receive the message you can use a combination of mediators to mediate the message.(Message transformations, calling different services etc.) So the mediators within a WSO2 environment are common to all types of services. Hence whatever mediators you use in proxy service can be used in APIs, or in Inbound Endpoints. Other than the mediators WSO2 also provides connectors to extend the functionality of the Server. So in conjunction with Mediators, you can use connectors as well. So in your case either you can use the mailto transport or you can consider using the Email connector. Email sending will look like something below with the connector.
<email.send configKey="smtpsconnection">
<from>{json-eval($.from)}</from>
<to>{json-eval($.to)}</to>
<subject>{json-eval($.subject)}</subject>
<content>{json-eval($.content)}</content>
</email.send>
One additional thing, If your SMTP server doesn't need authentication set the following property to false in axis2.xml.
<parameter name="mail.smtp.auth">false</parameter>
You can create api and try something like this
<property name="messageType" value="text/html" scope="axis2" type="STRING"/>
<property name="ContentType" value="text/html" scope="axis2" type="STRING"/>
<property name="Subject" value="Test Message" scope="transport" type="STRING"/>
<property name="OUT_ONLY" value="true" scope="default" type="STRING"/>
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<test xmlns="">This is a test email.</test>
</soapenv:Body>
</soapenv:Envelope>
</format>
</payloadFactory>
<send>
<endpoint>
<address uri="mailto:another.user#example.com"/>
</endpoint>
</send>
http://wso2experts.com/ei/send-email-from-wso2-ei-mailto-transport/

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 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>

How to convert the JSON in WSO2 ESB

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

Define an custom jackson objectmapper for a spring3 mvc project

I am using mvc:annotation-driven which meant that simply puttin the jackson jar files onto the classpath configured the Jackson json marhsaller. Now I would like to provide a custom object mapper to be able to control the serialization of Date fields on a global level.
I began to define the AnnotationMethodhandlerAdapter and referenced my own jsonConverter bean (see below)
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
<ref bean="xmlMarshallingConverter" />
</list>
</property>
</bean>
The problem is dates are still written as timestamps. The custom objectmapper bean is created (got logs for that), but it seesm the objectmapper is never used. I assume it still uses the default MappingJacksonHttpMessageConverter and the default ObjectMapper.
Any ideas? How would I figure out what exact beans to overwrite to chagne the default mapper?
Thanx!
Have you configured the view resolver to also use your custom object mapper instance (see reference to jacksonObjectMapper below)?
It's my understanding that AnnotationMethodHandlerAdapter handles the inbound conversions and ContentNegotiatingViewResolver handles outbound conversions.
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="json" value="application/json"/>
<!-- xml etc -->
</map>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="objectMapper" ref="jacksonObjectMapper"/>
</bean>
<!-- xml etc -->
</list>
</property>
</bean>