Fields Summary |
---|
public static final long | serialVersionUIDWe hardcode the serialVersionUID so that from James 1.2 on,
MailImpl will be deserializable (so your mail doesn't get lost) |
private String | errorMessageThe error message, if any, associated with this mail. |
private String | stateThe state of this mail, which determines how it is processed. |
private MimeMessage | messageThe MimeMessage that holds the mail data. |
private org.apache.mailet.MailAddress | senderThe sender of this mail. |
private Collection | recipientsThe collection of recipients to whom this mail was sent. |
private String | nameThe identifier for this mail message |
private String | remoteHostThe remote host from which this mail was sent. |
private String | remoteAddrThe remote address from which this mail was sent. |
private Date | lastUpdatedThe last time this message was updated. |
private HashMap | attributesAttributes added to this MailImpl instance |
Constructors Summary |
---|
public MailImpl()A constructor that creates a new, uninitialized MailImpl
setState(Mail.DEFAULT);
attributes = new HashMap();
|
public MailImpl(String name, org.apache.mailet.MailAddress sender, Collection recipients)A constructor that creates a MailImpl with the specified name,
sender, and recipients.
this();
this.name = name;
this.sender = sender;
this.recipients = null;
// Copy the recipient list
if (recipients != null) {
Iterator theIterator = recipients.iterator();
this.recipients = new ArrayList();
while (theIterator.hasNext()) {
this.recipients.add(theIterator.next());
}
}
|
public MailImpl(org.apache.mailet.Mail mail, String newName)
this(newName, mail.getSender(), mail.getRecipients(), mail.getMessage());
setRemoteHost(mail.getRemoteHost());
setRemoteAddr(mail.getRemoteAddr());
setLastUpdated(mail.getLastUpdated());
try {
if (mail instanceof MailImpl) {
setAttributesRaw((HashMap) cloneSerializableObject(((MailImpl) mail).getAttributesRaw()));
} else {
HashMap attribs = new HashMap();
for (Iterator i = mail.getAttributeNames(); i.hasNext(); ) {
String hashKey = (String) i.next();
attribs.put(hashKey,cloneSerializableObject(mail.getAttribute(hashKey)));
}
setAttributesRaw(attribs);
}
} catch (IOException e) {
// should never happen for in memory streams
setAttributesRaw(new HashMap());
} catch (ClassNotFoundException e) {
// should never happen as we just serialized it
setAttributesRaw(new HashMap());
}
|
public MailImpl(String name, org.apache.mailet.MailAddress sender, Collection recipients, InputStream messageIn)A constructor that creates a MailImpl with the specified name,
sender, recipients, and message data.
this(name, sender, recipients);
MimeMessageSource source = new MimeMessageInputStreamSource(name, messageIn);
this.setMessage(new MimeMessageCopyOnWriteProxy(source));
|
public MailImpl(String name, org.apache.mailet.MailAddress sender, Collection recipients, MimeMessage message)A constructor that creates a MailImpl with the specified name,
sender, recipients, and MimeMessage.
this(name, sender, recipients);
this.setMessage(new MimeMessageCopyOnWriteProxy(message));
|
public MailImpl(MimeMessage message)A constructor which will attempt to obtain sender and recipients from the headers of the MimeMessage supplied.
this();
MailAddress sender = getReturnPath(message);
Collection recipients = null;
Address[] addresses = message.getRecipients(MimeMessage.RecipientType.TO);
if (addresses != null) {
recipients = new ArrayList();
for (int i = 0; i < addresses.length; i++) {
try {
recipients.add(new MailAddress(new InternetAddress(addresses[i].toString(), false)));
} catch (ParseException pe) {
// RFC 2822 section 3.4 allows To: fields without <>
// Let's give this one more try with <>.
try {
recipients.add(new MailAddress("<" + new InternetAddress(addresses[i].toString()).toString() + ">"));
} catch (ParseException _) {
throw new MessagingException("Could not parse address: " + addresses[i].toString() + " from " + message.getHeader(RFC2822Headers.TO, ", "), pe);
}
}
}
}
this.name = message.toString();
this.sender = sender;
this.recipients = recipients;
this.setMessage(message);
|
Methods Summary |
---|
private static java.lang.Object | cloneSerializableObject(java.lang.Object o)This methods provide cloning for serializable objects.
Mail Attributes are Serializable but not Clonable so we need a deep copy
ByteArrayOutputStream b = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(b);
out.writeObject(o);
out.flush();
out.close();
ByteArrayInputStream bi=new ByteArrayInputStream(b.toByteArray());
ObjectInputStream in = new ObjectInputStream(bi);
Object no = in.readObject();
return no;
|
public void | dispose()
ContainerUtil.dispose(message);
message = null;
|
public org.apache.mailet.Mail | duplicate()Duplicate the MailImpl.
return duplicate(name);
|
public org.apache.mailet.Mail | duplicate(java.lang.String newName)Duplicate the MailImpl, replacing the mail name with the one
passed in as an argument.
try {
return new MailImpl(this, newName);
} catch (MessagingException me) {
// Ignored. Return null in the case of an error.
}
return null;
|
public java.io.Serializable | getAttribute(java.lang.String key)
return (Serializable)attributes.get(key);
|
public java.util.Iterator | getAttributeNames()
return attributes.keySet().iterator();
|
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.
return attributes;
|
public java.lang.String | getErrorMessage()Get the error message associated with this MailImpl.
return errorMessage;
|
public java.util.Date | getLastUpdated()Get the last updated time for this MailImpl.
return lastUpdated;
|
public javax.mail.internet.MimeMessage | getMessage()Get the MimeMessage associated with this MailImpl.
return message;
|
public long | getMessageSize()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().
return MimeMessageUtil.getMessageSize(message);
|
public java.lang.String | getName()Get the name of this MailImpl.
return name;
|
public java.util.Collection | getRecipients()Get the recipients of this MailImpl.
return recipients;
|
public java.lang.String | getRemoteAddr()Get the remote address associated with this MailImpl.
return remoteAddr;
|
public java.lang.String | getRemoteHost()Get the remote host associated with this MailImpl.
return remoteHost;
|
private org.apache.mailet.MailAddress | getReturnPath(javax.mail.internet.MimeMessage message)Gets the MailAddress corresponding to the existing "Return-Path" of
message.
If missing or empty returns null ,
MailAddress mailAddress = null;
String[] returnPathHeaders = message.getHeader(RFC2822Headers.RETURN_PATH);
String returnPathHeader = null;
if (returnPathHeaders != null) {
returnPathHeader = returnPathHeaders[0];
if (returnPathHeader != null) {
returnPathHeader = returnPathHeader.trim();
if (!returnPathHeader.equals("<>")) {
try {
mailAddress = new MailAddress(new InternetAddress(returnPathHeader, false));
} catch (ParseException pe) {
throw new MessagingException("Could not parse address: " + returnPathHeader + " from " + message.getHeader(RFC2822Headers.RETURN_PATH, ", "), pe);
}
}
}
}
return mailAddress;
|
public org.apache.mailet.MailAddress | getSender()Get the sender of this MailImpl.
return sender;
|
public java.lang.String | getState()Get the state of this MailImpl.
return state;
|
public boolean | hasAttributes()
return !attributes.isEmpty();
|
private void | readObject(java.io.ObjectInputStream in)Read the MailImpl from an ObjectInputStream .
try {
Object obj = in.readObject();
if (obj == null) {
sender = null;
} else if (obj instanceof String) {
sender = new MailAddress((String) obj);
} else if (obj instanceof MailAddress) {
sender = (MailAddress) obj;
}
} catch (ParseException pe) {
throw new IOException("Error parsing sender address: " + pe.getMessage());
}
recipients = (Collection) in.readObject();
state = (String) in.readObject();
errorMessage = (String) in.readObject();
name = (String) in.readObject();
remoteHost = (String) in.readObject();
remoteAddr = (String) in.readObject();
setLastUpdated((Date) in.readObject());
// the following is under try/catch to be backwards compatible
// with messages created with James version <= 2.2.0a8
try {
attributes = (HashMap) in.readObject();
} catch (OptionalDataException ode) {
if (ode.eof) {
attributes = new HashMap();
} else {
throw ode;
}
}
|
public void | removeAllAttributes()
attributes.clear();
|
public java.io.Serializable | removeAttribute(java.lang.String key)
return (Serializable)attributes.remove(key);
|
public java.io.Serializable | setAttribute(java.lang.String key, java.io.Serializable object)
return (Serializable)attributes.put(key, object);
|
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.
this.attributes = (attr == null) ? new HashMap() : attr;
|
public void | setErrorMessage(java.lang.String msg)Set the error message associated with this MailImpl.
this.errorMessage = msg;
|
public void | setLastUpdated(java.util.Date lastUpdated)Set the date this mail was last updated.
// Make a defensive copy to ensure that the date
// doesn't get changed external to the class
if (lastUpdated != null) {
lastUpdated = new Date(lastUpdated.getTime());
}
this.lastUpdated = lastUpdated;
|
public void | setMessage(javax.mail.internet.MimeMessage message)Set the MimeMessage associated with this MailImpl.
if (this.message != message) {
// If a setMessage is called on a Mail that already have a message
// (discouraged) we have to make sure that the message we remove is
// correctly unreferenced and disposed, otherwise it will keep locks
if (this.message != null) {
ContainerUtil.dispose(this.message);
}
this.message = message;
}
|
public void | setName(java.lang.String name)Set the name of this MailImpl.
this.name = name;
|
public void | setRecipients(java.util.Collection recipients)Set the recipients for this MailImpl.
this.recipients = recipients;
|
public void | setRemoteAddr(java.lang.String remoteAddr)Set the remote address associated with this MailImpl.
this.remoteAddr = remoteAddr;
|
public void | setRemoteHost(java.lang.String remoteHost)Set the remote address associated with this MailImpl.
this.remoteHost = remoteHost;
|
public void | setSender(org.apache.mailet.MailAddress sender)Set the sender of this MailImpl.
this.sender = sender;
|
public void | setState(java.lang.String state)Set the state of this MailImpl.
this.state = state;
|
public void | writeMessageTo(java.io.OutputStream out)Writes the message out to an OutputStream.
if (message != null) {
message.writeTo(out);
} else {
throw new MessagingException("No message set for this MailImpl.");
}
|
private void | writeObject(java.io.ObjectOutputStream out)Write the MailImpl to an ObjectOutputStream .
out.writeObject(sender);
out.writeObject(recipients);
out.writeObject(state);
out.writeObject(errorMessage);
out.writeObject(name);
out.writeObject(remoteHost);
out.writeObject(remoteAddr);
out.writeObject(lastUpdated);
out.writeObject(attributes);
|