PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  1package com.google.sitebricks.mail.imap;
  2
  3import com.google.common.base.Supplier;
  4import com.google.common.collect.*;
  5
  6import java.util.ArrayList;
  7import java.util.Collection;
  8import java.util.List;
  9
 10/**
 11 * Represents a complete IMAP message with all body parts materialized
 12 * and decoded as appropriate (for example, non-UTF8 encodings are re-encoded
 13 * into UTF8 for raw and rich text).
 14 *
 15 * @author dhanji@gmail.com (Dhanji R. Prasanna)
 16 */
 17public class Message implements HasBodyParts {
 18  public static final Message ERROR = new Message();
 19  public static final Message EMPTIED = new Message();
 20
 21  private MessageStatus status;
 22  private int imapUid;
 23
 24  // A header can have multiple, different values.
 25  private Multimap<String, String> headers = newListMultimap();
 26  private List<BodyPart> bodyParts = new ArrayList<BodyPart>();
 27
 28  public void setImapUid(int imapUid) {
 29    this.imapUid = imapUid;
 30  }
 31
 32  public int getImapUid() {
 33    return imapUid;
 34  }
 35
 36  public void setHeaders(Multimap<String, String> headers) {
 37    this.headers = headers;
 38  }
 39
 40  public MessageStatus getStatus() {
 41    return status;
 42  }
 43
 44  public void setStatus(MessageStatus status) {
 45    this.status = status;
 46  }
 47  public Multimap<String, String> getHeaders() {
 48    return headers;
 49  }
 50
 51  public List<BodyPart> getBodyParts() {
 52    return bodyParts;
 53  }
 54
 55  @Override public void createBodyParts() { /* Noop */ }
 56
 57  // Short hand.
 58  @Override public void setBody(String body) {
 59    assert bodyParts.isEmpty() : "Unexpected set body call to a multipart email";
 60    bodyParts.add(new BodyPart(body));
 61  }
 62
 63  // http://jira.codehaus.org/browse/JACKSON-739, can't have methods of same name.
 64  @Override public void setBodyBytes(byte[] body) {
 65    assert bodyParts.isEmpty() : "Unexpected set body call to a multipart email";
 66    bodyParts.add(new BodyPart(body));
 67  }
 68
 69  public static class BodyPart implements HasBodyParts {
 70    private Multimap<String, String> headers = newListMultimap();
 71
 72    // This field is set for HTML or text emails. and is mutually exclusive with binBody.
 73    private String body;
 74
 75    // This field is set for all binary attachment and body types.
 76    private byte[] binBody;
 77
 78    private List<BodyPart> bodyParts;
 79
 80    public BodyPart(String body) {
 81      this.body = body;
 82    }
 83
 84    public BodyPart() {
 85    }
 86
 87    public BodyPart(byte[] body) {
 88      this.binBody = body;
 89    }
 90
 91    public List<BodyPart> getBodyParts() {
 92      return bodyParts;
 93    }
 94
 95    @Override public void createBodyParts() {
 96      if (null == bodyParts)
 97        bodyParts = Lists.newArrayList();
 98    }
 99
100    public Multimap<String, String> getHeaders() {
101      return headers;
102    }
103
104    public String getBody() {
105      return body;
106    }
107
108    public void setBody(String body) {
109      this.body = body;
110    }
111
112    public byte[] getBinBody() {
113      return binBody;
114    }
115
116    public void setBodyBytes(byte[] binBody) {
117      this.binBody = binBody;
118    }
119  }
120
121  private static ListMultimap<String, String> newListMultimap() {
122    return Multimaps.newListMultimap(
123        Maps.<String, Collection<String>>newLinkedHashMap(), new Supplier<List<String>>() {
124      @Override public List<String> get() {
125        return Lists.newArrayList();
126      }
127    });
128  }
129}