com.google.sitebricks.mail.imap.Message Maven / Gradle / Ivy
The newest version!
package com.google.sitebricks.mail.imap;
import com.google.common.base.Supplier;
import com.google.common.collect.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Represents a complete IMAP message with all body parts materialized
* and decoded as appropriate (for example, non-UTF8 encodings are re-encoded
* into UTF8 for raw and rich text).
*
* @author [email protected] (Dhanji R. Prasanna)
*/
public class Message implements HasBodyParts {
public static final Message ERROR = new Message();
public static final Message EMPTIED = new Message();
private MessageStatus status;
private int imapUid;
// A header can have multiple, different values.
private Multimap headers = newListMultimap();
private List bodyParts = new ArrayList();
public void setImapUid(int imapUid) {
this.imapUid = imapUid;
}
public int getImapUid() {
return imapUid;
}
public void setHeaders(Multimap headers) {
this.headers = headers;
}
public MessageStatus getStatus() {
return status;
}
public void setStatus(MessageStatus status) {
this.status = status;
}
public Multimap getHeaders() {
return headers;
}
public List getBodyParts() {
return bodyParts;
}
@Override public void createBodyParts() { /* Noop */ }
// Short hand.
@Override public void setBody(String body) {
assert bodyParts.isEmpty() : "Unexpected set body call to a multipart email";
bodyParts.add(new BodyPart(body));
}
// http://jira.codehaus.org/browse/JACKSON-739, can't have methods of same name.
@Override public void setBodyBytes(byte[] body) {
assert bodyParts.isEmpty() : "Unexpected set body call to a multipart email";
bodyParts.add(new BodyPart(body));
}
public static class BodyPart implements HasBodyParts {
private Multimap headers = newListMultimap();
// This field is set for HTML or text emails. and is mutually exclusive with binBody.
private String body;
// This field is set for all binary attachment and body types.
private byte[] binBody;
private List bodyParts;
public BodyPart(String body) {
this.body = body;
}
public BodyPart() {
}
public BodyPart(byte[] body) {
this.binBody = body;
}
public List getBodyParts() {
return bodyParts;
}
@Override public void createBodyParts() {
if (null == bodyParts)
bodyParts = Lists.newArrayList();
}
public Multimap getHeaders() {
return headers;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public byte[] getBinBody() {
return binBody;
}
public void setBodyBytes(byte[] binBody) {
this.binBody = binBody;
}
}
private static ListMultimap newListMultimap() {
return Multimaps.newListMultimap(
Maps.>newLinkedHashMap(), new Supplier>() {
@Override public List get() {
return Lists.newArrayList();
}
});
}
}