WSO2 REST - ESB - REST - 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

Related

Cannot post to the REST API on WSO2 ESB

I Created the following REST API on WSO2 Integrator 6.4 with the sequences:
<?xml version="1.0" encoding="UTF-8"?>
<api context="/dsb" name="evCaptureJSONProperties"
xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST" uri-template="/api/SendSMS">
<inSequence>
<log description="Entry Confirmation" level="custom">
<property name="message" value="Request Entered the
evCaptureJSONProperties REST API"/>
</log>
<payloadFactory media-type="json">
<format><!-- A json message was here --></format>
<args/>
</payloadFactory>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
And then I posted the request JSON using the following command:
Curl -v -X POST --data #evCaptureJSONProperties.json http://localhost:8280/dsb/api/SendSMS --header "Content-Type:application/json"
But instead of getting back in return the payload an error appeared in the logs and the console:
[2018-12-03 10:23:02,432] [EI-Core] ERROR - RelayUtils Error while building
Passthrough stream
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at org.apache.synapse.commons.builders.XFormURLEncodedBuilder.
extractParametersFromRequest(XFormURLEncodedBuilder.java:223)
at
org.apache.synapse.commons.builders.XFormURLEncodedBuilder.
processDocumentWrapper(XFormURLEncodedBuilder.java:128)
at org.apache.synapse.commons.builders.XFormURLEncodedBuilder.
processDocument(XFormURLEncodedBuilder.java:52)
I humbly request for some assistance. Thank you very much in advance.

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

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

WSO2 REST to SOAP passing operation parameters

Using WSO2 ESB 4.8.1, I have configured a WSDL proxy that I want to access over REST. the proxy points to the SOAP WSDL URI and has publish WSDL turned on. This seem to work fine and I can see the service and its various operations in the WSO2 admin UI. Likewise if I go to localhost:8280/services/
The questions is how do I pass operation specific parameters when accessing over HTTP REST?
Let's say my FooService OperationX expects a "p1" parameter, can I pass this directly when accessing localhost:8280/services/FooService/OperationX in a browser?
I tried for example localhost:8280/services/FooService/SomeOperation?p1=somevalue, but always get a validation error that the required parameter is missing:
cvc-complex-type.2.4.b: The content of element 'axis2ns15:OperationXRequest' is not complete. One of '{"somenamespace":p1}' is expected.
Can this be supported by a basic WSDL proxy? Or do I need to use the API?
I think the better option for your scenario is to use api to access over REST. Here I have created an api (I used http://jsonplaceholder.typicode.com/comments as my REST back end) which gets the query parameter(postId) which was sent in REST request (http://172.22.99.96:8290/test/comments?postId=1) and assign that value to a property called mypostId inside the api.
Then I am modifying the payload by adding the mypostId property using payload factory mediator which will match to the echo service request(I have used echo service as the SOAP backend).
Then I use enrich mediator to change my soap envelope to match the echo service request soap envelope by adding "xmlns:echo="http://echo.services.core.carbon.wso2.org"" name space. Finally I am sending my created request to echo service proxy.
<api xmlns="http://ws.apache.org/ns/synapse" name="test" context="/test">
<resource methods="GET" uri-template="/comments?postId={postId}">
<inSequence>
<log level="custom">
<property name="Message Flow" value="--- Order GET ---"></property>
</log>
<log level="custom">
<property name="postId" expression="$url:postId"></property>
</log>
<property name="mypostId" expression="$url:postId"></property>
<call>
<endpoint>
<http method="GET" uri-template="http://jsonplaceholder.typicode.com/comments?postId={uri.var.postId}"></http>
</endpoint>
</call>
<payloadFactory media-type="xml">
<format>
<echo:echoInt xmlns:echo="http://echo.services.core.carbon.wso2.org">
<in>$1</in>
</echo:echoInt>
</format>
<args>
<arg evaluator="xml" expression="get-property('mypostId')"></arg>
</args>
</payloadFactory>
<log level="full"></log>
<log level="custom">
<property name="Message Flow" value="--- After Palyload factory---"></property>
</log>
<property name="extarctedBody" expression="$body"></property>
<log level="custom">
<property name="MyextarctedBody" expression="get-property('extarctedBody')"></property>
</log>
<log level="full"></log>
<enrich>
<source type="inline" clone="true">
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:echo="http://echo.services.core.carbon.wso2.org"></soapenv:Envelope>
</source>
<target type="envelope"></target>
</enrich>
<log level="custom">
<property name="Message Flow" value="--- Order GET2 ---"></property>
</log>
<log level="full"></log>
<enrich>
<source type="property" clone="true" property="extarctedBody"></source>
<target xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:echo="http://echo.services.core.carbon.wso2.org" action="child" xpath="//soapenv:Envelope"></target>
</enrich>
<log level="full"></log>
<send>
<endpoint>
<address uri="http://localhost:8290/services/echo"></address>
</endpoint>
</send>
</inSequence>
<outSequence>
<send></send>
</outSequence>
</resource>
</api>
Hope this may help you .

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>