Reading Local Entry (XML,URL) in WSO2 ESB - wso2

I have configured below two local entry in WSO2 WSB4.9.0 how can I read the node values in proxy or sequence.
In-Line XML Local Entry
<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="test" xmlns="http://ws.apache.org/ns/synapse">
<list>
<flag>a</flag>
<path>b</path>
</list>
</localEntry>
and
Source URL Entry
<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="sample" src="file:/C:/Apache24/bin/ApacheMonitor" xmlns="http://ws.apache.org/ns/synapse"/>
Please help.

If the entry is in the filesystem, you can use:
<property name="testProp" expression="get-property('test')" scope="default" type="STRING"/>
and
<property name="sampleProp" expression="get-property('sample')" scope="default" type="STRING"/>
If you want to get access to the values inside XML set OM type:
<property name="testProp" expression="get-property('test')" scope="default" type="OM"/>
<log level="custom">
<property expression="$ctx:testProp" name="FullValue" />
<property expression="$ctx:testProp//tt:flag" name="flagValue" xmlns:tt="http://ws.apache.org/ns/synapse"/>
<property expression="$ctx:testProp//tt:path" name="pathValue" xmlns:tt="http://ws.apache.org/ns/synapse"/>
</log>
My full proxy:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="testProxy6"
transports="https http"
startOnLoad="true"
trace="disable">
<target>
<inSequence>
<property name="testProp" expression="get-property('test')" scope="default" type="OM"/>
<log level="custom">
<property expression="$ctx:testProp" name="FullValue" />
<property expression="$ctx:testProp//tt:flag" name="flagValue" xmlns:tt="http://ws.apache.org/ns/synapse"/>
<property expression="$ctx:testProp//tt:path" name="pathValue" xmlns:tt="http://ws.apache.org/ns/synapse"/>
</log>
<respond/>
</inSequence>
<outSequence>
<log level="full">
<property value="SEQUENCE: " name="OUT"/>
</log>
<send/>
</outSequence>
</target>
</proxy>
My local entry in file test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="test" xmlns="http://ws.apache.org/ns/synapse">
<list>
<flag>a</flag>
<path>b</path>
</list>
</localEntry>
My log output:
[2016-05-11 12:21:30,999] INFO - LogMediator FullValue = <list xmlns="http://ws.apache.org/ns/synapse">
<flag>a</flag>
<path>b</path>
</list>, flagValue = a, pathValue = b

If you need to obtain values from an xml in a local entry, first of all you should have to get the xml content into a property and set its type to OM as below.
<property expression="get-property('xmlLocalEntrySample')" name="xmlTest" scope="default" type="OM"/>
Now from the message context you can read the property values like this. Here $ctx is the prefix for Synapse Message-Context properties and gets a property at the default scope using this. If you log the values of testFlagA and testPath you can see a and b print in the console respectively.
<property expression="$ctx:xmlTest//*[local-name()='flag']" name="testFlagA"/>
<property expression="$ctx:xmlTest//*[local-name()='path']" name="testPath"/>

For me get-property does not work for local entries, while xslt mediator works well . ie6.4

Related

wso2 conversion, xml to json conversion

I am completely new to wso2 and I need to change my given input request xml to json and hit this to an adapter and get the response back in json and then in xml. How is this possible??
I am using wso2 integration studio for the development.
<Request>
<requestId><![CDATA[11111111111111111]]></requestId>
<timeStamp><![CDATA[2019/12/25 12:12:12]]> </timeStamp>
<msisdn> <![CDATA[888]]></msisdn>
<keyWord><![CDATA[TEST_API]]></keyWord>
<dataSet>
<param>
<id><![CDATA[first_id]]></id>
<value><![CDATA[12310209842396]]></value>
</param>
<param>
<id><![CDATA[second_id]]></id>
<value><![CDATA[1]]></value>
</param>
</dataSet>
</Request>
In the mediation sequence, you can use the messageType property to indicate that the message should be converted to JSON when sending it to your adapter. And in the return phase, you can use the messageType property again to convert the message to XML.
<property name="messageType" value="application/json" scope="axis2"/>
Example:
<?xml version="1.0" encoding="UTF-8"?>
<api name="toJson" context="/tojson" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<property name="messageType" value="application/json" scope="axis2" />
<send>
<endpoint key="adapter"/>
</send>
</inSequence>
<outSequence>
<property name="messageType" value="application/xml" scope="axis2" />
<send />
</outSequence>
</resource>
</api>

To read LocalEntry values in WSO2 EI

I am trying to read local Entry values based on dynamic user input(if Feed is user Input, then values inside Feed (host,user,password etc) should be fetched. but values not getting fetched for me. can anyone suggest me to achueve this?
LocalEntry:
<?xml version="1.0" encoding="UTF-8"?>
<localEntry xmlns="http://ws.apache.org/ns/synapse" key="MailTo_Details">
<MAIL_Details xmlns="http://mail.com/localentry">
<Lead>
<credentials>
<host>smtp.gmail.com</host>
<port>587</port>
<starttls.enable>true</starttls.enable>
<auth>false</auth>
<user>Demouser</user>
<password>email1pws</password>
<from>email1#gmail.com</from>
</credentials>
</Lead>
<Feed>
<credentials>
<host>smtp.gmail.com</host>
<port>587</port>
<starttls.enable>true</starttls.enable>
<auth>false</auth>
<user>Testuser</user>
<password>email2pws</password>
<from>email2#gmail.com</from>
</credentials>
</Feed>
</MAIL_Details>
<description/>
</localEntry>
I have loaded localentry and setting values as OM type.But i don't know way to retrieving values by dynamically.
<property xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
xmlns:ns="http://org.apache.synapse/xsd"
xmlns:ns3="http://org.apache.synapse/xsd"
name="mailConfig"
expression="get-property('MailTo_Details')"
scope="default"
type="OM"/>
Try this proxy:
<?xml version="1.0" encoding="UTF-8"?>
<proxy name="loadPayloadfromLocalEntry" startOnLoad="true" transports="http https" xmlns="http://ws.apache.org/ns/synapse">
<target>
<inSequence>
<property expression="get-property('MailTo_Details')" name="mailConfig" scope="default" type="OM" xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd" xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"/>
<property name="getinput" scope="default" type="STRING" expression="$body/getdata/mailinput"/>
<property name="apos" scope="default" type="STRING" value="'"/>
<log>
<property name="printValueEmail" expression="$ctx:mailConfig"/>
<property name="getinput" scope="default" type="STRING" expression="$body/getdata/mailinput"/>
<property name="HOST" expression="evaluate(fn:concat('$ctx:mailConfig//ns:',get-property('getinput'),'/ns:credentials/ns:host'))" xmlns:ns="http://mail.com/localentry"/>
</log>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</target>
</proxy>
With payload as body:
<body>
<getdata>
<mailinput>Feed</mailinput>
</getdata>
</body>
My log message:
[2020-07-27 11:39:32,487] INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: /services/loadPayloadfromLocalEntry.loadPayloadfromLocalEntryHttpSoap12Endpoint, WSAction: urn:mediate, SOAPAction: urn:mediate, MessageID: urn:uuid:bc74622a-3843-4813-ab35-769138d5f8e6, Direction: request, printValueEmail = <MAIL_Details xmlns="http://mail.com/localentry">
<Lead>
<credentials>
<host>smtp.gmail.com</host>
<port>587</port>
<starttls.enable>true</starttls.enable>
<auth>false</auth>
<user>Demouser</user>
<password>email1pws</password>
<from>email1#gmail.com</from>
</credentials>
</Lead>
<Feed>
<credentials>
<host>smtp.gmail.com</host>
<port>587</port>
<starttls.enable>true</starttls.enable>
<auth>false</auth>
<user>Testuser</user>
<password>email2pws</password>
<from>email2#gmail.com</from>
</credentials>
</Feed>
</MAIL_Details>, getinput = Feed, HOST = smtp.gmail.com

remove <text>-tag from output while writing file with vfs

i want to write a file with the payload as csv(as plan taxt) in an sequence.
My problem is that i always get the <text xmlns="http://ws.apache.org/commons/ns/payload">-tag surrounding my data.
Can anybody help my removing this tag?
Result:
<text xmlns="http://ws.apache.org/commons/ns/payload">HALLO 13,hallo 11,hallo 12,hallo 11hallo 12
HALLO 23,hallo 21,hallo 22,hallo 21hallo 22
HALLO 33,hallo 31,hallo 32,hallo 31hallo 32</text>
Sequence:
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="fileWriteSequence" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<property name="OUT_ONLY" scope="default" type="STRING" value="true"/>
<property expression="fn:concat( get-property('NewFileName'), '.', get-property('NewFileFormat'))" name="transport.vfs.ReplyFileName" scope="transport" type="STRING" xmlns:ns2="http://org.apache.synapse/xsd"/>
<property name="messageType" scope="axis2-client" type="STRING" value="text/plain"/>
<send>
<endpoint name="FileEpr">
<address format="pox" uri="vfs:file:///C:/WSO2/ESB/VFS/OUTPUT/"/>
</endpoint>
</send>
</sequence>
This was working fine for old releases of WSO2 products and we also faced the same issue with WSO2 EI 6.3.0 and 6.2.0 versions.
It seems WSO2 did a change on message formatter direction logic since format="pox" is defined in the endpoint level. It's Ignoring the messageType property in newer releases. Therefore you have to remove endpoint format configuration from your address endpoint.
So you need to change your logic as below:
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="fileWriteSequence" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<property action="set" name="OUT_ONLY" value="true"/>
<property expression="fn:concat( get-property('NewFileName'), '.', get-property('NewFileFormat'))" name="transport.vfs.ReplyFileName" scope="transport" type="STRING" xmlns:ns2="http://org.apache.synapse/xsd"/>
<property name="messageType" value="text/plain" scope="axis2"/>
<property name="ContentType" value="text/plain" scope="axis2"/>
<send>
<endpoint name="FileEpr">
<address uri="vfs:file:///C:/WSO2/ESB/VFS/OUTPUT/"/>
</endpoint>
</send>
</sequence>

call external web service from WSO2 ESB

I'm in an internel network and I want to create a service (ESB) to call an external get service.
for example my ESB service path: uri/esb/path/param1/param2
my external path: external/path/param1/param2
here my entry
<?xml version="1.0" encoding="UTF-8"?>
<api context="/services/KRI-getIndividualVehicleFuelco2" name="KRI-getIndividualVehicleFuelco2" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET">
<inSequence>
<property name="renault_error_format" scope="default" type="STRING" value="json"/>
<log level="custom">
<property name="getIndividualVehicleFuelco2" value="********** begin **********"/>
<property expression="get-property('RequestID')" name="RequestID"/>
</log>
<Delegation.DelegationIn>
<Jboss-Endpoint-Key>KRI-customAddressingTransformation.xml</Jboss-Endpoint-Key>
<Custom-Sequence-Header>KRI-commonsequences-processSpecialHeadersIn</Custom-Sequence-Header>
</Delegation.DelegationIn>
<loopback/>
</inSequence>
<outSequence>
<sequence key="KRI-commonsequences-genericErrorHandling"/>
<Delegation.DelegationOut>
<Jboss-Endpoint-Key>KRI-customAddressingTransformation.xml</Jboss-Endpoint-Key>
<Custom-Sequence-Header>KRI-commonsequences-processSpecialHeadersOut</Custom-Sequence-Header>
</Delegation.DelegationOut>
<log level="custom">
<property name="getIndividualVehicleFuelco2" value="********** end **********"/>
<property expression="get-property('RequestID')" name="RequestID"/>
</log>
</outSequence>
<faultSequence>
<loopback/>
</faultSequence>
</resource>
</api>
I created an endpoint with the base uri of the external service.
<?xml version="1.0" encoding="UTF-8"?>
<endpoint name="KIS-fuelco-PPD" xmlns="http://ws.apache.org/ns/synapse">
<address uri="http://kisre701.intra.com/kis/api/v2/individual-vehicle-fuelco2/"/>
</endpoint>
how can I parse parameters and send them to endpoint?

Get Text from Envelope

I want to get text from following Envelope so that I can use it as REST_URL_POSTFIX.
I am sending a plain text message to a JMS queue and following WSO2-ESB proxy service is used as a receiver/listener of JMS queue. I have tried following expressions but they does not work :
$body
$body/text
$body/text()
The SOAP 1.1 or 1.2 body element. For example, the expression
$body/getQuote refers to the first getQuote element in a SOAP body,
regardless of whether the message is SOAP-11 or SOAP-12.
and
//Envelope//Body//text
//Envelope//Body//text()
INFO - (Send
LogMediator To: , MessageID: ID:sunnydyal-K55VM-44230-1439804805911-3:2:1:1:9,
Direction: request, ##### Message = ,
Envelope:
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<axis2ns8:text xmlns:axis2ns8="http://ws.apache.org/commons/ns/payload">Test</axis2ns8:text>
</soapenv:Body>
</soapenv:Envelope>
#Proxy Service:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="JmsToRestProxy"
transports="jms"
statistics="enable"
trace="enable"
startOnLoad="true">
<target>
<inSequence>
<property name="OUT_ONLY" value="true"/>
<property name="HTTP_METHOD" value="GET" scope="axis2" type="STRING"/>
<property name="SOAPAction" scope="default" action="remove"/>
<header name="Action" scope="default" action="remove"/>
<property name="REST_URL_POSTFIX"
expression="//Envelope//Body//text"
scope="axis2"
type="STRING"/>
<switch source="$axis2:HTTP_METHOD">
<case regex="GET">
<property name="HTTP_METHOD" value="GET" scope="axis2" type="STRING"/>
</case>
<case regex="POST">
<property name="HTTP_METHOD" value="POST" scope="axis2" type="STRING"/>
</case>
<default/>
</switch>
<log level="full">
<property name="##### Message" expression="$body/text"/>
</log>
<send>
<endpoint>
<address uri="http://localhost:8080/Rest/rest" format="rest"/>
</endpoint>
</send>
</inSequence>
</target>
<parameter name="transport.jms.ContentType">
<rules>
<jmsProperty>contentType</jmsProperty>
<default>text/plain</default>
</rules>
</parameter>
<description/>
</proxy>
text node belong to a specific namespace "http://ws.apache.org/commons/ns/payload" :
<log level="full">
<property xmlns:syn="http://ws.apache.org/commons/ns/payload" name="##### Message" expression="$body/syn:text/text()"/>
</log>
You can get the body using the $body variable[1]. /text() is trying to access the text element of the body which doesn't exist.
<log level="custom">
<property name="Body" expression="$body" />
</log>
[1] - https://docs.wso2.com/display/ESB481/Synapse+XPath+Variables#SynapseXPathVariables-$body