Tuesday, June 7, 2016

Sending mails with attachments via WSO2 ESB

1) Enable mail transport in axis2.xml

Navigate to <ESB_HOME>/repository/conf and enable following in axis2.xml


<transportReceiver name="mailto" class="org.apache.axis2.transport.mail.MailTransportListener">
        <!-- configure any optional POP3/IMAP properties
        check com.sun.mail.pop3 and com.sun.mail.imap package documentation for more details-->
  </transportReceiver>

<transportSender class="org.apache.axis2.transport.mail.MailTransportSender" 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">abc@gmail.com</parameter>
        <parameter name="mail.smtp.password">abc123</parameter>
        <parameter name="mail.smtp.from">abc@gmail.com</parameter>
</transportSender>

2) Create following proxy 


<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="MTOMService"
       transports="http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <log level="custom">
            <property name="sequence" value="sendMailSequence"/>
         </log>
         <property name="Subject" value="sac" scope="transport"/>
         <property name="OUT_ONLY" value="true" scope="default" type="STRING"/>
         <log level="full"/>
         <send>
            <endpoint name="FileEpr">
               <address uri="mailto:mymail@gmail.com" optimize="mtom"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
   </target>
   <publishWSDL key="gov:/trunk/services/wsdl/mywsdl"/>
   <description/>
</proxy>

3) Go to soap UI and set Enable MTOM to true which is in left side of UI.

4) Attach the file you want  to send and set the cid:filename in your request as below


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mtom="http://org.wso2/sample/mtom">
   <soapenv:Header/>
   <soapenv:Body>
      <mtom:getBinaryData>
         <!--Optional:-->
         <mtom:dataHandler>cid:sac.pdf</mtom:dataHandler>
      </mtom:getBinaryData>
   </soapenv:Body>
</soapenv:Envelope>    

            
       
5) Once you invoke the proxy you will be able to see the attached file (sac.pdf) in recipient side.

6)  In here you will get the message body as "Web Service Message Attached" in receiving mail. This is the default message body sent by ESB. So if you want to add a customized body add the following property to the proxy.


<property name="transport.mail.bodyWhenAttached"
                   value="This text will appear in body" 
                   scope="axis2"/> 

Then you will get "This text will appear in body" message as the body of receiving email.

Monday, February 29, 2016

ESB Debugging Code ,Building and Do Patching

From this blog I will explain following.
  1. Debug the source code of ESB 4.9.0
  2. Apply a diff to ESB and build the code to do patching
  1. Debug ESB 4.9.0
  • Download the source code from https://github.com/wso2/wso2-synapse/tree/Apache-Synapse-2.1.3-wso2v11
  • Add the pom located in <CODE_BASE_490>/wso2-synapse-Apache-Synapse-2.1.3-wso2v11 to Intellij Idea
  • Navigate to Run-> Edit Configuration and add a new configuration by giving a name and a port (I have set up to 5008)
  • Then add debug points to code and you will be able to see the added  points by clicking view Breakpoints

  • Then start the ESB Server by ./wso2server.sh -debug 5008 and you will see "Listening for transport dt_socket at address: 5008 " in your terminal
  • Go to intellij and and click on green colour insect icon or Run-> Debug <your configuration name>
2.jpg

  • Once the first breakpoint reached it will stop so you can see how the code is running after the breakpoint by clicking F8. You can see what are values in variables etc.

  • You can navigate through by breakpoints by clicking green arrow (similar to play icon)Apply a diff to ESB and build the code to do patching.
     2. Apply a diff to ESB and build the code to do patching

  • Copy the diff you are having and paste to wso2-synapse-Apache-Synapse-2.1.3-wso2v11 from intellij (which is the root directory) 
  • Then right click on the uploaded .diff file and click Apply patch
  • If we made any changes inside the synapse core then go to <CODE_BASE_490>/wso2-synapse-Apache-Synapse-2.1.3-wso2v11 and run mvn clean install -Dmaven.test.skip=true
  • To reflect the changes you have done to code, you need to put the generated jar to the repository/components/patches folder inside the remote ESB. For that the newly build jar can be found at <CODE_BASE_490>/wso2-synapse-Apache-Synapse-2.1.3-wso2v11\modules\core\target
  • Copy the synapse-core-2.1.3-wso2v11.jar file to the patches directory in the ESB and rename it as the name inside the (mostly instead of - need to put _) repository/components/plugin folder(synapse-core_2.1.3.wso2v11.jar).
  • Restart the ESB to apply the patch.
Blogger Widgets