WSO2: using a property to save the current message - wso2

I'm a WSO2 newbie, so sorry in advance if my answer seems a stupid one ...
In a sequence I've the following code ....
<property name="InitialMessage" expression="$body" scope="operation" type="STRING"/>
<log level="full">
<property name="INITIAL MESSAGE" expression="get-property('InitialMessage')"/>
</log>
In the server logs I've the following ...
TID: [0] [ESB] [2014-07-04 11:27:46,569] INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: process, SOAPAction: process, MessageID: urn:uuid:0D6752DAEC5AA438C92484199554844771301894479, Direction: request, **INITIAL MESSAGE = null**,..............
What am I doing wrong?
Thank you very much in advance .. .
Cesare

Please never think that your questions are stupid ones :). I think scope value that you have defined in the property mediator is invalid. Scoped must be default. It means you do not want to configure it and please tryout by removing it You can find detail about using $body from WSO2 ESB doc

Related

Dynamically replace the PATH value of an Endpoint (WSO2 - API Manager)

I have created an API in the WSO2 API Manager (1.10.0) with the next info:
POST:
/regularPath/*
API URL:
http://<ip-address-1>/t/tenant.com/api/1.0.0/
HTTP-Endpoint:
http://<ip-address-2>:8181/{uri.var.newRestVar}
The issue is that the HTTP Endpoint has more than one path, e.g.:
http://<ip-address-2>:8181/mainService/cityInfo
http://<ip-address-2>:8181/country/cityInfo
http://<ip-address-2>:8181/country/dataKey/amountOfUsers
http://<ip-address-2>:8181/main/city/data/users
And I want that the resulting API URLs look like this:
http://<ip-address-1>/t/tenant.com/api/1.0.0/regularPath/mainService/cityInfo
http://<ip-address-1>/t/tenant.com/api/1.0.0/regularPath/country/cityInfo
http://<ip-address-1>/t/tenant.com/api/1.0.0/regularPath/country/dataKey/amountOfUsers
http://<ip-address-1>/t/tenant.com/api/1.0.0/regularPath/main/city/data/users
My initial approach was to use the REST_URL_PREFIX variable in order to capture the path after the 1.0.0 part of the API URL (e.g.: regularPath/country/dataKey/amountOfUsers) and then, assign that value to the uri.var.newRestVar variable.
Next, is the modified Service Bus Configuration of the API Manager (Carbon) I've created in the API Manager:
...
<inSequence>
<filter regex="PRODUCTION" source="$ctx:AM_KEY_TYPE">
<then>
<property expression="get-property('SYSTEM_TIME')" name="api.ut.backendRequestTime"/>
<log>
<property expression="$trp:To" name="ToURL"/>
</log>
<property expression="$axis2:REST_URL_POSTFIX" name="restURL" scope="default" type="STRING"/>
<log>
<property
expression="get-property('restURL')" name="logRestURL"/>
</log>
<script description="JS" language="js">
<![CDATA[
var unmodifiedRestPostfix = new String(mc.getProperty("restURL"));
print("value = " + unmodifiedRestPostfix);
unmodifiedRestPostfix = unmodifiedRestPostfix.replace("/regularPath/", "");
mc.setProperty("uri.var.newRestVar", unmodifiedRestPostfix);
]]>
</script>
<log>
<property expression="get-property('uri.var.newRestVar')" name="URI_VAR_NEWRESTVAR"/>
</log>
<send>
<endpoint name="admin-AT-tenant.com--API-Main_APIproductionEndpoint_0">
<http uri-template="http://<ip-address-2>:8181/{uri.var.newRestVar}"/>
</endpoint>
</send>
...
But it does not work. I checked the logs but everything seems to be OK:
TID: [35] [] [2016-04-11 16:47:01,955] #bank.com [35] [AM] INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: local://axis2services/api/1.0.0/regularPath/mainService/cityInfo, MessageID: urn:uuid:091613ce-9fd3-4094-8638-5b112
a4214ad, Direction: request, logRestURL = /regularPath/mainService/cityInfo {org.apache.synapse.mediators.builtin.LogMediator}
a4214ad, Direction: request, URI_VAR_NEWRESTVAR = /regularPath/mainService/cityInfo {org.apache.synapse.mediators.builtin.LogMediator}
What configuration should I change in order to successfully access the HTTP-Endpoint using the paths of the API I've mentioned?
You can do the same thing without modifying the synapse configuration. Following are the steps. Hope it would be a solution for you
Create the api as following
API context : api/{version}/regularPath
Note: you can define the version in the context as a template in AM 1.10. once you define the version, above context will get the version
resources:
POST mainService/cityInfo
POST country/cityInfo
POST country/dataKey/amountOfUsers
POST main/city/data/users
HTTP Endpoint
http://<ip-address-2>:8181
After that you would be able to call a backend . No need to add custom modifications to synapse config
http://<ip-address-2>:8181/country/dataKey/amountOfUsers
with
http://<ip-address-1>/t/tenant.com/api/1.0.0/regularPath/country/dataKey/amountOfUsers
Additional information
If something went wrong, you can enable the wire logs and check what kind of request coming and going out in the gateway. see http://mytecheye.blogspot.com/2013/09/wso2-esb-all-about-wire-logs.html on how to enable wirelogs and debug using it.

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>

How to handle endpoint failure in wso2esb and wso2dss

I created proxy services using wso2dss for data insertion, so services working fine I am quite happy with this. But while any data duplication errors occurred in wso2dss I need to handle it in wso2esb so I kept this property in wso2esb for dss level error handle:
<property name="FORCE_ERROR_ON_SOAP_FAULT" value="true"/>
So its working fine I am getting 50000 code using this I am able to handle error while this error my esb showing this message:
[2014-03-07 11:22:40,778] INFO - LogMediator To: /services/GeoLocationInsertion, MessageID: urn:uuid:b51629e2-934e-4227-8f50-65fd9f719b8e, Direction: request, userid = -1212807836, username = sa|214057357158656, password = sa
[2014-03-07 11:22:40,783] INFO - LogMediator To: /services/ServiceLogin, MessageID: urn:uuid:1967bde1-d820-46f9-957d-55fbb6f7ea9e, Direction: request, usercode = sa, clientid = 214057357158656
[2014-03-07 11:22:40,833] INFO - LogMediator To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , SOAPAction: , MessageID: urn:uuid:24aea5ed-f4e2-4214-809b-b3101031edf7, Direction: response, faisal = true
[2014-03-07 11:22:40,838] INFO - LogMediator To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , SOAPAction: , MessageID: urn:uuid:42a17360-cd04-43bc-83ef-1f53b639de11, Direction: response, kk = true
[2014-03-07 11:22:40,853] WARN - EndpointContext Endpoint : endpoint_71f1485e740c2b97ec407fe4d3bf86929122480f208b6642 will be marked SUSPENDED as it failed
[2014-03-07 11:22:40,853] WARN - EndpointContext Suspending endpoint : endpoint_71f1485e740c2b97ec407fe4d3bf86929122480f208b6642 - last suspend duration was : 30000ms and current suspend duration is : 30000ms - Next retry after : Fri Mar 07 11:23:10 IST 2014
[2014-03-07 11:22:40,856] INFO - LogMediator To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , SOAPAction: , MessageID: urn:uuid:42a17360-cd04-43bc-83ef-1f53b639de11, Direction: response, MESSAGE = Executing default 'fault' sequence, ERROR_CODE = 500000, ERROR_MESSAGE = null
[2014-03-07 11:22:40,857] INFO - LogMediator To: , WSAction: , SOAPAction: , MessageID: urn:uuid:42a17360-cd04-43bc-83ef-1f53b639de11, Direction: response, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Body><ResponseJSON><Body><Data><Exception>duplicate key value violates or The system is attempting to access an inactive service </Exception></Data></Body><Status>500000</Status></ResponseJSON></soapenv:Body></soapenv:Envelope>
Above error message means my endpoint gone into suspend mode after 30sec it will automatically in active mode.
If any user tries on the same time he is unable to insert his correct data and he is losing the data due to endpoint nonavailability. So how we can keep endpoint in active mode?
If any other error will come this issue is not raising:
[2014-03-07 11:22:40,853] WARN - EndpointContext Suspending endpoint : endpoint_71f1485e740c2b97ec407fe4d3bf86929122480f208b6642 - last suspend duration was : 30000ms and current suspend duration is : 30000ms - Next retry after : Fri Mar 07 11:23:10 IST 2014
So how would I do this?
If I remove the property I am unable to handle DSS level message and I am getting error like this even my user not getting any response:
[2014-03-07 11:20:51,816] ERROR - NativeWorkerPool Uncaught exception
java.lang.ClassCastException: org.apache.axiom.om.impl.llom.OMElementImpl cannot be cast to org.apache.axiom.soap.SOAPFault
at org.apache.axiom.soap.impl.llom.SOAPBodyImpl.getFault(SOAPBodyImpl.java:120)
at org.apache.synapse.util.POXUtils.convertSOAPFaultToPOX(POXUtils.java:46)
at org.apache.synapse.core.axis2.Axis2Sender.sendBack(Axis2Sender.java:91)
at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.send(Axis2SynapseEnvironment.java:308)
at org.apache.synapse.mediators.builtin.SendMediator.mediate(SendMediator.java:92)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:71)
at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:114)
at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:232)
at org.apache.synapse.core.axis2.SynapseCallbackReceiver.handleMessage(SynapseCallbackReceiver.java:443)
at org.apache.synapse.core.axis2.SynapseCallbackReceiver.receive(SynapseCallbackReceiver.java:166)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
at org.apache.synapse.transport.passthru.ClientWorker.run(ClientWorker.java:222)
at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:679)
So that 30sec are so important, it causes my client's data loss. How could I avoid this endpoint failure?
The actual error in wso2dss is:
Nested Exception:-
org.postgresql.util.PSQLException: ERROR: column "deviceid" is of type bigint but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
Position: 81
at org.wso2.carbon.dataservices.core.engine.DSOMDataSource.execute(DSOMDataSource.java:105)
at org.wso2.carbon.dataservices.core.engine.DSOMDataSource.serialize(DSOMDataSource.java:110)
at org.wso2.carbon.dataservices.core.engine.DSOMDataSource.getReader(DSOMDataSource.java:116)
at org.apache.axiom.om.impl.llom.OMSourcedElementImpl.getDirectReader(OMSourcedElementImpl.java:225)
... 41 more
I handle it in wso2esb fault and working fine also but endpoint failure is uncatchble
If you want to configure your DSS endpoint's suspension behavior in the ESB, please refer to the Endpoint Error Handling documentation.
If you drop the FORCE_ERROR_ON_SOAP_FAULT property, you should still be able to handle a soap fault from your DSS response in the out sequence of an ESB proxy.
yo can disable that config with this configuration in your endpoint:
<?xml version="1.0"?>
<endpoint>
<address uri="http://localhost:9000/services/SimpleStockQuoteService">
<timeout>
<duration>30000</duration>
<responseAction>fault</responseAction>
</timeout>
<suspendOnFailure>
<errorCodes>-1</errorCodes>
<initialDuration>0</initialDuration>
<progressionFactor>1.0</progressionFactor>
<maximumDuration>0</maximumDuration>
</suspendOnFailure>
<markForSuspension>
<errorCodes>-1</errorCodes>
</markForSuspension>
</address>
</endpoint>
I came up with this error myself and it was terrible to identify it and find a way to work with it. It seems there is a bug in older versions of WSO2 ESB (we have this problem with version 4.8.1) when dealing with a returned soap fault.
You can see the answer of #JorgeInfanteOsorio to disable the endpoint suspension, but you will need an workaround for the "Uncaught exception" issue. What I found is that it is possible to retrieve the parameters from the fault message and then "clear" the current payload, so any subsequent calls wont return the exception issue.
Example:
<!--
After we call a webservice on DSS or other webservice we check if the message contains a fault message
-->
<property xmlns:s="http://www.w3.org/2003/05/soap-envelope"
name="return_fault"
expression="/s:Envelope/s:Body/s:Fault"/>
<filter source="boolean(get-property('return_payzen_fault'))" regex="true">
<then>
<!--
There is a fault message on the payload...
-->
<!--
Retrieve the information from the fault message. How you will access it and which information you will retrieve may be different depending if the answer comes from a third part webservice or from your own DSS.
-->
<property xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" name="code" expression="/s:Envelope/s:Body/s:Fault/s:Code/s:faultcode"/>
<property xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" name="code" expression="/s:Envelope/s:Body/s:Fault/s:Code/s:faultstring"/>
<!--
Clear the payload containing the fault message
-->
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body/>
</soapenv:Envelope>
</format>
<args/>
</payloadFactory>
<!--
Now you can proceed and process the message as usual without getting the exception. You may probably use the parameters stored in the properties at the beginning to determine which error occured and what you are going to do next.
-->
</then>
</filter>
<!--
There is no fault message, you can proceed with the message processing here.
-->

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.

Exposing WSO2 ESB as REST for use in JavaScript

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