org.apache.james.core
Class MailImpl

java.lang.Object
  extended by org.apache.james.core.MailImpl
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, org.apache.avalon.framework.activity.Disposable, Mail

public class MailImpl
extends java.lang.Object
implements org.apache.avalon.framework.activity.Disposable, Mail

Wraps a MimeMessage adding routing information (from SMTP) and some simple API enhancements.

From James version > 2.2.0a8 "mail attributes" have been added. Backward and forward compatibility is supported: messages stored in file repositories without attributes by James version <= 2.2.0a8 will be processed by later versions as having an empty attributes hashmap; messages stored in file repositories with attributes by James version > 2.2.0a8 will be processed by previous versions, ignoring the attributes.

Version:
CVS $Revision: 442571 $ $Date: 2006-09-12 12:38:57 +0000 (mar, 12 set 2006) $
See Also:
Serialized Form

Field Summary
static long serialVersionUID
          We hardcode the serialVersionUID so that from James 1.2 on, MailImpl will be deserializable (so your mail doesn't get lost)
 
Fields inherited from interface org.apache.mailet.Mail
DEFAULT, ERROR, GHOST, TRANSPORT
 
Constructor Summary
MailImpl()
          A constructor that creates a new, uninitialized MailImpl
MailImpl(Mail mail, java.lang.String newName)
           
MailImpl(javax.mail.internet.MimeMessage message)
          A constructor which will attempt to obtain sender and recipients from the headers of the MimeMessage supplied.
MailImpl(java.lang.String name, MailAddress sender, java.util.Collection recipients)
          A constructor that creates a MailImpl with the specified name, sender, and recipients.
MailImpl(java.lang.String name, MailAddress sender, java.util.Collection recipients, java.io.InputStream messageIn)
          A constructor that creates a MailImpl with the specified name, sender, recipients, and message data.
MailImpl(java.lang.String name, MailAddress sender, java.util.Collection recipients, javax.mail.internet.MimeMessage message)
          A constructor that creates a MailImpl with the specified name, sender, recipients, and MimeMessage.
 
Method Summary
 void dispose()
           
 Mail duplicate()
          Duplicate the MailImpl.
 Mail duplicate(java.lang.String newName)
          Duplicate the MailImpl, replacing the mail name with the one passed in as an argument.
 java.io.Serializable getAttribute(java.lang.String key)
          Returns the Mail session attribute with the given name, or null if there is no attribute by that name.
 java.util.Iterator getAttributeNames()
          Returns an Iterator containing the attribute names currently available within this Mail instance.
 java.util.HashMap getAttributesRaw()
          This method is necessary, when Mail repositories needs to deal explicitly with storing Mail attributes as a Serializable Note: This method is not exposed in the Mail interface, it is for internal use by James only.
 java.lang.String getErrorMessage()
          Get the error message associated with this MailImpl.
 java.util.Date getLastUpdated()
          Get the last updated time for this MailImpl.
 javax.mail.internet.MimeMessage getMessage()
          Get the MimeMessage associated with this MailImpl.
 long getMessageSize()
          Return the size of the message including its headers.
 java.lang.String getName()
          Get the name of this MailImpl.
 java.util.Collection getRecipients()
          Get the recipients of this MailImpl.
 java.lang.String getRemoteAddr()
          Get the remote address associated with this MailImpl.
 java.lang.String getRemoteHost()
          Get the remote host associated with this MailImpl.
 MailAddress getSender()
          Get the sender of this MailImpl.
 java.lang.String getState()
          Get the state of this MailImpl.
 boolean hasAttributes()
           
 void removeAllAttributes()
          Removes all the attributes associated with this Mail instance.
 java.io.Serializable removeAttribute(java.lang.String key)
          Removes the attribute with the given name from this Mail instance.
 java.io.Serializable setAttribute(java.lang.String key, java.io.Serializable object)
          Binds an object to a given attribute name in this Mail instance.
 void setAttributesRaw(java.util.HashMap attr)
          This method is necessary, when Mail repositories needs to deal explicitly with retriving Mail attributes as a Serializable Note: This method is not exposed in the Mail interface, it is for internal use by James only.
 void setErrorMessage(java.lang.String msg)
          Set the error message associated with this MailImpl.
 void setLastUpdated(java.util.Date lastUpdated)
          Set the date this mail was last updated.
 void setMessage(javax.mail.internet.MimeMessage message)
          Set the MimeMessage associated with this MailImpl.
 void setName(java.lang.String name)
          Set the name of this MailImpl.
 void setRecipients(java.util.Collection recipients)
          Set the recipients for this MailImpl.
 void setRemoteAddr(java.lang.String remoteAddr)
          Set the remote address associated with this MailImpl.
 void setRemoteHost(java.lang.String remoteHost)
          Set the remote address associated with this MailImpl.
 void setSender(MailAddress sender)
          Set the sender of this MailImpl.
 void setState(java.lang.String state)
          Set the state of this MailImpl.
 void writeMessageTo(java.io.OutputStream out)
          Writes the message out to an OutputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

public static final long serialVersionUID
We hardcode the serialVersionUID so that from James 1.2 on, MailImpl will be deserializable (so your mail doesn't get lost)

See Also:
Constant Field Values
Constructor Detail

MailImpl

public MailImpl()
A constructor that creates a new, uninitialized MailImpl


MailImpl

public MailImpl(java.lang.String name,
                MailAddress sender,
                java.util.Collection recipients)
A constructor that creates a MailImpl with the specified name, sender, and recipients.

Parameters:
name - the name of the MailImpl
sender - the sender for this MailImpl
recipients - the collection of recipients of this MailImpl

MailImpl

public MailImpl(Mail mail,
                java.lang.String newName)
         throws javax.mail.MessagingException
Parameters:
mail -
newName -
Throws:
javax.mail.MessagingException

MailImpl

public MailImpl(java.lang.String name,
                MailAddress sender,
                java.util.Collection recipients,
                java.io.InputStream messageIn)
         throws javax.mail.MessagingException
A constructor that creates a MailImpl with the specified name, sender, recipients, and message data.

Parameters:
name - the name of the MailImpl
sender - the sender for this MailImpl
recipients - the collection of recipients of this MailImpl
messageIn - a stream containing the message source
Throws:
javax.mail.MessagingException

MailImpl

public MailImpl(java.lang.String name,
                MailAddress sender,
                java.util.Collection recipients,
                javax.mail.internet.MimeMessage message)
         throws javax.mail.MessagingException
A constructor that creates a MailImpl with the specified name, sender, recipients, and MimeMessage.

Parameters:
name - the name of the MailImpl
sender - the sender for this MailImpl
recipients - the collection of recipients of this MailImpl
message - the MimeMessage associated with this MailImpl
Throws:
javax.mail.MessagingException

MailImpl

public MailImpl(javax.mail.internet.MimeMessage message)
         throws javax.mail.MessagingException
A constructor which will attempt to obtain sender and recipients from the headers of the MimeMessage supplied.

Parameters:
message - - a MimeMessage from which to construct a Mail
Throws:
javax.mail.MessagingException
Method Detail

duplicate

public Mail duplicate()
Duplicate the MailImpl.

Returns:
a MailImpl that is a duplicate of this one

duplicate

public Mail duplicate(java.lang.String newName)
Duplicate the MailImpl, replacing the mail name with the one passed in as an argument.

Parameters:
newName - the name for the duplicated mail
Returns:
a MailImpl that is a duplicate of this one with a different name

getErrorMessage

public java.lang.String getErrorMessage()
Get the error message associated with this MailImpl.

Specified by:
getErrorMessage in interface Mail
Returns:
the error message associated with this MailImpl

getMessage

public javax.mail.internet.MimeMessage getMessage()
                                           throws javax.mail.MessagingException
Get the MimeMessage associated with this MailImpl.

Specified by:
getMessage in interface Mail
Returns:
the MimeMessage associated with this MailImpl
Throws:
javax.mail.MessagingException - - an error occured while loading this object

setName

public void setName(java.lang.String name)
Set the name of this MailImpl.

Specified by:
setName in interface Mail
Parameters:
name - the name of this MailImpl

getName

public java.lang.String getName()
Get the name of this MailImpl.

Specified by:
getName in interface Mail
Returns:
the name of this MailImpl

getRecipients

public java.util.Collection getRecipients()
Get the recipients of this MailImpl.

Specified by:
getRecipients in interface Mail
Returns:
the recipients of this MailImpl

getSender

public MailAddress getSender()
Get the sender of this MailImpl.

Specified by:
getSender in interface Mail
Returns:
the sender of this MailImpl

getState

public java.lang.String getState()
Get the state of this MailImpl.

Specified by:
getState in interface Mail
Returns:
the state of this MailImpl

getRemoteHost

public java.lang.String getRemoteHost()
Get the remote host associated with this MailImpl.

Specified by:
getRemoteHost in interface Mail
Returns:
the remote host associated with this MailImpl

getRemoteAddr

public java.lang.String getRemoteAddr()
Get the remote address associated with this MailImpl.

Specified by:
getRemoteAddr in interface Mail
Returns:
the remote address associated with this MailImpl

getLastUpdated

public java.util.Date getLastUpdated()
Get the last updated time for this MailImpl.

Specified by:
getLastUpdated in interface Mail
Returns:
the last updated time for this MailImpl

getMessageSize

public long getMessageSize()
                    throws javax.mail.MessagingException

Return the size of the message including its headers. MimeMessage.getSize() method only returns the size of the message body.

Note: this size is not guaranteed to be accurate - see Sun's documentation of MimeMessage.getSize().

Specified by:
getMessageSize in interface Mail
Returns:
approximate size of full message including headers.
Throws:
javax.mail.MessagingException - if a problem occurs while computing the message size

setErrorMessage

public void setErrorMessage(java.lang.String msg)
Set the error message associated with this MailImpl.

Specified by:
setErrorMessage in interface Mail
Parameters:
msg - the new error message associated with this MailImpl

setMessage

public void setMessage(javax.mail.internet.MimeMessage message)
Set the MimeMessage associated with this MailImpl.

Specified by:
setMessage in interface Mail
Parameters:
message - the new MimeMessage associated with this MailImpl

setRecipients

public void setRecipients(java.util.Collection recipients)
Set the recipients for this MailImpl.

Specified by:
setRecipients in interface Mail
Parameters:
recipients - the recipients for this MailImpl

setSender

public void setSender(MailAddress sender)
Set the sender of this MailImpl.

Parameters:
sender - the sender of this MailImpl

setState

public void setState(java.lang.String state)
Set the state of this MailImpl.

Specified by:
setState in interface Mail
Parameters:
state - the state of this MailImpl

setRemoteHost

public void setRemoteHost(java.lang.String remoteHost)
Set the remote address associated with this MailImpl.

Parameters:
remoteHost - the new remote host associated with this MailImpl

setRemoteAddr

public void setRemoteAddr(java.lang.String remoteAddr)
Set the remote address associated with this MailImpl.

Parameters:
remoteAddr - the new remote address associated with this MailImpl

setLastUpdated

public void setLastUpdated(java.util.Date lastUpdated)
Set the date this mail was last updated.

Specified by:
setLastUpdated in interface Mail
Parameters:
lastUpdated - the date the mail was last updated

writeMessageTo

public void writeMessageTo(java.io.OutputStream out)
                    throws java.io.IOException,
                           javax.mail.MessagingException
Writes the message out to an OutputStream.

Parameters:
out - the OutputStream to which to write the content
Throws:
javax.mail.MessagingException - if the MimeMessage is not set for this MailImpl
java.io.IOException - if an error occurs while reading or writing from the stream

dispose

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

getAttributesRaw

public java.util.HashMap getAttributesRaw()
This method is necessary, when Mail repositories needs to deal explicitly with storing Mail attributes as a Serializable Note: This method is not exposed in the Mail interface, it is for internal use by James only.

Returns:
Serializable of the entire attributes collection
Since:
2.2.0

setAttributesRaw

public void setAttributesRaw(java.util.HashMap attr)
This method is necessary, when Mail repositories needs to deal explicitly with retriving Mail attributes as a Serializable Note: This method is not exposed in the Mail interface, it is for internal use by James only.

Since:
2.2.0

getAttribute

public java.io.Serializable getAttribute(java.lang.String key)
Description copied from interface: Mail
Returns the Mail session attribute with the given name, or null if there is no attribute by that name. An attribute allows a mailet to give this Mail instance additional information not already provided by this interface.

A list of currently set attributes can be retrieved using getAttributeNames.

The attribute is returned as a java.lang.Object or some subclass. Attribute names should follow the same convention as package names. The Mailet API specification reserves names matching org.apache.james.* and org.apache.mailet.*.

Specified by:
getAttribute in interface Mail
Parameters:
key - - a String specifying the name of the attribute
Returns:
an Object containing the value of the attribute, or null if no attribute exists matching the given name
Since:
2.2.0
See Also:
Mail.getAttribute(String)

setAttribute

public java.io.Serializable setAttribute(java.lang.String key,
                                         java.io.Serializable object)
Description copied from interface: Mail
Binds an object to a given attribute name in this Mail instance. If the name specified is already used for an attribute, this method will remove the old attribute and bind the name to the new attribute. As instances of Mail is Serializable, it is necessary that the attributes being Serializable as well

Attribute names should follow the same convention as package names. The Mailet API specification reserves names matching org.apache.james.* and org.apache.mailet.*.

Specified by:
setAttribute in interface Mail
Parameters:
key - - a String specifying the name of the attribute
object - - a Serializable Object representing the attribute to be bound
Returns:
the object previously bound to the name, null if the name was not bound (null can also mean that null was bound to the name)
Since:
2.2.0
See Also:
Mail.setAttribute(String,Serializable)

removeAttribute

public java.io.Serializable removeAttribute(java.lang.String key)
Description copied from interface: Mail
Removes the attribute with the given name from this Mail instance. After removal, subsequent calls to getAttribute(java.lang.String) to retrieve the attribute's value will return null.

Specified by:
removeAttribute in interface Mail
Parameters:
key - - a String specifying the name of the attribute to be removed
Returns:
previous attribute value associated with specified name, or null if there was no mapping for name (null can also mean that null was bound to the name)
Since:
2.2.0
See Also:
Mail.removeAttribute(String)

removeAllAttributes

public void removeAllAttributes()
Description copied from interface: Mail
Removes all the attributes associated with this Mail instance.

Specified by:
removeAllAttributes in interface Mail
Since:
2.2.0
See Also:
Mail.removeAllAttributes()

getAttributeNames

public java.util.Iterator getAttributeNames()
Description copied from interface: Mail
Returns an Iterator containing the attribute names currently available within this Mail instance. Use the getAttribute(java.lang.String) method with an attribute name to get the value of an attribute.

Specified by:
getAttributeNames in interface Mail
Returns:
an Iterator of attribute names
Since:
2.2.0
See Also:
Mail.getAttributeNames()

hasAttributes

public boolean hasAttributes()
Specified by:
hasAttributes in interface Mail
Returns:
true if this Mail instance has any attributes set.
Since:
2.2.0
See Also:
Mail.hasAttributes()


Copyright © 2002-2007 The Apache Software Foundation. All Rights Reserved.