import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
import com.sentilla.host.client.HostClient;
import com.sentilla.net.SenderDriver;
import com.sentilla.net.Sender;
/**
* Downloads an xml file from gmail,
* parses the number of undread messages,
* and sends it to a JCreate running GmailMoteApp
*
* @author Dan Steingart
*/
public class GmailClientApp {
public static void main(String[] args)
{
//Fill in gmail info
String name = "foo@gmail.com"; //expects someone@gmail.com
String password = "abc123"; //expects your password, don't worry, we're using HTTPS
//Create Connection To host
HostClient host = new HostClient();
try
{
host.connect();
}
catch (Exception e){}
//Create Sender
Sender sender = SenderDriver.create("local");
//Create GmailMoteApp object to send out
GmailMoteApp.GmailCount out = new GmailMoteApp.GmailCount();
while (true)
{
try
{
//Create SSL authentication, send to gmail, get XML
Authenticator.setDefault(new MyAuthenticator(name,password));
URL gmail = new URL("https://mail.google.com/gmail/feed/atom");
BufferedReader in = new BufferedReader(
new InputStreamReader(
gmail.openStream()));
//Check for message count
String line = "";
String total = "";
while ((line = in.readLine()) != null )
{
if (line.contains("fullcount"))
{
total = line.substring("".length(), line.length()- "".length());
}
}
//Send message count to JCreate
out.count = Integer.parseInt(total);
sender.send(out);
//Sleep for ten seconds
Thread.sleep(10000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
//Code from http://www.java-forums.org/java-net/7242-reading-urls-protected-http-authentication.html
static class MyAuthenticator extends Authenticator {
private String username, password;
public MyAuthenticator(String user, String pass) {
username = user;
password = pass;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
}
}