WSO2 EI custom exception or error message in REST API - wso2

Can we create custom exception in a REST API? We can use Fault mediator for proxy services. Do we have same kind of option for API's as well.
I need this because I'm using fault sequence to log errors in third party system. For this purpose once error is thrown I'm sending custom message to a REST API. Since this log message is success I'm not getting an error to original client who calling my rest api and end up with 200 OK.
So can we create a custom fault once after fault sequence is invoked?

Yes, you can create a custom fault sequence and add it inside the API. Further, you can modify it as you required.
Sample API:
<api xmlns="http://ws.apache.org/ns/synapse" name="testAPI" context="/test12" version="1.0" version-type="context">
<resource methods="GET" faultSequence="CustomFault">
<inSequence>
<log level="custom">
<property name="message" value="Inside the API"/>
</log>
</inSequence>
</resource>
</api>
Sample Fault Sequence :
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="CustomFault" xmlns="http://ws.apache.org/ns/synapse">
<log level="full">
<property name="MESSAGE" value="Executing default 'fault' sequence"/>
<property expression="get-property('ERROR_CODE')"
name="ERROR_CODE" xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd"/>
<property expression="get-property('ERROR_MESSAGE')"
name="ERROR_MESSAGE"
xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd"/>
</log>
</sequence>

Related

WSO2 APIM - Retrieve Information about an API call in fault sequence

Trying to get the API name in the fault sequence(ex: auth_failure_handler.xml).
Tested as below and it is not working,
<sequence name="_auth_failure_handler_" xmlns="http://ws.apache.org/ns/synapse">
<property name="error_message_type" value="application/xml"/>
<log level="custom">
<property name="ApiName" scope="transport" expression="$ctx:API_NAME"/>
</log>
</sequence>
I think you can try with this:
<property name="ApiName" expression="$ctx:api.ut.api"/>
There are, some other useful properties, witch You can find on github:
APIMgtGatewayConstants

How to avoid ignore the next mediator using response mediator in WSO2 EI?

I have a REST service that returns a json simple {message: "ok"}. When I use Respond Mediator this stop further processing of a message and send the message back to the client.
I need to send response to client, and then execute task asynchronously, but if I use it ignores the next call to a sequence or an endpoint.
How can you respond synchronously (response to client) and then invoke a service, without response mediator ignore the next mediator?
thanks;
I was able to solve the challenge. effectively Clone mediator not just clone message, it give new separate life to new message.
<api xmlns="http://ws.apache.org/ns/synapse" name="porticApiAsyn" context="/test">
<resource methods="POST" url-mapping="/asyn">
<inSequence>
<log>
<property name="message" value="asyn"/>
</log>
<payloadFactory media-type="json">
<format>{"message":"asyn test"}</format>
<args/>
</payloadFactory>
<clone continueParent="true" sequential="true">
<target sequence="logSequence"/>
</clone>
<respond/>
</inSequence>
In this case, the message is sent to logsequence, then the response mediator is executed. Thanks for your help.
For your case you could even do the following
<api xmlns="http://ws.apache.org/ns/synapse" name="porticApiAsyn" context="/test">
<resource methods="POST" url-mapping="/asyn">
<inSequence>
<log>
<property name="message" value="asyn"/>
</log>
<payloadFactory media-type="json">
<format>{"message":"asyn test"}</format>
<args/>
</payloadFactory>
<clone continueParent="true" sequential="true">
<target sequence="logSequence"/>
<target>
<sequence>
<respond/>
</sequence>
</target>
</clone>
</inSequence>
I guess it should work as well and is maybe more readable

WSO2 enterprise integrator, adding header to backend request

I've just started looking at WSO2 enterprise integrator.
I'm currently stuck when I'm trying to create an API which calls a backend service which requires authentication.
I'm currently stuck trying to add a simple header which its suppose to send to the backend as it needs authentication to access it.
I'm using their eclipse tooling and my XML currently looks like this.
<?xml version="1.0" encoding="UTF-8"?>
<api context="/one" name="singleRestApi" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET">
<inSequence>
<header name="Authorization" scope="transport" value="Basic ZWNYGWRtaH42Q2xvdGRBZG1pbjmeMw=="/>
<send>
<endpoint key="repos"/>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
<faultSequence/>
</resource>
</api>
Currently following the integration tutorials: here
Any help or direction would be greatly appreciated.
You should try to put your Authorization in a property instead of an header (if you want to do a simple basic authentication)
<?xml version="1.0" encoding="UTF-8"?>
<api context="/one" name="singleRestApi" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET">
<inSequence>
<property action="remove" name="REST_URL_POSTFIX" scope="axis2"/>
<property name="Authorization" scope="transport" value="Basic ZWNYGWRtaH42Q2xvdGRBZG1pbjmeMw=="/>
<send>
<endpoint key="repos"/>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
<faultSequence/>
</resource>
</api>
the <property action="remove" name="REST_URL_POSTFIX" scope="axis2"/>
part is to remove the api context (after /one) when sending to your backend endpoint, if you need to keep it you can remove this line

WSO2 ESB Service Proxy not timing out as per configuration

We are using latest version of WSO2 ESB(4.6.0).We are exploring on implementing Proxy Service to a web service. The details are as follows:
An Axis2 Webservice Custom service proxy is setup using WSO2. The configuration is as follows:
Proxy XML: This synapse configuration was generated using WSO2 UI.
<proxy xmlns="http://ws.apache.org/ns/synapse" name="StockQuoteProxy" transports="https,http" statistics="enable" trace="enable" startOnLoad="true">
<target faultSequence="myFaultHandler">
<inSequence>
<property name="DISABLE_CHUNKING" value="true" scope="axis2" type="STRING"/>
</inSequence>
<outSequence>
<send/>
</outSequence>
<endpoint>
<address uri="http://localhost:9000/services/SimpleStockQuoteService">
<timeout>
<duration>3000</duration>
<responseAction>fault</responseAction>
</timeout>
<suspendOnFailure>
<errorCodes>101504,101505</errorCodes>
<initialDuration>1000</initialDuration>
<progressionFactor>2.0</progressionFactor>
<maximumDuration>10000</maximumDuration>
</suspendOnFailure>
<markForSuspension>
<errorCodes>101507,101508,101505,101506,101509,101500,101510,101001,101000,101503,101504,101501</errorCodes>
<retriesBeforeSuspension>1</retriesBeforeSuspension>
<retryDelay>1</retryDelay>
</markForSuspension>
</address>
</endpoint>
</target>
<publishWSDL uri="http:// localhost:9000/services/SimpleStockQuoteService?wsdl"/>
<description></description>
</proxy>
Sequence myFaultHandler XML:
<sequence xmlns="http://ws.apache.org/ns/synapse" name="myFaultHandler" trace="enable">
<header name="To" action="remove"/>
<property name="RESPONSE" value="true"/>
<property name="NO_ENTITY_BODY" action="remove" scope="axis2"/>
<log level="custom">
<property xmlns:ns="http://org.apache.synapse/xsd" name="error-message" expression="get-property('ERROR_MESSAGE')"/>
<property xmlns:ns="http://org.apache.synapse/xsd" name="error-code" expression="get-property('ERROR_CODE')"/>
<property xmlns:ns="http://org.apache.synapse/xsd" name="error-detail" expression="get-property('ERROR_DETAIL')"/>
<property xmlns:ns="http://org.apache.synapse/xsd" name="error-exception" expression="get-property('ERROR_EXCEPTION')"/>
</log>
<makefault version="soap12">
<code xmlns:soap12Env="http://www.w3.org/2003/05/soap-envelope" value="soap12Env:Receiver"/>
<reason value="Webservice is either down or currently not reachable."/>
<node></node>
<role></role>
</makefault>
<send/>
</sequence>
When the webservice is down, this configuration throws a soap fault as defined.
When the webservice is taking time to send the response back, as defined in the proxy XML configuration it should timeout after 3 sec:
<timeout>
<duration>3000</duration>
<responseAction>fault</responseAction>
</timeout>
Even after the timeout period the proxy is still waiting for the response instead of throwing the fault back.
On analysis of the log file, we tried by modifying the following parameters in the below mentioned properties file but still the thread was stuck waiting for the response.
**synapse.properties**
synapse.global_timeout_interval=3000
synapse.connection.read_timeout=3000
synapse.connection.connect_timeout=3000
synapse.timeout_handler_interval=3000
**nhttp.properties**
http.socket.timeout=5000
It eventually times out and throws socket exception.
As per the specification (http://wso2.com/library/articles/wso2-enterprise-service-bus-endpoint-error-handling) after the timeout happens the endpoint should go to Timeout state, but in this case the endpoint is still in Active state and it is neither faulting nor discarding message. Some times it throws an error code 504. But this action is not consistent.
Please let know the changes required for a given proxy service to timeout/discard message, if the final webservice is very slow.
If you are using http transport from the axis2.xml (which locates at CARBON_HOME/repository/conf/axis2 directory), you can fix that by configuring the time out parameters in that particular transport sender by adding parameters. For example,
<parameter name="SO_TIMEOUT">3000</parameter>
<parameter name="CONNECTION_TIMEOUT">3000</parameter>
Regards,
Asanka Sanjeewa.

Invoking RESTful service from WSO2 ESB using method POST

I have a simple RESTful service that I want to expose as SOAP based Web Service using WSO2 ESB.
My simple RESTful service can be invoked like http://<<my system>>:8080/myapp/person/read
As response, I get JSON data of the Person entity.
Problem: I am not able to pass parameters to the RESTful service. I am to strip the param value from the SOAP input, but don't know how to pass it to my RESTful; service using ESB.
I have configured the following in WSO2 ESB
<proxy xmlns="http://ws.apache.org/ns/synapse" name="PersonProxy" transports="https,http" statistics="enable" trace="enable" startOnLoad="true">
<target>
<inSequence>
<property xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" name="PERSON_ID" expression="//soapenv:Body/person/id"/>
<log level="full">
<property name="PERSON_ID" expression="get-property('PERSON_ID')"/>
</log>
<filter xpath="//person">
<then>
<property name="REST_URL_POSTFIX" value="read" scope="axis2" type="STRING"/>
<property name="HTTP_METHOD" value="POST" scope="axis2" type="STRING"/>
<property name="id" expression="get-property('PERSON_ID')" scope="axis2" type="STRING"/>
<property name="ContentType" value="application/x-www-form-urlencoded" scope="axis2" type="STRING"/>
</then>
<else/>
</filter>
<send>
<endpoint>
<address uri="http://<<my system>>:8080/myapp/person" format="rest"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
</target>
<description></description>
</proxy>
My SOAP request looks like following
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<person>
<id>3</id>
</person>
</soapenv:Body>
</soapenv:Envelope>
I have another RESTful service with GET method and id as part of the URL itself, and that works fine. The ESB config looks like
<property name="REST_URL_POSTFIX" expression="get-property('PERSON_ID')" scope="axis2" type="STRING"/>
<property name="HTTP_METHOD" value="GET" scope="axis2" type="STRING"/>
<endpoint>
<address uri="http://<<my system>>:8080/cfs/person" format="rest"/>
</endpoint>
Appreciate any pointers or help.
If your service can be invoked as http://<<my system>>:8080/myapp/person/id, you can read the id from the SOAP request and send it using "REST_URL_POSTFIX" property as below.
<property name="REST_URL_POSTFIX" expression="//person/id" scope="axis2" type="STRING"/>
Take a look at this example which implements a similar scenario.
You can also try using the HTTP Endpoint which is new in ESB 4.7.0. You can define a URI Template much like in the REST API. Populating the template variables is done via property mediators - so anything you can do with a property mediator can be used to define the endpoint URL during mediation run time.