To read LocalEntry values in WSO2 EI - wso2

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

Related

Reading Local Entry (XML,URL) in WSO2 ESB

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

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

Converting a SOAP request to a HTTP 200 return on wsO2 ESB

I have a instance where I need to implement a proxy service that takes a SOAP message, forwards it to an internal system (SOAP) and returns a HTTP 200 response to the original server.
Basically the response should be completely void of any soap detail (there's technically no output message in the WSDL that I have to implement).
Here's what I have so far (which simply takes the request & echos it back as the response):
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="ExampleHttp200Return"
transports="http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<log level="full" separator=", - inSequence: received - "/>
<header name="To" action="remove"/>
<property name="RESPONSE" value="true" scope="default" type="STRING"/>
<property name="SC_ACCEPTED" value="false" scope="axis2"/>
<property name="HTTP_SC" value="200" scope="axis2"/>
<send/>
</inSequence>
</target>
<description/>
</proxy>
If you want to send back an empty response, you can add that before send mediator :
<property name="messageType" scope="axis2" value="text/plain"/>
<enrich>
<source type="inline">
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Header/>
<soapenv:Body>
<text xmlns="http://ws.apache.org/commons/ns/payload"/>
</soapenv:Body>
</soapenv:Envelope>
</source>
<target type="envelope"/>
</enrich>

How to convert (JSON toXML) request and Response(XML to JSON) to call an external Soap Service

i am new to wso2.. My requirement is
1) I want to transform incoming JSON request into XML format
2) Send that XML request to an external SOAP service
3) Response will get as XML and need to convert it into JSON format
Did somebody ever did that? If so, could you please share how you did?
I have the below configuration for a PROXY service
`<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="CelsiusToFahrenheitService"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<property name="messageType" value="text/xml" scope="axis2"/>
<property name="Proxy-Authorization"
expression="fn:concat('Basic', base64Encode('INDIA\username:pwd'))"
scope="transport"/>
<property name="POST_TO_URI" value="true" scope="axis2"/>
<property name="DISABLE_CHUNKING" value="true" scope="axis2"/>
<header name="Action"
value="http://www.w3schools.com/webservices/CelsiusToFahrenheit"/>
<send>
<endpoint>
<address uri="http://www.w3schools.com/webservices/tempconvert.asmx"
format="soap11"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<property name="messageType" value="text/xml" scope="axis2"/>
<send/>
</outSequence>
</target>
<description/>
</proxy>`
XML Request
<CelsiusToFahrenheit xmlns="http://www.w3schools.com/webservices/">
<Celsius>20</Celsius>
</CelsiusToFahrenheit>
XMl Response
<CelsiusToFahrenheitResponse xmlns="http://www.w3schools.com/webservices/">
<CelsiusToFahrenheitResult>68</CelsiusToFahrenheitResult>
</CelsiusToFahrenheitResponse>
Need to send REQUEST as JSON and RESPOSNE also get as JSON
Can anyone please help me the scenario
How to use ScriptMediator to do the above proxy service. I have got sample from here (https://docs.wso2.org/display/ESB481/Sample+441%3A+Converting+JSON+to+XML+Using+JavaScript)
I have did my configuration like this (Don't know is it correct or not)
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="JsonToXMLProxy"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<script language="js">var cel= mc.getPayloadJSON().CelsiusToFahrenheit.Celsius.toString();
mc.setPayloadXML(
<CelsiusToFahrenheit xmlns="http://www.w3schools.com/webservices/">
<Celsius>{cel}</Celsius>
</CelsiusToFahrenheit>);</script>
<property name="messageType" value="text/xml" scope="axis2"/>
<log level="full"/>
</inSequence>
<outSequence>
<log level="full"/>
<property name="messageType" value="text/xml" scope="axis2"/>
<send/>
</outSequence>
<endpoint>
<address uri="http://www.w3schools.com/webservices/tempconvert.asmx"
format="soap11"/>
</endpoint>
</target>
<description/>
</proxy>
But i am getting the exception like this
`[2014-03-20 18:19:02,391] INFO - LogMediator To: /services/JsonToXMLProxy.JsonToXMLProxyHttpEndpoint, MessageID: urn:uuid:a2eeeb26-94e1-4ed1-a3f9-79f1d1461821, Direction: request, Envelope: <?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><CelsiusToFahrenheit xmlns="http://www.w3schools.com/webservices/"><Celsius>20</Celsius></CelsiusToFahrenheit></soapenv:Body></soapenv:Envelope>
[2014-03-20 18:19:03,130] WARN - ClientHandler Received an unexpected response - of content type : text/html and status code : 411 with reason : Length Required For : 172.26.40.214:8080 For Request : Axis2Request [Message ID : urn:uuid:39d47344-f74c-4ec5-855b-5eb90e178b6d] [Status Completed : true] [Status SendingCompleted : true]
[2014-03-20 18:19:04,133] INFO - BuilderUtil OMException in getSOAPBuilder
org.apache.axiom.om.OMException: SOAP message MUST NOT contain a Document Type Declaration(DTD)
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.createDTD(StAXSOAPModelBuilder.java:462)
at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:282)
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.getSOAPEnvelope(StAXSOAPModelBuilder.java:204)
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.<init>(StAXSOAPModelBuilder.java:154)
at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.<init>(StAXSOAPModelBuilder.java:140)
at org.apache.axis2.builder.BuilderUtil.getSOAPBuilder(BuilderUtil.java:659)
at org.apache.axis2.transport.TransportUtils.createDocumentElement(TransportUtils.java:198)
at org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:146)
at org.apache.synapse.transport.nhttp.ClientWorker.run(ClientWorker.java:253)
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)
[2014-03-20 18:19:04,137] INFO - BuilderUtil Remaining input stream :[]
[2014-03-20 18:19:04,137] WARN - ClientWorker Unexpected response received. HTTP response code : 411 HTTP status : Length Required exception : SOAP message MUST NOT contain a Document Type Declaration(DTD)
[2014-03-20 18:19:04,153] INFO - LogMediator To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , SOAPAction: , MessageID: urn:uuid:39d47344-f74c-4ec5-855b-5eb90e178b6d, Direction: response, Envelope: <?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><soapenv:Fault><faultcode>411</faultcode><faultstring>Unexpected response received. HTTP response code : 411 HTTP status : Length Required exception : SOAP message MUST NOT contain a Document Type Declaration(DTD)</faultstring><detail>Unexpected response received. HTTP response code : 411 HTTP status : Length Required exception : SOAP message MUST NOT contain a Document Type Declaration(DTD)</detail></soapenv:Fault></soapenv:Body></soapenv:Envelope>`
Please help me for this issue
Thanks in advance.
you could use this example using de payloadfactory mediator to build the messages, it work for me in your scenario:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="JsonToXMLProxy"
transports="https http local"
startOnLoad="true"
trace="disable">
<description/>
<target>
<endpoint>
<address uri="http://www.w3schools.com/webservices/tempconvert.asmx" format="soap11"/>
</endpoint>
<inSequence>
<log>
<property name="TEMPERATURA_ENTRADA" expression="json-eval($.celsius)"/>
</log>
<payloadFactory media-type="xml">
<format>
<web:CelsiusToFahrenheit xmlns:web="http://www.w3schools.com/webservices/">
<web:Celsius>$1</web:Celsius>
</web:CelsiusToFahrenheit>
</format>
<args>
<arg evaluator="json" expression="$.celsius"/>
</args>
</payloadFactory>
<header name="Action"
value="http://www.w3schools.com/webservices/CelsiusToFahrenheit"/>
</inSequence>
<outSequence>
<log>
<property xmlns:p="http://www.w3schools.com/webservices/"
name="TEMPERATURA_SALIDA"
expression="//p:CelsiusToFahrenheitResponse/p:CelsiusToFahrenheitResult"/>
</log>
<payloadFactory media-type="json">
<format>
"Temperatura" : {
"EnFahrenheit" : $1
}
</format>
<args>
<arg xmlns:p="http://www.w3schools.com/webservices/"
evaluator="xml"
expression="//p:CelsiusToFahrenheitResponse/p:CelsiusToFahrenheitResult"/>
</args>
</payloadFactory>
<property name="messageType" value="application/json" scope="axis2"/>
<send/>
</outSequence>
</target>
</proxy>

WSO2 ESB logging in proxy

I have simple proxy with send messages to some url. I would like to know when something is send through proxy, and when response is send back.
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse" name="SynchronizeService" transports="https http" startOnLoad="true" trace="disable">
<target>
<inSequence>
<log level="simple"/>
<send>
<endpoint key="SynchronizeServiceEndpoint"/>
</send>
</inSequence>
<outSequence>
<log level="simple"/>
<send/>
</outSequence>
</target>
</proxy>
I added log mediator but the problem is, it don't log any information which allow to connect request and response. So example log looks like:
[2013-06-03 15:38:07,914] INFO - LogMediator To: http://esb-ip:9763/services/SynchronizeService, WSAction: http://test.pl/WebService/getWorkPlan, SOAPAction: http://test.pl/WebService/getWorkPlan, ReplyTo: http://www.w3.org/2005/08/addressing/anonymous, MessageID: urn:uuid:36b60af3-dc30-4004-a239-26523774f52b, Direction: request
[2013-06-03 15:38:08,016] INFO - LogMediator To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , SOAPAction: , ReplyTo: http://www.w3.org/2005/08/addressing/anonymous, MessageID: urn:uuid:8f753934-c64a-4276-a916-dceaeda3def0, Direction: response
In logged response there is no information about SOAPAction, messageIds are different. How can I connect request with response in logs? I would like to known when response was send. How can I do this?
You did a simple log, which will log very basic info about the message. Do a log level=full, that will log the full message which is passed through the system
I didn't know that is is possible to assign variables in input Sequence and use them in output Sequence. I assign messageId from input sequence and log it in output. My proxy after changes looks like:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse" name="SynchronizeService" transports="https http" startOnLoad="true" trace="disable">
<target>
<inSequence>
<log level="simple"/>
<property name="requestSysDate" expression="get-property('SYSTEM_DATE')" scope="default" type="STRING"/>
<property name="requestMsgId" expression="get-property('MessageID')" scope="default" type="STRING"/>
<filter xmlns:procsyn="http://test.pl/WebService/Synchronize" xpath="//procsyn:getWorkPlanIn">
<property name="requestMethod" value="getWorkPlan" scope="default" type="STRING"/>
</filter>
<send>
<endpoint key="SynchronizeServiceEndpoint"/>
</send>
</inSequence>
<outSequence>
<log level="custom">
<property name="proxyName" value="SynchronizeService"/>
<property name="requestMethod" expression="get-property('requestMethod')"/>
<property name="requestSysDate" expression="get-property('requestSysDate')"/>
<property name="requestMsgId" expression="get-property('requestMsgId')"/>
</log>
<send/>
</outSequence>
</target>
</proxy>