Exposing WSO2 ESB as REST for use in JavaScript - wso2

I have been digging around in Stackoverflow and WSO2 ESB documentation for days but I have yet to come across any clear answer.
My application is in JavaScript and using the Dojo framework. Ideally all the web services call should be make against REST and returns JSON. I know how to get the SOAP xml to come back as JSON because of this example 440: "http//wso2.org/project/esb/java/4.0.3/docs/samples/advanced_mediation_samples.html#Sample440" I have looked at the sample posted on this blog as well: "http//vvratha.blogspot.ca/2011/11/invoking-restful-service-via-wso2esb.html" I seems to be able to call the proxy service from tools like soapUI and get my result in JSON. So I can only assume that ESB is working properly However when I try to call it using dojo.xhrGet or dojo.io.script.get, the data returns as NULL always!!!
What is the method in REST am I supposed to call for a ESB Proxy Service? If I look at the wsdl of the service, the operation seems to call "mediate" but that did not get me any further. I have looked at example 800: "http//wso2.org/project/esb/java/4.0.3/docs/samples/rest_api_samples.html#Sample800" but I am not clear where to use this.
Note: Please add ":" after http for those http links. I seems to be running out of quotas for hyperlinks.
More information after some help from Ratha:
1. This is how I want to call the proxy service: (There will be more querystring parameters later)
http//loh7:8280/services/TestRestProxy?address=1460%20nicholson%20road
Query string is null from this Mediation Message Tracker dump:
13:10:10,803 [-] [HttpServerWorker-16] INFO Proxy Service TestRestProxy received a new message from : 192.168.7.143
13:10:10,803 [-] [HttpServerWorker-16] INFO Message To: /services/TestRestProxy?address=1460%20nicholson%20road
13:10:10,803 [-] [HttpServerWorker-16] INFO SOAPAction: null
13:10:10,803 [-] [HttpServerWorker-16] INFO WSA-Action: null
13:10:10,803 [-] [HttpServerWorker-16] INFO Using the anonymous in-sequence of the proxy service for mediation
13:10:10,804 [-] [HttpServerWorker-16] INFO Start : Sequence
13:10:10,804 [-] [HttpServerWorker-16] INFO Sequence :: mediate()
13:10:10,804 [-] [HttpServerWorker-16] INFO Start : Log mediator
13:10:10,805 [-] [HttpServerWorker-16] INFO querystring = null
13:10:10,805 [-] [HttpServerWorker-16] INFO End : Log mediator
13:10:10,806 [-] [HttpServerWorker-16] INFO Start : Log mediator
13:10:10,806 [-] [HttpServerWorker-16] INFO End : Log mediator
13:10:10,806 [-] [HttpServerWorker-16] INFO Start : Send mediator
13:10:10,806 [-] [HttpServerWorker-16] INFO Sending message through endpoint : EsriNA10 resolving to address =
This is how the proxy looks like:
Note: I wanted to extract the "address" value and put it in the $1 in the request soap message.
<proxy xmlns="http://ws.apache.org/ns/synapse" name="TestRestProxy" transports="https,http" statistics="disable" trace="enable" startOnLoad="true">
<target>
<inSequence>
<log level="custom">
<property name="querystring" expression="get-property('REST_URL_POSTFIX')" />
</log>
<payloadFactory>
<format>
<GeocodeAddress xmlns="http://www.esri.com/schemas/ArcGIS/10.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Address xmlns="">
<PropertyArray>
<PropertySetProperty>
<Key>Street</Key>
<Value>$1</Value>
</PropertySetProperty>
<PropertySetProperty>
<Key>City</Key>
<Value>Newmarket</Value>
</PropertySetProperty>
<PropertySetProperty>
<Key>State</Key>
<Value>ON</Value>
</PropertySetProperty>
<PropertySetProperty>
<Key>Zip</Key>
<Value>L3Y 9C3</Value>
</PropertySetProperty>
<PropertySetProperty>
<Key>Country</Key>
<Value>CANADA</Value>
</PropertySetProperty>
</PropertyArray>
</Address>
<PropMods xmlns="">
<PropertyArray>
<PropertySetProperty>
<Key>OutputSpatialReference</Key>
<Value xmlns:q2="http://www.esri.com/schemas/ArcGIS/10.0" xsi:type="q2:ProjectedCoordinateSystem">
<WKID>102100</WKID>
</Value>
</PropertySetProperty>
</PropertyArray>
</PropMods>
</GeocodeAddress>
</format>
<args>
<arg expression="$ctx:querystring" />
</args>
</payloadFactory>
<log level="full" category="TRACE" separator="," />
<send>
<endpoint key="EsriNA10" />
</send>
</inSequence>
<outSequence>
<log level="full" separator="," />
<xslt key="out_transform" />
<property name="messageType" value="application/json" scope="axis2" type="STRING" />
<send />
</outSequence>
</target>
</proxy>

In the sample 800 it explains how you can directly invoke RESTAPIs..
For example, if you have your REST service endpoints point that as your service endpoint. Note that the following configuration;
<api name="StockQuoteAPI" context="/stockquote">
<resource uri-template="/view/{symbol}" methods="GET">
Where we define the RESTAPI call stockquoteAPI and defining the context and uritemplate..
So, when you try to execute above API, use curl or other REST client and execute like;
curl -v http://127.0.0.1:8280/stockquote/view/IBM
Here you will see we provide the context and uri template..from that we pick symbol call IBM and sending to stcokquote service..But it is your logic, you can define your own logic in the sequence.
Edit
From browser try like this;
http://localhost:8280/services/yourProxy/<Operation>?param1=21312&param2=wqeqw
Here the "operation" is the operation(which is present at your backend REST service) you try to invoke.
"Mediate" is the default operation..If you want to execute your actual backend service operation provide that operation as i mentioned above.
When you define proxy follow my sample in the blog..
<proxy name="yourProxy" transports="https http" startOnLoad="true" trace="disable">
<target>
<endpoint>
<address uri="Your REST SERVICE ENDPOINT URL"/>
</endpoint>
<inSequenc>
<log level ="custom">
<property name="querystrings" expression=get-property('REST_URL_POSTFIX')/>
</log>
</inSequnece>
<outSequence>
<send/>
</outSequence>
</target>
</proxy>
Try above proxy and see what it logs fro "querystrings"

You can use REST API to invoke allt hese http verbs.Simply define what type of the verb you like to execute..and route the request to the service endpoint
This is a simple introduction part..hope may help you..
http://vvratha.blogspot.com/2012/09/rest-support-in-wso2esb-introduction.html

Related

WSO2 ESB: Can we use Call Mediator for Rest Service to post (POST HTTP Method) the xml request?

When I create proxy with send mediator with Rest service Post HTTP Method in HTTP endpoint url. Selected the endpoint as HTTP endpoint on proxy and post the request xml without soap envelop, this perfectly works and get the response in the response window.
But when I use the call mediator with the same HTTP end point url configuration, this does not works. I would like to know can we use call mediator for Post HTTP method? When I use Call mediator for the GET HTTP method which require only query parameters and does not require any request xml this works absolutely fine.
Here is the further information:
However issue is resloved by using the address endpoint in callmediator. When I Invoke the proxy from external Restt client ot Soap UI, it does works. If I use the Try this Service option in wso2 ESB will fail with the results 1. When Soap12 endpoint is selected and 2 when HTTP end point is selected as shown below.
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="postIDMPCall"
transports="https http"
startOnLoad="true"
trace="disable">
<description/>
<target>
<inSequence>
<property name="ContentType" value="text/xml" scope="axis2" type="STRING"/>
<property name="HTTP_METHOD" value="POST" scope="axis2" type="STRING"/>
<payloadFactory media-type="xml">
<format>
<organizationList xmlns="">
<xml content>
</organizationList>
</format>
<args/>
</payloadFactory>
<header name="_user" scope="transport" value="username"/>
<header name="_password" scope="transport" value="Password"/>
<call blocking="true">
<endpoint>
<address uri="http://<ip-address>:<port>/<resource-path>/UpdateOrganization"
format="rest"/>
</endpoint>
</call>
</inSequence>
</target>
</proxy>
Output: When soap12 endpoint is selected
Though posted the correct xml service does not recorgonize the correct xml format for soap12 endpoint.
FAILURE
Record is not processed successfully. Please Provide valid Request XML
When Http end point is selected
[2016-04-21 12:07:50,179] INFO - HTTPSender Unable to sendViaPost to url[http://://UpdateOrganization/mediate]
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
I think we can't use call mediator for this purpose because call mediator is context unaware mediator.
Your call should already perform a post out the box.
Did you try to set format="pox" if you expect simple xml as a response

WSO2 Api Manager log Http request in log mediator

Is there a way to include the http request remote host in the log mediator being used for a specific api?
Having followed the instructions, i can log the soap message, but i have no info regarding the caller of the service.
I currently use the following sequence definition:
<sequence xmlns="http://ws.apache.org/ns/synapse" name="WSO2AM--Ext--In">
<log level="full">
<property name="TRACE" value="Gunet Mediation Extension"/>
</log>
</sequence>
And the result i get when having a call is the following:
INFO - LogMediator To: /SmartSearch/1, WSAction: urn:SearchStudentStatus, SOAPAction: urn:SearchStudentStatus, MessageID: urn:uuid:36f9a5cd-c8cb-4e1e-97a6-f3ebd3303589, Direction: request, TRACE = Gunet Mediation Extension, Envelope: <?xml version='1.0' encoding='utf-8'?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><SearchStudentStatus xmlns="gunet.gr"><SSN>12312312312</SSN><institution>aueb</institution></SearchStudentStatus></S:Body></S:Envelope>
I would like to have information about the remote host that made the specific call. Is that possible, using the log mediator, or should i use a combination of log4j configs?
Thanks!
You can get the remote IP which call the API using following property mediator.
<log level="full">
<property name="Actual Remote Address" expression="get-property('axis2','REMOTE_ADDR')"/>
</log>

WSO2 ESB message id

Do I have any possibility to find message on WSO2 ESB by MessageId, like urn:uuid:e11893c5-b033-4e99-9473-a43d66b65fbb ? For example if some flow failed and server logged such ID.
Maryan,
ESB itself doesn't log messages anywhere until you tell it to.
a) The first approach is to write incoming and outgoing messages into the log using log mediator:
<proxy xmlns="http://ws.apache.org/ns/synapse" name="YourProxyService" transports="https http" startOnLoad="true">
<target>
<inSequence>
<log level="full">
<property name="MESSAGE_ID" expression="get-property('MessageID')"/>
</log>
...
</inSequence>
<outSequence>
<log level="full">
<property name="MESSAGE_ID" expression="get-property('MessageID')"/>
</log>
...
</outSequence>
</target>
</proxy>
Then you'll be able to find your incoming and outgoing messages in log files as the logs would contains something like following:
INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: http://localhost:9763/services/YourProxyService, From: 127.0.0.1, WSAction: urn:mediate, SOAPAction: urn:mediate, Direction: request, MESSAGE_ID = urn:uuid:e11893c5-b033-4e99-9473-a43d66b65fbb , Envelope: <ENVELOPE_GOES_HERE>
b) Another approach would be to create table in the database and store message id and envelope to it.
Hope this helps.
Vladimir.
UPD: You can also use built in SOAP tracer, but enable it with caution - it hits ESB performance. So I suggest use it only for short term debugging activities.
Yes, you can get the message ID using property mediator,
<property name="MessageID" expression="get-property('MessageID')"/>
As others pointed out, you can use log statements in WSO2 ESB to log the messages and search and find later.
But it becomes complex when the message flow fails at some point, as the message IDs will be different across different message flows.
A simple approach would be to read the incoming messagID and use it till the final response message.
Have a look here for detailed explanation.

How to send wso2 esb error message or exception to my client

I provoke an error in wso2ESB in order to have control in the client of some errors, while the esb console displays:
ERROR {org.wso2.carbon.identity.entitlement.mediator.EntitlementMediator} - Error occurred while evaluating the policy {org.wso2.carbon.identity.entitlement.mediator.EntitlementMediator}
org.apache.axis2.AxisFault: Access Denied. Authentication failed - Invalid credentials provided.
...
...
the client console displays:
org.apache.axiom.soap.SOAPProcessingException: Transport level information does not match with SOAP Message namespace URI
I'm seeing different errors in consoles, I need to take the esb error message so that the client code can catch the same error esb
At this moment i have my sequence 'fault' configured like this:
<sequence xmlns="http://ws.apache.org/ns/synapse" name="fault" trace="enable">
<makefault version="soap12">
<code xmlns:soap12Env="http://www.w3.org/2003/05/soap-envelope" value="soap12Env:Receiver"/>
<reason xmlns:ns="http://org.apache.synapse/xsd" expression="get-property('ERROR_MESSAGE')"/>
</makefault>
<send/>
</sequence>
and a sequence in sequence to my proxy service configured like this:
<sequence xmlns="http://ws.apache.org/ns/synapse" name="InSequence" trace="enable">
<entitlementService remoteServiceUrl="https://localhost:9443/services/" remoteServiceUserName="srxxx" remoteServicePassword="kiytr">
<onReject/>
<onAccept/>
<advice/>
<obligations/>
</entitlementService>
<header xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" name="wsse:Security" action="remove"/>
<send/>
<drop/>
</sequence>
I've tried several ways to get the error in my client without success if anyone can help me I would appreciate so much
Configure a faultSequence inside your proxy service as shown in below. In your case you have to use the fault mediator inside the faultSequence.
<faultSequence>
<makefault version="soap12">
<code xmlns:soap12Env="http://www.w3.org/2003/05/soap-envelope" value="soap12Env:Receiver"/>
<reason xmlns:ns="http://org.apache.synapse/xsd" expression="get-property('ERROR_MESSAGE')"/>
</makefault>
<send/>
</faultSequence>
For more information needed for sending the error message to the client use following documentations
[1] http://docs.wso2.org/wiki/display/ESB460/Error+Handling+and+Error+Codes
[2] http://docs.wso2.org/wiki/display/ESB460/Fault+Mediator

How can I configure WSO2 ESB 4.0.3 to use corporate proxy to access external web services (over HTTP)

I am trying to access external web services (outside of intranet) using WSO2 ESB 4.0.3 but I do not know how to configure the proxy to let the request go outside. I can send SOAP messages over HTTP from my basic Java client app without issues; I could specify teh proxy details using Apache HttpComponents. So, connection details are OK.
I tried:
Editing <transportSender name="http" ... > and specifying parameters like "http.proxyHost" and "http.proxyPort".
Setting up a global parameter <parameter name="Proxy"> ...
Configuring the HTTP Transport Sender and specifying the appropriate parameters.
The best I got was:
ERROR_CODE = 101503, ERROR_MESSAGE = Connection refused or failed for : ...
So, what did I wrong and what should I do instead? Thanks.
Does your proxy server has any authentication? If yes and it uses Basic Auth you need to specify followoing properties before sending out the request..
<property name="Proxy-Authorization" expression="fn:concat('Basic', base64Encode('userName:password'))" scope="transport"/>
<property name="POST_TO_URI" value="true" scope="axis2"/>
The first property sets the Proxy-Authorization HTTP transport header with the base64 encoded user name and password as expected by the HTTP basic authentication.
The second property makes the out-going URL a complete URL understandable by the Proxy Server.
Following is an example proxy config...
<proxy name="StockQuoteProxy" startOnLoad="true">
<target>
<inSequence>
<property name="Proxy-Authorization" expression="fn:concat('Basic ', base64Encode('udayanga:test123'))" scope="transport"/>
<property name="POST_TO_URI" value="true" scope="axis2"/>
<send>
<endpoint>
<address uri="http://www.wso2.com:9000/services/SimpleStockQuoteService"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
</target>
<publishWSDL uri="file:repository/samples/resources/proxy/sample_proxy_1.wsdl"/>
</proxy>