Showing posts with label wso2 esb. Show all posts
Showing posts with label wso2 esb. Show all posts

Monday, June 23, 2014

WSO2 ESB : Mail sending with HTML complex tag supports


You can use WSO2 ESB to send mails and receive mails with polling using Mail Transport facility as explained in https://docs.wso2.org/display/ESB481/MailTo+Transport


Sending Simple HTML email from ESB:

Sending a simple email body with following html content can be done easily using following builder and formatter in axis2 configuration for given content-type (text/html).

axis2.xml changes:

 <messageBuilder contentType="text/html"   
             class="org.apache.axis2.builder.ApplicationXMLBuilder"/>  
 <messageFormatter contentType="text/html"   
              class="org.apache.axis2.transport.http.ApplicationXMLFormatter"/>   

Sample HTML mail:

 <html>   
      <head>   
       <meta http-equiv="content-type" content="text/html" />   
      </head>   
      <body>   
           <h2 style='color:green'>Testing Simple mail</h2><p>Simple mail details..</p>   
      </body>   
 </html>  



However if we include special html tags to the email content like 
 <DOCTYPE> , 
<!-- Comment --> 
<?xml - xml declaration tags for rich content validating and rendering purpose , you cannot use above mentioned XML builder and formatter and you will get errors while passing the mail message through ESB as below.

 ERROR {org.apache.axis2.transport.mail.MailTransportListener} - Failed to process message {org.apache.axis2.transport.mail.MailTransportListener}   
 org.apache.axiom.om.OMException: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,122]   
 Message: DOCTYPE is not allowed   
     at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:296)   
     at org.apache.axiom.om.impl.llom.OMDocumentImpl.getOMDocumentElement(OMDocumentImpl.java:109)   
     at org.apache.axiom.om.impl.builder.StAXOMBuilder.getDocumentElement(StAXOMBuilder.java:570)   
     at org.apache.axiom.om.impl.builder.StAXOMBuilder.getDocumentElement(StAXOMBuilder.java:566)   
     at org.apache.axis2.builder.ApplicationXMLBuilder.processDocument(ApplicationXMLBuilder.java:81)   
     at org.apache.axis2.transport.TransportUtils.createDocumentElement(TransportUtils.java:180)   
     at org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:146)   
     at org.apache.axis2.transport.mail.MailTransportListener.processMail(MailTransportListener.java:501)   
     at org.apache.axis2.transport.mail.MailTransportListener.access$000(MailTransportListener.java:61)   
     at org.apache.axis2.transport.mail.MailTransportListener$MailProcessor.run(MailTransportListener.java:331)   
     at org.apache.axis2.transport.mail.MailTransportListener.processMail(MailTransportListener.java:295)   
     at org.apache.axis2.transport.mail.MailTransportListener.checkMail(MailTransportListener.java:199)   
     at org.apache.axis2.transport.mail.MailTransportListener.poll(MailTransportListener.java:80)   
     at org.apache.axis2.transport.mail.MailTransportListener.poll(MailTransportListener.java:61)   
     at org.apache.axis2.transport.base.AbstractPollingTransportListener$1$1.run(AbstractPollingTransportListener.java:67)   
     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)   


How to overcome this issue :


You can use BinaryRelayBuilder and ExpandingMessageFormatter to overcome this error and send text/html content-type mails through ESB as explained in below steps.

Step1: Configure mailto transport in ESB

- Download and install the ESB latest version ( http://wso2.com/products/enterprise-service-bus/ )

- Uncomment the mail TransportSender and TransportReceiver from the <ESB_HOME>/repository/conf/axis2/axis2.xml
-Configure with your mail server credentials as required.


 <transportSender name="mailto">  
    <parameter name="mail.smtp.host">smtp.gmail.com</parameter>  
    <parameter name="mail.smtp.port">587</parameter>  
    <parameter name="mail.smtp.starttls.enable">true</parameter>  
    <parameter name="mail.smtp.auth">true</parameter>  
    <parameter name="mail.smtp.user">synapse.demo.0</parameter>  
    <parameter name="mail.smtp.password">mailpassword</parameter>  
    <parameter name="mail.smtp.from">synapse.demo.0@gmail.com</parameter>  
 </transportSender>  


 <transportReceiver name="mailto">  
 </transportReceiver>  



Step 2: Add builder and formatter for content-type “text/html”

 <messageBuilder contentType="text/html"   
             class="org.wso2.carbon.relay.BinaryRelayBuilder"/>  
 <messageFormatter contentType="text/html"   
             class="org.wso2.carbon.relay.ExpandingMessageFormatter"/>  


Step 3: Add the mail sending proxy in your ESB.

Please note that you need to force axis2 mail transport sender to use content-type based formatter specially formatting the mail message with with special html tags mentioned previously.

 <property name="FORCE_CONTENT_TYPE_BASED_FORMATTER" value="true" scope="axis2"/>  

Note: Please note, you need to have latest version of ESB or the patch https://wso2.org/jira/browse/CARBON-14796 applied to ESB 4.8.1 version to effect this property

If you have not specified the above parameter with value “true” , ESB will select default formatter related to the BinaryRelayBuilder which is org.apache.axis2.format.BinaryFormatter as default to handle binary content regardless you had specified the formatter in axis2 configuration file.


1:  <?xml version="1.0" encoding="UTF-8"?>   
2:  <proxy xmlns="http://ws.apache.org/ns/synapse"   
3:      name="JKMailTestProxy"   
4:      transports="https http"   
5:      startOnLoad="true"   
6:      trace="disable">   
7:    <description/>   
8:    <target>   
9:     <inSequence>   
10:       <property name="Subject" value="Testing rich text mail" scope="transport"/>   
11:       <property name="FORCE_CONTENT_TYPE_BASED_FORMATTER"   
12:            value="true"   
13:            scope="axis2"/>   
14:       <property name="OUT_ONLY" value="true"/>   
15:       <property name="FORCE_SC_ACCEPTED" value="true" scope="axis2"/>   
16:       <send>   
17:        <endpoint >   
18:          <address uri="mailto:synapse.demo.1@gmail.com"/>   
19:        </endpoint>   
20:       </send>   
21:       <drop/>   
22:     </inSequence>   
23:    </target>   
24:  </proxy>  



Step 4: Send mail with content including rich text and verify

Lets call proxy using SoapUI tool or curl commands.

Content-type :text/html

Content in  SOAP Body: 

 <?xml version="1.0" encoding="UTF-8"?>   
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
 <!-- Testing mail with rich content -->  
 <html>   
      <head>   
       <meta http-equiv="content-type" content="text/html" />   
      </head>   
      <body>   
           <h2 style='color:green'>Testing Simple mail</h2><p>Simple mail details..</p>   
      </body>   
 </html>  



The address  synapse.demo.1@gmail.com   will receive a mail from  synapse.demo.0@gmail.com






Wednesday, April 2, 2014

WSO2 ESB: File processing using VFS transport and mediation inside a sequence

VFS transport feature in ESB can be used to process the files in the system periodically and take actions as configured. Basically you can use this to transfer file from one location to other with the mediation in between.
Below example shows how to process simple csv flat file in a folder and process each record inside ESB proxy service and forward for further processing into a separate sequence.
We will be using smooks mediator to read csv records from the file and load those into the proxy payload as xml records.

Step 1: Download and install WSO2 ESB




Step 2: Enable VFS transport on ESB and enable JMS transport.

  • Uncomment the below two lines from /<WSO2_ESB>/repository/conf/axis2/axis2.xml


<transportSender name="vfs" class="org.apache.synapse.transport.vfs.VFSTransportSender"/>
<transportReceiver name="vfs" class="org.apache.synapse.transport.vfs.VFSTransportListener"/>


Step 3: Add local entry to refer smooks configuration file

  • Go to the WSO2 ESB Management console ( https://localhost:9443/carbon )
  • Select “Local Entries” from the left panel and add new “Source URL Entry” with below values
    • Name : smook_config
    • URL : file:repository/resources/smooks/smooks-config-mapping.xml
  • Also add copy smook-config-mapping.xml file to the <WSO2_ESB>/repository/resources/smooks/ folder

smook-config-mapping.xml
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.0.xsd">
<!--Configure the CSVParser to parse the message into a stream of SAX events. -->
<resource-config selector="org.xml.sax.driver">
<resource>org.milyn.csv.CSVParser</resource>
<param name="fields" type="string-list">firstname,lastname,gender,age,country</param>
</resource-config>

</smooks-resource-list>


Step 4: Create Proxy as VFS Listener and Sequence to process each record


Add Sequence to used by proxy to forward the each record:

<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="RecProcessSequence">
<log level="full">
<property name="==Processing record =====" value="=="/>
</log>
</sequence>

Add the Proxy :


This Listner proxy will read the files from the input path and load into the proxy payload and iterate each record and call sequence (RecProcessSequence) to process each record further.
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="TestSmookRecVFS"
transports="vfs"
startOnLoad="true"
trace="disable">
<description/>
<target>
<inSequence>
<smooks config-key="smook_config">
<input type="text"/>
<output type="xml"/>
</smooks>
<iterate xmlns:ns2="http://org.apache.synapse/xsd"
xmlns:ns="http://org.apache.synapse/xsd"
xmlns:sec="http://secservice.samples.esb.wso2.org"
expression="//csv-set/csv-record">
<target>
<sequence>
<clone>
<target sequence="RecProcessSequence"/>
</clone>
</sequence>
</target>
</iterate>
</inSequence>
</target>
<parameter name="transport.vfs.ActionAfterProcess">MOVE</parameter>
<parameter name="transport.PollInterval">10</parameter>
<parameter name="transport.vfs.MoveAfterProcess">file:///home/jayalal/Work/SmookRes/out</parameter>
<parameter name="transport.vfs.FileURI">file:///home/jayalal/Work//SmookRes/in</parameter>
<parameter name="transport.vfs.MoveAfterFailure">file:///home/jayalal/Work/ /SmookRes/failed</parameter>
<parameter name="transport.vfs.FileNamePattern">.*.txt</parameter>
<parameter name="transport.vfs.ContentType">text/plain</parameter>
<parameter name="transport.vfs.ActionAfterFailure">MOVE</parameter>
</proxy>


Step 5: Testing and Verify

Copy the input.txt file into the “transport.vfs.FileURI” folder.
name1,lastname1,Male,30,country1
name2,lastname2,Female,40,country2
name3,lastname3,Female,40,country3
name4,lastname4,Female,30,country4
name5,lastname5,Female,40,country5


You will see the ESB logs will print the messages process by ESB.
[2014-01-29 15:11:41,922] INFO - LogMediator To: , WSAction: urn:mediate, SOAPAction: urn:mediate, MessageID: urn:uuid:de01ae68-6afc-4539-9c0e-f820fa181c19, Direction: request, ==Processing record ===== = ==, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><csv-record number="1"><firstname>name1</firstname><lastname>lastname1</lastname><gender>Male</gender><age>30</age><country>SriLanka</country></csv-record></soapenv:Body></soapenv:Envelope>
[2014-01-29 15:11:41,924] INFO - LogMediator To: , WSAction: urn:mediate, SOAPAction: urn:mediate, MessageID: urn:uuid:b436bd8d-a30b-40e1-83c2-00f109b793a5, Direction: request, ==Processing record ===== = ==, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><csv-record number="2"><firstname>name2</firstname><lastname>lastname2</lastname><gender>Female</gender><age>40</age><country>SriLanka</country></csv-record></soapenv:Body></soapenv:Envelope>


More References: