I have a soap service and I need to expose this service as a rest api in Wso2 EI, content type is text/xml, I tried with
<property name="messageType" value="application/json"
scope="axis2"/>
in the outsequence, but it doesnt convert my response into a json. Can you please assist me on how to do it.
I tried this,
<resource methods="POST">
<inSequence>
<send>
<endpoint>
<address uri="http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<property name="messageType" value="application/json" scope="axis2"/>
<send/>
</outSequence>
</resource>
Reason for this is that WSO2 EI or ESB is developed in a manner where it responds to a SOAP request with a SOAP response by default. When you invoke the PROXY or API with SOAPAction and Content-Type: text/xml, EI understands this as a SOAP request and it'll respond with a SOAP response.
So if the client request is in SOAP-1.1 EI responds with a SOAP-1.1 response or if the client request is in SOAP-1.2 EI responds with a SOAP-1.2.
To bypass this behaviour they have provided an additional property as below.
<property name="IsClientDoingREST" scope="default" type="BOOLEAN" value="true"/>
So, before responding the client, properties should be set as below to get the expected behaviour of responding with a JSON.
<property name="IsClientDoingREST" scope="default" type="BOOLEAN" value="true"/>
<property name="messageType" scope="axis2" value="application/json"/>
This approach will help you to get the JSON response out of a SOAP request.
It should work perfectly. A sample REST API configuration given below.
<api xmlns="http://ws.apache.org/ns/synapse" name="CheckREST" context="/samplerest">
<resource methods="GET">
<inSequence>
<send>
<endpoint>
<http uri-template="http://localhost:8280/services/sampleSOAPproxy"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<property name="messageType" value="application/json" scope="axis2" type="STRING"/>
<send/>
</outSequence>
</resource>
</api>
If not working, please mention the EI version you are using.
Related
I have a need to send pdf/png/jpeg files to emails via WSO2 EI 6.5.0
I have a proxy:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="EmailService"
startOnLoad="true"
statistics="disable"
trace="disable"
transports="http,https">
<target>
<inSequence>
<log level="custom">
<property name="[EmailService] Initialize" value="SendMailSequence"/>
</log>
<property name="Subject" scope="transport" value="WSO2 EI Testing"/>
<property name="transport.mail.bodyWhenAttached"
scope="axis2"
value="This text will appear in body"/>
<property name="enableMTOM" scope="axis2" type="STRING" value="true"/>
<log level="custom">
<property expression="/" name="[EmailService] Before Send"/>
</log>
<property name="FORCE_SC_ACCEPTED" scope="axis2" value="true"/>
<property name="OUT_ONLY" value="true"/>
<property name="messageType"
scope="axis2"
type="STRING"
value="multipart/form-data"/>
<send>
<endpoint name="FileEpr">
<address optimize="mtom" uri="mailtoOwn:listopadov_as#kk.bank"/>
</endpoint>
</send>
</inSequence>
<outSequence/>
</target>
<description/>
</proxy>
I am trying to send a file (a cachier cheque) using Fiddler:
POST http://localhost:8280/services/EmailService HTTP/1.1
Content-Type: multipart/form-data; boundary=NewBoundary
--NewBoundary
Content-Disposition: form-data; name="attachment"; filename="cheque.png"
Content-Type: image/png
<...binary data...>
--NewBoundary--
And I get this in the WSO2 EI 6.5.0 console:
[2020-09-07 16:24:19,136] [] INFO - LogMediator [EmailService] Initialize = sendMailSequence
[2020-09-07 16:24:19,138] [] INFO - LogMediator [EmailService] Before Send = <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Body><mediate><attachment filename="cheque.png">iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAFwUlEQVR42rWWaWxUVRTHz3nLvJl5nZkuTBcoVHYqixpMUBCDRgUpoEBcQkICo2DZGmyjCRqEgKBWC9awFdLGhJCoIahoUBMNGjYDJBIRKpCCNKW0pdN2On2zvveO591SLKQf/KA3uTNz37v
3/M4593/PHYT/uaHz0ZSXJ8l5ebPNWGy6nUpJfS/pnsk0wPN7n6Gq2m6v9xiFw0cKm5ttAbhRXDxX37z5q87mZslKp/vmY7+12M9g31h80z1zGEDZBQVkbNjw/LCLF78RD//IyfmQysoqaPDg/yYtiQQkN26sejgcfkMATsvyNlixYq0yapQYW0RU39WFI3w+8CrKXRHAPRHETBOuRqNUnJmJMop
pRLEYRtev3z7DssrFk1Oy/D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ZB/gPga
kRuaFks2tyKcYHtqyTUhRKObGFYq9Mxc9iTbQ3b21iHONcS1I7o1fozR0LPBhI0e3v71dDvW1taQOyUOrobU2RbcBRQwwx+SH4o0t4PerMK1mP2Q/+SxYjZehpaIENPbcw8bdau9BSvCVEXcU4wlCftURkIeNgY6j38GJ5YuhuzsNWlE+mJdb6u4AhjLALi4IGQ03Uec98PlUGhsqxehPn4HLuEW
sKtRcAKrcG0HaAkyyNFkxmPIGwffUy3Spbg9Go2kweJO9o/IxXd9Sm3YAWxyAqu6BSYXLOs5dEwBHRY5RIVn+7WHJOgdP7r1MybLZcPq29pO90nRgjoocQOaEQkxfuLk3ZVqlojR8qbkWp2ZP/zTR1IASn1AuE3d1hT3nsuOUENFs3iGGgGlx2Tb/6SnuJnCZ92eR/8SFJS+a1n6x5AeuCoamvtQ
F9JiZNuWBLvqB/gQM1GRZsgIEx/2m/fksZva/qWCro5x/aWjAu9gplNzf6mfib96T0koiuIbpAAAAAElFTkSuQmCC</attachment></mediate></soapenv:Body></soapenv:Envelope>
And in result I get the cheque.png file but it is corrupted because the data inside is base64 encoded and even if I decode it manually and insert decoded data back into the file the file is still corrupted so there is no way I can view it.
I tried several other options and tried placing base64Decode in the payload:
<payloadFactory media-type="xml">
<format>
<root>
<file filename="cheque.png">$1</file>
</root>
</format>
<args>
<arg evaluator="xml" expression="base64Decode(//*[local-name()='attachment']/text())"/>
</args>
</payloadFactory>
But still nothing helps.
Please help me with a working example of how to send a file over email in WSO2 EI 6.5.0
My axis2 messageBuilders are set and the MailTransportSender is uncommented and works fine.
Using WSO2 ESB 4.8.1, I have configured a WSDL proxy that I want to access over REST. the proxy points to the SOAP WSDL URI and has publish WSDL turned on. This seem to work fine and I can see the service and its various operations in the WSO2 admin UI. Likewise if I go to localhost:8280/services/
The questions is how do I pass operation specific parameters when accessing over HTTP REST?
Let's say my FooService OperationX expects a "p1" parameter, can I pass this directly when accessing localhost:8280/services/FooService/OperationX in a browser?
I tried for example localhost:8280/services/FooService/SomeOperation?p1=somevalue, but always get a validation error that the required parameter is missing:
cvc-complex-type.2.4.b: The content of element 'axis2ns15:OperationXRequest' is not complete. One of '{"somenamespace":p1}' is expected.
Can this be supported by a basic WSDL proxy? Or do I need to use the API?
I think the better option for your scenario is to use api to access over REST. Here I have created an api (I used http://jsonplaceholder.typicode.com/comments as my REST back end) which gets the query parameter(postId) which was sent in REST request (http://172.22.99.96:8290/test/comments?postId=1) and assign that value to a property called mypostId inside the api.
Then I am modifying the payload by adding the mypostId property using payload factory mediator which will match to the echo service request(I have used echo service as the SOAP backend).
Then I use enrich mediator to change my soap envelope to match the echo service request soap envelope by adding "xmlns:echo="http://echo.services.core.carbon.wso2.org"" name space. Finally I am sending my created request to echo service proxy.
<api xmlns="http://ws.apache.org/ns/synapse" name="test" context="/test">
<resource methods="GET" uri-template="/comments?postId={postId}">
<inSequence>
<log level="custom">
<property name="Message Flow" value="--- Order GET ---"></property>
</log>
<log level="custom">
<property name="postId" expression="$url:postId"></property>
</log>
<property name="mypostId" expression="$url:postId"></property>
<call>
<endpoint>
<http method="GET" uri-template="http://jsonplaceholder.typicode.com/comments?postId={uri.var.postId}"></http>
</endpoint>
</call>
<payloadFactory media-type="xml">
<format>
<echo:echoInt xmlns:echo="http://echo.services.core.carbon.wso2.org">
<in>$1</in>
</echo:echoInt>
</format>
<args>
<arg evaluator="xml" expression="get-property('mypostId')"></arg>
</args>
</payloadFactory>
<log level="full"></log>
<log level="custom">
<property name="Message Flow" value="--- After Palyload factory---"></property>
</log>
<property name="extarctedBody" expression="$body"></property>
<log level="custom">
<property name="MyextarctedBody" expression="get-property('extarctedBody')"></property>
</log>
<log level="full"></log>
<enrich>
<source type="inline" clone="true">
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:echo="http://echo.services.core.carbon.wso2.org"></soapenv:Envelope>
</source>
<target type="envelope"></target>
</enrich>
<log level="custom">
<property name="Message Flow" value="--- Order GET2 ---"></property>
</log>
<log level="full"></log>
<enrich>
<source type="property" clone="true" property="extarctedBody"></source>
<target xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:echo="http://echo.services.core.carbon.wso2.org" action="child" xpath="//soapenv:Envelope"></target>
</enrich>
<log level="full"></log>
<send>
<endpoint>
<address uri="http://localhost:8290/services/echo"></address>
</endpoint>
</send>
</inSequence>
<outSequence>
<send></send>
</outSequence>
</resource>
</api>
Hope this may help you .
I have been trying to implement a simple heart beat REST API in WSO2 ESB and am not able to get a response back. Below is the API
<api xmlns="http://ws.apache.org/ns/synapse" name="HealthCheckAPI" context="/HealthCheck">
<resource methods="GET" url-mapping="/status" faultSequence="fault">
<inSequence>
<payloadFactory media-type="json">
<format>{"Status":"OK"}</format>
<args></args>
</payloadFactory>
<log>
<property name="JSON-Payload" expression="json-eval($.)"></property>
</log>
<property name="messageType" value="application/json" scope="axis2" type="STRING"></property>
<respond></respond>
</inSequence>
</resource>
</api>
When I curl the API, the response is zero length. What could be wrong?
curl -v http://localhost:8280/HealthCheck/status
You need to set following property .
<property name="NO_ENTITY_BODY" scope="axis2" action="remove"></property>
Please find the working example
<api xmlns="http://ws.apache.org/ns/synapse" name="HealthCheckAPI" context="/HealthCheck">
<resource methods="GET" url-mapping="/status" faultSequence="fault">
<inSequence>
<payloadFactory media-type="json">
<format>{"Status":"OK"}</format>
<args></args>
</payloadFactory>
<log>
<property name="JSON-Payload" expression="json-eval($.)"></property>
</log>
<property name="NO_ENTITY_BODY" scope="axis2" action="remove"></property>
<property name="messageType" value="application/json" scope="axis2" type="STRING"></property>
<respond></respond>
</inSequence>
</resource>
</api>
Shibu,
Use following expression in your property mediator.
json-eval($.Status)
I'm wondering if someone can help me with the following setup.
I want to send a message from my application via JMS to WSO2 ESB so the ESB can send it as en email. I'm using ActiveMQ as queue. Until now, when I send a message via the ActiveMQ interface to the queue, wso2 esb gets it. Then, wso2 esb send the message as email to a specific email address.
So I could configure ActiveMQ and WSO2 esb to send the JMS message to a specific email address (eg. specificaddress#test.com).
And here is my question. How can I modify the receiver address for the email? In the ESB sequence configuration, I currently use a specific address. But the address is dependant on the user that uses my application. So I have to change the "To" property, dependant on the user that has to receive the email.
So how can I pass the values for the properties "To", but also for "Subject", through a JMS message to WSO2 esb sequence?
That's the configuration of the sequence I have:
<sequence xmlns="http://ws.apache.org/ns/synapse" name="sendMail">
<property name="messageType" value="text/html" scope="axis2" type="STRING"></property>
<property name="ContentType" value="text/html" scope="axis2"></property>
<property name="Subject" value="This is the subject." scope="transport"></property>
<property name="To" value="specificaddress#test.com" scope="transport"></property>
<property name="OUT_ONLY" value="true" scope="default" type="STRING"></property>
<log level="full"></log>
<send>
<endpoint>
<address uri="mailto:"></address>
</endpoint>
</send>
</sequence>
And this is my proxy:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="sendToMailIn"
transports="jms"
statistics="disable"
trace="disable"
startOnLoad="true">
<target inSequence="sendMail"/>
<description/>
</proxy>
I hope someone has a clue.
UPDATE
I think I have the solution!!! Wow :-) Maybe, at first, I was stupid, but here it is ...
What you can do is sending a SOAP envelop through a JMS message to WSO2 ESB. And then, with an XPath expression, you can get the passed values. A little bit has to changed at the proxy and the sequence.
This is the new sequence:
<sequence xmlns="http://ws.apache.org/ns/synapse" name="sendMail">
<property name="messageType" value="text/html" scope="axis2" type="STRING"></property>
<property name="ContentType" value="text/html" scope="axis2"></property>
<property xmlns:ns="http://org.apache.synapse/xsd" name="Subject" expression="$body/subject" scope="transport"></property>
<property xmlns:ns="http://org.apache.synapse/xsd" name="To" expression="$body/to" scope="transport"></property>
<property name="OUT_ONLY" value="true" scope="default" type="STRING"></property>
<log level="full"></log>
<send>
<endpoint>
<address uri="mailto:"></address>
</endpoint>
</send>
</sequence>
And this is the new proxy:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="sendToMailIn"
transports="jms"
statistics="disable"
trace="disable"
startOnLoad="true">
<target inSequence="sendMail"/>
<parameter name="transport.jms.ContentType">
<rules>
<jmsProperty>contentType</jmsProperty>
<default>text/xml</default>
</rules>
</parameter>
<description/>
</proxy>
And this was my SOAP Envelop that WSO2 ESB receives from my ActiMQ queue as JMS message:
<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org /soap/envelope/">
<soapenv:Body>
<subject>Email subject comes here.</subject>
<to>address#test.com</to>
</soapenv:Body>
</soapenv:Envelope>
you have couple of options here.
you can use http headers and send the "to", "subject" values to ESB.
Send it as a payload value and extract using XPath expression
I solved it with using JSON in a JMS message. Here is my setup that works for me.
This is my JSON message:
{"to":"mail#test.com","subject":"TestSubject","mailbody":"Some body text ..."}
This is my proxy:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="sendToMailIn"
transports="jms"
statistics="disable"
trace="disable"
startOnLoad="true">
<target inSequence="sendMail"/>
<parameter name="transport.jms.ContentType">
<rules>
<jmsProperty>contentType</jmsProperty>
<default>application/json</default>
</rules>
</parameter>
<parameter name="transport.mail.ContentType">application/xml</parameter>
<description/>
</proxy>
And this is my sequence:
<sequence xmlns="http://ws.apache.org/ns/synapse" name="sendMail">
<property name="messageType" value="text/plain" scope="axis2" type="STRING"></property>
<property name="ContentType" value="text/plain" scope="axis2"></property>
<property xmlns:ns="http://org.apache.synapse/xsd" name="Subject" expression="json-eval($.subject)" scope="transport"></property>
<property xmlns:ns="http://org.apache.synapse/xsd" name="To" expression="json-eval($.to)" scope="transport"></property>
<property name="OUT_ONLY" value="true" scope="default" type="STRING"></property>
<script language="js">
<![CDATA[var mailbody = mc.getPayloadJSON().mailbody.toString(); mc.setPayloadXML(
<ns:text xmlns:ns="http://ws.apache.org/commons/ns/payload">{mailbody}</ns:text>);]]>
</script>
<log level="full"></log>
<send>
<endpoint>
<address uri="mailto:"></address>
</endpoint>
</send>
</sequence>
I am relatively new to WSO2 ESB,
Trying to invoke a REST Web service with GET parameter which is hosted on WSO2 ESB through another REST API.
This is a simple Web service(SampleREST) which replies with welcome messsage in XML format,
When i am invoking this service directly; i can see the correct response on browser, shown below
<Message xmlns="http://ws.apache.org/ns/synapse">WelcomeRanjan</Message>
Now i created another REST web service(InvokeSampleRest) which in turn invokes SampleREST web service with the parametrized GET,
the Invoke WebService Client returns me the XML response wrapped inside the mediator xml tag and other malformed tags, shown below
<mediate><<Message xmlns>"http://ws.apache.org/ns/synapse">WelcomeRanjan</Message></<Message xmlns></mediate>
Here is my Code for the SampleREST API
<?xml version="1.0" encoding="UTF-8"?>
<api xmlns="http://ws.apache.org/ns/synapse" name="SampleREST" context="/SampleRest" hostname="10.203.245.47">
<resource methods="GET" uri-template="/{str1}">
<inSequence>
<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>
<Message>$1</Message>
</format>
<args>
<arg expression="get-property('uri.var.str1')"/>
</args>
</payloadFactory>
<log level="full" separator=",">
<property name="sequence" value="*** Got Request ***"/>
</log>
<send/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
The code for the InvokeSampleREST code is as shown below( i am calling SampleREST service from here )
<?xml version="1.0" encoding="UTF-8"?>
<api xmlns="http://ws.apache.org/ns/synapse" name="InvokeSampleREST" context="/InvokeSampleREST" hostname="10.203.245.47">
<resource methods="GET" uri-template="/{str1}">
<inSequence>
<log level="full" separator=","/>
<property name="REST_URL_POSTFIX" expression="fn:concat('/Welcome',get-property('uri.var.str1'))" scope="axis2" type="STRING"/>
<log level="full">
<property name="sequence" value="****Message Sent *** "/>
</log>
<send>
<endpoint>
<address uri="http://10.203.245.47:8280/SampleRest/"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<log level="full" separator=",">
<property name="out" value="** Ouput of Rest call ***"/>
</log>
<property name="ContentType" value="application/xml" scope="axis2" type="STRING"/>
<send/>
</outSequence>
<faultSequence/>
</resource>
</api>
Appreciate any help.
Thanks,
Ranjan
Why are you using two APIs? You can design your flow, in a single API. I mean you can merge them together.
BTW, you need to set the contentType property in your backend API(ie:At SampleREST API)
Because, when you send back the response to "InvokeSampleREST" API, System doesnt know the content-type of the incoming response and try to handle it as text message.
Eg:
<api name="SampleREST" context="/SampleRest" hostname="localhost">
<resource methods="GET" uri-template="/{str1}">
<inSequence>
<header name="To" action="remove"/>
<property name="NO_ENTITY_BODY" scope="axis2" action="remove"/>
<property name="RESPONSE" value="true" scope="default" type="STRING"/>
<payloadFactory>
<format>
<Message>$1</Message>
</format>
<args>
<arg expression="get-property('uri.var.str1')"/>
</args>
</payloadFactory>
<log level="full" separator=",">
<property name="sequence" value="*** Got Request ***"/>
</log>
<property name="Content-Type"
value="application/xml"
scope="transport"
type="STRING"/>
<send/>
</inSequence>
<faultSequence/>
</resource>
Try doing the following change at the InvokeSampleREST API.
At the outSequence, set the messageType as follows before the <send> mediator.
<property name="messageType" value="application/xml" scope="axis2"/>