Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  *   - Redistributions of source code must retain the above copyright
  9.  *     notice, this list of conditions and the following disclaimer.
  10.  *
  11.  *   - Redistributions in binary form must reproduce the above copyright
  12.  *     notice, this list of conditions and the following disclaimer in the
  13.  *     documentation and/or other materials provided with the distribution.
  14.  *
  15.  *   - Neither the name of Sun Microsystems nor the names of its
  16.  *     contributors may be used to endorse or promote products derived
  17.  *     from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. package demo;
  33.  
  34. import java.text.*;
  35. import java.util.*;
  36. import javax.mail.*;
  37. import javax.mail.internet.*;
  38.  
  39. /**
  40.  * Used to store message information.
  41.  */
  42. public class MessageInfo {
  43.     private Message message;
  44.    
  45.    
  46.     /**
  47.      * Returns the bcc field.
  48.      */
  49.     public String getBcc() throws MessagingException {
  50.         return formatAddresses(
  51.             message.getRecipients(Message.RecipientType.BCC));
  52.     }
  53.    
  54.     /**
  55.      * Returns the body of the message (if it's plain text).
  56.      */
  57.     public String getBody() throws MessagingException, java.io.IOException {
  58.         Object content = message.getContent();
  59.         if (message.isMimeType("text/plain")) {
  60.             return (String)content;
  61.         } else if (message.isMimeType("multipart/alternative")) {
  62.             Multipart mp = (Multipart)message.getContent();
  63.             int numParts = mp.getCount();
  64.             for (int i = 0; i < numParts; ++i) {
  65.                 if (mp.getBodyPart(i).isMimeType("text/plain"))
  66.                     return (String)mp.getBodyPart(i).getContent();
  67.             }
  68.             return "";  
  69.         } else if (message.isMimeType("multipart/*")) {
  70.             Multipart mp = (Multipart)content;
  71.             if (mp.getBodyPart(0).isMimeType("text/plain"))
  72.                 return (String)mp.getBodyPart(0).getContent();
  73.             else
  74.                 return "";
  75.         } else
  76.             return "";
  77.     }
  78.    
  79.     /**
  80.      * Returns the cc field.
  81.      */
  82.     public String getCc() throws MessagingException {
  83.         return formatAddresses(
  84.             message.getRecipients(Message.RecipientType.CC));
  85.     }
  86.    
  87.     /**
  88.      * Returns the date the message was sent (or received if the sent date
  89.      * is null.
  90.      */
  91.     public String getDate() throws MessagingException {
  92.         Date date;
  93.         SimpleDateFormat df = new SimpleDateFormat("EE M/d/yy");
  94.         if ((date = message.getSentDate()) != null)
  95.             return (df.format(date));
  96.         else if ((date = message.getReceivedDate()) != null)
  97.             return (df.format(date));
  98.         else
  99.             return "";
  100.      }
  101.        
  102.     /**
  103.      * Returns the from field.
  104.      */
  105.     public String getFrom() throws MessagingException {
  106.         return formatAddresses(message.getFrom());
  107.     }
  108.  
  109.     /**
  110.      * Returns the address to reply to.
  111.      */
  112.     public String getReplyTo() throws MessagingException {
  113.         Address[] a = message.getReplyTo();
  114.         if (a.length > 0)
  115.             return ((InternetAddress)a[0]).getAddress();
  116.         else
  117.             return "";
  118.     }
  119.    
  120.     /**
  121.      * Returns the javax.mail.Message object.
  122.      */
  123.     public Message getMessage() {
  124.         return message;
  125.     }
  126.    
  127.     /**
  128.      * Returns the message number.
  129.      */
  130.     public String getNum() {
  131.         return (Integer.toString(message.getMessageNumber()));
  132.     }
  133.    
  134.     /**
  135.      * Returns the received date field.
  136.      */
  137.     public String getReceivedDate() throws MessagingException {
  138.         if (hasReceivedDate())
  139.             return (message.getReceivedDate().toString());
  140.         else
  141.             return "";
  142.     }
  143.    
  144.     /**
  145.      * Returns the sent date field.
  146.      */
  147.     public String getSentDate() throws MessagingException {
  148.         if (hasSentDate())
  149.             return (message.getSentDate().toString());
  150.         else
  151.             return "";
  152.     }
  153.    
  154.     /**
  155.      * Returns the subject field.
  156.      */
  157.     public String getSubject() throws MessagingException {
  158.         if (hasSubject())
  159.             return message.getSubject();
  160.         else
  161.             return "";
  162.     }
  163.    
  164.     /**
  165.      * Returns the to field.
  166.      */
  167.     public String getTo() throws MessagingException {
  168.         return formatAddresses(
  169.             message.getRecipients(Message.RecipientType.TO));
  170.     }
  171.    
  172.     /**
  173.      * Method for checking if the message has attachments.
  174.      */
  175.     public boolean hasAttachments() throws java.io.IOException,
  176.                                            MessagingException {
  177.         boolean hasAttachments = false;
  178.         if (message.isMimeType("multipart/*")) {
  179.             Multipart mp = (Multipart)message.getContent();
  180.             if (mp.getCount() > 1)
  181.                 hasAttachments = true;
  182.         }
  183.            
  184.         return hasAttachments;
  185.     }
  186.    
  187.     /**
  188.      * Method for checking if the message has a bcc field.
  189.      */
  190.     public boolean hasBcc() throws MessagingException {
  191.         return (message.getRecipients(Message.RecipientType.BCC) != null);
  192.     }
  193.    
  194.     /**
  195.      * Method for checking if the message has a cc field.
  196.      */
  197.     public boolean hasCc() throws MessagingException {
  198.         return (message.getRecipients(Message.RecipientType.CC) != null);
  199.     }
  200.    
  201.     /**
  202.      * Method for checking if the message has a date field.
  203.      */
  204.     public boolean hasDate() throws MessagingException {
  205.         return (hasSentDate() || hasReceivedDate());
  206.     }
  207.    
  208.     /**
  209.      * Method for checking if the message has a from field.
  210.      */
  211.     public boolean hasFrom() throws MessagingException {
  212.         return (message.getFrom() != null);
  213.     }
  214.    
  215.     /**
  216.      * Method for checking if the message has the desired mime type.
  217.      */
  218.     public boolean hasMimeType(String mimeType) throws MessagingException {
  219.         return message.isMimeType(mimeType);
  220.     }
  221.    
  222.     /**
  223.      * Method for checking if the message has a received date field.
  224.      */
  225.     public boolean hasReceivedDate() throws MessagingException {
  226.         return (message.getReceivedDate() != null);
  227.     }
  228.    
  229.     /**
  230.      * Method for checking if the message has a sent date field.
  231.      */
  232.     public boolean hasSentDate() throws MessagingException {
  233.         return (message.getSentDate() != null);
  234.     }
  235.    
  236.     /**
  237.      * Method for checking if the message has a subject field.
  238.      */
  239.     public boolean hasSubject() throws MessagingException {
  240.         return (message.getSubject() != null);
  241.     }
  242.    
  243.     /**
  244.      * Method for checking if the message has a to field.
  245.      */
  246.     public boolean hasTo() throws MessagingException {
  247.         return (message.getRecipients(Message.RecipientType.TO) != null);
  248.     }
  249.    
  250.     /**
  251.      * Method for mapping a message to this MessageInfo class.
  252.      */
  253.     public void setMessage(Message message) {
  254.         this.message = message;
  255.     }
  256.  
  257.     /**
  258.      * Utility method for formatting msg header addresses.
  259.      */
  260.     private String formatAddresses(Address[] addrs) {
  261.         if (addrs == null)
  262.             return "";
  263.         StringBuffer strBuf = new StringBuffer(getDisplayAddress(addrs[0]));
  264.         for (int i = 1; i < addrs.length; i++) {
  265.             strBuf.append(", ").append(getDisplayAddress(addrs[i]));
  266.         }
  267.         return strBuf.toString();
  268.     }
  269.    
  270.     /**
  271.      * Utility method which returns a string suitable for msg header display.
  272.      */
  273.     private String getDisplayAddress(Address a) {
  274.         String pers = null;
  275.         String addr = null;
  276.         if (a instanceof InternetAddress &&
  277.            ((pers = ((InternetAddress)a).getPersonal()) != null)) {
  278.             addr = pers + "  "+"&lt;"+((InternetAddress)a).getAddress()+"&gt;";
  279.         } else
  280.             addr = a.toString();
  281.         return addr;
  282.     }
  283. }
  284.  
  285.