Configurable Values In WSO2 ESB similar to .properties file - wso2

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"/>

Related

How to log request headers within script mediator in Wso2 API Manager 3.2?

I am trying to get all transport headers used when calling a specific API and log that for debugging, since we do not know what the name of the headers will be I want to log them all.
I know this can be done via a class mediator as well as by enabling wire logs but I am looking for an option to achieve this without having to do either of those.
I have tried using script mediator and then using: mc.getProperty("org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS") to fetch them from message context but it just returns null.
Any suggestions?
You are trying to get a property with the name "org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS" from the default Context, hence it's returning null. You can't really access TRANSPORT_HEADERS from the Axis2Context within a ScriptMediator. Your best option is to write a class mediator.
Use get-property from the transport layer.
Only you need to know the name of the variables.
<log level="headers" category="INFO">
<property name="inicio" value="------ begin -------"/>
<property name="X_Teste_FOS" expression="get-property('transport','X-Test-FOS')"/>
<property name="inicio" value="------ end -------"/>
</log>

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>

Can an excel datasource in WSO2 data service take a variable name for the excelURL

The following is the excel data source definition in my wso2 data service.
Is there a way to define a property in a proxy and use that as the excel Url in the excel data source of the data service ?
In other words, I need the excel file to be dynamically determined.
The following is the data source definition in the data service
<config id="OrganisationImportDataSource">
<property name="excel_datasource">C:/Organisations.xls</property>
</config>
Instead of this, is it possible to use something like the below
<config id="OrganisationImportDataSource">
<property name="excel_datasource">get-property('fileName', 'default')
</property>
</config> where fileName is a property defined in a calling proxy
The excel data source is validated at wso2 startup. Thus what I was trying like above does not work.
We re-aligned our requirement to the limitation that excel data source need to be pre-defined and redefined our requirement to ensure tenant specificity of excel data source. For this, we uploaded the excel in a specific location within configuration / goverance registry. This is tenant specific even if we have the same name for excel data source.
So the following configuration is tenant specific
<config id="OrganisationImportDataSource">
<property name="excel_datasource">conf:/repository/Organisations.xls</property>
</config>

How to use WSO2 ESB to mediate fixed length text data

I have the needs to use WSO2 ESB to mediate the incoming fixed length text data as received via MQ, one line of text per message, into XML format and then send the transformed data onto an SOAP endpoint via HTTP.
I understand that I could use WSO2 ESB admin console to configure an InSequence to do the data parsing and mapping with substring function and then set up a proxy service to include this configured sequenece.
I would need helps about how to do all these in details in steps. Greatly appreciate if someone could provide some examples or links to some webpages about how-to.
Thanks!
You should have a look to smooks, I think this is the best solution :
CSV : http://wso2.com/library/blog-post/2013/09/csv-to-xml-transformation-with-wso2-esb-smooks-mediator/
Fixed Lengh text : http://vvratha.blogspot.fr/2014/05/processing-large-text-file-using-smooks.html
An other solution would be to write your own messageBuilder, search "org.apache.axis2.format.PlainTextBuilder" to find the source code...
I agree with Jean-Michel, that smooks would be a good solution. But, this is also possible to do within a single simple proxy service. Set up a simple pass-through proxy to your endpoint. Then, open it up in source view (or the wizard) and configure the insequence to add a PayloadMediator.
Here is an example of how to use Payload Mediator [1]
Here is an excerpted example of what that would look like with a few xpath expressions to extract fixed-length fields from your input:
<payloadFactory media-type="xml">
<format>
<m:body xmlns:m="http://services.samples">
<m:field1>$1</m:field1>
<m:field2>$2</m:field2>
</m:body>
</format>
<args>
<arg expression="substring(//*,0,10)"/>
<arg expression="substring(//*,10,10)"/>
</args>
</payloadFactory>
You may also need to use the content type property in your sequence, because you're changing the content type to xml:
<property name="ContentType" value="text/xml" scope="axis2"/>
Best of luck!
[1] https://docs.wso2.com/pages/viewpage.action?pageId=33136018
For those who are interested in a working solution, here is my smooks configuration:
<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:fl="http://www.milyn.org/xsd/smooks/fixed-length-1.3.xsd">
<fl:reader fields="price[5]?trim,quantity[5]?trim,symbol[5]?trim.upper_case,comment[10]?trim" recordElementName="order">
<fl:listBinding beanId="order" class="test.Order" />
</fl:reader>
</smooks-resource-list>
Also, need to add the jar file of test.Order to the classpath of WSO2 ESB.

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.