[WSO2 ESB V4.5.0]
What is wrong with how I'm configuring the enrich mediator to accumulate XML? I have a sequence of n PojoMediators that retrieve XML from a database with each setting a context property with the XML represented as a string. For example, after the first PojoMediator executes, its' context property is set to:
customerInformation = <cust><id>1</id><oc></oc><ca>0</ca></cust>
and I'm trying to enrich the body with that XML content but end up with:
[snip]
</header>
<cust><id>1</id><oc></oc><ca>0</ca></cust></root></soapenv:Body></soapenv:Envelope> {org.apache.synapse.mediators.builtin.LogMediator}
..where the enrich mediator is escaping the referenced "custInfo" XML. My enrich configuration is:
<enrich>
<source type="property" property="custInfo"/>
<target type="body"/>
</enrich>
Is there a means to coerce the enrich mediator to treat the property ("custInfo") as an XML fragment rather than as straight text? I'm assuming that this is why the XML is getting escaped as the mediator believes it is setting the content of a node rather than specifying an XML fragment.
How you defined property ? Can you try after adding
type="OM"
to the property definition and try again?
Related
In wso2 ESB I'm having the below request payload of multipart/formdata.
key: file value(file): image
key: data value(text): {"a":"b","c":"d"}
But first I want to store this and later I want to use this payload. How to achive this. I mean after storing this multipart/formadata request, I am doing some other processing, then at the end I am giving response as same as the request that I've stored earlier.
I tried with property but as it stores as String it while processing afterwards multipart/formadata treats it as text(encoding as UTF-8 or text),So image will
be treated as text.
I Solved the above question by first setting the property using enrich mediator and then use that property afterwards using enrich mediator. Thus it preserves the format, if we store the property using the property mediator we have to store as string or OM. So , the format will not be preserved and while running it considers image encoding also as text.
<enrich>
<source clone="false" type="body"/>
<target type="property" property="inputpayload"/>
</enrich>
.
.
.
<enrich xmlns="http://ws.apache.org/ns/synapse">
<source clone="false" property="inputpayload"/>
<target action="replace" type="body"/>
</enrich>
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 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.
I am designing a Proxy Service sequence that includes an XQuery transformation. I have a problem with referencing the registry file containing the transformation.
Here is the sequence:
<sequence name="MySequence" xmlns="http://ws.apache.org/ns/synapse" >
<xquery key="conf:/wsdl/xqueryRequest.xq"
target="..."
xmlns:ns="http://org.apache.synapse/xsd" >
<variable xmlns:ns2="http://..." name="var1" expression="..." type="ELEMENT" />
</xquery>
<send>
<endpoint>...</endpoint>
</send>
</sequence>
The file containing the XQuery transformation is uploaded in /_system/config/wsdl/xqueryRequest.xq
And this is the exception I am getting:
WARN ERROR_DETAIL : org.apache.synapse.SynapseException:
Unable to execute the query at
org.apache.synapse.mediators.xquery.XQueryMediator.handleException(XQueryMediator.java:627) at
org.apache.synapse.mediators.xquery.XQueryMediator.mediate(XQueryMediator.java:130) at
org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:60) at
...
Caused by: java.lang.NullPointerException at
org.wso2.carbon.mediation.registry.WSO2Registry.lookup(WSO2Registry.java:177) at
org.apache.synapse.registry.AbstractRegistry.getResource(AbstractRegistry.java:63) at
org.apache.synapse.config.SynapseConfiguration.getEntry(SynapseConfiguration.java:693) at
org.apache.synapse.core.axis2.Axis2MessageContext.getEntry(Axis2MessageContext.java:194) at
org.apache.synapse.mediators.xquery.XQueryMediator.performQuery(XQueryMediator.java:233) at
org.apache.synapse.mediators.xquery.XQueryMediator.mediate(XQueryMediator.java:123)
... 11 more
The exception shows a NullPointer at WSO2Registry.lookup so I assume that the problem is in finding the XQuery file in the registry, this part:
key="conf:/wsdl/xqueryRequest.xq"
The documentation shows two code snippets(390 and 391) with XQuery transformation but non of them explains howto reference the XQuery file.
Question: What should be the correct value for the key parameter? Do I need to use the "Local Entry" feature? If yes, then should I define the value as "Inline XML Entry" or "Inline Text"?
UPDATE 03/23/12:
Question 2: Perhaps a simpler question: Where should I copy/upload the xqueryRequest.xq file when I set Xquery mediator's key field to key="xqueryRequest.xq"?
Can you try saving your xquery as a local entry and refer it from the XQuery mediator.
(Please have a look at local entry samples)