com.openexchange.mail.mime.converters
Class MIMEMessageConverter

java.lang.Object
  extended by com.openexchange.mail.mime.converters.MIMEMessageConverter

public final class MIMEMessageConverter
extends java.lang.Object

MIMEMessageConverter - Provides several methods to convert instances of MimeMessage to MailMessage in vice versa.

Author:
Thorben Betten

Method Summary
static void checkFieldExistence(MailMessage[] mails, MailField[] fields)
          Checks field existence.
static javax.mail.Message convertComposedMailMessage(ComposedMailMessage composedMail)
          Converts given instance of ComposedMailMessage into a JavaMail-conform Message object.
static javax.mail.Flags convertMailFlags(int flags)
          Converts specified flags bit mask to an instance of Flags.
static javax.mail.Message convertMailMessage(MailMessage mail)
          Converts given instance of MailMessage into a JavaMail-conform Message object.
static javax.mail.Message convertMailMessage(MailMessage mail, boolean clone)
          Converts given instance of MailMessage into a JavaMail-conform Message object.
static javax.mail.Message[] convertMailMessages(MailMessage[] mails)
          Converts given instances of MailMessage into JavaMail-conform Message objects.
static javax.mail.Message[] convertMailMessages(MailMessage[] mails, boolean clone)
          Converts given instances of MailMessage into JavaMail-conform Message objects.
static javax.mail.Part convertMailPart(MailPart mailPart)
          Creates a Part object from given instance of MailPart.
static MailMessage convertMessage(byte[] asciiBytes)
          Creates a message data object from given message bytes conform to RFC822.
static MailMessage convertMessage(byte[] asciiBytes, java.lang.String uid, java.lang.String fullname, char separator, MailField[] fields)
          Creates a message data object from given message/rfc822 content filled with desired fields.
static MailMessage convertMessage(javax.mail.internet.MimeMessage msg)
          Creates a message data object from given MIME message.
static MailMessage convertMessage(javax.mail.internet.MimeMessage msg, java.lang.String uid, java.lang.String fullname, char separator, MailField[] fields)
          Creates a message data object from given MIME message filled with desired fields.
static MailMessage[] convertMessages(javax.mail.Message[] msgs, javax.mail.Folder folder, MailField[] fields, boolean includeBody)
          Converts given array of Message instances to an array of MailMessage instances.
static MailMessage[] convertMessages(javax.mail.Message[] msgs, MailField[] fields)
          Converts given array of Message instances to an array of MailMessage instances.
static MailMessage[] convertMessages(javax.mail.Message[] msgs, MailField[] fields, java.lang.String[] headerNames, boolean includeBody)
          Converts given array of Message instances to an array of MailMessage instances.
static MailPart convertPart(byte[] asciiBytes)
          Creates a MIME mail part object from given raw bytes.
static MailPart convertPart(javax.mail.Part part)
          Creates a MIME mail part object from given MIME part.
static MailPart convertPart(javax.mail.Part part, boolean enforeSize)
          Creates a MIME mail part object from given MIME part.
static MailMessage fillComposedMailMessage(ComposedMailMessage composedMail)
          Fills specified instance of ComposedMailMessage with MIMEMessageFiller.
static javax.mail.internet.InternetAddress[] getAddressHeader(java.lang.String addresses)
          Gets the address header from given address header value.
static javax.mail.internet.InternetAddress[] getAddressHeader(java.lang.String name, javax.mail.Message message)
          Gets the address headers denoted by specified header name in a safe manner.
static java.lang.String getStringHeader(java.lang.String name, javax.mail.Message message)
          Gets the first header denoted by specified header name and decodes its value to a unicode string if necessary.
static java.lang.String getStringHeader(java.lang.String name, javax.mail.Message message, char delimiter)
          Gets the headers denoted by specified header name and decodes its value to a unicode string if necessary.
static java.lang.String getSubject(javax.mail.Message message)
          Gets the first header denoted by specified header name and decodes its value to a unicode string if necessary.
static HeaderCollection loadHeaders(byte[] bytes)
          Parses given headers' byte array into a collection until EOF or 2 subsequent CRLFs occur.
static HeaderCollection loadHeaders(java.io.InputStream inputStream)
          Parses given headers' input stream into a collection until EOF or 2 subsequent CRLFs occur.
static HeaderCollection loadHeaders(java.lang.String messageSrc)
          Parses given message source's headers into a collection until EOF or 2 subsequent CRLFs occur.
static MailMessage newMailMessage()
          Returns a new instance of MailMessage ready to get filled with header and/or flag information, but not capable to reference to body content.
static void parseFlags(javax.mail.Flags flags, MailMessage mailMessage)
          Parses specified flags to given mail.
static int parseImportance(java.lang.String importance)
          Parses the value of header Importance.
static void parseImportance(java.lang.String importance, MailMessage mailMessage)
          Parses the value of header Importance.
static void parseMimeFlags(int flags, javax.mail.Message msg)
          Parses specified flags to given message.
static int parsePriority(java.lang.String priorityStr)
          Parses the value of header X-Priority.
static void parsePriority(java.lang.String priorityStr, MailMessage mailMessage)
          Parses the value of header X-Priority.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

convertMailPart

public static javax.mail.Part convertMailPart(MailPart mailPart)
                                       throws MailException
Creates a Part object from given instance of MailPart.

Parameters:
mailPart - The instance of MailPart
Returns:
Appropriate instance of Part
Throws:
MailException

convertMailMessages

public static javax.mail.Message[] convertMailMessages(MailMessage[] mails)
                                                throws MailException
Converts given instances of MailMessage into JavaMail-conform Message objects.

Note: This is just a convenience method that invokes convertMailMessage(MailMessage) for each instance of MailMessage

Parameters:
mails - The source instances of MailMessage
Returns:
JavaMail-conform Message objects.
Throws:
MailException - If conversion fails

convertMailMessages

public static javax.mail.Message[] convertMailMessages(MailMessage[] mails,
                                                       boolean clone)
                                                throws MailException
Converts given instances of MailMessage into JavaMail-conform Message objects.

Note: This is just a convenience method that invokes convertMailMessage(MailMessage) for each instance of MailMessage

Parameters:
mails - The source instances of MailMessage
clone - true to clone message source; otherwise false to return a reference if possible
Returns:
JavaMail-conform Message objects.
Throws:
MailException - If conversion fails

convertMailMessage

public static javax.mail.Message convertMailMessage(MailMessage mail)
                                             throws MailException
Converts given instance of MailMessage into a JavaMail-conform Message object.

Parameters:
mail - The source instance of MailMessage
Returns:
A JavaMail-conform Message object
Throws:
MailException - If conversion fails

convertMailMessage

public static javax.mail.Message convertMailMessage(MailMessage mail,
                                                    boolean clone)
                                             throws MailException
Converts given instance of MailMessage into a JavaMail-conform Message object.

Parameters:
mail - The source instance of MailMessage
clone - true to clone message source; otherwise false to return a reference if possible
Returns:
A JavaMail-conform Message object
Throws:
MailException - If conversion fails

convertComposedMailMessage

public static javax.mail.Message convertComposedMailMessage(ComposedMailMessage composedMail)
                                                     throws MailException
Converts given instance of ComposedMailMessage into a JavaMail-conform Message object.

Parameters:
composedMail - The source instance of ComposedMailMessage
Returns:
A JavaMail-conform Message object
Throws:
MailException - If conversion fails

fillComposedMailMessage

public static MailMessage fillComposedMailMessage(ComposedMailMessage composedMail)
                                           throws MailException
Fills specified instance of ComposedMailMessage with MIMEMessageFiller.

Parameters:
composedMail - The composed mail
Returns:
A filled instance of MailMessage ready for further usage
Throws:
MailException - If mail cannot be filled.

parseMimeFlags

public static void parseMimeFlags(int flags,
                                  javax.mail.Message msg)
                           throws javax.mail.MessagingException
Parses specified flags to given message.

Parameters:
flags - The flags to parse
msg - The message to fill
Throws:
javax.mail.MessagingException - If a messaging error occurs

convertMessages

public static MailMessage[] convertMessages(javax.mail.Message[] msgs,
                                            MailField[] fields)
                                     throws MailException
Converts given array of Message instances to an array of MailMessage instances. The single elements of the array are expected to be instances of ExtendedMimeMessage; meaning the messages were created through a manual fetch.

Only the fields specified through parameter fields are going to be set

Parameters:
msgs - The source messages
fields - The fields to fill
Returns:
The converted array of Message instances
Throws:
MailException - If conversion fails
See Also:
to convert common instances of {@link Message}

convertMessages

public static MailMessage[] convertMessages(javax.mail.Message[] msgs,
                                            MailField[] fields,
                                            java.lang.String[] headerNames,
                                            boolean includeBody)
                                     throws MailException
Converts given array of Message instances to an array of MailMessage instances. The single elements of the array are expected to be instances of ExtendedMimeMessage; meaning the messages were created through a manual fetch.

Only the fields specified through parameter fields are going to be set

Parameters:
msgs - The source messages
fields - The fields to fill
headerNames - The header names
includeBody - Whether to create mail messages with reference to content or not
Returns:
The converted array of Message instances
Throws:
MailException - If conversion fails
See Also:
#convertMessages(Message[], Folder, MailField[])

convertMessages

public static MailMessage[] convertMessages(javax.mail.Message[] msgs,
                                            javax.mail.Folder folder,
                                            MailField[] fields,
                                            boolean includeBody)
                                     throws MailException
Converts given array of Message instances to an array of MailMessage instances.

Only the fields specified through parameter fields are going to be set

Parameters:
msgs - The source messages
folder - The folder containing source messages
fields - The fields to fill
includeBody - true to include body; otherwise false
Returns:
The converted array of Message instances
Throws:
MailException - If conversion fails

checkFieldExistence

public static void checkFieldExistence(MailMessage[] mails,
                                       MailField[] fields)
Checks field existence.

Parameters:
mails - The mails to checks
fields - The field to check for

newMailMessage

public static MailMessage newMailMessage()
Returns a new instance of MailMessage ready to get filled with header and/or flag information, but not capable to reference to body content.

Returns:
A new instance of MailMessage

convertMessage

public static MailMessage convertMessage(byte[] asciiBytes)
                                  throws MailException
Creates a message data object from given message bytes conform to RFC822.

Parameters:
asciiBytes - The message bytes conform to RFC822
Returns:
An instance of MailMessage
Throws:
MailException - If conversion fails

convertMessage

public static MailMessage convertMessage(javax.mail.internet.MimeMessage msg)
                                  throws MailException
Creates a message data object from given MIME message.

Parameters:
msg - The MIME message
Returns:
An instance of MailMessage containing the attributes from given MIME message
Throws:
MailException - If conversion fails

convertMessage

public static MailMessage convertMessage(byte[] asciiBytes,
                                         java.lang.String uid,
                                         java.lang.String fullname,
                                         char separator,
                                         MailField[] fields)
                                  throws MailException
Creates a message data object from given message/rfc822 content filled with desired fields.

Parameters:
asciiBytes - The message/rfc822 content
uid - The UID or -1
fullname - The folder's fullname
separator - The folder's separator
fields - The desired fields to fill
Returns:
An instance of MailMessage filled with desired fields
Throws:
MailException - If conversion fails

convertMessage

public static MailMessage convertMessage(javax.mail.internet.MimeMessage msg,
                                         java.lang.String uid,
                                         java.lang.String fullname,
                                         char separator,
                                         MailField[] fields)
                                  throws MailException
Creates a message data object from given MIME message filled with desired fields.

Parameters:
msg - The MIME message
uid - The UID or null
fullname - The folder fullname
separator - The folder separator character
fields - The desired fields to fill
Returns:
An instance of MailMessage filled with desired fields
Throws:
MailException - If conversion fails

convertPart

public static MailPart convertPart(byte[] asciiBytes)
                            throws MailException
Creates a MIME mail part object from given raw bytes.

Parameters:
asciiBytes - The raw bytes
Returns:
A MIME mail part object
Throws:
MailException - If creating MIME mail part object fails

convertPart

public static MailPart convertPart(javax.mail.Part part)
                            throws MailException
Creates a MIME mail part object from given MIME part.

Parameters:
part - The part
Returns:
an instance of MailPart containing the attributes from given part
Throws:
MailException

convertPart

public static MailPart convertPart(javax.mail.Part part,
                                   boolean enforeSize)
                            throws MailException
Creates a MIME mail part object from given MIME part.

Parameters:
part - The part
enforeSize - true to ensure size is set in returned mail part; otherwise false. If set given part's input stream is examined which might unnecessarily load data from backend.
Returns:
an instance of MailPart containing the attributes from given part
Throws:
MailException

convertMailFlags

public static javax.mail.Flags convertMailFlags(int flags)
Converts specified flags bit mask to an instance of Flags.

Parameters:
flags - The flags bit mask
Returns:
The corresponding instance of Flags

parseFlags

public static void parseFlags(javax.mail.Flags flags,
                              MailMessage mailMessage)
                       throws MailException
Parses specified flags to given mail.

Parameters:
flags - The flags to parse
mailMessage - The mail to apply the flags to
Throws:
MailException - If a mail error occurs

loadHeaders

public static HeaderCollection loadHeaders(java.io.InputStream inputStream)
                                    throws java.io.IOException
Parses given headers' input stream into a collection until EOF or 2 subsequent CRLFs occur.

This is a convenience method that delegates to loadHeaders(byte[]).

Parameters:
inputStream - The headers' input stream
Returns:
The parsed headers as a collection.
Throws:
java.io.IOException - If an I/O error occurs.

loadHeaders

public static HeaderCollection loadHeaders(byte[] bytes)
Parses given headers' byte array into a collection until EOF or 2 subsequent CRLFs occur.

This is a convenience method that delegates to loadHeaders(String).

Parameters:
bytes - The headers' byte array
Returns:
The parsed headers as a collection.

loadHeaders

public static HeaderCollection loadHeaders(java.lang.String messageSrc)
Parses given message source's headers into a collection until EOF or 2 subsequent CRLFs occur.

Parameters:
messageSrc - The message source
Returns:
The parsed headers as a collection.

getSubject

public static java.lang.String getSubject(javax.mail.Message message)
                                   throws javax.mail.MessagingException
Gets the first header denoted by specified header name and decodes its value to a unicode string if necessary.
 "=?UTF-8?Q?=C3=BCber?="    is decoded to    "&uumlber"
 

Parameters:
name - The header name
message - The message providing the header
Returns:
The decoded header
Throws:
javax.mail.MessagingException - If a messaging error occurs

getStringHeader

public static java.lang.String getStringHeader(java.lang.String name,
                                               javax.mail.Message message)
                                        throws javax.mail.MessagingException
Gets the first header denoted by specified header name and decodes its value to a unicode string if necessary.
 "=?UTF-8?Q?=C3=BCber?="    is decoded to    "&uumlber"
 

Parameters:
name - The header name
message - The message providing the header
Returns:
The decoded header
Throws:
javax.mail.MessagingException - If a messaging error occurs

getStringHeader

public static java.lang.String getStringHeader(java.lang.String name,
                                               javax.mail.Message message,
                                               char delimiter)
                                        throws javax.mail.MessagingException
Gets the headers denoted by specified header name and decodes its value to a unicode string if necessary.
 "=?UTF-8?Q?=C3=BCber?="    is decoded to    "über"
 

Parameters:
name - The header name
message - The message providing the header
delimiter - The delimiter character if message contains multiple header values; set to '\0' to only consider first one
Returns:
The decoded header
Throws:
javax.mail.MessagingException - If a messaging error occurs

getAddressHeader

public static javax.mail.internet.InternetAddress[] getAddressHeader(java.lang.String name,
                                                                     javax.mail.Message message)
                                                              throws javax.mail.MessagingException
Gets the address headers denoted by specified header name in a safe manner.

If strict parsing of address headers yields a AddressException, then a plain-text version is generated to display broken address header as it is.

Parameters:
name - The address header name
message - The message providing the address header
Returns:
The parsed address headers as an array of InternetAddress instances
Throws:
javax.mail.MessagingException - If a messaging error occurs

getAddressHeader

public static javax.mail.internet.InternetAddress[] getAddressHeader(java.lang.String addresses)
Gets the address header from given address header value.

Parameters:
addresses - The address header value
Returns:
The parsed addresses

parsePriority

public static void parsePriority(java.lang.String priorityStr,
                                 MailMessage mailMessage)
Parses the value of header X-Priority.

Parameters:
priorityStr - The header value
mailMessage - The mail message to fill

parseImportance

public static void parseImportance(java.lang.String importance,
                                   MailMessage mailMessage)
Parses the value of header Importance.

Parameters:
importance - The header value
mailMessage - The mail message to fill

parsePriority

public static int parsePriority(java.lang.String priorityStr)
Parses the value of header X-Priority.

Parameters:
priorityStr - The header value

parseImportance

public static int parseImportance(java.lang.String importance)
Parses the value of header Importance.

Parameters:
importance - The header value