WSO2 ESB Collout address from Property or XPath-expression - wso2

I have the config with development and production sections. This sections contain the URLs of backends. In my inSequence i need it to Callout to these backends several times per request.
<config>
<env>prod</env>
<backend env="prod">http://localhost:1234/</backend>
<backend env="dev">http://localhost:2345/</backend>
</config>
I read this config from Local Entry (as XML) and want to set Callout's URL as an Property.
I don't want to hardcode these backends inside my code with "Switch" statement, because it's possible to use more than two environments.
Could you please show me an example?
Thank you.

You can read xml file in registry. Simply define property of OM type like this:
<property name="test" expression="get-property('registry','conf:/test.xml')" scope="default" type="OM" />
Then you can see the value by logging like this:
<log level="custom"> <property name="test.b" expression="$ctx:test//b" /> </log>
And in the xml file that you have put in the root of registry, you would fill it like:
<a>Hello<b>WSO2</b></a>
I have learned it from this link.

I found the answer. According to source of Callout mediator:
CalloutMediator.java
It uses "To" header if URL is not specified.

Related

how to convert json to path variable data in wso2 esb?

I have an Endpoint : http://localhost:8080/customer/get/1
and i want to send a json data to api in wso2esb and transform the value of id to path variable and send it to the Endpoint.
something like this:
{"id":"1"}
how can i do this?
please give me some advice.
You should build Uri-template path, with property named with prefix uri.var. For example named: uri.var.myId. and extract from JSON interesting value.
Next you can use that property in http endpoint in uri-template in brackets.
For your example, what you are looking for is something like this:
<property name="uri.var.myId" expression="json-eval($.id)" scope="default" type="STRING"/>
<call>
<endpoint>
<http uri-template="http://localhost:8080/customer/get/{uri.var.myId}"/>
</endpoint>
</call>
It is more precise described in wso2esb 4.9.0 documentation

WSO2 ESB Dynamic Configuration for Inbound Endpoint

I configured an inbound endpoint, but I want to manage java.naming.provider.url value in a conf.xml file that under config directory in WSO2.
In each environment I have a conf.xml file which includes the URL values for that environment like:
<environment>
<JNPU>test</JNPU>
</environment>
<test>
<JNPU>failover:tcp://localhost:61616</JNPU>
</test>
I just want first read this environment value of the JNPU which is test in this example. Then, I want to change the java.naming.provider.url value in inbound endpoint with this value.
In a sequence, I can read the values with using these properties:
<property name="confFile" expression="get-property('registry','conf:endpoints/conf.xml')" scope="default" type="OM" xmlns:ns="http://org.apache.synapse/xsd"/>
<property name="JNPUEnvValue" expression="evaluate(fn:concat('$confFile//environment//','JNPU'))" scope="default" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
<property name="JNPUValue" expression="evaluate(fn:concat('$ctx:epConfiguration','//',get-property('JNPUEnvValue'),'//','JNPU'))" scope="default" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
I used them as parameters in inbound endpoint but I could not get the value of java.naming.provider.url in conf.xml file.
Thanks for any idea.
We can dynamically configure inbound endpoint parameters, if we save only the respective parameter value as the content of the file saved in registry. For example, in your case, the value "failover:tcp://localhost:61616" should be saved in the file. You can keep separate files for each parameter in a registry location. And, you need to only change file content for each environment.
Please refer the section Specifying inbound endpoint parameters as registry values at https://docs.wso2.com/display/EI650/WSO2+EI+Inbound+Endpoints.
If you can keep the url in the registry, then you can use it from the registry as follows.
<parameter name="java.naming.provider.url" key="gov:/Path/javaNamingProviderURL"/>

WSO2 ESB. Accessing Secure Vault programmaticaly

I am implementing handler for REST API in Java (org.apache.synapse.rest.Handler interface). And there is a case, when I need to access Secure Vault and get a value.
I know that you are able to achieve this by expression="wso2:vault-lookup('YOUR.KEY.HERE')" in sequence, but can't find api to do this in handler. I believe that org.apache.synapse.MessageContext can help, but not sure how.
You can use below code segment in the custom handler.
public String getSecretPassword(String alias, MessageContext messageContext){
RegistrySecretRepository regRepo = new RegistrySecretRepository();
regRepo.setSynCtx(messageContext);
return regRepo.getSecret(alias);
}
Dependency for pom.xml, the version needs to be changed according to your product version.
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.mediation.security</artifactId>
<version>4.2.0</version>
</dependency
Please refer - http://malantech.blogspot.com/2016/10/basic-authentication-handler-with.html
Thanks
I believe you will not be able to get the value of the security vault directly from your handler so I advise you to recover the password and put it in a property and inside your handler to retrieve the property.
<property name="passwordvault"
expression="wso2:vault-lookup('YOUR.KEY.HERE')"
scope="default"/>
And use the MessageContext to get the propertie like this:
context.getProperty("passwordvault");
That's just a workaround which is not advisable , i believe you can try below code as i have used similar earlier as well and it worked
<property expression="wso2:vault-lookup('ei.training.userid')" name="UserID" scope="default" type="STRING"/>
<log>
<property expression="wso2:vault-lookup('ei.training.userid')" name="UID"/>
</log>
And I will answer my own question.
I've created a dummy sequence and placed it into Registry
<sequence name="SecureVaultSeq" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<property expression="wso2:vault-lookup('MY.PASS')" name="NAME"
scope="default" type="STRING"
xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd"/>
</sequence>
Then in my handler i retrieved it like this:
messageContext.getConfiguration().getSequence("conf:Resources/sequences/SecureVaultSeq.xml").mediate(messageContext);
key = (String) messageContext.getProperty("NAME");
Hope this will help someone.

Parse JSON Response Array using ForEach component in WSO2 ESB

I have a requirement to parse JSON response which contain array of products(P1,P2,P3,etc.). Each product contains multiple information like name, type, cost, etc.
I need to read each product one by one and append additional data got from the another service into an new JSON output. I am thinking of using ForEach component of WSO2 ESB to iterate each product one by one.
Problem is that ForEach component uses ForEachExpression which expects XML expression in the configuration.
Please suggest on the method to parse array of JSON response one by one in WSO2 ESB.
/Abhishek
Can use both Iterate or ForEach mediator for iterating the JSON array, since both are content aware mediators and support JSON.
Which one to use depends upon the specific usecase as Iterate provides the capability to use call / callout / send mediators in the sequence whereas ForEach doesn´t allow it.
For the below given sample JSON request, the following iteration works and the JSON array and objects can be referred like the same.
{
"products":[
{
"product":{
"id":"1234",
"size":"20",
"quantity":"1",
"price":"990",
"type":"Electronics",
"store":{
"id":"001"
}
}
}
]
}
<iterate expression="//products" id="PRD_ITR">
<target>
<sequence>
<sequence key="Product_Enrich_Sequence_s"/>
<!-- For example, Switching sequence based on the product type -->
<switch source="//products/product/type">
<case regex="Computer">
<sequence key="Computer_Product_Enrich_Sequence_s"/>
</case>
<case regex="Mobile">
<sequence key="Mobile_Product_Enrich_Sequence_s"/>
</case>
<case regex="Grocery">
<sequence key="GR_Product_Enrich_Sequence_s"/>
</case>
<default>
<!-- default stuff --!>
</default>
</switch>
</sequence>
</target>
</iterate>
FYR
https://docs.wso2.com/display/ESB490/Iterate+Mediator
https://docs.wso2.com/display/ESB490/ForEach+Mediator
Note: tested in WSO2 ESB 4.9.0

wso2: Looking for XSLT transformation working example

Can some one point me to a working example of xsl transformation using the proxy services xslt mediator option.
Basically, my requirement is, i will have a request where i will get some data which determines the routing and after that from other elements of requested data i have to re frame soap request to trigger another bpel service.
Please let me know the better approach to this.
You can very well use XSLT transformation in your sequence, using XSLT Mediator.
In your sequence file you can specify the XSLT file to tranform the request. Sample sequence code snippet:
<sequence xmlns="http://ws.apache.org/ns/synapse" name="SampleInterceptorSequence">
<in>
<log level="full" category="DEBUG">
<property name="sequence" value="inSequence-Request Before XSLT" />
</log>
<xslt key="RequestTranformerXSLT" />
<log level="full" category="DEBUG">
<property name="sequence" value="inSequence-Request After XSLT" />
</log>
<send>
<endpoint key="MyActualServiceEPR" />
</send>
</in>
Your xslt would contain the style for the actual request to be formed for hitting the end point reference.
Further if you can check this nice article of web service chaining to get a real time idea of xslt mediation.
Web Service Chaining from WSO2 ESB Developers
Hope this helps.
Thanks.
Find the sample below..
http://wso2.org/project/esb/java/4.0.0/docs/samples/message_mediation_samples.html#Sample8