How to access system property from WSO2 ESB and Registry - wso2

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.

Related

WSO2 ESB: Get Address Endpoint URI from registry

I want to define my URL in the registry so that I can change it between environments without having to redeploy CAR files. I have done this successfully in the past for HTTP Endpoints, but with Address Endpoints, I cannot get it to work.
How I normally would do it, is to declare the property in the API.xml file:
<property name="uri.var.my_EP" expression="get-property('registry', 'gov:/integration/endpoints/myapp/my_EP')" scope="default" type="STRING"/>
Then I simply replace the hard-coded URI with my property in the endpoint.xml file:
<http method="post" uri-template="{uri.var.my_EP}">
If I do the following on the Address Endpoint, I get a "The system cannot infer the transport information from the {uri.var.my_EP}" error:
<address uri="{uri.var.my_EP}">
What/how must I do to define the URI for Address Endpoints in the registry?
If you have properly stored in registry, you can use endpoint directly like below:
<endpoint key="gov:/integration/endpoints/myapp/my_EP"/>
Personally, for endpoints i use conf:/.. repository.

In wso2 esb how to read endpoint from a file

In wso2 esb what is the best practice for Endpoint maintenance. From some article I got to know that we can read from file So, if this the best approach how achieve this one.
In a scenario where the endpoint is saved as a file within the carbon registry (You can even upload the file via management console or can use WSO2 EI Tooling to create an endpoint template), we can read the content of the endpoint as follows.
Add the endpoint registry resource.
Log the endpoint content using the following synapse configuration. (Can even retrieve specific attributes within the endpoint using their xpath expressions)
<log level="custom">
<property name="Endpoint Content:" expression="$ctx:endpointFile//*"/>
<property name="Endpoint URL:" expression="$ctx:endpointFile//*[local-name() = 'address'][1]/#uri"/>
</log>
You can see the logged endpoint content as below.
There are 3 ways to Endpoint Maintenance.
Using ESB Tooling
From Command Line
Using a Script
More details can be found in WSO2 Documentation.

wso2am Elastic Search lookup

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.

Configurable Values In WSO2 ESB similar to .properties file

I would like to know if there is a way to maintain certain custom configuration values in a .properties file (in Java) and load the properties at ESB startup and use the custom property values within a mediation flow? any ideas on this would be really helpful.
This might be what you are looking for:
https://dzone.com/articles/retrieve-values-xml-config (page dated ~2013)
It shows how you can have a file in the WSO2 registry and read it in your proxy.
(the file stored in the registry can contain your properties)
I would agree with the suggestion to save the properties in an XML file which you then upload to your registry, independent of the code.
That way, the properties can be updated or deleted without having to touch the code.
If you are constrained to use Java .properties files, then it might be advisable to create a JAR that is deployed as a library, which you then call from your ESB sequence.
For a similar use case, we had a custom configuration values / parameters or constants into a global_parameter.xml file and have it managed in a governance registry (decide based on your stack).
For example, the global_parameter.xml can be like the following.
<custom>
<Version>2.3</Version>
<Type>FOR</Type>
</custom>
We can load the parameters through a sequence and then use the parameters by reusing the sequence file.
<sequence xmlns="http://ws.apache.org/ns/synapse" name="loadProperties_seq">
<property xmlns:ns="http://org.apache.synapse/xsd" name="localProperties" expression="get-property('registry', 'gov:/common/utils/properties/global_parameter.xml')" scope="default" type="OM"/>
</sequence>
Refer the properties..
<property name="url_reg" expression="//custom/Version"/>
<property name="user_reg" expression="//constants/Type"/>

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>