org.mailster.util
Class MailUtilities

java.lang.Object
  extended by org.mailster.util.MailUtilities

public class MailUtilities
extends java.lang.Object

---
Mailster (C) 2007-2009 De Oliveira Edouard

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

See  Mailster Web Site
---

MailUtilities.java - Various methods to help in message handling, parsing, formatting, writting etc...

Version:
$Revision: 1.19 $, $Date: 2009/03/22 21:56:40 $
Author:
Edouard De Oliveira

Field Summary
private static org.slf4j.Logger LOG
          Log object for this class.
static int MAX_LINE_LENGTH
          Maximum SMTP communication line width (RFC 822 sets it to "72 or 73 characters" so we give some extra characters just to ensure not loosing characters).
static java.lang.String tempDirectory
          Temporary directory name used when saving temporary data.
 
Constructor Summary
MailUtilities()
           
 
Method Summary
static java.lang.String decode(java.lang.String encodedString, java.lang.String charsetName, java.lang.String encoding)
          Decodes the string encodedString encoded as encoding with the charset named charsetName.
static java.lang.String decodeHeaderValue(java.lang.String encodedHeaderValue)
          Decodes a encoded header value.
static java.lang.String escapeHTMLString(java.lang.String string)
          Returns an escaped HTML string.
static java.lang.String formatFlowed(java.lang.String body)
          Returns a decorated version of body compliant with RFC 2646 when interpreted in a browser.
private static java.lang.String getFlowedMarkerHMTLColor(int level)
          Computes and returns the HTML color string (#xxyyzz) associated with the quote level level.
static java.lang.String getHeaderParameterValue(SmtpHeadersInterface headers, java.lang.String name, java.lang.String parameterName)
          Returns the value of a specific parameter named parameterName which is a sub value of the header name from the headers headers.
private static int getLineLevel(java.lang.String line)
          Returns the quote level of the format flowed message line line.
static java.lang.String getNonNullHeaderValue(SmtpHeadersInterface headers, java.lang.String headerName, boolean decode)
          Returns the value of the header headerName.
private static java.lang.String getPartBoundary(SmtpHeadersInterface headers)
          Returns the string that delimits sub parts of a MIME multipart message.
private static SmtpMessagePart handleBodyPart(java.io.BufferedReader reader, SmtpHeadersInterface parentHeaders, boolean readHeader)
          Returns a SmtpMessagePart from parsing reader.
private static java.util.List<SmtpMessagePart> handleMultiPart(java.io.BufferedReader reader, SmtpHeadersInterface parentHeaders)
          Returns a SmtpMessagePart's list from parsing reader.
private static void handleRFC822MessagePart(java.io.BufferedReader reader, SmtpMessagePart part, java.lang.String boundary)
          Reads an embedded RFC 822 message and writes it to the part body's part.
private static boolean isMessagePart(SmtpHeadersInterface headers)
          Returns true if headers headers represent a message content.
static boolean isMultiPart(SmtpHeadersInterface headers)
          Returns true if headers headers represent a multipart content.
static boolean outputStreamToFile(java.lang.String directory, java.lang.String fileName, java.io.InputStream bin)
          Writes the stream to the specified directory and filename.
static SmtpMessagePart parseInternalParts(SmtpMessage msg, java.lang.String body)
          Parses msg and returns a SmtpMessagePart representing the contents of the message.
static java.util.HashMap<java.lang.String,java.lang.String> saveCIDFilesToTemporaryDirectory(SmtpMessagePart part)
          Saves the content of the sub parts of the given message part to the system default temporary directory if they have a Content-ID header.
static void write(java.io.OutputStream os, java.lang.String encodedString, java.lang.String charsetName, java.lang.String encoding)
          Outputs the decoded content from the encodedString string to the os output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG

private static final org.slf4j.Logger LOG
Log object for this class.


MAX_LINE_LENGTH

public static final int MAX_LINE_LENGTH
Maximum SMTP communication line width (RFC 822 sets it to "72 or 73 characters" so we give some extra characters just to ensure not loosing characters).

See Also:
Constant Field Values

tempDirectory

public static final java.lang.String tempDirectory
Temporary directory name used when saving temporary data. Value is set to the value of the 'java.io.tmpdir' system property.

See Also:
System.getProperty(String)
Constructor Detail

MailUtilities

public MailUtilities()
Method Detail

getNonNullHeaderValue

public static java.lang.String getNonNullHeaderValue(SmtpHeadersInterface headers,
                                                     java.lang.String headerName,
                                                     boolean decode)
Returns the value of the header headerName. If the parameter decode is set to true the the value is decoded. If value is null, returns "" unless required header is of the following types :

Date : returns a RFC 822 compliant string of the current date and time
Subject : returns the "{No subject}" string

This is a convenience method for GUI programs to avoid returning a null value for headers that are not commonly expected to be null but can be according to RFCs.

Parameters:
headers - all the headers in which to search
headerName - the name of the header
decode - if true the header value is decoded
Returns:
the non null header value

decodeHeaderValue

public static java.lang.String decodeHeaderValue(java.lang.String encodedHeaderValue)
Decodes a encoded header value. Returns null if string is null. If decoding fails, it returns the original string.

All the encodings defined in RFC 2045 are supported here. They include "base64", "quoted-printable", "7bit", "8bit", and "binary". In addition, "uuencode" is also supported.

Parameters:
encodedHeaderValue - the header value to decode
Returns:
the decoded string
See Also:
MimeUtility.decodeText(String)

decode

public static java.lang.String decode(java.lang.String encodedString,
                                      java.lang.String charsetName,
                                      java.lang.String encoding)
Decodes the string encodedString encoded as encoding with the charset named charsetName. If decoding fails, it returns the original string.

All the encodings defined in RFC 2045 are supported here. They include "base64", "quoted-printable", "7bit", "8bit", and "binary". In addition, "uuencode" is also supported.

Parameters:
encodedString - the string to decode
charsetName - the name of the charset
encoding - the encoding used for the string
Returns:
the decoded String
See Also:
MimeUtility.decode(java.io.InputStream, java.lang.String)

saveCIDFilesToTemporaryDirectory

public static java.util.HashMap<java.lang.String,java.lang.String> saveCIDFilesToTemporaryDirectory(SmtpMessagePart part)
Saves the content of the sub parts of the given message part to the system default temporary directory if they have a Content-ID header. Method returns a HashMap where CID aka Content-ID is the key to a string representing the URL of the corresponding local file.

Parameters:
part - the message part containing relative files to save
Returns:
a hashmap where CID is the key to a string representing the URL to a local file

write

public static void write(java.io.OutputStream os,
                         java.lang.String encodedString,
                         java.lang.String charsetName,
                         java.lang.String encoding)
                  throws java.io.IOException,
                         javax.mail.MessagingException
Outputs the decoded content from the encodedString string to the os output stream.

Parameters:
os - the output stream
encodedString - the content to write
charsetName - the name of the charset
encoding - the name of the encoding
Throws:
java.io.IOException
javax.mail.MessagingException

getLineLevel

private static int getLineLevel(java.lang.String line)
Returns the quote level of the format flowed message line line.

Parameters:
line - the text line
Returns:
the quote level

getFlowedMarkerHMTLColor

private static java.lang.String getFlowedMarkerHMTLColor(int level)
Computes and returns the HTML color string (#xxyyzz) associated with the quote level level.

Parameters:
level - the quote level
Returns:
the HTML color string

formatFlowed

public static java.lang.String formatFlowed(java.lang.String body)
Returns a decorated version of body compliant with RFC 2646 when interpreted in a browser.

Parameters:
body - the body of the message
Returns:
the decorated body

getHeaderParameterValue

public static java.lang.String getHeaderParameterValue(SmtpHeadersInterface headers,
                                                       java.lang.String name,
                                                       java.lang.String parameterName)
Returns the value of a specific parameter named parameterName which is a sub value of the header name from the headers headers.

Parameters:
headers - the headers
name - the header name
parameterName - the parameter name
Returns:
the value as a string

getPartBoundary

private static java.lang.String getPartBoundary(SmtpHeadersInterface headers)
Returns the string that delimits sub parts of a MIME multipart message. This string is the value of the boundary parameter which is a sub value of the Content-Type header from the headers headers.

Parameters:
headers - the headers of the part
Returns:
the boundary string

isMultiPart

public static boolean isMultiPart(SmtpHeadersInterface headers)
Returns true if headers headers represent a multipart content.

Parameters:
headers - the headers
Returns:
true if part content is of type multipart

isMessagePart

private static boolean isMessagePart(SmtpHeadersInterface headers)
Returns true if headers headers represent a message content.

Parameters:
headers - the headers
Returns:
true if part content is of type message

handleBodyPart

private static SmtpMessagePart handleBodyPart(java.io.BufferedReader reader,
                                              SmtpHeadersInterface parentHeaders,
                                              boolean readHeader)
                                       throws java.io.IOException
Returns a SmtpMessagePart from parsing reader. If readHeader is true then data read from reader should contain the headers of the part (always the case except for a simple single part message where headers are set within the message headers).

Parameters:
reader - the mail body reader stream
parentHeaders - the headers of the parent part
readHeader - true if method should read headers from the reader
Returns:
a part representing the reader contents
Throws:
java.io.IOException

handleRFC822MessagePart

private static void handleRFC822MessagePart(java.io.BufferedReader reader,
                                            SmtpMessagePart part,
                                            java.lang.String boundary)
                                     throws java.io.IOException
Reads an embedded RFC 822 message and writes it to the part body's part.

Parameters:
reader - the reader stream
part - the target part
boundary - the boundary of the part
Throws:
java.io.IOException

handleMultiPart

private static java.util.List<SmtpMessagePart> handleMultiPart(java.io.BufferedReader reader,
                                                               SmtpHeadersInterface parentHeaders)
                                                        throws java.io.IOException
Returns a SmtpMessagePart's list from parsing reader.

Parameters:
reader - the mail body reader stream
parentHeaders - the headers of the parent part
Returns:
the list of parts
Throws:
java.io.IOException

parseInternalParts

public static SmtpMessagePart parseInternalParts(SmtpMessage msg,
                                                 java.lang.String body)
Parses msg and returns a SmtpMessagePart representing the contents of the message.

Parameters:
msg - the message to be parsed
body - the body of the message
Returns:
the object representation of the message's body

escapeHTMLString

public static java.lang.String escapeHTMLString(java.lang.String string)
Returns an escaped HTML string.

Parameters:
string - the string to escape
Returns:
the escaped string

outputStreamToFile

public static boolean outputStreamToFile(java.lang.String directory,
                                         java.lang.String fileName,
                                         java.io.InputStream bin)
Writes the stream to the specified directory and filename.

Parameters:
directory - the directory to write to
fileName - the image file name
bin - the inputstream to read from
Returns:
true if write succeeded