I have scenario where I want to convert string to lowercase inside mediation sequence. Mediation sequence I have is as follows
<property name=":: ID::" expression="get-property('Id')"/>
I want to convert the ID to lowercase. How can I do it?
Depending on whether you have XPath 2.0 support configured in your product, you can use: lower-case() function:
<property name=":: ID::" expression="lower-case(get-property('Id'))"/>
If you are restricted to XPath 1.0, you can convert with the translate() function like below:
<property name=":: ID::" expression="translate(get-property('Id'), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/>
Something more you can read in this topic xpath lowercase - Is there xpath function to do this?
Related
I have a structure with APIM, IS and Microintegrator. Configured APIM to pass attributes to backend via X-JWT-Assertion header.
Now I need to decode this JWT into Json format to use some claims.
I tried it with the function:
<property expression="base64Decode(get-property('JWT_HEADER'))" name="decoded"/>
but it returns the scrambled data.
enter image description here
Does anyone know how I can do decoding?
Thanks.
A JWT token contains three parts separated by dots (.) as follows [JWT.io]
Header
Body
Signature
If you are trying to access the claims, you have to split the body part and then decode it. Otherwise, it will result in an unreadable format as you have mentioned above.
The following is a sample expression to split and decode the Body of the JWT Token
<property name="assertion" expression="$trp:X-JWT-Assertion" />
<!-- split the body content using substring functions -->
<property name="decoded" expression="base64Decode(fn:substring-before(fn:substring-after($ctx:assertion, '.'), '.'))" />
<log level="custom">
<property name="decoded-body" expression="$ctx:decoded" />
</log>
how to use pattern and group for validating property.
<property name="string" [action=set|remove] [type="string"]
(value="literal" | expression="xpath")
[scope=default|transport|axis2|axis2-client] [pattern="regex"
[group="integer"]]>
</property>
Pattern in Property mediator can be used to match the value or the result of an expression against a given pattern. If the pattern matches, Property mediator returns the value. Else, it returns an empty string.
For example, in the following example, only Test1 property value matches with the pattern. Hence, only Test1 returns the value. Test2 returns an empty string.
<property name="Test1" value="5" scope="default" type="STRING" pattern="[0-9]" group="0"/>
<property name="Test2" value="20" scope="default" type="STRING" pattern="[0-9]" group="0"/>
<log level="custom">
<property name="Test1 Value : " expression="get-property('Test1')"/>
<property name="Test2 Value : " expression="get-property('Test2')"/>
</log>
And, "group" attribute in Property mediator is intended to be used to evaluate against the number of capturing groups in this matcher's pattern, similar to java.util.regex.Matcher.groupCount() in Java. However, apparently the group support is not available in WSO2 EI 6.5.0 at the moment.
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
I am trying the below option and i m getting error, can anyone suggest me the solution. I have added synapse.xpath.dom.failover.enabled=true
Code:trying to replace ~TOKEN~ with the property value
<property name="verificationLink" expression="fn:replace($ctx:reqVerifyUrl , '~TOKEN~', get-property('verification_code'))" scope="default" type="STRING" xmlns:fn="http://www.w3.org/2005/xpath-functions" />
Error:
TID: [-1234] [] [2017-02-15 00:14:19,318] ERROR {org.apache.synapse.util.xpath.SynapseXPath} - Evaluation of the XPath expression fn:replace($ctx:reqVerifyUrl , '~TOKEN~', get-property('verification_code')) resulted in an error {org.apache.synapse.util.xpath.SynapseXPath} net.sf.saxon.trans.XPathException: Unknown system function get-property()
get-property is not a standard xpath function and you must say to the xpath engine that this function comes from synapse :
<property name="verificationLink" expression="fn:replace($ctx:reqVerifyUrl , '~TOKEN~', syn:get-property('verification_code'))" scope="default" type="STRING" xmlns:fn="http://www.w3.org/2005/xpath-functions" />
You do not need to add the definition of 'syn' namespace, it is well known in your mediation (xmlns:syn="http://ws.apache.org/ns/synapse")
Two things need to be done:
A. Review this and ensure your Synapse config is set correctly. Is there any replace function in wso2esb?
B. Then you need to add prefix fn & syn if you need to use methods like get-property() with XPath 2.0. Also note, you must use (include) the following namespaces values for ‘syn’ and ‘fn’.
xmlns:syn=”http://ws.apache.org/commons/ns/payload”
xmlns:fn=”http://www.w3.org/2005/xpath-functions”
Here is a sample property mediator using replace.
<property xmlns:syn="http://ws.apache.org/commons/ns/payload" xmlns:fn="http://www.w3.org/2005/xpath-functions" name="xmlValue" expression="fn:replace(syn:get-property('textValue'), 'xmlData=', '')></property>;
I am using WSO2 ESB 4.8.1. I need to check if a particular field exists in the Incoming Payload XML. If Iit does, a Switch Case should run, otherwise it should run a default scenario. The field on which I am checking will always have a dynamic value and it would look like following:
20000569899
I wrote following logic in ESB:
<property name="CheckIdExist" expression="//*[local-name()='Id']" scope="default" type="STRING" description="CheckIdExist"/>
<switch source="get-property('CheckIdExist')">
<case regex="[a-zA-Z0-9]">
<sequence key="SequenceA"/>
<sequence key="SequenceB"/>
<sequence key="SequenceC"/>
</case>
<default>
<log></log>
</default>
</switch>
Problem:
The problem is that Switch is able to pick a value from the property but it does not match the Case regular expression. Following are some ESB Logs Entries:
Logs Entries:
XPath : get-property('CheckIdExist') evaluates to : 20001089900
None of the switch cases matched - executing default
I checked online, this regular expression works. So what i am unable to understand is that why WSO2 ESB is not recognizing it as a valid expression. Or is there any other way to check that if a field exists in an input request then a particular switch case should work, Otherwise not.
Your regular expression lacks a quantifier + meaning 1 or more occurrences:
<case regex="[a-zA-Z0-9]+">