Public Member Functions | Public Attributes | Private Member Functions | Private Attributes

MessageStore Class Reference

Collaboration diagram for MessageStore:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 MessageStore (GetMailApp parent, String popServer, String username, String password, boolean showContents)
 Creates instance of the class and starts worker thread immediately to receive messages.
void run ()
 Message store receiving worker thread.

Public Attributes

StringBuffer outs = new StringBuffer ()
 Output buffer where messages are dumped to.

Private Member Functions

void open ()
 Opens messages store (authenticates to server) and opens the INBOX folder in the store.
void close ()
 Closes connection to message store.
void dumpAll (boolean showContents)
 Dumps all messages to the output buffer.
MessageStore println ()
 Appends new-line to output buffer.
MessageStore print (String str)
 Appends message to output buffer.
MessageStore println (String str)
 Appends message terminated with new-line to output buffer.
MessageStore printEscHtml (String str)
 Cleans message text from HTML entities (i.e.
void printHeader (Message message)
 Dumps HTML formatted message header to the output buffer.
void printContents (Message message)
 Dumps HTML escaped message content to the output buffer.

Private Attributes

GetMailApp parent
 Rendering context for the message store.
String popServer
 POP3 server name or IP address.
String username
 User ID on the POP3 server.
String password
 User's password on the POP3 server.
boolean showContents
 Indicates whether to include contents of the messages in the dump.
Store store = null
 Javamail message store.
Folder folder = null
 Javamail message folder.

Detailed Description

Author:
Mikica B Kocic

Definition at line 24 of file MessageStore.java.


Constructor & Destructor Documentation

MessageStore.MessageStore ( GetMailApp  parent,
String  popServer,
String  username,
String  password,
boolean  showContents 
)

Creates instance of the class and starts worker thread immediately to receive messages.

Parameters:
parentrendering context where to write result
popServerhost name or IP address of the POP3 server
usernameuser ID on the server
passworduser's password on the server
showContentsshow contents of the messages (or only headers)

Definition at line 76 of file MessageStore.java.

References parent, password, popServer, showContents, and username.

    {
        this.parent    = parent;
        this.popServer = popServer;
        this.username  = username;
        this.password  = password;
        this.showContents = showContents;
        
        new Thread( this ).start ();
    }

Member Function Documentation

void MessageStore.close (  ) [private]

Closes connection to message store.

Definition at line 126 of file MessageStore.java.

References folder, println(), and store.

Referenced by run().

    {
        /* Close the connection 
         * but don't remove the messages from the server
         */
        
        if ( folder != null ) {
            try {
                folder.close( false );
            } catch( MessagingException e ) {
                /* ignored */
            }
        }
        
        if ( store != null ) {
            try {
                store.close ();
            } catch( MessagingException e ) {
                /* ignored */
            }
        }
        
        println( "</body></html>" );
    }
void MessageStore.dumpAll ( boolean  showContents ) [private]

Dumps all messages to the output buffer.

Parameters:
showContentsshow contents of the messages or not (i.e. only headers)

Definition at line 156 of file MessageStore.java.

References folder, GetMailApp.onGetMessagesCompleted(), parent, printContents(), printHeader(), and println().

Referenced by run().

    {
        if ( folder == null ) {
            return;
        }
        
        try 
        {
            Message[] msgs = folder.getMessages ();

            if ( msgs != null && msgs.length > 0 )
            {
                int current = 0; // count messages
                for( Message m : folder.getMessages () )
                {
                    ++current;

                    /* Show progress..
                     */
                    StringBuffer str = new StringBuffer ();
                    str.append( "<html><header></header><body><h3>Receiving message " ) 
                       .append( current )
                       .append( " of " )
                       .append( msgs.length )
                       .append( "...</h3>" )
                       .append( current >= msgs.length ?
                               "<p>Receiving completed. Rendering information...</p>" : "" )
                       .append( "</body></html>");
                    
                    parent.onGetMessagesCompleted( str );
                    
                    /* Dump header + contents optionally
                     */
                    printHeader( m );
                    if ( showContents ) {
                        printContents( m );
                    }
                }
            }
            else
            {
                println( "<html><header></header><body><h3>Receiving completed</h3>" ); 
                println( "<div style='color:#800000;font-weight:bold;'>No messages.</div>" );
            }
        }    
        catch( MessagingException e )
        {
            println( "<h3>Error: Messaging Exception while retrieving...</h3>" );
            println( "<pre style='color:red'>" + e.toString () + "</pre>" );
        }
        
    }
void MessageStore.open (  ) [private]

Opens messages store (authenticates to server) and opens the INBOX folder in the store.

Definition at line 94 of file MessageStore.java.

References folder, password, popServer, println(), store, and username.

Referenced by run().

    {
        println( "<html><head><title>Email from " + popServer + "</title></head><body>" );
        
        try
        {
            Properties props = System.getProperties ();
            Session session = Session.getDefaultInstance( props, null );
    
            store = session.getStore( "pop3" );
            store.connect( popServer, username, password );
    
            folder = store.getDefaultFolder ();
            folder = folder.getFolder( "INBOX" );
    
            folder.open( Folder.READ_ONLY );
        }
        catch( NoSuchProviderException e )
        {
            println( "<h3>Error: No Such Provider Exception while connecting...</h3>" );
            println( "<pre style='color:red'>" + e.toString () + "</pre>" );
        }
        catch( MessagingException e )
        {
            println( "<h3>Error: Messaging Exception while connecting...</h3>" );
            println( "<pre style='color:red'>" + e.toString () + "</pre>" );
        }
    }
MessageStore MessageStore.print ( String  str ) [private]

Appends message to output buffer.

Definition at line 221 of file MessageStore.java.

References outs.

Referenced by printContents(), and printHeader().

    {
        outs.append( str != null ? str : "" );
        return this;
    }
void MessageStore.printContents ( Message  message ) [private]

Dumps HTML escaped message content to the output buffer.

Definition at line 413 of file MessageStore.java.

References print(), printEscHtml(), and println().

Referenced by dumpAll().

    {
        try
        {
            Part messagePart = message;

            println( "<hr />" );
            
            print( "<table border='0' bgcolor='#FFFFF0' width='100%'>"
                    + "<tr><td style='font-size:18;font-weight:bold'>"
                    + "Message Content</td></tr><tr><td><pre>" );
           
            InputStream inputStream = messagePart.getInputStream ();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader( inputStream ) );

            for( String str = reader.readLine(); str != null; str = reader.readLine() )
            {
                printEscHtml( str ).println ();
            }

            println( "</pre></td></tr></table>" );
        }
        catch( MessagingException e )
        {
            println( "</pre></td></tr></table>" );
            println( "<h3>Error: Messaging Exception while displaying contents...</h3>" );
            println( "<pre style='color:red'>" + e.toString () + "</pre>" );
        }
        catch( IOException e )
        {
            println( "</pre>" );
            println( "<h3>Error: I/O Exception while displaying contents...</h3>" );
            println( "<pre style='color:red'>" + e.toString () + "</pre>" );
        }
    }
MessageStore MessageStore.printEscHtml ( String  str ) [private]

Cleans message text from HTML entities (i.e.

escapes HTML entities) and appends message to the output buffer

Definition at line 240 of file MessageStore.java.

References outs.

Referenced by printContents(), and printHeader().

    {
        if ( str == null ) {
            return this;
        }

        outs.append( str.replaceAll( "&",  "&amp;"  )
                        .replaceAll( "<",  "&lt;"   )
                        .replaceAll( ">",  "&gt;"   )
                        .replaceAll( "\"", "&quot;" ) );
        return this;
    }
void MessageStore.printHeader ( Message  message ) [private]

Dumps HTML formatted message header to the output buffer.

Definition at line 256 of file MessageStore.java.

References print(), printEscHtml(), and println().

Referenced by dumpAll().

    {
        try
        {
            println( "<hr />" );
            
            /* Begins table
             */
            println( "<table border='0' bgcolor='#F0F0FF' width='100%'>" );

            /* Shows header FROM:
             */
            InternetAddress[] aList = null;
            try {
                aList = (InternetAddress[])( message.getFrom() );
            } catch( AddressException e ) {
                /* ignored */
            }

            if ( aList != null )
            {
                int count = 0;
                print( "<tr><td style='font-weight:bold' width='120'>&nbsp;From:</td>" );
                print( "<td style='font-weight:bold'>" );
                
                for ( InternetAddress a : aList )
                {
                    if ( ++count > 1 ) {
                        print( "; " );
                    }
                    String from = a.getPersonal ();
                    if ( from == null ) {
                        printEscHtml( a.getAddress () );
                    } else {
                        printEscHtml( from + " <" + a.getAddress () + ">" );
                    }
                }

                println( "</td></tr>" );
            }
            
            /* Shows header SUBJECT:
             */
            if ( message.getSubject () != null ) 
            {
                print( "<tr><td style='font-weight:bold'>&nbsp;Subject:</td>" );
                print( "<td style='font-weight:bold'>" );
                printEscHtml( message.getSubject() );
                println( "</td></tr>" );
            }

            /* Shows header TO:
             */
            aList = null;
            try {
                aList = (InternetAddress[])( message.getRecipients( RecipientType.TO ) );
            } catch( AddressException e ) {
                /* ignored */
            }

            if ( aList != null ) 
            {
                int count = 0;
                print( "<tr><td style='font-weight:bold'>&nbsp;To:</td>" );
                print( "<td>" );
                
                for ( InternetAddress a : aList )
                {
                    if ( ++count > 1 ) {
                        print( "; " );
                    }
                    String to = a.getPersonal ();
                    if ( to == null ) {
                        printEscHtml( a.getAddress () );
                    } else {
                        printEscHtml( to + " <" + a.getAddress () + ">" );
                    }
                }

                println( "</td></tr>" );
            }
            
            /* Shows header RECEIVED:
             */
            java.util.Date sent = message.getSentDate ();

            if ( sent != null ) {
                print( "<tr><td style='font-weight:bold'>&nbsp;Sent:</td><td>" );
                printEscHtml( sent.toString () );
                println( "</td></tr>" );
            }

            /* Shows header CONTENT TYPE:
             */
            String contentType = message.getContentType ();

            if ( contentType != null ) {
                print( "<tr><td style='font-weight:bold'>&nbsp;Content Type:</td>" );
                print( "<td><code>" );
                printEscHtml( contentType );
                println( "</code></td></tr>" );
            }

            /* Display disposition (attachments etc.)
             */
            if ( message.getContent () != null 
                    && ( message.getContent() instanceof MimeMultipart ) )
               {
                   MimeMultipart multipart = (MimeMultipart)message.getContent ();

                   for( int i = 0; i < multipart.getCount(); i++ )
                   { 
                       BodyPart bodyPart = multipart.getBodyPart( i );
                       String disposition = bodyPart.getDisposition ();

                       if ( disposition == null ) {
                           /* no disposition */
                       }
                       else if ( disposition.equalsIgnoreCase( Part.INLINE ) ) {
                           print( "<tr><td style='font-weight:bold'>&nbsp;Attachment:</td>" );
                           print( "<td>Inline: <code>" );
                           printEscHtml( bodyPart.getContentType () );
                           println( "</code></td></tr>" );
                       }
                       else if ( disposition.equalsIgnoreCase( Part.ATTACHMENT ) ) {
                           print( "<tr><td style='font-weight:bold'>&nbsp;Attachment:</td>" );
                           print( "<td>File: <code>" );
                           printEscHtml( bodyPart.getFileName () );
                           println( "</code></td></tr>" );
                       }
                   }
               }

            /* Finishes the table
             */
            println( "</table>" );
        }
        catch( MessagingException e )
        {
            println( "</table>" );
            println( "<hr />" );
            println( "<h3>Error: Messaging Exception while displaying header...</h3>" );
            println( "<pre style='color:red'>" + e.toString () + "</pre>" );
        }
        catch( IOException e ) 
        {
            println( "</table>" );
            println( "<hr />" );
            println( "<h3>Error: I/O Exception while displaying header...</h3>" );
            println( "<pre style='color:red'>" + e.toString () + "</pre>" );
        }

    }
MessageStore MessageStore.println (  ) [private]

Appends new-line to output buffer.

Definition at line 212 of file MessageStore.java.

References outs.

Referenced by close(), dumpAll(), open(), printContents(), and printHeader().

    {
        outs.append( "\n" );
        return this;
    }
MessageStore MessageStore.println ( String  str ) [private]

Appends message terminated with new-line to output buffer.

Definition at line 230 of file MessageStore.java.

References outs.

    {
        outs.append( str != null ? str : "" ).append( "\n" );
        return this;
    }
void MessageStore.run (  )

Message store receiving worker thread.

Opens the store, dumps all messages and closes the store. At the end, renders output buffer in parent's context.

Definition at line 456 of file MessageStore.java.

References close(), dumpAll(), GetMailApp.onGetMessagesCompleted(), open(), outs, parent, and showContents.

    {
        this.open ();
        this.dumpAll( showContents );
        this.close ();
        parent.onGetMessagesCompleted( this.outs );
    }

Member Data Documentation

Folder MessageStore.folder = null [private]

Javamail message folder.

Definition at line 64 of file MessageStore.java.

Referenced by close(), dumpAll(), and open().

StringBuffer MessageStore.outs = new StringBuffer ()

Output buffer where messages are dumped to.

Definition at line 54 of file MessageStore.java.

Referenced by print(), printEscHtml(), println(), and run().

Rendering context for the message store.

Definition at line 29 of file MessageStore.java.

Referenced by dumpAll(), MessageStore(), and run().

String MessageStore.password [private]

User's password on the POP3 server.

Definition at line 44 of file MessageStore.java.

Referenced by MessageStore(), and open().

String MessageStore.popServer [private]

POP3 server name or IP address.

Definition at line 34 of file MessageStore.java.

Referenced by MessageStore(), and open().

boolean MessageStore.showContents [private]

Indicates whether to include contents of the messages in the dump.

Definition at line 49 of file MessageStore.java.

Referenced by MessageStore(), and run().

Store MessageStore.store = null [private]

Javamail message store.

Definition at line 59 of file MessageStore.java.

Referenced by close(), and open().

String MessageStore.username [private]

User ID on the POP3 server.

Definition at line 39 of file MessageStore.java.

Referenced by MessageStore(), and open().


The documentation for this class was generated from the following file: