org.cert.cc.jira.email
Class MessageUtils

java.lang.Object
  extended by org.cert.cc.jira.email.MessageUtils

public final class MessageUtils
extends Object

MessageUtils provides of set of static methods that perform various operations on a Message.

© 2009 Carnegie Mellon University

Author:
Steve Scholnick

Field Summary
static String CONTENT_TYPE_HEADER
          Content-Type header
static String DEFAULT_MIME_TYPE
          Default MIME type
static String MULTIPART_MIME_TYPE
          Multi-Part MIME type
static String PLAIN_TEXT_MIME_TYPE
          Plain text MIME type
static List<Message.RecipientType> RECIPIENT_TYPES
          Holds all of the Message.RecipientTypes
 
Method Summary
static Message copyHeaders(Message message)
          Creates a new Message with headers from message.
static Message copyHeaders(Message source, Message destination)
          Copies headers from source to destination.
static Message create()
          Constructs a new empty Message
static Message create(InputStream is)
          Constructs a new Message from an InputStream of RFC 822 data.
static Message createFromAttachment(Part p)
          Creates and returns a new Message from a Part.
static void debug(Logger log, Message message)
          Logs a mail Message at debug Level.
static List<InternetAddress> getAllRecipients(Message message, boolean includeBCC)
          Returns all recipients as InternetAddress objects.
static int getAttachmentCount(Message message, boolean emailsOnly)
          Returns the number of non-PKCS7 signature attachments for a Message
static String getAttachmentFileName(Part p)
          Returns the filename for a Part or null if the Part does not have an attachment.
static int getFileAttachmentCount(Message message)
          Returns the number of actual file attachments.
static Set<String> getFilenames(Message m)
          Returns all of the non-S/MIME signature filenames associated with the Message m.
static Set<String> getFilenames(Multipart m)
          Returns all of the non-S/MIME signature filenames associated with the Multipart m.
static Part getFirstEmailAttachment(Message message)
          Returns the first RFC-822 compatible attachment found.
static InternetAddress getFrom(Message message)
          Returns the From address.
static String getHeaderValue(Message message, String headerName)
          Returns an header value if and only if there is only one.
static byte[] getPartContent(Part p)
          Returns the raw content from a Part
static List<InternetAddress> getRecipients(Message message, Message.RecipientType type)
          Returns all recipients of a specific type as a List of InternetAddress objects.
static void info(Logger log, Message message)
          Logs a mail Message at info Level.
static boolean isPKCS7Signature(Part p)
          Returns if the Part is an S/MIME signature attachment
static void logAllHeaders(Logger log, Level level, Part message)
          Logs all of the headers for a Part at the specified logging Level using log
static void logMessage(Logger log, Level level, Message message)
          Logs a mail Message at the specified logging Level using log
static void logPart(Logger log, Level level, Part part)
          Logs a mail Part at the specified logging Level using log
static void setAllContentTypes(Multipart mp)
          Sets the Content-Type for all BodyParts contained
static void setContentType(Part p)
          Sets the Content-Type on a Part based on the Part's filename
static void setContentType(Part p, String contentType)
          Sets the Content-Type on a Part.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PLAIN_TEXT_MIME_TYPE

public static final String PLAIN_TEXT_MIME_TYPE
Plain text MIME type

See Also:
Constant Field Values

DEFAULT_MIME_TYPE

public static final String DEFAULT_MIME_TYPE
Default MIME type

See Also:
Constant Field Values

MULTIPART_MIME_TYPE

public static final String MULTIPART_MIME_TYPE
Multi-Part MIME type

See Also:
Constant Field Values

CONTENT_TYPE_HEADER

public static final String CONTENT_TYPE_HEADER
Content-Type header

See Also:
Constant Field Values

RECIPIENT_TYPES

public static final List<Message.RecipientType> RECIPIENT_TYPES
Holds all of the Message.RecipientTypes

Method Detail

create

public static Message create()
                      throws MessagingException
Constructs a new empty Message

Returns:
newly created Message
Throws:
MessagingException - If unable to create a Message from the InputStream

create

public static Message create(InputStream is)
                      throws MessagingException
Constructs a new Message from an InputStream of RFC 822 data.

Parameters:
is - InputStream of data
Returns:
newly created Message
Throws:
MessagingException - If unable to create a Message from the InputStream

copyHeaders

public static Message copyHeaders(Message message)
                           throws MessagingException
Creates a new Message with headers from message. The following headers are copied:

Parameters:
message - Input Message data
Returns:
Copy of the input Message data
Throws:
MessagingException - If unable to interroage the input Message data

copyHeaders

public static Message copyHeaders(Message source,
                                  Message destination)
                           throws MessagingException
Copies headers from source to destination. If destination is null, a new Message is created. The following headers are copied:

Parameters:
source - Source Message
destination - Destination Message
Returns:
Destination Message with Source's headers
Throws:
MessagingException - If unable to interroage the input Message data

getFrom

public static InternetAddress getFrom(Message message)
                               throws MessagingException
Returns the From address. If there is more than one From (is there ever?), null is returned. If there is only one, it is returned as an InternetAddress.

Parameters:
message - Email Message
Returns:
From address as a an InternetAddress.
Throws:
MessagingException - If unable to interrogate the Message

getAllRecipients

public static List<InternetAddress> getAllRecipients(Message message,
                                                     boolean includeBCC)
                                              throws MessagingException
Returns all recipients as InternetAddress objects. The resulting List is returned in a specific order: TO recipients, CC recipients, and then BCC recipients (if includeBCC is true). The order within each group is JavaMail dependent.

Parameters:
message - Email Message
includeBCC - Include BCC recipients in the returned List
Returns:
All recipients as InternetAddress objects
Throws:
MessagingException - If unable to interrogate the Message

getRecipients

public static List<InternetAddress> getRecipients(Message message,
                                                  Message.RecipientType type)
                                           throws MessagingException
Returns all recipients of a specific type as a List of InternetAddress objects.

Parameters:
message - Email Message
type - Type of Recipients to be returned
Returns:
Recipients as InternetAddress objects
Throws:
MessagingException - If unable to interrogate the Message

getHeaderValue

public static String getHeaderValue(Message message,
                                    String headerName)
                             throws MessagingException
Returns an header value if and only if there is only one. This is a convenient method for retrieving X-Header values. If the header is not present or there are more than one value, null is returned.

Parameters:
message - Email Message
headerName - Header name
Returns:
Header value as a trimmed String or null
Throws:
MessagingException - If unable to interrogate the Message

getAttachmentCount

public static int getAttachmentCount(Message message,
                                     boolean emailsOnly)
                              throws MessagingException
Returns the number of non-PKCS7 signature attachments for a Message

Parameters:
message - Email Message
emailsOnly - Count RFC-822 attachments only?
Returns:
Number of non-PKCS7 signature attachments
Throws:
MessagingException - If unable to interrogate the Message

getFileAttachmentCount

public static int getFileAttachmentCount(Message message)
                                  throws MessagingException
Returns the number of actual file attachments. File attachments all have a filename associated with them. Thunderbird's forward as attachment feature creates an attachment without a filename. S/MIME signature files are not included in the returned count.

Parameters:
message - Email Message
Returns:
Number of non-PKCS7 signature attachments
Throws:
MessagingException - If unable to interrogate the Message

isPKCS7Signature

public static boolean isPKCS7Signature(Part p)
                                throws MessagingException
Returns if the Part is an S/MIME signature attachment

Parameters:
p - Part to be inspected
Returns:
If the Part is an S/MIME signature attachment
Throws:
MessagingException - If unable to inspect the Part

getAttachmentFileName

public static String getAttachmentFileName(Part p)
                                    throws MessagingException
Returns the filename for a Part or null if the Part does not have an attachment.

Parameters:
p - Part data
Returns:
Filename or null
Throws:
MessagingException - If unable to interrogate the Part

createFromAttachment

public static Message createFromAttachment(Part p)
                                    throws IOException,
                                           MessagingException
Creates and returns a new Message from a Part. If unable to create the Message or the part isn't RFC-822 compatible, null will be returned.

Parameters:
p - Part data
Returns:
new Message
Throws:
IOException - If unable to interrogate the Part
MessagingException - If unable to interrogate the Part

getFirstEmailAttachment

public static Part getFirstEmailAttachment(Message message)
                                    throws IOException,
                                           MessagingException
Returns the first RFC-822 compatible attachment found. null is found if no RFC-822 attachment is found.

Parameters:
message - Email Message to be searched
Returns:
First RFC-822 compatible attachment found or null
Throws:
IOException - If unable to interrogate the Message
MessagingException - If unable to interrogate the Message

getPartContent

public static byte[] getPartContent(Part p)
                             throws IOException,
                                    MessagingException
Returns the raw content from a Part

Parameters:
p - Part data
Returns:
Raw content
Throws:
IOException - If unable to retrieve the raw data
MessagingException - If unable to retrieve the raw data

setAllContentTypes

public static void setAllContentTypes(Multipart mp)
                               throws MessagingException
Sets the Content-Type for all BodyParts contained

Parameters:
mp - Multipart data
Throws:
MessagingException - If unable to set the Content-Type

setContentType

public static void setContentType(Part p)
                           throws MessagingException
Sets the Content-Type on a Part based on the Part's filename

Parameters:
p - Part data
Throws:
MessagingException - If unable to set the Content-Type

setContentType

public static void setContentType(Part p,
                                  String contentType)
                           throws MessagingException
Sets the Content-Type on a Part. If contentType is null and the Part has a filename, the Content-Type is calculated from the filename.

Parameters:
p - Part data
contentType - Content-Type or null
Throws:
MessagingException - If unable to set the Content-Type

getFilenames

public static Set<String> getFilenames(Message m)
                                throws MessagingException,
                                       IOException
Returns all of the non-S/MIME signature filenames associated with the Message m. If the message is not multi-part or there are no non-S/MIME signature attachments, an empty Set will be returned.

Parameters:
m - Message to be interrogated
Returns:
Set of filenames
Throws:
MessagingException - If unable to interrogate the Message
IOException

getFilenames

public static Set<String> getFilenames(Multipart m)
                                throws MessagingException
Returns all of the non-S/MIME signature filenames associated with the Multipart m. If there are no non-S/MIME signature attachments, an empty Set will be returned.

Parameters:
m - Multipart to be interrogated
Returns:
Set of filenames
Throws:
MessagingException - If unable to interrogate the Multipart

debug

public static void debug(Logger log,
                         Message message)
Logs a mail Message at debug Level.

Parameters:
log - Logger instances
message - Email Message

info

public static void info(Logger log,
                        Message message)
Logs a mail Message at info Level.

Parameters:
log - Logger instances
message - Email Message

logMessage

public static void logMessage(Logger log,
                              Level level,
                              Message message)
Logs a mail Message at the specified logging Level using log

Parameters:
log - Logger instances
level - Logging Level
message - Email Message

logAllHeaders

public static void logAllHeaders(Logger log,
                                 Level level,
                                 Part message)
Logs all of the headers for a Part at the specified logging Level using log

Parameters:
log - Logger instances
level - Logging Level
message - Part data

logPart

public static void logPart(Logger log,
                           Level level,
                           Part part)
Logs a mail Part at the specified logging Level using log

Parameters:
log - Logger instances
level - Logging Level
part - Email Part


Copyright © 2012 Carnegie Mellon University