/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

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