org.apache.james.core
Class MimeMessageWrapper

java.lang.Object
  extended by javax.mail.Message
      extended by javax.mail.internet.MimeMessage
          extended by org.apache.james.core.MimeMessageWrapper
All Implemented Interfaces:
javax.mail.internet.MimePart, javax.mail.Part, org.apache.avalon.framework.activity.Disposable

public class MimeMessageWrapper
extends javax.mail.internet.MimeMessage
implements org.apache.avalon.framework.activity.Disposable

This object wraps a MimeMessage, only loading the underlying MimeMessage object when needed. Also tracks if changes were made to reduce unnecessary saves.


Nested Class Summary
 
Nested classes/interfaces inherited from class javax.mail.internet.MimeMessage
javax.mail.internet.MimeMessage.RecipientType
 
Field Summary
protected  boolean bodyModified
          This is false until we parse the message
protected  boolean headersModified
          This is false until we parse the message
protected  boolean messageParsed
          This is false until we parse the message
protected  MimeMessageSource source
          Can provide an input stream to the data
 
Fields inherited from class javax.mail.internet.MimeMessage
content, contentStream, dh, flags, headers, modified, saved
 
Fields inherited from class javax.mail.Message
expunged, folder, msgnum, session
 
Fields inherited from interface javax.mail.Part
ATTACHMENT, INLINE
 
Constructor Summary
MimeMessageWrapper(javax.mail.internet.MimeMessage original)
           
MimeMessageWrapper(MimeMessageSource source)
          A constructor that instantiates a MimeMessageWrapper based on a MimeMessageSource
MimeMessageWrapper(javax.mail.Session session, MimeMessageSource source)
          A constructor that instantiates a MimeMessageWrapper based on a MimeMessageSource
 
Method Summary
 void addHeader(java.lang.String name, java.lang.String value)
           
 void addHeaderLine(java.lang.String line)
           
protected  javax.mail.internet.InternetHeaders createInternetHeaders(java.io.InputStream is)
          If we already parsed the headers then we simply return the updated ones.
 void dispose()
           
 java.util.Enumeration getAllHeaderLines()
           
 java.util.Enumeration getAllHeaders()
           
protected  java.io.InputStream getContentStream()
           
 java.lang.String[] getHeader(java.lang.String name)
          We override all the "headers" access methods to be sure that we loaded the headers
 java.lang.String getHeader(java.lang.String name, java.lang.String delimiter)
           
 int getLineCount()
          Corrects JavaMail 1.1 version which always returns -1.
 java.util.Enumeration getMatchingHeaderLines(java.lang.String[] names)
           
 java.util.Enumeration getMatchingHeaders(java.lang.String[] names)
           
 long getMessageSize()
          Returns size of message, ie headers and content
 java.util.Enumeration getNonMatchingHeaderLines(java.lang.String[] names)
           
 java.util.Enumeration getNonMatchingHeaders(java.lang.String[] names)
           
 java.io.InputStream getRawInputStream()
           
 int getSize()
          This is the MimeMessage implementation - this should return ONLY the body, not the entire message (should not count headers).
 java.lang.String getSourceId()
          Returns the source ID of the MimeMessageSource that is supplying this with data.
 boolean isModified()
          Get whether the message has been modified.
protected  void loadHeaders()
          Load the message headers from the internal source.
protected  void loadMessage()
          Load the complete MimeMessage from the internal source.
protected  void parse(java.io.InputStream is)
           
 void removeHeader(java.lang.String name)
           
 void setDataHandler(javax.activation.DataHandler arg0)
          The message is changed when working with headers and when altering the content.
 void setHeader(java.lang.String name, java.lang.String value)
           
protected  void updateMessageID()
          Overrides standard implementation to ensure JavaMail works appropriately for an email server.
 void writeTo(java.io.OutputStream os)
          Rewritten for optimization purposes
 void writeTo(java.io.OutputStream headerOs, java.io.OutputStream bodyOs)
          Write
 void writeTo(java.io.OutputStream headerOs, java.io.OutputStream bodyOs, java.lang.String[] ignoreList)
           
 void writeTo(java.io.OutputStream os, java.lang.String[] ignoreList)
          Rewritten for optimization purposes
 
Methods inherited from class javax.mail.internet.MimeMessage
addFrom, addRecipients, addRecipients, createMimeMessage, getAllRecipients, getContent, getContentID, getContentLanguage, getContentMD5, getContentType, getDataHandler, getDescription, getDisposition, getEncoding, getFileName, getFlags, getFrom, getInputStream, getMessageID, getReceivedDate, getRecipients, getReplyTo, getSender, getSentDate, getSubject, isMimeType, isSet, reply, saveChanges, setContent, setContent, setContentID, setContentLanguage, setContentMD5, setDescription, setDescription, setDisposition, setFileName, setFlags, setFrom, setFrom, setRecipients, setRecipients, setReplyTo, setSender, setSentDate, setSubject, setSubject, setText, setText, setText, updateHeaders
 
Methods inherited from class javax.mail.Message
addRecipient, getFolder, getMessageNumber, isExpunged, match, setExpunged, setFlag, setMessageNumber, setRecipient
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

source

protected MimeMessageSource source
Can provide an input stream to the data


messageParsed

protected boolean messageParsed
This is false until we parse the message


headersModified

protected boolean headersModified
This is false until we parse the message


bodyModified

protected boolean bodyModified
This is false until we parse the message

Constructor Detail

MimeMessageWrapper

public MimeMessageWrapper(javax.mail.Session session,
                          MimeMessageSource source)
                   throws javax.mail.MessagingException
A constructor that instantiates a MimeMessageWrapper based on a MimeMessageSource

Parameters:
source - the MimeMessageSource
Throws:
javax.mail.MessagingException

MimeMessageWrapper

public MimeMessageWrapper(MimeMessageSource source)
                   throws javax.mail.MessagingException
A constructor that instantiates a MimeMessageWrapper based on a MimeMessageSource

Parameters:
source - the MimeMessageSource
Throws:
javax.mail.MessagingException
javax.mail.MessagingException

MimeMessageWrapper

public MimeMessageWrapper(javax.mail.internet.MimeMessage original)
                   throws javax.mail.MessagingException
Throws:
javax.mail.MessagingException
Method Detail

getSourceId

public java.lang.String getSourceId()
Returns the source ID of the MimeMessageSource that is supplying this with data.

See Also:
MimeMessageSource

loadHeaders

protected void loadHeaders()
                    throws javax.mail.MessagingException
Load the message headers from the internal source.

Throws:
javax.mail.MessagingException - if an error is encountered while loading the headers

loadMessage

protected void loadMessage()
                    throws javax.mail.MessagingException
Load the complete MimeMessage from the internal source.

Throws:
javax.mail.MessagingException - if an error is encountered while loading the message

isModified

public boolean isModified()
Get whether the message has been modified.

Returns:
whether the message has been modified

writeTo

public void writeTo(java.io.OutputStream os)
             throws java.io.IOException,
                    javax.mail.MessagingException
Rewritten for optimization purposes

Specified by:
writeTo in interface javax.mail.Part
Overrides:
writeTo in class javax.mail.internet.MimeMessage
Throws:
java.io.IOException
javax.mail.MessagingException

writeTo

public void writeTo(java.io.OutputStream os,
                    java.lang.String[] ignoreList)
             throws java.io.IOException,
                    javax.mail.MessagingException
Rewritten for optimization purposes

Overrides:
writeTo in class javax.mail.internet.MimeMessage
Throws:
java.io.IOException
javax.mail.MessagingException

writeTo

public void writeTo(java.io.OutputStream headerOs,
                    java.io.OutputStream bodyOs)
             throws java.io.IOException,
                    javax.mail.MessagingException
Write

Throws:
java.io.IOException
javax.mail.MessagingException

writeTo

public void writeTo(java.io.OutputStream headerOs,
                    java.io.OutputStream bodyOs,
                    java.lang.String[] ignoreList)
             throws java.io.IOException,
                    javax.mail.MessagingException
Throws:
java.io.IOException
javax.mail.MessagingException

getSize

public int getSize()
            throws javax.mail.MessagingException
This is the MimeMessage implementation - this should return ONLY the body, not the entire message (should not count headers). Will have to parse the message.

Specified by:
getSize in interface javax.mail.Part
Overrides:
getSize in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException

getLineCount

public int getLineCount()
                 throws javax.mail.MessagingException
Corrects JavaMail 1.1 version which always returns -1. Only corrected for content less than 5000 bytes, to avoid memory hogging.

Specified by:
getLineCount in interface javax.mail.Part
Overrides:
getLineCount in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException

getMessageSize

public long getMessageSize()
                    throws javax.mail.MessagingException
Returns size of message, ie headers and content

Throws:
javax.mail.MessagingException

getHeader

public java.lang.String[] getHeader(java.lang.String name)
                             throws javax.mail.MessagingException
We override all the "headers" access methods to be sure that we loaded the headers

Specified by:
getHeader in interface javax.mail.Part
Overrides:
getHeader in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException

getHeader

public java.lang.String getHeader(java.lang.String name,
                                  java.lang.String delimiter)
                           throws javax.mail.MessagingException
Specified by:
getHeader in interface javax.mail.internet.MimePart
Overrides:
getHeader in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException

getAllHeaders

public java.util.Enumeration getAllHeaders()
                                    throws javax.mail.MessagingException
Specified by:
getAllHeaders in interface javax.mail.Part
Overrides:
getAllHeaders in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException

getMatchingHeaders

public java.util.Enumeration getMatchingHeaders(java.lang.String[] names)
                                         throws javax.mail.MessagingException
Specified by:
getMatchingHeaders in interface javax.mail.Part
Overrides:
getMatchingHeaders in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException

getNonMatchingHeaders

public java.util.Enumeration getNonMatchingHeaders(java.lang.String[] names)
                                            throws javax.mail.MessagingException
Specified by:
getNonMatchingHeaders in interface javax.mail.Part
Overrides:
getNonMatchingHeaders in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException

getAllHeaderLines

public java.util.Enumeration getAllHeaderLines()
                                        throws javax.mail.MessagingException
Specified by:
getAllHeaderLines in interface javax.mail.internet.MimePart
Overrides:
getAllHeaderLines in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException

getMatchingHeaderLines

public java.util.Enumeration getMatchingHeaderLines(java.lang.String[] names)
                                             throws javax.mail.MessagingException
Specified by:
getMatchingHeaderLines in interface javax.mail.internet.MimePart
Overrides:
getMatchingHeaderLines in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException

getNonMatchingHeaderLines

public java.util.Enumeration getNonMatchingHeaderLines(java.lang.String[] names)
                                                throws javax.mail.MessagingException
Specified by:
getNonMatchingHeaderLines in interface javax.mail.internet.MimePart
Overrides:
getNonMatchingHeaderLines in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException

setHeader

public void setHeader(java.lang.String name,
                      java.lang.String value)
               throws javax.mail.MessagingException
Specified by:
setHeader in interface javax.mail.Part
Overrides:
setHeader in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException

addHeader

public void addHeader(java.lang.String name,
                      java.lang.String value)
               throws javax.mail.MessagingException
Specified by:
addHeader in interface javax.mail.Part
Overrides:
addHeader in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException

removeHeader

public void removeHeader(java.lang.String name)
                  throws javax.mail.MessagingException
Specified by:
removeHeader in interface javax.mail.Part
Overrides:
removeHeader in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException

addHeaderLine

public void addHeaderLine(java.lang.String line)
                   throws javax.mail.MessagingException
Specified by:
addHeaderLine in interface javax.mail.internet.MimePart
Overrides:
addHeaderLine in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException

setDataHandler

public void setDataHandler(javax.activation.DataHandler arg0)
                    throws javax.mail.MessagingException
The message is changed when working with headers and when altering the content. Every method that alter the content will fallback to this one.

Specified by:
setDataHandler in interface javax.mail.Part
Overrides:
setDataHandler in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException
See Also:
Part.setDataHandler(javax.activation.DataHandler)

dispose

public void dispose()
Specified by:
dispose in interface org.apache.avalon.framework.activity.Disposable
See Also:
Disposable.dispose()

parse

protected void parse(java.io.InputStream is)
              throws javax.mail.MessagingException
Overrides:
parse in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException
See Also:
MimeMessage.parse(java.io.InputStream)

createInternetHeaders

protected javax.mail.internet.InternetHeaders createInternetHeaders(java.io.InputStream is)
                                                             throws javax.mail.MessagingException
If we already parsed the headers then we simply return the updated ones. Otherwise we parse

Overrides:
createInternetHeaders in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException
See Also:
MimeMessage.createInternetHeaders(java.io.InputStream)

getContentStream

protected java.io.InputStream getContentStream()
                                        throws javax.mail.MessagingException
Overrides:
getContentStream in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException
See Also:
MimeMessage.getContentStream()

getRawInputStream

public java.io.InputStream getRawInputStream()
                                      throws javax.mail.MessagingException
Overrides:
getRawInputStream in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException
See Also:
MimeMessage.getRawInputStream()

updateMessageID

protected void updateMessageID()
                        throws javax.mail.MessagingException

Overrides standard implementation to ensure JavaMail works appropriately for an email server. Note that MessageID now needs to be explicitly set on different cloned instances.

See JAMES-875

Overrides:
updateMessageID in class javax.mail.internet.MimeMessage
Throws:
javax.mail.MessagingException
See Also:
MimeMessage.updateMessageID()


Copyright ? 2002-2009 The Apache Software Foundation. All Rights Reserved.