Error evaluating JSON Path <$.element>. Returning empty result. Error>>> invalid path - wso2-esb

I have a json request where some elements are optional, if i pass them then it works fine, if i don't send these elements in the request then i get below error getting logged unnecessary, how to avoid it.
Error:
ERROR {org.apache.synapse.util.xpath.SynapseJsonPath} - #stringValueOf. Error evaluating JSON Path <$.name>. Returning empty result. Error>>> invalid path
Code:
<property expression="json-eval($.name)" name="name" scope="default" type="STRING"/>

Perform a filter on the element availability first as below :
<filter xpath="json-eval($.name)">
<then>
<property expression="json-eval($.name)" name="name" scope="default" type="STRING"/>
</then>
<else/>
</filter>
Reference : https://docs.wso2.com/display/ESB480/Filter+Mediator

Related

how can set header mediator value with runtime value in WSO2 APIM 2.6?

I get a token authorization parameter from a Login API. I need to use this token in my header parameters I gave with In Flow mediator in another API.so I try use CORS configuration and get the token parameter as type header and fill it when invoking the API.
I gave this token value in mediator with static value and It worked. But this value is dynamically changed by invoking Login API so I have to give run time value.
my mediator is :
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="tokenMediator" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<log level="full"/>
<header name="caller-token" scope="transport" value="123"/>
<header name="Content-Type" scope="transport" value="application/json"/>
<header name="Access-Control-Allow-Origin" scope="transport" value="http://onlinewebapi"/>
<header expression="$header:token" name="Authorization" scope="transport"/>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
</sequence>
when I invoke API I get this : "TypeError: Failed to fetch"
and it don't pass WSO2 to reach my local codes.
I changed the name of parameter from token to authorization and result don't changed.

How to trigger an error in a WSO2 sequence?

I would like to create a input sequence where I check a param with a switch mediator. If the value of the param does not correspond to a specific string, then an error should be thrown and move the flow in the Fault sequence.
For example, I created this sequence:
<sequence name="testEquivalence" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<switch source="$ctx:uri.var.myParam">
<case regex="hello">
<log description="DISPLAY" level="full">
<property name="value" value="RIGHT VALUE FOR THE PARAM myParam"/>
</case>
<default>
<log description="FAULT" level="full">
<property name="FAULT" value="WRONG VALUE FOR THE PARAM myParam"/>
</log>
<makefault version="pox">
<reason value="Wrong value"/>
<detail expression="'Wrong value. Try with hello.'"/>
</makefault>
</default>
</switch>
</sequence>
As you can see, I want to display a log saying "RIGHT VALUE FOR THE PARAM myParam" if it is OK. Or, I would like to display a log and then stop the process in the default part to pass in the Fault sequence.
However, I created a makefault mediator to "modelize" my fault but is it possible to replace it by something more... executive to stop the process and redirect the flow in the Fault sequence like if an exception was thrown?
There is no way to throw an exception in WSO2 ESB/EI. Generally after creating a SOAPFault you want to return this to the caller of the service. A Solution is to use the Response Mediator after the Fault mediator:
<sequence name="testEquivalence" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<switch source="$ctx:uri.var.myParam">
<case regex="hello">
<log description="DISPLAY" level="full">
<property name="value" value="RIGHT VALUE FOR THE PARAM myParam"/>
</case>
<default>
<log description="FAULT" level="full">
<property name="FAULT" value="WRONG VALUE FOR THE PARAM myParam"/>
</log>
<makefault version="pox">
<reason value="Wrong value"/>
<detail expression="'Wrong value. Try with hello.'"/>
</makefault>
<respond/>
</default>
</switch>
</sequence>

Getting wrong value while using property mediator in WSO2 ESB

I am using a property "INC" to store a incoming message(which is showing properly in logs) and doing mapping properly by payload mediator.
The modified structure that I am getting by payload is as below.(which is getting properly mapped)
Later, I am using callout mediator as below to get response from this endpoint.
using LogText to see the response.
Then adding filter as below:
<property name="INC" expression="get-property('IntRequest')" scope="default" type="STRING"/>
<property name="LogText" value="call response" scope="default" type="STRING"/>
<filter xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:platformFaults="urn:faults_2015_1.platform.webservices.com" source="//platformFaults:code/text()" regex="WS_CONCUR_SESSION_DISALLWD">
<then>
<log level="full" separator=",*Fault in Call***">
<property name="REQUEST" expression="get-property('INC')" scope="default" type="STRING" />
</log>
</then>
<else/>
</filter>
But, here I am not getting value of REQUEST.. it is showing the value of "call response"..
How will I get the value of REQUEST properly??
Try to use the enrich mediator to store the request message body
<enrich>
<source clone="true" type="body"/>
<target property="Request_Message" type="property"/>
</enrich>
then call the property
<log>
<property expression="get-property('Request_Message')" name="REQUEST"/>
</log>
where did you define "IntRequest" property in your sequence? I couldn't find it in your configuration. If you change the log level to custom it would be more clear to see the value of request in the system log

WSO2 xml property xpath to child element

I have following code:
<property name="resultOM" scope="default" type="OM">
<test><test1>testing111</test1></test>
</property>
<log level="full">
<property name="ROOT" expression="$ctx:resultOM" />
<property name="resultOM.test" expression="$ctx:resultOM//test1" />
</log>
Then I would expect to see in logs values
ROOT = testing111
resultOM.test = testing111
and although ROOT value is fine, the value of resultOM.test is empty.
What should be the correct xpath to retrieve the child value?
Version of used WSO2ESB is 4.5.1.
A default namespace (http://ws.apache.org/ns/synapse) is defined when you create an OM type property. So you need to perform xpath expressions with namespaces. Try this;
<log level="full">
<property name="ROOT" expression="$ctx:resultOM"/>
<property name="resultOM.test" xmlns:ns="http://ws.apache.org/ns/synapse" expression="$ctx:resultOM//ns:test1"/>
</log>
Read [1] for more details.
[1] http://isharapremadasa.blogspot.com/2014/08/wso2-esb-property-mediator-performing.html

Read from a file in wso2 esb

I want to read from an xml file in my esb home directory called test.xml whose content is:
<a>Helloo
<b>World</b>
</a>
I have written a proxy to access content of node b as:
<proxy xmlns="http://ws.apache.org/ns/synapse" name="ReadFileProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<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>
<header name="To" action="remove"/>
<property name="RESPONSE" value="true" scope="default" type="STRING"/>
<property name="NO_ENTITY_BODY" scope="axis2" action="remove"/>
<payloadFactory>
<format>
<getCombinedResponse>Succefully read</getCombinedResponse>
</format>
</payloadFactory>
<send/>
</inSequence>
</target>
<description></description>
</proxy>
But it is giving me error as:
[2013-06-18 16:29:09,524] ERROR - NativeWorkerPool Uncaught exception
org.apache.axiom.om.OMException: com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog
at [row,col {unknown-source}]: [1,0]
at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:296)
at org.apache.axiom.om.impl.llom.OMDocumentImpl.getOMDocumentElement(OMDocumentImpl.java:109)
at org.apache.axiom.om.impl.builder.StAXOMBuilder.getDocumentElement(StAXOMBuilder.java:570)
at org.apache.axiom.om.impl.builder.StAXOMBuilder.getDocumentElement(StAXOMBuilder.java:566)
at org.apache.axiom.om.util.AXIOMUtil.stringToOM(AXIOMUtil.java:54)
at org.apache.axiom.om.util.AXIOMUtil.stringToOM(AXIOMUtil.java:39)
at org.apache.synapse.config.SynapseConfigUtils.stringToOM(SynapseConfigUtils.java:766)
at org.apache.synapse.mediators.builtin.PropertyMediator.convertValue(PropertyMediator.java:317)
at org.apache.synapse.mediators.builtin.PropertyMediator.getResultValue(PropertyMediator.java:299)
at org.apache.synapse.mediators.builtin.PropertyMediator.mediate(PropertyMediator.java:95)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:71)
at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:114)
at org.apache.synapse.core.axis2.ProxyServiceMessageReceiver.receive(ProxyServiceMessageReceiver.java:154)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
at org.apache.synapse.transport.passthru.ServerWorker.processEntityEnclosingRequest(ServerWorker.java:434)
at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:219)
at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
What am i doing wrong in this. Is there any other method to read from any file(pdf,txt and xml only).
As you have specified in the configuration
<property name="test" expression="get-property('registry','conf:/test.xml')" scope="default" type="OM"/>
you are referring a file which is saved in the registry. This is why you need to save that in the registry.
You can use VFS transport to read from a file. Please reffer the sample [1]. From this sample you can get to know how to read from a file. This sample directly sends the content to an endpoint. For your case you can use an xpath expression to retrieve the element b.
<property name="test.b" expression="//a/b"/>
[1]http://docs.wso2.org/wiki/pages/viewpage.action?pageId=16846489