org.jasen.util
Class MimeUtils

java.lang.Object
  extended byorg.jasen.util.MimeUtils

public class MimeUtils
extends Object

General Mime utilities.

Author:
Jason Polites

Field Summary
static String[] ATTACHMENT_DISPOSITIONS
           
static int FORGERY_CONFIRMED
          Confirmed forgery
static int FORGERY_REJECTED
          Confirmed authentic
static int FORGERY_UNDETERMINED
          Undetermined (test not yet performed)
static int FORGERY_UNKNOWN
          Forgery status could not be determined with absolute certainty
 
Constructor Summary
MimeUtils()
           
 
Method Summary
static List getAllAttachmentParts(List parts)
          Gets all parts which can be considered an attachment.
static javax.mail.Header[] getAllHeaders(javax.mail.internet.MimeMessage message)
           
static String getDomainFromAddress(String emailAddress)
          Returns the domain component of an email address
static javax.mail.Part getFirstPartFromList(List parts, String contentType, String disposition)
           
static List getMultiplePartsFromList(List parts, String contentType, String[] dispositions)
          Gets the parts which match any of the dispositions
static Object getPartContent(javax.mail.Part part)
           
static void getParts(List parts, javax.mail.Part p)
           
static void getParts(List parts, javax.mail.Part p, String contentType, String disposition)
          Gets the parts from the MimeMessage

Use null disposition to ignore
static List getPartsFromList(List parts, String contentType, String disposition)
           
static List getSubMessagePartsFromList(List parts)
          Gets a list of all parts which are themselves MimeMessages
static List getUnknownPartsFromList(List parts)
          Gets all parts which are unknown.
static boolean isAttachment(String disposition)
           
static int isForgery(InetAddressResolver resolver, javax.mail.internet.MimeMessage message, String senderAddress, ReceivedHeaderParser parser)
          Determines if the loaded message is a forgery.
static boolean isValidAddress(String address)
           
static javax.mail.internet.InternetAddress toInternetAddress(javax.mail.Address address)
          Converts an Address object to an Internet Address with strict address parsing
static javax.mail.internet.InternetAddress toInternetAddress(javax.mail.Address address, boolean strict)
          Converts an Address object to an Internet Address
static boolean verifySenderAddress(DNSResolver dresolver, InetAddressResolver iresolver, ReceivedHeaderParser parser, String receivedHeaderLine, String senderAddress)
          Verifies the given sender address against the information in the last (most recent) received header line

Specifically, this does the following:

get domain of sender
get IP address of last (most recent) MTA
get hostname of last (most recent) MTA
if (MTA IP Address resolves to MTA hostname) then
use MTA hostname for MX IP records
else
use MTA IP Address
get MX records for sender domain
if(at least 1 MX record IP matches MTA IP) then valid
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FORGERY_UNDETERMINED

public static final int FORGERY_UNDETERMINED
Undetermined (test not yet performed)

See Also:
Constant Field Values

FORGERY_CONFIRMED

public static final int FORGERY_CONFIRMED
Confirmed forgery

See Also:
Constant Field Values

FORGERY_REJECTED

public static final int FORGERY_REJECTED
Confirmed authentic

See Also:
Constant Field Values

FORGERY_UNKNOWN

public static final int FORGERY_UNKNOWN
Forgery status could not be determined with absolute certainty

See Also:
Constant Field Values

ATTACHMENT_DISPOSITIONS

public static String[] ATTACHMENT_DISPOSITIONS
Constructor Detail

MimeUtils

public MimeUtils()
Method Detail

getAllHeaders

public static javax.mail.Header[] getAllHeaders(javax.mail.internet.MimeMessage message)
                                         throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException

isValidAddress

public static boolean isValidAddress(String address)

verifySenderAddress

public static boolean verifySenderAddress(DNSResolver dresolver,
                                          InetAddressResolver iresolver,
                                          ReceivedHeaderParser parser,
                                          String receivedHeaderLine,
                                          String senderAddress)
                                   throws JasenParseException,
                                          UnknownHostException,
                                          DNSException
Verifies the given sender address against the information in the last (most recent) received header line

Specifically, this does the following:

get domain of sender
get IP address of last (most recent) MTA
get hostname of last (most recent) MTA
if (MTA IP Address resolves to MTA hostname) then
use MTA hostname for MX IP records
else
use MTA IP Address
get MX records for sender domain
if(at least 1 MX record IP matches MTA IP) then valid

Parameters:
parser - The parser to use on the header line
receivedHeaderLine - The last (most recent) received header line
senderAddress - The From address (NOT the envelope address)
Returns:
True if the sender is valid, false otherwise
Throws:
JasenParseException
UnknownHostException
DNSException

toInternetAddress

public static javax.mail.internet.InternetAddress toInternetAddress(javax.mail.Address address)
                                                             throws javax.mail.internet.AddressException
Converts an Address object to an Internet Address with strict address parsing

Parameters:
address -
Returns:
The parsed InternetAddress
Throws:
javax.mail.internet.AddressException

toInternetAddress

public static javax.mail.internet.InternetAddress toInternetAddress(javax.mail.Address address,
                                                                    boolean strict)
                                                             throws javax.mail.internet.AddressException
Converts an Address object to an Internet Address

Parameters:
address -
strict - If true, address parsing is strict
Returns:
The parsed InternetAddress
Throws:
javax.mail.internet.AddressException

isForgery

public static int isForgery(InetAddressResolver resolver,
                            javax.mail.internet.MimeMessage message,
                            String senderAddress,
                            ReceivedHeaderParser parser)
                     throws javax.mail.MessagingException,
                            JasenParseException
Determines if the loaded message is a forgery.
This is done by looking at the last (most recent) received header and determining if the hostname of the sending server matches the hostname provided by the header information via the DNS

Parameters:
resolver - The resolver used to resolve InetAddresses
message - The MimeMessage to test
senderAddress - The envelope sender
parser - The parser to use to parse the "Received" header(s)
Returns:
An integer representing the determination. 0 = Not a forgery, 1 = Definately a forgery, 2 = Unsure or could not be determined
Throws:
javax.mail.MessagingException
CacheException
JasenParseException

getDomainFromAddress

public static String getDomainFromAddress(String emailAddress)
Returns the domain component of an email address

Parameters:
emailAddress -
Returns:
The domain ofthe address (everything after the @)

isAttachment

public static boolean isAttachment(String disposition)

getParts

public static void getParts(List parts,
                            javax.mail.Part p)
                     throws javax.mail.MessagingException,
                            IOException
Throws:
javax.mail.MessagingException
IOException

getParts

public static void getParts(List parts,
                            javax.mail.Part p,
                            String contentType,
                            String disposition)
                     throws javax.mail.MessagingException,
                            IOException
Gets the parts from the MimeMessage

Use null disposition to ignore

Parameters:
parts - A new list to hold the parts
p - The current Part
contentType - If specified, returns only parts matching the given content type. Use null to ignore
disposition - If specified, returns only parts matching the given disposition. Use null to ignore
Throws:
javax.mail.MessagingException
IOException

getMultiplePartsFromList

public static List getMultiplePartsFromList(List parts,
                                            String contentType,
                                            String[] dispositions)
                                     throws IOException,
                                            javax.mail.MessagingException
Gets the parts which match any of the dispositions

Parameters:
parts - A list of parts
contentType - The content type required
dispositions - The content dispositions required
Returns:
All the parts in the message which match the given content type and disposition
Throws:
IOException
javax.mail.MessagingException

getSubMessagePartsFromList

public static List getSubMessagePartsFromList(List parts)
                                       throws IOException,
                                              javax.mail.MessagingException
Gets a list of all parts which are themselves MimeMessages

Parameters:
parts -
Returns:
Throws:
IOException
javax.mail.MessagingException

getUnknownPartsFromList

public static List getUnknownPartsFromList(List parts)
                                    throws IOException,
                                           javax.mail.MessagingException
Gets all parts which are unknown. An unknown part is one which has no disposition, and is not text or html

Parameters:
parts -
Returns:
Throws:
IOException
javax.mail.MessagingException

getAllAttachmentParts

public static List getAllAttachmentParts(List parts)
                                  throws IOException,
                                         javax.mail.MessagingException
Gets all parts which can be considered an attachment. This includes sub messages and unknown parts

Parameters:
parts -
Returns:
A List of Part objects
Throws:
IOException
javax.mail.MessagingException

getFirstPartFromList

public static javax.mail.Part getFirstPartFromList(List parts,
                                                   String contentType,
                                                   String disposition)
                                            throws javax.mail.MessagingException,
                                                   IOException
Throws:
javax.mail.MessagingException
IOException

getPartsFromList

public static List getPartsFromList(List parts,
                                    String contentType,
                                    String disposition)
                             throws javax.mail.MessagingException,
                                    IOException
Throws:
javax.mail.MessagingException
IOException

getPartContent

public static Object getPartContent(javax.mail.Part part)
                             throws IOException,
                                    javax.mail.MessagingException
Throws:
IOException
javax.mail.MessagingException