Monday, October 24, 2011

Mule smtp outbound using Gmail


Following mule flow reads files from a directory and sends the content of the files to an email using gmail account.


<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:smtps="http://www.mulesoft.org/schema/mule/smtps" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:email="http://www.mulesoft.org/schema/mule/email" xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/3.1/mule-file.xsd
http://www.mulesoft.org/schema/mule/smtps http://www.mulesoft.org/schema/mule/smtps/3.1/mule-smtps.xsd
http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/3.1/mule-email.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.1/mule.xsd ">
<spring:beans />

<smtps:connector name="smtpsGmailConnector"
fromAddress="syourid@gmail.com" ccAddresses="toid@domain.com">

<smtps:header key="mail.transport.protocol" value="smtps" />
<smtps:header key="mail.smtps.auth" value="true" />
<smtps:header key="mail.smtps.quitwait" value="false" />
<smtps:tls-client />
<smtps:tls-trust-store path="greenmail-truststore" />
</smtps:connector>

<flow name="9f1f86a7-021f-4d20-b062-40fcd1c6c9fa">
<file:inbound-endpoint path="E:/someDir"
pollingFrequency="1000" fileAge="500" reverseOrder="false" doc:name="File"
doc:description="Read/write a file from the filesystem" />
<echo-component></echo-component>

<smtps:outbound-endpoint connector-ref="smtpsGmailConnector"
user="yourid" password="yourpassword" host="smtp.gmail.com"
subject="Your order has been placed!">

<email:string-to-email-transformer />
</smtps:outbound-endpoint>
</flow>
</mule>



Run with mule

Put some text file int the directory "E:/someDir"

Console will look something like the following image


Thursday, October 20, 2011

Simple Reverse Geo-coding in Java using Google Map

The Java Class

package geo;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class MyGeo {
public static void main(String ar[]) throws Exception {
System.out.println(new MyGeo().getAddress("13.031067,80.239656"));
}
public String getAddress(String latlong){
String address = null;
String gURL = "http://maps.google.com/maps/api/geocode/xml?latlng=" + latlong + "&sensor=true";
try {
DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
DocumentBuilder db = df.newDocumentBuilder();
Document dom = db.parse(gURL);
Element docEl = dom.getDocumentElement();
NodeList nl = docEl.getElementsByTagName("result");
if (nl != null && nl.getLength() > 0){
address=((Element)nl.item(0)).getElementsByTagName("formatted_address").item(0).getTextContent();
for(int i=0;i<nl.getLength();i++){
String temp=((Element)nl.item(i)).getElementsByTagName("formatted_address").item(0).getTextContent();
}
}
} catch (Exception ex) {
address = "Err";
}
return address;
}
public String getAddress(String lat, String lon) {
return getAddress(lat+ "," + lon);
}
public String getAddress(double lat, double lon) {
return getAddress("" + lat, "" + lon);
}
}

Run
>java geo.MyGeo
Venkatanarayana Rd, CIT Nagar, Chennai, Tamil Nadu, India


Note: Google has some restriction in this web-service call like number of requests per day from one IP.
Ref: http://code.google.com/apis/maps/documentation/geocoding/#ReverseGeocoding