WSO2 HTTP Response - rest

I am using WSO2 to send rest request, which I am able to do. But I would like to have some logic before I send the response to the client.
The Backend Rest service returns "Successful" as a response string. I have a Java code which used to do this, and I would like to use the same logic in WSO2. The logic is simple
1. If
the HTTP Status is greater than eq to 200 and less than 300 and resonseStr.equalsIgnoreCase("Successful") THEN
return "RetCode=C;Message=Success"
ELSE
return "RetCode=F;Message=Failed because Itegration Exception";
responseStr is the response from the Backend Rest Service.
My API looks like this:
<api xmlns="http://ws.apache.org/ns/synapse" name="TririgaApi" context="/tririga">
<resource methods="GET" url-mapping="/employee">
<inSequence>
<sequence key="conf:/tririgaConf"/>
<property name="POST_TO_URI" value="true" scope="axis2"/>
<send>
<endpoint>
<http trace="enable" method="GET" uri-template="http://{uri.var.service.host}:{uri.var.service.port}/html/en/default/rest/Integration?USERNAME={uri.var.service.user}&PASSWORD={uri.var.service.pass}&ioName={uri.var.wfname}"/>
</endpoint>
</send>
</inSequence>
</resource>
</api>
Thanks

Add an outSequence to your api and implement your mediation inside it
You can access to http status code with $axis2:HTTP_SC
You can test it's value with filter mediator
You can create a message with payloadFactory mediator

Related

different Content-Types in the end point response

I have a rest API with the Content-Type = application/json, provided by a tomcat server.
This means that all the responses are supposed to be in the json format.
The rest API is called by the WSO2 ESB to pass some data.
in case that the application providing the rest API is down (but the tomcat server is still up), the tomcat server replays with the http code=404 with the Content-Type=text/html (sending the HTML page "The requested resource is not available.") what results into the ESB error "Error while building message" exception and ESB crashes and losses the message.
Can you please suggest how to handle such a scenario? I'd need to receive the msg and react on this event. Is there perhaps a way how to dynamically switch content-types?
Can you try this with your own modification:
<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="fault_filter_based_http_status_code" trace="disable">
<filter regex="401" source="get-property('axis2', 'HTTP_SC')">
<then>
<makefault version="soap11">
<code xmlns:soap11Env="http://schemas.xmlsoap.org/soap/envelope/" value="soap11Env:Server"/>
<reason value="Unauthorized to access the resource"/>
<role/>
</makefault>
<send/>
</then>
<else/>
</filter>
<filter regex="500" source="get-property('axis2', 'HTTP_SC')">
<then>
<makefault version="soap11">
<code xmlns:soap11Env="http://schemas.xmlsoap.org/soap/envelope/" value="soap11Env:Server"/>
<reason value="Internal Server Error Occurred"/>
<role/>
</makefault>
<send/>
</then>
<else/>
</filter>
</sequence>
Take a look: http://harshcreationz.blogspot.com/2016/02/common-and-error-handling-sequences.html

WSO2 REST proxy answer back with SOAP instead of REST

Recently I had a problem with wso2 esb that I cannot resolve (maybe a bug). I will try to explain it as clear as possible.
I use the wso2 ESB with a rest proxy in order to communicate from a frontend application to a backend Rest service.
I defined a rest proxy with an endpoint like this and it worked fine:
<endpoint>
<address uri="http://127.0.0.1:8099/DummyRestServiceWSO2"/>
</endpoint>
But our goal is to integrate WSO2 esb in a container inside an openshift environment. In openshift the ip are set dynamically but accessible through system environment variable. So I tried to solve my endpoint dynamically byusing the header tag:
<endpoint>
<default/>
</endpoint>
<inSequence>
<script language="js">mc.setProperty("url",java.lang.System.getenv("HOST_IP"));</script>
<property name="service_url" expression="fn:concat(get-property(url),'/DummyRestServiceWSO2')"/>
<header name="To" expression="get-property('service_ep')"/>
<send/>
</inSequence>
Where HOST_IP is defined as: http://127.0.0.1:8099
Since I made this change my call to the backend rest service is still working fine but the response to the caller (frontend) is now formatted as soap (I tried to draw the problem, see attachment).
Thanks a lot for your help and I wish a wonderfull day to whoever read this post :)
If by "REST" you mean "JSON" format, just define this property before sending back the reponse to the client :
<property name="messageType" value="application/json" scope="axis2"/>
Use application/xml for plain xml (no SOAP envelope / body)
You also can use a default endpoint in send mediator and define the format :
<send>
<endpoint>
<default format="rest">
<timeout>
<responseAction>fault</responseAction>
</timeout>
</default>
</endpoint>
</send>

Rest Service on WSO2 ESB API with a Mal Formed XML request

I have created an api proxy to call my rest service, but when I send in a Mal formed XML request, I only receive an HTTP status code 202.
I have coded my service to handle this mal formed xml, and I just want the ESB to pass through the request.
Here is the code to my ESB API:
<?xml version="1.0" encoding="UTF-8"?>
<api xmlns="http://ws.apache.org/ns/synapse" name="myApi" context="/restService">
<resource methods="POST">
<inSequence>
<send>
<endpoint>
<address uri="http://myserver/MyRestService"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
<faultSequence/>
</resource>
</api>
Thanks
In this case, ESB should be able to successfully send the message to the backend. By enabling wire logs you can make sure message sending correctly to the backend.
If you are not expecting any response from your backend, Please set out_only property in your inseqeunce

WSO2 REST - ESB - REST

I'm wondering for solution to my Issue but I didn't find anything that can help me :(
THIS IS MY ISSUE:
I'd like to call a remote REST web service by passing it trought an ESB to log the client call on DB.
I'd like to pass a POST query var to my remote ws too, for example name=value & name2=value2!
I make a proxy service but I don't know how I can append the query variable to IT.
I can contact correctly remote ws with this proxy but I can't pass a POST VAR because I don't know how to do that.
I make a below curl call by client shell:
curl -k -i http://neanb330:8281/services/BioframeProxyService
in my proxy service I have this endpoint :
http://www.ebi.ac.uk/Tools/services/rest/emboss_matcher/run
BUT this service require two params in input and put out a jobid that I want to write in out sequence for client.
Have I to make a REST API?
How I can Log client call on db?
Thanks
This post describes how to process rest requests within WSO2 ESB in detail with examples.
http://wso2.com/library/articles/2012/09/get-cup-coffee-wso2-way/
This is the official documentation that explains rest url mapping
http://docs.wso2.org/display/ESB470/Getting+Started+with+REST+APIs
If your question is how you can send data to your end point using curl then this is the way
curl -v --request POST -d '<Values><name1>ABC</name1><name2>Smith</name2></Values>' -H Content-Type:"text/xml" http://neanb330:8281/services/BioframeProxyService
Then you can get the values to ESB as shown below
<property name="name1" expression="//name1/text()"/>
<property name="name2" expression="//name2/text()"/>
I find a solution for both GET and POST in Rest to rest scenario.
This for POST. I USE a proxy service and a curl call:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="BioframeProxyServiceRunBis"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<log level="full"/>
<property name="ContentType" value="text/xml" scope="axis2" type="STRING"/>
<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://www.ebi.ac.uk/Tools/services/rest/emboss_matcher/run/"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
</target>
<description/>
</proxy>
curl -k -X POST https://neanb330:8244/services/BioframeProxyServiceRunBis -d asequence=FASTA -d bsequence=FASTA -d email=maforast#gmail.com -v
I don't find solution for save rest call to DB

Proxy a RESTful service using SOAP with WSO2 ESB

We want to proxy a RESTful web service with SOAP.
The REST service uses the GET method and accepts inputs via query parameters. It produces a resource of type application/csv.
Does WSO2 ESB/Synapse support such a scenario, and is there an example available?
Example Request
SOAP Proxy Request:
<request>
<fromDate>2012-01-01</fromDate>
<toDate>2012-12-31</toDate>
</request>
REST Endpoint Request:
http://localhost/person?fromDate=2012-01-01&toDate=2012-12-31
Example Response
REST Endpoint Response
"Name","Age","Sex"
"Geoff","22","Male"
SOAP Proxy Response
<person>
<name>Geoff</name>
<age>22</age>
<sex>Male</sex>
<person>
Many thanks.
If I understand you correctly, you want to expose a REST service as a SOAP service, so that SOAP clients can access your REST service through the ESB?
If that is the case, it is possible :) You should check out sample 152 from these: http://docs.wso2.org/wiki/display/ESB451/Proxy+Service+Samples
It'll explain how you take a SOAP request and pass it to a REST backend and then transform the REST response into a SOAP response.
EDIT: Here's a sample configuration on how to do what you asked in the comments, hopefully it will get you started :)
<proxy xmlns="http://ws.apache.org/ns/synapse" name="RESTProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<!-- We set the HTTP Method we want to use to make the REST request here -->
<property name="HTTP_METHOD" value="GET" scope="axis2"/>
<!-- This is where the magic happens, for what you want i.e. mapping SOAP "params" to REST query param's -->
<property name="messageType" value="application/x-www-form-urlencoded" scope="axis2"/>
<send>
<endpoint>
<!-- This is the RESTful URL we are going to query, like the one in the ESB example 152 -->
<address uri="http://localhost/person" />
</endpoint>
</send>
</inSequence>
<outSequence>
<log level="full"/>
<property name="messageType" value="text/xml" scope="axis2"/>
<send/>
</outSequence>
</target>
<description></description>
</proxy>
Then the SOAP request you make to the ESB should be something like:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<person>
<fromDate>2012-01-01</fromDate>
<toDate>2012-12-31</toDate>
</person>
</soapenv:Body>
</soapenv:Envelope>
Hope that helps :)
Hope This will be very helpful you to understand of SOAP Client and REST Service communication
http://docs.wso2.org/display/ESB460/Using+REST+with+a+Proxy+Service#UsingRESTwithaProxyService-SOAPClientandRESTService
You can use class mediator to extract the SOAP parameters using XPATH. Than build the REST URL and send it back to IN sequence flow.
1. you need to get the value from SOAP PROXY
2. you need to store it in a local variable
3. you need to pass the value to the REST SERVICE using Query Parameters
4. you need to format the response from REST Service to an SOAP Format
The SOAP Request will be,
<request>
<fromDate>2012-01-01</fromDate>
<toDate>2012-12-31</toDate>
</request>
You can store the value from SOAP PROXY Request as,
<proxy xmlns="http://ws.apache.org/ns/synapse" name="RESTProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true><target>
<inSequence>
<property name="fromDate" expression="//fromDate" scope="default" type="STRING"/>
<property name="toDate" expression="//toDate" scope="default" type="STRING"/>
Then you can pass the values to the REST Service by,
<send>
<endpoint>
<http method="GET" uri-template="http://localhost/person?fromDate=={get-property('fromDate')}&toDate={get-property('toDate')}"/>
</endpoint>
</send>
</inSequence>
Then You can Format the Response using PayloadFactory mediator,
<outSequence>
<payloadFactory media-type="xml">
<format>
<person>
<Name>$1</Name>
<Age>$2</Age>
<Sex>$3</Sex>
</person>
</format>
<args>
<arg evaluator="json" expression="$.Name"/>
<arg evaluator="json" expression="$.Age"/>
<arg evaluator="json" expression="$.Sex"/>
</args>
</payloadFactory>
<send/>
</outSequence>
</target>
<description/>
</proxy>
So the Response of Proxy will be,
<person>
<name>Geoff</name>
<age>22</age>
<sex>Male</sex>
<person>