WSO2 enterprise integrator, adding header to backend request - wso2

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

Related

WSO2 Datamapper shwoing not found in postman

I am trying to use data mapper in wso2 and translating the json to xml but it doesn't showing any result instead it showing page not found in POSTMAN.
<?xml version="1.0" encoding="UTF-8"?>
<api context="/TEST" name="TEST" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<datamapper config="gov:datamapper/NewConfig.dmc" inputSchema="gov:datamapper/NewConfig_inputSchema.json" inputType="JSON" outputSchema="gov:datamapper/NewConfig_outputSchema.json" outputType="XML"/>
<property description="" name="ContentType" scope="axis2" type="STRING" value="application/xml"/>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
Can anyone help me in this?

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

How to read query param in WSO2 ESB 4.8 or above?

I have a rest end point in WSO2ESB (4.8),I need to read query parameter to set as dynamic payload as the my business ,But i failed to read it due to newer with wso2 ESB.Any help ?
The bellow code 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>
Define a REST API inside ESB and access to query params with get-property('query.param.xxx') or get-property('uri.var.yyy'), sample :
<resource methods="GET" uri-template="/testwso2/{symbol}?arg1={value1}">
get-property('query.param.arg1')
get-property('uri.var.symbol')

using cache with json\rest services in wso2esb

with wso2esb 4.7.0 i deployed the following service to perform a proxy and expose the same json i get from a different server:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="crmws"
transports="http"
statistics="enable"
trace="enable"
startOnLoad="true">
<target>
<inSequence>
<log level="full"/>
<property name="messageType" value="text/xml" scope="axis2"/>
<cache id="c1"
scope="per-host"
collector="false"
hashGenerator="org.wso2.caching.digest.DOMHASHGenerator"
timeout="6"
maxMessageSize="10000">
<implementation type="memory" maxSize="10000"/>
</cache>
<send>
<endpoint>
<address uri="http://crm/backoffice/webservice.php" format="rest">
<timeout>
<duration>3000</duration>
<responseAction>fault</responseAction>
</timeout>
</address>
</endpoint>
</send>
</inSequence>
<outSequence>
<log level="full"/>
<property name="messageType" value="application/json" scope="axis2"/>
<cache id="c1" scope="per-host" collector="true"/>
<send/>
</outSequence>
</target>
<description/>
</proxy>
i then try to implement some cache functionality, but even if first connection goes well, when i hit cache i get an empty answer with text/xml content-type.
UPDATE 20131204
I try again after 4.8.0 release and then enabling cache i get the full representation of my json in xml when i hit cache
anyone know where i have to look to fix this issue?
regards,
try adding the following property in your insequence.
eg:
<inSequence>
<property name="NO_ENTITY_BODY" scope="axis2" action="remove"/>
---------------------------

adding post request in my WSO2 proxy service

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.