How to add a URL parameter using a WSO2 Mediation Flow? - wso2

I have successfully deployed a WSO2 API Manager. I am already using mediation flows for setting Header information, but now I am adding an API that requires a key to be set as an URL parameter. However I would like this to be added in the background so that the end-users don't have to worry about this key.
How can this be done in a Message Mediation Policy/Flow? Obviously the other parameters that are already present should stay untouched.
Thanks in advance

Hope you can access the key inside the synapse context. Then you can assign the key value to the uri.var object as below.
<property name="uri.var.key" expression="get-property('userParames.key')"/>
Now you can simply construct the endpoint as,
<endpoint>
<http uri-template="https://{uri.var.hostname}:{uri.var.portnum}/abc/{uri.var.key}"/>
</endpoint>

Related

wso2 apeman3: Where do I put the APPID when configuring to call OpenWeather api?

I'm wrapping OpenweatherAPI in a wso2 api : WeatherAPI
The openweatherapi has an appid= passed in every call. Where do I put it in WeatherAPI wso2-config ?
Note that I'm incorrectly showing resource and APPID in the endpoint section.
The production endpoint must only read:
https://api.openweathermap.org/data/2.5/
Please provide screenshot in your answer.
Backend Endpoint: https://api.openweathermap.org/data/2.5/
There should be a resource named /weather which accepts query parameter q. (i.e the API query)
APPID is the token that should be sent in every call, which is an access key for Weather API.
Since the APPID is a constant and should be sent with every request, you can add it in a mediation sequence and engage it in the as the In-Sequence of the API.
You can use a sequence similar to this example.
<sequence xmlns="http://ws.apache.org/ns/synapse" name="append_app_id">
<property name="REST_URL_POSTFIX" expression="fn:concat(get-property('axis2','REST_URL_POSTFIX'), '&APPID=<value>')" scope="axis2" type="STRING"/>
</sequence>

WSO2 ESB REST API Chaining issue

I am facing some issues when doing service chaining is WSO2 ESB. Below is the xml file.
Following is my use case. I need to call Service 1, get the response, do validation check on it and then call Service 2. Through the below code I am successfully able to call Service 1. For the service two request, I have hard coded the request in the payload. Issue is coming when setting the header parameters. The header properties are not getting set due to which call to Service 2 is not going. For testing purpose I have kept both the URLs same.
Please let me know the following:
1. How to set HTTP Header values.
2. Is there a way to persist the Initial input request and then use it in the second Service call.
Although your synapse configs are not there I'll answer your question.
You can do this two ways. One is by using the Header Mediator. You can reffer this Doc. Example code below,
<header name="Accept" value="image/jpeg" scope="transport"/>
The second approach is using the property mediator, You can set the Header value and set the scope to transport. So the Header property will be added.
What you simply need to do it assign the original request content to a Property, so you can use it later. There are many ways to do this, Following example is by using the enrich mediator
<enrich>
<source type="body" clone="true"/>
<target type="property" property="request"/>
</enrich>

How do I pass a dynamic parameter as a part of the URL of a REST service while using WSO2 ESB Proxy Service?

I am trying to simply host a REST service as a proxy service in WSO2 ESB. I am using Custom Proxy to do this. When I run the created proxy, I am not able to pass parameters to the proxy service at run time. How do I do this?
My REST service will be hit on a URL of format:http://ip:host/requestMapping/{name}
The parameter 'name' has to be passed from the UI through the ESB to the service through a proxy service hosted on the ESB. Can you help me with the steps to follow to make this work?
I tried using this page:
http://wso2.com/library/articles/2013/12/restful-integration-with-wso2-esb/
But that is for creating APIs which I have been successfully in creating. But I am unable to do this using Proxy services.
Basically in my program, when the user interacts with the UI, he enters a name as input. This name has to be passed to the proxy service hosted in the ESB which should forward this as a path variable to my REST service.
Right now, my service body is:
<inSequence>
<send>
<endpoint>
<http method="POST" uri-template="http://ip:port/resourceMapping/{uri.var.name}"></http>
</endpoint>
</send>
</inSequence>
<outSequence>
<send></send>
</outSequence>
When I run this, instead of sending the name as entered by the user, it sends "uri.var.name" to the REST service. On the other hand, if I define a property tag and define a value for it and then do this, the REST service gets the value of the property that I have set. But I need this value to be sent dynamically by the user so I can't define property and value inside.
Please help me.
I think, the best approach is to use API in this case because the request to the ESB is REST and endpoint also accepting REST. This is the best practice.

WSO2 indirect Endpoint inside Failover Group

Is it possible to use an Indirect Enpoint inside the definition of a Failover Group endpoint?
I mean something like this:
<endpoint xmlns="http://ws.apache.org/ns/synapse" name="failOver2">
<failover>
<endpoint key="LBEndpoint" />
</failover>
</endpoint>
Where LBEndpoint is a load balance endpoint defined and saved previously in the registry.
When i click on the Save button... all my configuration is lost and the fail over group comes back into this form:
<endpoint xmlns="http://ws.apache.org/ns/synapse" name="failOver2">
<failover/>
</endpoint>
Is this a bug? Inside the WSO2 ESB documentation it says that all this is possible.
Can you try editing synapse configuration directly? That is, not using sequence editor UI. Go to sourceview and manually edit and point the endpoint key in failoverendpoint.

How to access system property from WSO2 ESB and Registry

My WSO2 ESB proxy service references an endpoint which is located at different URLs in various environments - DEV, TEST, PROD. According to the WSO2 documentation, I need to store the endpoint definition in the Governance Registry and modify the URL in endpoint XML file in each environment. That might work fine for the organizations with 1 or 2 proxies, but becomes a significant overhead for a 10+ proxies.
Another scenario is when I need to read certain environment-specific properties in my ESB sequence.
Is there a way to define a bunch of properties in the external *.properties file and then read them within the ESB and Registry definitions?
You can access system properties inside ESB sequences/proxy services using the script mediator as follows;
<script language="js">mc.setProperty("file.separator",java.lang.System.getProperty("file.separator"));</script>
<log level="custom">
<property name="file.separator" expression="get-property('file.separator')"/>
</log>
Here "file.separator" property is set as the property in the message context inside the script mediator and it can be used in subsequent mediators.
You also can access properties defined in a file in ESB registry. For example if you have a file in configuration registry (test.xml) with the following content,
<a>Helloo<b>World</b></a>
The text element "World" in <b> can be accessed using property mediator as follows,
<property name="test" expression="get-property('registry','conf:/test.xml')" scope="default" type="OM"/>
<log level="custom">
<property name="test.b" expression="$ctx:test//b"/>
</log>
here is a blog post on how to access registry resources from a classmeditor1. You can access any resources as mentioned in the post and do modifications.
Likewise you can keep the external properties file and read that from the classmeditor and set all properties in synapse message context using class meditaor.