/sitebricks-mail/src/main/java/com/google/sitebricks/mail/imap/Message.java
http://github.com/dhanji/sitebricks · Java · 129 lines · 88 code · 29 blank · 12 comment · 2 complexity · b40b32b8e09a581b3cb8f07b286dc4e8 MD5 · raw file
- 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 dhanji@gmail.com (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<String, String> headers = newListMultimap();
- private List<BodyPart> bodyParts = new ArrayList<BodyPart>();
- public void setImapUid(int imapUid) {
- this.imapUid = imapUid;
- }
- public int getImapUid() {
- return imapUid;
- }
- public void setHeaders(Multimap<String, String> headers) {
- this.headers = headers;
- }
- public MessageStatus getStatus() {
- return status;
- }
- public void setStatus(MessageStatus status) {
- this.status = status;
- }
- public Multimap<String, String> getHeaders() {
- return headers;
- }
- public List<BodyPart> 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<String, String> 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<BodyPart> bodyParts;
- public BodyPart(String body) {
- this.body = body;
- }
- public BodyPart() {
- }
- public BodyPart(byte[] body) {
- this.binBody = body;
- }
- public List<BodyPart> getBodyParts() {
- return bodyParts;
- }
- @Override public void createBodyParts() {
- if (null == bodyParts)
- bodyParts = Lists.newArrayList();
- }
- public Multimap<String, String> 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<String, String> newListMultimap() {
- return Multimaps.newListMultimap(
- Maps.<String, Collection<String>>newLinkedHashMap(), new Supplier<List<String>>() {
- @Override public List<String> get() {
- return Lists.newArrayList();
- }
- });
- }
- }