How to limit length of properties in logs in WSO2 ESB? - wso2

I need to cut body content in logs in WSO2 ESB. For this purpose, I have two parameters in registry: enabled (true or false) and limit (integer, for example: 1024).
For that, I have next expression in a property:
<property name="BODY_TO_LOG" expression="fn:substring(get-property('default','BODY'),0,getproperty('default','LIMIT'))" type="STRING" scope="default"/>
Note that BODY and LIMIT properties have been set previously.
My problem is it does not limit in logs, although the value of these properties are right.
Is it correct that form of limit the content of property?

Your expression should work. I have tested it and came to the nonsensical conclusion that when you declare your LIMIT property with type INTEGER, it doesn't log anything. When you declare your LIMIT property without a type - it works. If you declare your LIMIT property with a type STRING it also works.
So, basically your declaration of the LIMIT property should resemble the following:
<property name="LIMIT" type="STRING" value="1024" scope="default"/>
or
<property name="LIMIT" value="1024" scope="default"/>

Related

How to Pass Incoming Query Parameter to Backend WSO2 API Manager

I am using wso2 API manager 3.2 and in my API there are some headers and Query Parameter.
My API has some path variable in URL in this way
http://example.com/data/readsomeData/{entityId}/{someId}
for the path variable I use the following mediator
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
and it works fine.
also I change some of incoming header's name . For example incoming header is IN_HEADER , I translate it to OUT_HEADER ,my backend expects OUT_HEADER.
I also use this mediator
<property name="IN_HEADER" expression="get-property('transport', 'IN_HEADER')"/>
<property name="OUT_HEADER" expression="get-property('IN_HEADER')" scope="transport"/>
<property name="IN_HEADER" scope="transport" action="remove"/>
It also works fine too.
The given API describe above also has some optional query parameters (limit, max, min) . For example if I use limit=10 in my API, I have to get 10 records, with out limit I get just one record.
In WSO2 API Manager Publisher I defined the above query parameters as the other parameters.
The problem is when I use each of query parameter I get the result same as the way I do not use the query parameters. I get only one record.
I think the API manager does not pass the query parameters to backend.
I don't know this problem related to the mediator I use or not!
You can approach this in a few different ways, below examples should also work on 3.2.0.
Because you remove the REST_URL_POSTFIX the APIM also removes the query parameters.
So option 1:
You could do something similar to what you did for the other headers.
Grab the query parameters and add them as headers for further use. (the $url shorthand should also be available in 3.2.0 as far as I know - it grabs a query parameter by name.)
<property name="limit" expression="$url:limit" scope="transport"/>
<property name="min" expression="$url:min" scope="transport"/>
<property name="max" expression="$url:max" scope="transport"/>
Or you could grab the query parameters from your URL and put them back after removing the POSTFIX.
<property name="newUrlPostfix" expression="substring after($axis2:REST_URL_POSTFIX, '?')"/>
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
<property name="REST_URL_POSTFIX" scope="axis2" expression="concat('?', $ctx:newUrlPostfix"/>

Set mediators variables with a file

It's possible to set a variable of mediator configuration field with a file? I just don't want to hard code the timeout field of a cache mediator, it is possible?
Thanks!
It seems that we are not able to pass an expression to timeout value. [1] only expects an integer to be provided not an expression.
[1]-https://github.com/wso2/carbon-mediation/blob/master/components/mediation-ui/mediators-ui/org.wso2.carbon.mediator.cache.ui/src/main/java/org/wso2/carbon/mediator/cache/ui/CacheMediator.java#L419
You can read a file stored in the registry like this, but I'm not sure if it's possible to change the cache timeout using a variable.
<property name="xmlFile" expression="get-property('registry','gov:/test.xml')" scope="default" type="OM"></property>
<log level="custom">
<property name="Book_Name" expression="$ctx:xmlFile//book"></property>
</log>
Ref: https://movingaheadblog.blogspot.com/2015/09/wso2-esb-how-to-read-value-from-xml.html

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 Collout address from Property or XPath-expression

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.

How to create Property array in wso2 ESB?

I have my request body as:
<tns:InputRequest xmlns:tns="http://tempuri.org/">
<tns:ID>ID_001</tns:ID>
<tns:ID>ID_002</tns:ID>
<tns:Description>Description for ID_001</tns:Description>
<tns:Description>Description for ID_002</tns:Description>
</tns:InputRequest>
and to get the value of ID and Description, i Have created property as:
<property xmlns:tns="http://tempuri.org/" name="ID" expression="//tns:ID" scope="default" type="STRING"/>
<property xmlns:tns="http://tempuri.org/" name="Description" expression="//tns:Description" scope="default" type="STRING"/>
But this gets me only one value. How can i make a property array so that i can store multiple values of ID and description in it and how to retreive from this array property?Looking forward to your reply.Thanks in advance
You should be able to extract those values using XPATH (//node/child::node()) and then set to property.
Below thread will help you to extract required nodes and set to property. You need to set the type as 'OM' to preserve XML as it is.
how to catch an array of nodes to a property