UltraESB auto Email reply sample

Today I am going to write about Free and Open Source UltraESB' s special usage. Before talking about that I'll explain the problem I faced behind this sample solution.

The problem of the most of, organizations which sell their products use emailing system(even today) to inform updated prices of items for customers of them. But they haven't automatic mail response system. So they send reply manually.


What is the ToolBox..?

The AdroitLogic UltraESB is designed to help, test and troubleshoot services and ESB test scenarios with ease. The ToolBox comprises of the following modules, and can be started with the "toolbox.sh" [Linux] or "toolbox.bat" [Windows] scripts like below on ubuntu.

run toolbox


Start toolbox on ubuntu........

After starting toolbox you can see toolbox dashboard as below, then you should  go on following steps and you should run the jetty server on toolbox.


File -> New -> jetty server  & click Start jetty button.

toolbox on ubuntu


Then fire up a HTTP/S client as below.

File -> New -> HTTP/S Client 

Then configure the HTTP/S client as below,

SimpleStockQuoteService on toolbox

New problem.......

But new problem is Email systems can't connect directly with SimpleStockQuoteService. The image bellow provides a clear idea of this scenario.



According to this situation we can use UltraESB as a intermediate device for connecting Email and SimpleStockQuoteService. So I created a new sample code as a solution for this problem.

  
How to Change Email settings to connect with UltraESB............

Before other configuration, organizations should need to change settings of email account which offers POP/IMAP, to connect with UltraESB.Then you need to go on following steps and you should set email settings as below.
 step 1: goto option -> Mail setting -> Forwarding and POP/IMAP
 step 2: Select the,
             Enable POP for all mail (even mail that's already been downloaded)
             of states.
 step 3: finally click Save Changes   

How to configure and set sample to UltraESB.......

In this configuration you need to add small code fragments for ultra-dynamic.xml and ultra-root.xml files which is in the ultraesb-1.4.0/conf folder.       

I created two xml beans for mail listening and mail sending in my new sample code. Bellow are those beans.             

       

    <bean id="mail-listener" class="org.adroitlogic.ultraesb.transport.mail.MailTransportListener">
        <property name="properties">
            <map>
                <entry key="mail.pop3.socketFactory.fallback" value="false"/>
                <entry key="mail.pop3.host" value="pop.gmail.com"/>
                <entry key="mail.pop3.socketFactory.class" value="javax.net.ssl.SSLSocketFactory"/>
                <entry key="mail.pop3.socketFactory.port" value="995"/>
                <entry key="mail.pop3.port" value="995"/>
                <entry key="removeHeaders" value="RemoveHeader, AnotherHeaderToRemove"/>
                <!--<entry key="preserveHeaders" value="To, Subject, From"/>-->
            </map>
        </property>
    </bean>
    <bean id="mail-sender" class="org.adroitlogic.ultraesb.transport.mail.MailTransportSender">
        <property name="properties">
            <map>
                <entry key="mail.smtp.host" value="smtp.gmail.com"/>
                <entry key="mail.smtp.port" value="587"/>
                <entry key="mail.smtp.user" value="pwickramage24"/>
                <entry key="mail.smtp.password" value="passwd"/>
                <entry key="mail.smtp.from" value="pwickramage24@gmail.com"/>
                <entry key="mail.smtp.starttls.enable" value="true"/>
                <entry key="mail.smtp.auth" value="true"/>
                <!--<entry key="mail.debug" value="true"/>-->
                <!--uncomment to debug any issues entry key="mail.debug" value="true"/-->
            </map>
        </property>
    </bean>


These two beans should be changed according to organisation's mail address. After changing these beans you should copy them to ultra-root.xml file which is in the ultraesb-1.4.0/conf folder.
Afterwards you need to copy proxy fragments to ultra-dynamic.xml file which is in the ultraesb-1.4.0/conf folder. Then you should change the transport id as mail-listener of proxy service according to bean.


    <u:proxy id="mail-proxy">
        <u:transport id="mail-listener">
            <u:property name="url" value="pwickramage24@gmail.com"/>
            <u:property name="mail.pop3.user" value="pwickramage24"/>
            <u:property name="mail.pop3.password" value="passwd"/>
            <u:property name="startDelay" value="20000"/>
            <u:property name="repeatInterval" value="30000"/>
            <u:property name="preserveHeaders" value="From,Subject"/>
            <u:property name="mainPartIdentificationPriority" value="application/soap+xml,text/xml,text/plain,text/html"/>
            <!--<u:property name="removeHeaders" value="RemoveHeader"/>-->
        </u:transport>

        <u:target>
            <u:inSequence>
                <u:java><![CDATA[
                    logger.info("Sender Address : " + msg.getFirstTransportHeader("From"));
                    logger.info("Sender Subject : " + msg.getFirstTransportHeader("Subject"));
                    msg.addResponseCorrelation("correlatedFrom", msg.getFirstTransportHeader("From"));
                    msg.addResponseCorrelation("correlatedSubject", msg.getFirstTransportHeader("Subject"));             
                    msg.setContentType("text/xml");
                ]]></u:java>
            </u:inSequence>
            <u:inDestination>
                <u:address>http://localhost:9000/service/SimpleStockQuoteService</u:address>
            </u:inDestination>
            <u:outSequence>
                <u:java><![CDATA[
                    Message resp = msg.createDefaultResponseMessage();
                    resp.addTransportHeader("Subject", "Re:" + msg.getMessageProperty("correlatedSubject"));
                    resp.setDestinationURL("mailto:" + msg.getMessageProperty("correlatedFrom"));
                    mediation.setPayloadFromString(resp,"Soap response attached");
                    resp.addAttachment("response.xml", msg.getCurrentPayload());
                    logger.info("Sending Reply to :" + resp.getDestinationURL() + " body : " + msg.getCurrentPayload());
                    mediation.sendToEndpoint(resp, "smtp-gmail");
                ]]></u:java>
            </u:outSequence>
        </u:target>
    </u:proxy>


Properties of proxy should be changed according to organisation mail address like wise. Most suitable things is to, give some more milliseconds to start delay and repeat interval because that configure can decrease login frequency to mail-inbox from ESB. So you can increase the protection of the Email account.


Description about Sample code....

I set "From" & "Subject" as transport property, because request sender address and sender subject need to create proxy target. Next property is "main part Identification Priority", it is use to give first priority for sending file which we need sent to server, because only the first priority file is sent to server in the "inDestination".

After getting the details of "request sender" in the target inSequence, the  message gets the default name as "msg".

Then I used "addResponse Correlation" to assign  variables to "From" & "Subject" of transport header. Then those variables were used in "outSequence" to send response. The property, "set content type" is decides the  transeport file type between server and ESB.

In "inDestination", the request is sent to SimpleStockQuoteService by the server. Then the SimpleStockQuoteService gives a random answer to ESB through the server.

Server gives response a massage by the default name "msg". Note that we can create  a new massage, so I created a new massage named as "resp" then added values to it. New Header and URL can be set for the "resp" message by using "addTransportHeader" and "setDestinationURL". The response message from the server, is given as an attachment of 'resp'.  We can use this command to set the messege as an attachment,

    resp.addAttachment("response.xml", msg.getCurrentPayload());

Finally,
       mediation.sendToEndpoint(resp, "smtp-gmail");

        can be use to send the new message to the request sender.