Terminating send mediator with mailto transport wso2 esb - esb

There are numerous examples of using the mailto transport in WSO2 ESB to send emails (based on filter mediator, fault sequence etc.). However, I have attempted this a number of ways all with the same result i.e. that the proxy service never terminates. I am testing this using SOAPUi.
This is my proxy service
<proxy xmlns="http://ws.apache.org/ns/synapse" name="TMCService" transports="https,http" statistics="enable" trace="enable" startOnLoad="true">
<target inSequence="gov:/trunk/sequences/seqGetTMCData">
<outSequence>
<filter xmlns:trac="http://mynamespaceuri" xpath="//trac:SaveIncidentsResponse">
<then>
<log level="full"/>
<property name="Subject" value="CEP Event" scope="transport"/>
<property name="OUT_ONLY" value="true"/>
<send>
<endpoint>
<address uri="mailto:conrad.crampton#gmail.com"/>
</endpoint>
</send>
</then>
<else>
<send/>
</else>
</filter>
</outSequence>
</target>
<description></description>
</proxy>
the email sends no problem with the response from the inSequence - no problem, it just never terminates. I have to manually stop it in SOAPUi to stop.
I've tried putting drop after the send in the then element, that doesn't work either.
I guess I am missing something fundamental in how I am understanding the out sequence to work, but this is pretty much lifted from the many examples out there and no one else appears to be having the same issue as me.
Any suggestions/ pointers??
WSO2 ESB v4.5.1

I have resolved this by cloning the response message, sending one to the mailto transport and sending the other one as a default send (no endpoint) which returns back to the client.
Kind of makes sense as the OUT_ONLY property is explicitly saying there will be no response, so have ensure that the client receives one by cloning the message.
Still welcome any other comments if there is another way of doing this without clone mediator.
So why doesn't the examples show this!

Related

ESB WSO2 How to send an email from esb

I want to make an api for sending some emails from a SMTP server but I don't a user and a password to put in axis2.xml file.
So my axis2.xml file looks like this:
<transportSender name="mailto" class="org.apache.axis2.transport.mail.MailTransportSender">
<parameter name="mail.smtp.host">email.example.ro</parameter>
<parameter name="mail.smtp.port">25</parameter>
<parameter name="mail.smtp.starttls.enable">true</parameter>
<parameter name="mail.smtp.auth">true</parameter>
<parameter name="mail.smtp.user"></parameter>
<parameter name="mail.smtp.password"></parameter>
<parameter name="mail.smtp.from">email#example.com</parameter>
</transportSender>
I searched on wso2 docs and I found that I have to do a proxy service for sending emails. Is it possible to do it with just an api?
I'm a real newbie with wso2 and my question is what should my proxy service look like and how can I call this service? (should I call it from an api, or what? because I want to send emails based on a value from a database).
#jakub.voves answer is correct. Let me add more details.
Proxy, APIs, Inbound Endpoints etc are a way to bring external messages into the system.(APIs help you to accept Restful invocation, Proxies allow you to consume SOAP messages, JMS Inbound Endpoints allow you to consume JMS messages from queues/topics) Once you receive the message you can use a combination of mediators to mediate the message.(Message transformations, calling different services etc.) So the mediators within a WSO2 environment are common to all types of services. Hence whatever mediators you use in proxy service can be used in APIs, or in Inbound Endpoints. Other than the mediators WSO2 also provides connectors to extend the functionality of the Server. So in conjunction with Mediators, you can use connectors as well. So in your case either you can use the mailto transport or you can consider using the Email connector. Email sending will look like something below with the connector.
<email.send configKey="smtpsconnection">
<from>{json-eval($.from)}</from>
<to>{json-eval($.to)}</to>
<subject>{json-eval($.subject)}</subject>
<content>{json-eval($.content)}</content>
</email.send>
One additional thing, If your SMTP server doesn't need authentication set the following property to false in axis2.xml.
<parameter name="mail.smtp.auth">false</parameter>
You can create api and try something like this
<property name="messageType" value="text/html" scope="axis2" type="STRING"/>
<property name="ContentType" value="text/html" scope="axis2" type="STRING"/>
<property name="Subject" value="Test Message" scope="transport" type="STRING"/>
<property name="OUT_ONLY" value="true" scope="default" type="STRING"/>
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<test xmlns="">This is a test email.</test>
</soapenv:Body>
</soapenv:Envelope>
</format>
</payloadFactory>
<send>
<endpoint>
<address uri="mailto:another.user#example.com"/>
</endpoint>
</send>
http://wso2experts.com/ei/send-email-from-wso2-ei-mailto-transport/

Handling large lists with wso2

I'm using the wso2 integration studio 7.1.0.
When I'm trying to modify the obtained payload from an external endpoint call, if the endpoint response has a list longer than 4549 elements, than the JsonXMLStreamReader gives a StackOverflowError.
This problem occurs with several mediators: Iterator, PayloadFactory, Script...
I've already checked the JVM memory parameters and I've already tried to change the axis2 JsonFormatter and JsonBuilder.
The simplest example I tried is to call the end point that returns a json holding a list of integers. Then a Script mediator with nashornJS takes the payload add field to the json and reset the payload. If the returned list from the endpoint is smaller than 4549 elements it works correctly, otherwise it gets a StackOverflowError exception.
wso2 code:
<resource methods="GET" uri-template="/cc">
<inSequence>
<call>
<endpoint key="l2-prova"/>
</call>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
<resource methods="GET" uri-template="/dd">
<inSequence>
<call>
<endpoint key="l2-prova"/>
</call>
<script language="nashornJs"><![CDATA[body = mc.getPayloadJSON();
body.len = body.list.length
mc.setPayloadJSON(body)]]></script>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
exception:
[2020-11-04 10:55:53,044] ERROR {org.apache.axis2.transport.base.threads.NativeWorkerPool} - Uncaught exception java.lang.StackOverflowError
at org.apache.synapse.commons.staxon.core.json.stream.impl.JsonScanner.yytext(JsonScanner.java:481)
at org.apache.synapse.commons.staxon.core.json.stream.impl.JsonScanner.yylex(JsonScanner.java:903)
at org.apache.synapse.commons.staxon.core.json.stream.impl.JsonScanner.nextSymbol(JsonScanner.java:310)
at org.apache.synapse.commons.staxon.core.json.stream.impl.JsonStreamSourceImpl.next(JsonStreamSourceImpl.java:156)
at org.apache.synapse.commons.staxon.core.json.stream.impl.JsonStreamSourceImpl.peek(JsonStreamSourceImpl.java:272)
at org.apache.synapse.commons.staxon.core.json.JsonXMLStreamReader.consume(JsonXMLStreamReader.java:151)
at org.apache.synapse.commons.staxon.core.json.JsonXMLStreamReader.consume(JsonXMLStreamReader.java:213)
at org.apache.synapse.commons.staxon.core.json.JsonXMLStreamReader.consume(JsonXMLStreamReader.java:213)
at org.apache.synapse.commons.staxon.core.json.JsonXMLStreamReader.consume(JsonXMLStreamReader.java:213)
...
When JsonXMLStreamReader consuming the json payload, it goes through the following method call.
https://github.com/wso2/wso2-synapse/blob/master/modules/commons/src/main/java/org/apache/synapse/commons/staxon/core/json/JsonXMLStreamReader.java#L149
The above is a recursive method that is consuming the stack until it finishes reading the last element of the array.
Hence, the StackOverflow error is causing by a large number of array elements.
try to increase the stack size from the JVM and check again.

How to host and transform SOAP message in WSO2

I am relative new in WSO2 to create services, and i have to do a project in the enterprise where i work. First i had to take a message in, for example, json and send xml. I was able to do so, with this code:
<?xml version="1.0" encoding="UTF-8"?>
<api context="/Test" name="JsonToXmlApi" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST" uri-template="/xml/">
<inSequence>
<property name="messageType" scope="axis2" type="STRING" value="text/xml"/>
<header action="remove" name="To" scope="default"/>
<property name="RESPONSE" scope="default" type="STRING" value="true"/>
<send/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
Now i need to do the following, i have to send a message in SOAP, XML format, consume it in SOAP and receive back with JSON or XML format.
Someone could help me? I am asking because i am searching for tutorials, for beginners, about it for more than a week, and wasn't able to solve my problem.
Another question, would be possible to do this in REST API project?
If someone could help me, with a detailed tutorial or good video teaching that, i would be grateful.
Thank you.
Your post is a bit unclear but I will try to summarise the typical common combinations of input interface, wso2 component and backend services when you are developing web services.
Web services are services you can invoke over http or https - you might have three types of interfaces for these and you might use two types of WSO2 ESB components - proxy and API - to develop these.
1) Input/Output: JSON
WSO2 component: API
Backend: Any (JSon/XML/SOAP)
2) Input/Output: Plain XML
WSO2 component: API
Backend: Any (JSon/XML/SOAP)
3) Input/Output: SOAP
WSO2 component: Proxy
Backend: Any (JSon/XML/SOAP)
One of the key components of any SOAP service is the WSDL - the WSO2 proxy allows you to publish a WSDL so that your caller knows what sort of data and operations your service provides.
See more here: https://docs.wso2.com/display/EI611/Working+with+Proxy+Services
Adding this note only because you asked:
Technically, you might be able to receive a SOAP message in an API component because SOAP is just an XML document - but it would be really pointless. The purpose of the API component is to expose resources and allow you to perform HTTP operations (GET, POST etc) on those resources (this model can be called REST).
The purpose of a SOAP based web service is to expose data and operations as defined in a WSDL and the WSO2 proxy component is what allows you to do that.

411 Response on Post from WSO2 API Manager to REST endpoint

I have setup a REST endpoint in WSO2 AM that calls out to another REST endpoint. When the call is made I am receiving a 411 response code. The request contains a JSON body, the content type and accepts header are set to application/json. I can curl the backing service that WSO2 is accessing directly with the same params and it works correctly. It seems that WSO2 AM is stripping or not sending the content-length.
Why is the content length not being sent to the REST endpoint that is being accessed?
Sending content-length is disabled by default because it can cause performance degradation. But you can enable it by adding following to the api's insequence. (see https://docs.wso2.com/display/AM1100/Adding+Mediation+Extensions on adding custom sequence to an api)
<property name="COPY_CONTENT_LENGTH_FROM_INCOMING" value="true" scope="axis2"/>
<property name="FORCE_HTTP_CONTENT_LENGTH" scope="axis2" value="true"></property>
following is a sample sequnce
<sequence xmlns="http://ws.apache.org/ns/synapse" name="contentLengthadd">
<property name="COPY_CONTENT_LENGTH_FROM_INCOMING" value="true" scope="axis2"/>
<property name="FORCE_HTTP_CONTENT_LENGTH" scope="axis2" value="true"></property>
</sequence>
This sequence will get the content length from incoming request request and pass it the request header to the backend.
you can read more about these two properties in https://docs.wso2.com/display/ESB481/HTTP+Transport+Properties#HTTPTransportProperties-FORCE_HTTP_CONTENT_LENGTH

Spring RESTful Web Service with JSON gives HTTP 406 error code

I am using Spring 3.2.5 to create a RESTful web service. To implement it I've used #ResponseBody tag. When I use InternalResourceViewResolver and try to load Html response then it is working fine. But when I call a URL which is marked as #ResponseBody then it gives HTTP 406 error code with error text as
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
I have included Jackson jar files in my lib directory as well.
Here is my controller method which handles service request.
#ResponseBody
#RequestMapping (value = "/resp.htm")
public Data jsonResp() {
Data d = new Data();
d.setName("TEst");
d.setAddr("Address....");
return d;
}
There are lots of questions have been asked & answered, I've tried many of them, but it still gives the same result. Then I came across a new kind of answer, which was stating to use ContentNegotiatingViewResolver. By using it I am able to view response in intended format. That is JSON format.
After using ContentNegotiatingViewResolver, servlet dispatcher code looks like this:
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
</map>
</property>
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
</bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="true" />
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
So, my question is, whenever we require to use Spring's Web Service feature, do we must require to have ContentNegotiatingViewResolver?
Add gson in your class path. as well as jackson
I had a similar issue while calling a spring rest service from angular 8 for uploading a file. I was using ResponseEntity to encapsulate the success or failure message for which I was having the 406 response. all I did was in the UI side this.httpClient.post(url, formData,{responseType: 'text'}) and I was able accept the string as response from the service response.
The annotation #ResponseBody used with custom class types, generally uses MappingJackson2HttpMessageConverter to convert the object to JSON and return it in application/json content.
Since your request doesn't contain an Accept header for application/json, Spring deems the content it's creating as unacceptable and instead returns a 406.
You could simply change your request to add the Accept header. (You can't do this easily in a browser).
In my case the request had .html suffix and received this error. Once removed, it worked fine.
All you need to do is to add jackson libraries to your classpath find them Here