wso2am Elastic Search lookup - wso2

I switched from Apigee to WSO2 2.1 but on piece of functionality is missing.
When my Oauth users make API calls, I want to add an additional Header
to the backend request. E.g.: "X-Customer-Name: CUST_NAME
I have the Java code to lookup "CUST_NAME" in ElasticSearch based on
the user's consumer key & secret. How would I integrate that code into
WSO2 to be able to lookup the values and send the extra X-Customer-Name header to my backend?

Java classes can be integrated / used in WSO2 mediations using <class/> mediator.
To achieve this, the custom java class can extend AbstractMediator class and can implement the logic in the mediate method.
Create a new insequence, like the following and invoke the custom class, this way the custom property can be set to the message context.
<sequence name="TokenExchange" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<class description="" name="com.customer.CustomerName"/>
<property expression="get-property('Customer-Name','custName')" name="cs" scope="default" type="STRING"/>
</sequence>
Use this newly built inSequence into your API In-Flow message mediation flow and pass the appropriate properties to the backend.

Related

API Name property in WSO2 Api Manager 3.2.0

In the 2.6.0 version of the Api Manager from WSO2, I was using custom sequence to log every flux with API Name, resources, applications, type of token etc... in Input and Output Seq
<log level="custom">
<property expression="fn:concat('[V2][API][',$ctx:api.ut.application.name,'][',$ctx:API_NAME,'][',$ctx:AM_KEY_TYPE,'][',$ctx:REST_SUB_REQUEST_PATH,'][',$ctx:api.ut.userId,'][',$ctx:EXECUTION_TIME_KAFKA,'][',$ctx:SYNAPSE_REST_API_VERSION,'][NOCOMMENT]')" name="LOG2PARSE"/>
</log>
Since 3.2.0, I am not able anymore to use the "$ctx:API_NAME" property.
Do you have any idea what is the right property to retrieve the API Name ? If I can, I do not want to use custom property to get the name.
Also, I can't find in the documentation which are the generics properties.
Do you have any idea ?
Thank you !
For API Name you can use the following.
$ctx:api.ut.api
Some of the other properties can be found here - https://github.com/wso2/carbon-apimgt/blob/master/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/APIMgtGatewayConstants.java#L24

WSO2 api manager mediation

I'm using apim 2 and I need to route client call to the back end service dynamically at runtime.
I have added a custom sequence in the folder WSOAM_HOME\repository\resources\customsequences\in
<sequence xmlns="http://ws.apache.org/ns/synapse" name="mySeq">
<class name="org.wso2.carbon.env.EnvironmentResolver"/>
</sequence>
I have added the jar containing my implementation under repository/component/lib.
I have configured my API to use this custom sequence throught the publisher portal,but The problem is that the EnvironmentResolver is not being called!!
Did i miss any thing?

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

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>

WSO2 API Manager - Replace URLs in response body

I'm trying to setup a proxy for my RESTful API using WSO2 API Manager. My problem is that the responses from the backend API are left untouched so all the urls that connect to other endpoints still reference the backend server rather than the proxy. I need a way to replace those url values in the response body to point to the proxied api. I understand this can be accomplished via Mediation Extensions, using ESB Mediators.
I'm not familiar enough with them to pick the one better suited for the job. URLRewrite mediator looks pretty straightforward, but it doesn't seem to apply to the message body but the headers. Payload Factory seems to require a fixed structure for the message, which is not very convenient for me, since I need it to work on the different responses that my API provides (and I wouldn't want to maintain those structures in the mediator definition).
I've managed to solve it by setting the headers my application checks to build its urls:X-Forwarded-Host and X-Forwarded-Proto.
So I've created a Header Mediator that looks like:
<sequence xmlns="http://ws.apache.org/ns/synapse" name="WSO2AM--Ext--In">
<header name="X-Forwarded-Host" expression="get-property('transport','Host')" scope="transport"/>
<header name="X-Forwarded-Proto" value="https" scope="transport"/>
</sequence>
And that did the trick.

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.