adding post request in my WSO2 proxy service - wso2

I have created a proxy service who transform a message using a xslt mediator and then transformed to JSON,
I want now to post the JSON message in a rest web service how i can do that directely in my proxy service?
This is my proxy service :
<proxy xmlns="http://ws.apache.org/ns/synapse" name="CategoryProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<property name="Authorization" expression="fn:concat('Basic ', base64Encode('admin:admin'))" scope="transport" type="STRING"/>
<send>
<endpoint>
<address uri="http://localhost:8068/database/library.author/301"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<xslt key="conf:/ressources/catXsl.xsl"/>
<property name="messageType" value="application/json" scope="axis2" type="STRING"/>
<send/>
</outSequence>
<faultSequence/>
</target>
<description></description>
</proxy>
I want the message sent by this proxy to be post in a rest web ressource, How I can do it in my proxy?

Looks like you are looking for service chaining and you can do that in two ways:
1) Using a receiving sequence
<inSequence>
<property name="Authorization" expression="fn:concat('Basic ', base64Encode('admin:admin'))" scope="transport" type="STRING"/>
<send receive="receivingSeqForChaining">
<endpoint>
<address uri="http://localhost:8068/database/library.author/301"/>
</endpoint>
</send>
</inSequence>
You can define whatever sequence of operations you want in that receiving sequence. Output from this will be handed out to receiving sequence. If you don't want to do any specific action in outSequence for this case you can just add a to that.
2) Using outSequence to trigger other service call
You can also make that http call directly from outSequence something like this:
<outSequence>
<send>
<endpoint>
<http method="get"
uri-template="https://www.abc.com/resource/foo"/>
</endpoint>
</send>
</outSequence>
Hope this helps.

Related

In wso2 esb how to call websokcet published by api manager

I have published a websocket api as given in the Documentation, So I wanted to call that websocket API through wso2 ESB. So, how to pass the Auth header as I'm getting the following error on the console.
[2018-10-02 18:24:36,503] ERROR - InboundWebsocketSourceHandler Endpoint not found for port : 9099 tenant domain : null
Below is the ESB to call websocket API:
<api context="/test" name="test" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<property name="OUT_ONLY" scope="default" type="STRING" value="true"/>
<log level="full">
<property name="message" value="********************************************input**********************"/>
</log>
<property name="Authorization" scope="transport" type="STRING" value="Bearer cf920eb2-960c-383c-b556-78e9258fd8d3"/>
<send>
<endpoint>
<http method="post" uri-template="ws://localhost:9099/echowebsocket/1.0"/>
</endpoint>
</send>
</inSequence>
<outSequence>
</outSequence>
<faultSequence/>
</resource>
</api>
outflow sequence:
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="outflowDispatchSeq" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<log level="full">
<property name="message" value="************************************************"/>
</log>
</sequence>

How to add query params to URL in API in WSO2 ESB

I need to append query params to endpoint URI according to query params in request.
I have an resource API in ESB published like this:
<resource methods="GET" url-mapping="/searchEngine/sortAndFilterVolunteers*">
<inSequence>
<send>
<endpoint>
<address
uri="http://localhost:8080/project-web-services/services/project-rs"></address>
</endpoint>
</send>
</inSequence>
I have to append query params dinamically.
How could I do that?
Following sample API definition may help you.
<api xmlns="http://ws.apache.org/ns/synapse" name="sample" context="/api/sample">
<resource methods="OPTIONS GET" uri-template="/{val1}/groups/{val2}.json?q1={v1}&q2={v2}">
<inSequence>
<property name="uri.var.q1" expression="$url:q1"></property>
<property name="uri.var.q2" expression="$url:q2"></property>
<property name="uri.var.val1" expression="get-property('uri.var.val1')"></property>
<property name="uri.var.val2" expression="get-property('uri.var.val2')"></property>
<send>
<endpoint>
<http method="GET" uri-template=""></http>
</endpoint>
</send>
</inSequence>
<outSequence>
<send></send>
</outSequence>
</resource>
</api>
Add the query parameter to uri-template in resource as below :
<resource methods="GET" uri-template="/getStudents/{id}">
When sending to the endpoint,
<send>
<endpoint>
<http method="get" uri-template="http://studentsapi/student/{uri.var.id}"/>
</endpoint>
</send>
Add the query parameter to url-mapping in resource as below : url-mapping="/*"
*
you can send a request may be server:port/service?q1={q1} and other request may be server:port/service?q2={q2}&q3={q3}.

How to extract http headers using a mediator

How do I extract http headers like
Authorization: ​"admin 0PN5J17HBGZHT7JJ3X82"
where admin is the username and 0PN5J17HBGZHT7JJ3X82 is the password and assign it to a property/variable which would be then passed to a dss service for user login verification. From what I know our API can be do this using custom sequences and mediators (https://docs.wso2.com/display/AM170/Adding+Mediation+Extensions) but its not clear tome on how to extract this header and assign it to different property names like login and password.
Does a mediator header can take care of this? Or this there another way of doing by using a proxy service?
Header Mediator
<in>
<header name="Authorization" value="admin 0PN5J17HBGZHT7JJ3X82" scope="transport"/>
<send>
<endpoint name="people">
<address uri="http://localhost:9443/testapi/" format="get"/>
</endpoint>
</send>
</in>
<out>
<send/>
</out>
Proxy Service
<proxy name="adminServiceProxy" transports="https http"
startOnLoad="true" trace="disable">
<description/>
<target>
<endpoint>
<address uri="https://localhost:9443/testapi"/>
</endpoint>
<inSequence>
<property name="Authorization"
expression="fn:concat('Basic ','admin:0PN5J17HBGZHT7JJ3X82')"
scope="transport"/>
</inSequence>
<outSequence>
<send/>
</outSequence>
</target>
</proxy>
Thank you
You can extract like this;
<property name="AuthHeader" expression="$trp:Authorization"/>
Then log it and see what you are retrieving..
<log>
<property name =" Authheder value" expression=get-property('AuthHeader')/>
</log>
Then construct Basic auth header as you pointed in your proxy configuration.
Here is a blog post which explains how you can retrive various information from a sequence

Adding parameters to the endpoint url wso2 esb

I am new to the WSO2 esb. I want to create a api in which i want to add a dynamic parameter from api Url to endpoint url.
My end point url is like http://example.com/api/sync/{session_id}.json
I tried to create api like
<api name="rest" context="/sync">
<resource methods="GET" uri-template="/{id}">
<inSequence>
<log level="full">
<property xmlns:ns="http://org.apache.synapse/xsd"
name="uri.var.session"
expression="get-property('uri.var.id')"
scope="axis2"/>
</log>
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
<send>
<endpoint name="Mecars">
<http trace="enable"
method="get"
uri-template="http://example.com/sync/{+uri.var.session}.json"/>
</endpoint>
</send>
</inSequence>
</resource>
</api>
In log i get the value of uri.var.session But on the endpoint Uri-template it is not appending.
Please guid me how append the {id} value in the api uri-template to end point uri-template?
Check this sample
I recommend you to do string concatenation at property mediator adn use that directly at uri-template, rather adding "variable +.json" at uri-template.
That is;
<property xmlns:ns="http://org.apache.synapse/xsd"
name="uri.var.session"
expression="get-property('uri.var.id')"
scope="axis2"/>
In the above for expression do string concatenation to construct full variable with ".json" ending.
Please remove the '+' symbol from your http end point.Its working fine to me
sample API
<api xmlns="http://ws.apache.org/ns/synapse" name="Elastic Search Using NPI" context="/api/npi/professional/npi.json">
<resource methods="OPTIONS GET" uri-template="/{npi}">
<inSequence>
<log level="full">
<property name="uri.var.npi" expression="get-property('uri.var.npi')"></property>
</log>
<send>
<endpoint>
<http method="get" uri-template="example.com/{uri.var.npi}"></http>
</endpoint>
</send>
</inSequence>
</resource>
</api>
Change the http endpoint uri template like follows.
<http trace="enable" method="get" uri-template="http://example.com/sync/{uri.var.id}.json"></http>
Following is the modified api configuration.
<api xmlns="http://ws.apache.org/ns/synapse" name="rest" context="/sync">
<resource methods="GET" uri-template="/{id}">
<inSequence>
<log level="full">
<property xmlns:ns="http://org.apache.synapse/xsd" name="uri.var.session" expression="get-property('uri.var.id')"></property>
</log>
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"></property>
<send>
<endpoint name="Mecars">
<http trace="enable" method="get" uri-template="http://example.com/sync/{uri.var.id}.json"></http>
</endpoint>
</send>
</inSequence>
</resource>
</api>
You can do the string concatenation at the property mediator as follows.
<property xmlns:ns="http://org.apache.synapse/xsd"
name="uri.var.session"
expression="fn:concat(get-property('uri.var.id'),'.json')"
scope="axis2"/>

how we can store a data or string in txt file in wso2 esb

my issue is i am getting data or json or string from front end or mobile app which contains details of error i need append the all details in single txt file in my local system
how can i do this we have any mediator for this or may i use vfs trnsport let me knoe i tried with this code giving error
My config is:
<proxy xmlns="http://ws.apache.org/ns/synapse" name="FileWrite" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<log level="custom">
<property name="sequence" value="fileWriteSequence"/>
</log>
<log>
<property name="transport.vfs.ReplyFileName" expression="fn:substring-after(get-property('MessageID'), 'urn:uuid:')"/>
<property name="OUT_ONLY" value="true"/>
</log>
<send>
<endpoint>
<address uri="///home/youtility2/Desktop/Errorlog"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
</target>
<parameter name="transport.vfs.ContentType">text/plain</parameter>
<description></description>
</proxy>
Error throing from esb side
2013-04-01 15:58:04,707] ERROR - ClientUtils The system cannot infer the transport information from the ///home/youtility2/Desktop/Errorlog URL.
[2013-04-01 15:58:04,708] ERROR - Axis2Sender Unexpected error during sending message out
org.apache.axis2.AxisFault: The system cannot infer the transport information from the ///home/youtility2/Desktop/Errorlog URL.
at org.apache.axis2.description.ClientUtils.inferOutTransport(ClientUtils.java:81
) have any refernace let me know.
You need to append the "transport.vfs.Append=true" to out-file URI to append the data in to the existing file... There is a thread regarding this in stackoverflow see the [1]. For more details regarding the VFS please refer the [2].
[1] How to append response message to a text file?
[2] http://docs.wso2.org/wiki/display/ESB403/VFS+Transport
Regards,
Mohan
Sequence Defined inside Log mediator.Define like below
<inSequence>
<log level="full"/>
<property name="sequence" value="fileWriteSequence"/>
<property name="transport.vfs.ReplyFileName" expression="fn:substring-after(get-property('MessageID'), 'urn:uuid:')"/>
<property name="OUT_ONLY" value="true"/>
<send>
<endpoint>
<address uri="///home/youtility2/Desktop/Errorlog"/>
</endpoint>
</send>
</inSequence>