FileDocCategorySizeDatePackage
EmailNotifierServlet.javaAPI DocGlassfish v2 API8242Wed Feb 01 13:07:32 GMT 2006org.glassfish.grizzly.async.servlet

EmailNotifierServlet

public class EmailNotifierServlet extends HttpServlet implements org.glassfish.grizzly.async.javamail.JavaMailAsyncFilterHandler
Simple servlet that use Grizzly Asynchronous Request Processing to fecth mail from an email account (Gmail, Yahoo, etc.). The Servlet will be interupted until new mails are available in the Gmail account.
author
Jeanfrancois Arcand

Fields Summary
private String
username
The username for the account
private String
password
The password for the account
private String
mailServer
The mail server
private String
mailServerPort
The port the mail server is listeningt
private Message[]
messages
The current Gmail message list.
Constructors Summary
public EmailNotifierServlet()

    
Methods Summary
private voiddisplayMessageHeaders(javax.mail.Message msg, java.io.PrintWriter out)


        try {
            out.println("<b>Date:</b> " + msg.getSentDate() + "<br>");

            Address[] fr = msg.getFrom();
            if (fr != null) {
                boolean tf = true;
                out.print("<b>From:</b> ");
                for (int i = 0; i < fr.length; i++) {
                    out.print(((tf) ? " " : ", ") + getDisplayAddress(fr[i]));
                    tf = false;
                }
                out.println("<br>");
            }

            Address[] to = msg.getRecipients(Message.RecipientType.TO);
            if (to != null) {
                boolean tf = true;
                out.print("<b>To:</b> ");
                for (int i = 0; i < to.length; i++) {
                    out.print(((tf) ? " " : ", ") + getDisplayAddress(to[i]));
                    tf = false;
                }
                out.println("<br>");
            }

            Address[] cc = msg.getRecipients(Message.RecipientType.CC);
            if (cc != null) {
                boolean cf = true;
                out.print("<b>CC:</b> ");
                for (int i = 0; i < cc.length; i++) {
                    out.print(((cf) ? " " : ", ") + getDisplayAddress(cc[i]));
            cf = false;
            }
                    out.println("<br>");
                }

            out.print("<b>Subject:</b> " +
                  ((msg.getSubject() !=null) ? msg.getSubject() : "") +
                  "<br>");

        } catch (MessagingException mex) {
            out.println(msg.toString());
        }   
    
public voiddoGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
The doGet will execute only when the Grizzly JavaMailAsyncFilter determines this servlet can execute. The JavaMailAsyncFilter will allow the execution of this Servlet only if a new email message arrive in the remote email account.

        
        PrintWriter out = response.getWriter();
        try{
            
            if ( messages != null) {           
                out.println("<html>");
                    out.println("<HEAD><TITLE>Async JavaMail Servlet</TITLE></HEAD>");
                out.println("<BODY bgcolor=\"#ccccff\">");
                out.print("<center><font face=\"Arial,Helvetica\" ");
                out.println("font size=\"+3\"><b>");
                out.println("You have " + messages.length + " messages in folder "
                        + "inbox</b></font></center><p>");

                for (int i=0; i < messages.length; i++){
                    try {
                        Message msg = messages[i];
                        displayMessageHeaders(msg, out);

                        try{
                            Object o = msg.getContent();

                            out.println("<pre>");
                            out.println((String)o);
                            out.println("</pre>");
                        } catch (NullPointerException ex){
                            ; //If the message isn't having body
                        }
                     } catch (MessagingException mex) {
                        out.println(mex.toString());
                    }
                }
            }
  
            out.println("</BODY></html>");
            out.close();
        } catch (Throwable t){
           t.printStackTrace(); 
        }
    
private java.lang.StringgetDisplayAddress(javax.mail.Address a)

        String pers = null;
        String addr = null;
        if (a instanceof InternetAddress &&
            ((pers = ((InternetAddress)a).getPersonal()) != null)) {

            addr = pers + "  "+"<"+((InternetAddress)a).getAddress()+">";
        } else
            addr = a.toString();

        return addr;
    
public java.lang.StringgetMailServer()

        return mailServer;
    
public java.lang.StringgetMailServerPort()

        return mailServerPort;
    
public java.lang.StringgetPassword()

        return password;
    
public java.lang.StringgetUserName()

       return username;
    
public booleanhandleEvent(org.glassfish.grizzly.async.javamail.JavaMailAsyncFilterEvent event)
The JavaMailAsyncFilter will invoke that method after fetching the email from the remote account. This method will return true if the messages retrived aren't new or no new message has been found. Returning true means the Servlet shouldn't yet be executed. Returning false will fire the execution of this Servlet.

paramc
event The JavaMailAsyncFilterEvent notification from JavaMailAsyncFilter

        messages = event.getMessages();

        if ( messages == null || messages.length == 0){
            return true;
        } else {
            return false;
        }      
    
public voidinit(javax.servlet.ServletConfig config)
Init this Servlet by registering it with the JavaMailAsyncFilter.

 
        username = config.getInitParameter("username");
        password = config.getInitParameter("password");
        mailServer = config.getInitParameter("mailServer");  
        mailServerPort = config.getInitParameter("mailServerPort");  
        
        JavaMailAsyncFilter.register(
                config.getInitParameter("contextPath"), this);