Xpath base64Decode not working in Wso2Esb 4.8.1 - xpath

I'm receiving a base64 stream that I need to decode.
I'm doing some research but I'm stuck, this is my code:
<outSequence>
<property name="cadena" value="Hola mundo" scope="default" type="STRING"/>
<property name="cadena64"
expression="base64Encode(get-property('cadena'))"
scope="default"
type="STRING"/>
<property
name="cadenaASCII"
expression="syn:base64Decode(syn:get-property('cadena64'))"
scope="default"
type="STRING"/>
<payloadFactory media-type="json">
<format>
{"cadena":"$1",
"cadena64":"$2",
"cadenaASCII":"$3",
}
</format>
<args>
<arg evaluator="xml" expression="get-property('cadena')"/>
<arg evaluator="xml" expression="get-property('cadena64')"/>
<arg evaluator="xml" expression="get-property('cadenaASCII')"/>
</args>
</payloadFactory>
<send/>
</outSequence>
I enabled Xpath 2.0 by uncommenting the line
synapse.xpath.dom.failover.enabled=true
located at $ESB_HOME/repository/conf directory (and restarting the service).
This is the error I get:
Evaluation of the XPath expression syn:base64Decode(syn:get-property('cadena64')) resulted in an error More TID[-1234] [ESB] [2019-03-04 19:27:23,359] ERROR {org.apache.synapse.util.xpath.SynapseXPath} - Evaluation of the XPath expression syn:base64Decode(syn:get-property('cadena64')) resulted in an error net.sf.saxon.xpath.XPathEvaluator.compile(XPathEvaluator.java:378) org.apache.synapse.util.xpath.SynapseXPath.evaluateDOMXPath(SynapseXPath.java:529) org.apache.synapse.util.xpath.SynapseXPath.stringValueOf(SynapseXPath.java:364) org.apache.synapse.mediators.builtin.PropertyMediator.getResultValue(PropertyMediator.java:302) org.apache.synapse.mediators.builtin.PropertyMediator.mediate(PropertyMediator.java:96) org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:77) org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:47) org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:131) org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:268) org.apache.synapse.core.axis2.SynapseCallbackReceiver.handleMessage(SynapseCallbackReceiver.java:488) org.apache.synapse.core.axis2.SynapseCallbackReceiver.receive(SynapseCallbackReceiver.java:170) org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180) org.apache.synapse.transport.passthru.ClientWorker.run(ClientWorker.java:225) org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) java.lang.Thread.run(Thread.java:745)

I think only encode is supported/implemented in 4.8.1 otherwise as already mentioned an option is to upgrade the esb. The following worked for me in 4.8.1
<property name="contentBase64" expression="//t:Content" scope="default" type="STRING"/>
<script language="js"><![CDATA[importPackage(Packages.org.apache.commons.codec.binary); var log = mc.getServiceLog(); log.info("1");var c = mc.getProperty("contentBase64");log.info("2:"+c);var bc = Base64.decodeBase64(c);log.info("3:"+bc); var result = ""; for(var i = 0; i
< bc.length; ++i){ result+= (String.fromCharCode(bc[i])); } log.info("result:"+result); mc.setProperty("contentDecoded", result);]]></script>
The decoded content can the be accessed in the property "contentDecoded".
Hope that helps.

The error you quoted contains a poorly formatted stack trace:
Evaluation of the XPath expression syn:base64Decode(syn:get-property('cadena64')) resulted in an error
More TID[-1234] [ESB] [2019-03-04 19:27:23,359] ERROR {org.apache.synapse.util.xpath.SynapseXPath} -
Evaluation of the XPath expression syn:base64Decode(syn:get-property('cadena64')) resulted in an error
net.sf.saxon.xpath.XPathEvaluator.compile(XPathEvaluator.java:378)
org.apache.synapse.util.xpath.SynapseXPath.evaluateDOMXPath(SynapseXPath.java:529)
....
This gives us some information, but not as much as we would like. It tells us that the XPath expression was being evaluated by Saxon, and it tells us that Saxon was reporting a static error in the expression. It also tells us that it's a rather old release of Saxon, since in releases at least since Saxon 9.6, the class XPathEvaluator has contained around 330 lines. What it doesn't tell us is what the error actually is.
The poor diagnostics appear to be the responsibility of Apache synapse.
I'm afraid that's only a small step towards answering the question but I hope the information is useful to someone.

your code works in ESB 5.0.0 so no help there (but upgrading would be a solution to your problem!)
I find it suspect though that Encode is working and Decode isn't. Could you try without base64Decode without syn:?

Related

WSO2 ESB- Error Handling - On Error Sequence

For which are all the mediators onError Sequence works?
ex: I tried Enrich mediator it didn't work and send mediator it worked.
<inSequence onError="errorhandler" xmlns="http://ws.apache.org/ns/synapse">
<enrich>
<source clone="true" type="custom" xpath="//Body/xs"/>
<target action="replace" property="ORIGREQUES" type="property"/>
</enrich>
Here given xpath is wrong. In wso2Carbon.log got the below message.
ERROR {org.apache.synapse.mediators.elementary.EnrichMediator} -
Specified node by xpath cannot be found.
{org.apache.synapse.mediators.elementary.EnrichMediator}
<api xmlns="http://ws.apache.org/ns/synapse" name="addTripService" context="/trip" hostname="localhost" port="8280"> <resource methods="POST" uri-template="/add/v2" faultSequence="fault">
<inSequence onError="errorhandler">
<enrich>
<source clone="true" xpath="//Body/xs"/>
<target type="property" property="ORIGREQUES"/>
</enrich>
<send/>
</inSequence>
Here getting the error message whatever I have given in Errorhandler sequence.
This means that onError Sequence works for send mediator and not for enrich mediator. So please let me know for which all mediators it works?
Thanks in advance.
This [1] article contains detailed description about error handling in WSO2 ESB. Go through it and you will understand how the mediators works and how to handle the errors properly.
[1] http://wso2.com/library/articles/2012/10/wso2-esb-examples-best-practises-error-handling-wso2-esb/

WSO2 Proxy: Catch exceptions thrown during runtime?

Is it possible to catch exceptions thrown during runtime in WSO2 Proxy? My proxy service throws a great deal of exception (in each iteration) and they tend to create much overhead for the ESB which runs short of memory. I would like to catch/suppress these exceptions and let the proxy run to its end after displaying a single error message for each exception.
You can try using makefault as explained here
In your proxy under target you can set faultsequence to point to a sequence defined separately as
Now nameoffaultseq will have code like this , this is supposed to catch runtime exceptions and does for me.
<makefault xmlns="http://ws.apache.org/ns/synapse" version="soap11">
<code xmlns:soap11Env="http://schemas.xmlsoap.org/soap/envelope/" value="soap11Env:VersionMismatch" />
<reason value="test message " />
<role></role>
</makefault>
This is for soap11 response
for non soap like rest you can use something like
<makefault version="pox">
<reason expression="get-property('ERROR_MESSAGE')"/>
</makefault>
<payloadFactory media-type="xml">
<format>
<Error>
<ErrorCode>$1</ErrorCode>
<ErrorDesc>$2</ErrorDesc>
<ErrorSource>$3</ErrorSource>
<ErrorType>SE</ErrorType>
</Error>
</format>
<args>
<arg evaluator="xml" expression="get-property('ERROR_CODE')"/>
<arg evaluator="xml" expression="get-property('ERROR_MESSAGE')"/>
<arg evaluator="xml" expression="get-property('ERROR_EXCEPTION')"/>
</args>
</payloadFactory>
<property name="HTTP_SC" value="500" scope="axis2" type="STRING"/>
<property name="messageType" expression="get-property('AcceptFromConsumer')" scope="axis2" type="STRING"/>`
Where the arguments are standard error properties provided by wso2esb , so in general for runtime exceptions this should be sufficient. More details on error handling are here

WSo2 Esb filtering messages to an output file

I am working with a filter mediator attempting to send messages to an output xml file. I have my sequence using a filter mediator based on a attribute value, if it is true I want to write the message to a new xml file in a certain directory. If it is false I will drop the record.
Here is my sequence:
<sequence xmlns="http://ws.apache.org/ns/synapse" name="RenaissanceIqtFilterSequence">
<log level="custom">
<property name="sequence" value="FilterSequence"></property>
</log>
<filter xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:ns="http://org.apache.synapse/xsd" xmlns:z="RowsetSchema" xpath="//z:row/#name='RP'">
<then>
<log level="custom">
<property name="sequence" value="Condition Write"></property>
</log>
<call-template target="FileWriteTemplate">
<with-param name="targetFileName" value="NEW_MESSAGE_FILE"></with-param>
<with-param name="addressUri" value="vfs:file:///var/process/rrout"></with-param>
</call-template>
</then>
<else>
<log level="custom">
<property name="sequence" value="Condition Drop"></property>
</log>
<drop></drop>
</else>
</filter>
</sequence>
*I am using a template as you can see to write out to my new output file setting parameters for the file name and uri.
The result is the whole file is being written out to the directory not just the messages I want. I have been running google searches trying to see where I am going wrong. I assume at this point I may be using the Filter mediator incorrectly? Maybe there is a better way or mediator to use to accomplish this task? I would appreciate any thoughts or recommendations folks may have. Thanks for your time!

WSO2 ESB aggregate mediator not working properly

i m using the wso2ESB 4.8.0 and i followed the sample at this url:
https://docs.wso2.org/display/ESB480/Sample+62:+Routing+a+Message+to+a+Dynamic+List+of+Recipients+and+Aggregating+Responses
writing this proxy service:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="adminBroadcastEndpoint"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<send>
<endpoint>
<recipientlist>
<endpoint>
<address uri="http://127.0.0.1:8080/RestService/rest/servizio/"/>
</endpoint>
<endpoint>
<address uri="http://127.0.0.1:8081/RestService/rest/servizio/"/>
</endpoint>
</recipientlist>
</endpoint>
</send>
<drop/>
</inSequence>
<outSequence>
<payloadFactory media-type="xml">
<format>
<broadcast>
$1
</broadcast>
</format>
<args>
<arg evaluator="xml" expression="$body/root"/>
</args>
</payloadFactory>
<aggregate>
<completeCondition>
<messageCount/>
</completeCondition>
<onComplete xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" expression="/">
<send/>
</onComplete>
</aggregate>
</outSequence>
</target>
<description/>
</proxy>
in the recipient list there are two servers who answer with an xml string. The first server answer with
<root>
<codice>0</codice>
<messaggio>MESSAGE1</messaggio>
<result><name>CorreggiPecJob1</name><group>POSTA1</group></result>
<result><name>SchedulaIndiceJob1</name><group>INDICE1</group></result>
</root>
and the second server with:
<root>
<codice>0</codice>
<messaggio>MESSAGE2</messaggio>
<result><name>CorreggiPecJob2</name><group>POSTA2</group></result>
<result><name>SchedulaIndiceJob2</name><group>INDICE2</group></result>
</root>
After the aggregate mediator i should expect a result with all those 2 message merged... but in the response from the proxy service i get just the second server answer or the first one randomly.
If i put a log mediator suddenly after the onComplete tag hawever i the esb prints the whole merged message with the two response, but it seems that on the send mediator something goes lost.
Why does this happen?
Another question is: why xpath expressions like
$body/broadcast//result
don't work rising this exception?
2014-03-02 17:37:32,021] ERROR - AggregateMediator Error evaluating expression: $body/broadcast//result
org.apache.synapse.SynapseException: Could not find matching elements to aggregate.
I realize that if i define a namespace and a prfix to append in the payloadfactory like:
<payloadFactory xmlns:m0="my.namespace" media-type="xml">
<m0:format>
<m0:broadcast>
$1
</m0:broadcast>
</m0:format>
<args>
<arg evaluator="xml" expression="$body/root"/>
</args>
</payloadFactory>
the expression: $body/m0:broadcast work fine... so is it the namespace definition mandatory?
Is it there anything i'm missing? thanks
If the XML elements are in namespaces, than you're XPath must either define and use namespace prefixes, or specify any namespace in the path steps, like this:
$body/*:broadcast
i found the answer here: wso2 ESB : Split / Gather Pattern - Single Response
i solved using the enrich mediator. But i think that the aggregate mediator has not a really correct behaviour... maybe should be checked.

Filter Mediator on XPATH Functions

I am facing a problem with my code using Filter mediator and XPATH functions. What I am trying to do in my code is to check if a particular block is empty, do not call/enrich that block and move to another one checks for if it is empty, if not process it and moves to next one and in the end do a commit. Currently I am getting the error as:
FilterMediator Error evaluating XPath expression : fn:exists($body/product/simpleProduct/standardAttributes)
My code is as follows:
<inSequence>
<log level="custom">
<property name="STATUS" value="************ REQUEST ***************"/>
</log>
<transaction action="new"/>
<filter xpath="fn:exists($body/product/simpleProduct/standardAttributes)">
</filter>
<enrich>
<source type="body" clone="true"/>
<target type="property" property="MSG_PAYLOAD"/>
</enrich>
<log level="full"/>
<xslt key="simpleAttributes"/>
<log level="custom">
<property name="STATUS"
value="*********INSERTING Simple Product Attributes *************"/>
</log>
</inSequence>
It fails at fn:exists(). Can you help me with your inputs where am I going wrong?
Apparently the function you use is xpath 2 function. OOTB xpath 2 functions won't work in ESB. If this is xpath2, you have to enable it. To enable xpath 2, for that uncomment synapse.xpath.dom.failover.enabled=true in synapse.properties file.

Resources