james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwiederk...@apache.org
Subject svn commit: r737839 - in /james/mime4j/trunk/src: main/java/org/apache/james/mime4j/message/Message.java test/java/org/apache/james/mime4j/message/MessageTest.java
Date Mon, 26 Jan 2009 21:03:07 GMT
Author: mwiederkehr
Date: Mon Jan 26 21:03:07 2009
New Revision: 737839

URL: http://svn.apache.org/viewvc?rev=737839&view=rev
Log:
MIME4J-100: getters and setters for address fields in class Message. 

Modified:
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Message.java
    james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/MessageTest.java

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Message.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Message.java?rev=737839&r1=737838&r2=737839&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Message.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Message.java Mon Jan
26 21:03:07 2009
@@ -21,15 +21,25 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.Date;
 import java.util.TimeZone;
 
 import org.apache.james.mime4j.MimeException;
 import org.apache.james.mime4j.MimeIOException;
+import org.apache.james.mime4j.field.AddressListField;
 import org.apache.james.mime4j.field.DateTimeField;
 import org.apache.james.mime4j.field.Field;
 import org.apache.james.mime4j.field.Fields;
+import org.apache.james.mime4j.field.MailboxField;
+import org.apache.james.mime4j.field.MailboxListField;
 import org.apache.james.mime4j.field.UnstructuredField;
+import org.apache.james.mime4j.field.address.Address;
+import org.apache.james.mime4j.field.address.AddressList;
+import org.apache.james.mime4j.field.address.Mailbox;
+import org.apache.james.mime4j.field.address.MailboxList;
 import org.apache.james.mime4j.parser.MimeEntityConfig;
 import org.apache.james.mime4j.parser.MimeStreamParser;
 import org.apache.james.mime4j.storage.DefaultStorageProvider;
@@ -143,7 +153,7 @@
      * A <code>Header</code> is created if this message does not already have
      * one.
      * 
-     * @param hostName
+     * @param hostname
      *            host name to be included in the identifier or
      *            <code>null</code> if no host name should be included.
      */
@@ -232,6 +242,168 @@
         }
     }
 
+    public Mailbox getSender() {
+        return getMailbox(Field.SENDER);
+    }
+
+    public void setSender(Mailbox sender) {
+        setMailbox(Field.SENDER, sender);
+    }
+
+    public MailboxList getFrom() {
+        return getMailboxList(Field.FROM);
+    }
+
+    public void setFrom(Mailbox from) {
+        setMailboxList(Field.FROM, from);
+    }
+
+    public void setFrom(Mailbox... from) {
+        setMailboxList(Field.FROM, from);
+    }
+
+    public void setFrom(Collection<Mailbox> from) {
+        setMailboxList(Field.FROM, from);
+    }
+
+    public AddressList getTo() {
+        return getAddressList(Field.TO);
+    }
+
+    public void setTo(Address to) {
+        setAddressList(Field.TO, to);
+    }
+
+    public void setTo(Address... to) {
+        setAddressList(Field.TO, to);
+    }
+
+    public void setTo(Collection<Address> to) {
+        setAddressList(Field.TO, to);
+    }
+
+    public AddressList getCc() {
+        return getAddressList(Field.CC);
+    }
+
+    public void setCc(Address cc) {
+        setAddressList(Field.CC, cc);
+    }
+
+    public void setCc(Address... cc) {
+        setAddressList(Field.CC, cc);
+    }
+
+    public void setCc(Collection<Address> cc) {
+        setAddressList(Field.CC, cc);
+    }
+
+    public AddressList getBcc() {
+        return getAddressList(Field.BCC);
+    }
+
+    public void setBcc(Address bcc) {
+        setAddressList(Field.BCC, bcc);
+    }
+
+    public void setBcc(Address... bcc) {
+        setAddressList(Field.BCC, bcc);
+    }
+
+    public void setBcc(Collection<Address> bcc) {
+        setAddressList(Field.BCC, bcc);
+    }
+
+    public AddressList getReplyTo() {
+        return getAddressList(Field.REPLY_TO);
+    }
+
+    public void setReplyTo(Address replyTo) {
+        setAddressList(Field.REPLY_TO, replyTo);
+    }
+
+    public void setReplyTo(Address... replyTo) {
+        setAddressList(Field.REPLY_TO, replyTo);
+    }
+
+    public void setReplyTo(Collection<Address> replyTo) {
+        setAddressList(Field.REPLY_TO, replyTo);
+    }
+
+    private Mailbox getMailbox(String fieldName) {
+        MailboxField field = obtainField(fieldName);
+        if (field == null)
+            return null;
+
+        return field.getMailbox();
+    }
+
+    private void setMailbox(String fieldName, Mailbox mailbox) {
+        Header header = obtainHeader();
+
+        if (mailbox == null) {
+            header.removeFields(fieldName);
+        } else {
+            header.setField(Fields.mailbox(fieldName, mailbox));
+        }
+    }
+
+    private MailboxList getMailboxList(String fieldName) {
+        MailboxListField field = obtainField(fieldName);
+        if (field == null)
+            return null;
+
+        return field.getMailboxList();
+    }
+
+    private void setMailboxList(String fieldName, Mailbox mailbox) {
+        setMailboxList(fieldName, mailbox == null ? null : Collections
+                .singleton(mailbox));
+    }
+
+    private void setMailboxList(String fieldName, Mailbox... mailboxes) {
+        setMailboxList(fieldName, mailboxes == null ? null : Arrays
+                .asList(mailboxes));
+    }
+
+    private void setMailboxList(String fieldName, Collection<Mailbox> mailboxes) {
+        Header header = obtainHeader();
+
+        if (mailboxes == null || mailboxes.isEmpty()) {
+            header.removeFields(fieldName);
+        } else {
+            header.setField(Fields.mailboxList(fieldName, mailboxes));
+        }
+    }
+
+    private AddressList getAddressList(String fieldName) {
+        AddressListField field = obtainField(fieldName);
+        if (field == null)
+            return null;
+
+        return field.getAddressList();
+    }
+
+    private void setAddressList(String fieldName, Address address) {
+        setAddressList(fieldName, address == null ? null : Collections
+                .singleton(address));
+    }
+
+    private void setAddressList(String fieldName, Address... addresses) {
+        setAddressList(fieldName, addresses == null ? null : Arrays
+                .asList(addresses));
+    }
+
+    private void setAddressList(String fieldName, Collection<Address> addresses) {
+        Header header = obtainHeader();
+
+        if (addresses == null || addresses.isEmpty()) {
+            header.removeFields(fieldName);
+        } else {
+            header.setField(Fields.addressList(fieldName, addresses));
+        }
+    }
+
     private <F extends Field> F obtainField(String fieldName) {
         Header header = getHeader();
         if (header == null)

Modified: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/MessageTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/MessageTest.java?rev=737839&r1=737838&r2=737839&view=diff
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/MessageTest.java (original)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/MessageTest.java Mon
Jan 26 21:03:07 2009
@@ -26,6 +26,7 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
+import java.util.Arrays;
 import java.util.Date;
 import java.util.List;
 import java.util.TimeZone;
@@ -34,6 +35,8 @@
 
 import org.apache.commons.io.IOUtils;
 import org.apache.james.mime4j.field.Field;
+import org.apache.james.mime4j.field.address.Group;
+import org.apache.james.mime4j.field.address.Mailbox;
 
 /**
  * 
@@ -204,11 +207,14 @@
 
     public void testSetSubject() throws Exception {
         Message m = new Message();
-        m.setSubject("Semmelbr\366sel");
 
+        m.setSubject("Semmelbr\366sel");
         assertEquals("Semmelbr\366sel", m.getSubject());
         assertEquals("=?ISO-8859-1?Q?Semmelbr=F6sel?=", m.getHeader().getField(
                 "Subject").getBody());
+
+        m.setSubject(null);
+        assertNull(m.getHeader().getField("Subject"));
     }
 
     public void testGetDate() throws Exception {
@@ -224,11 +230,221 @@
 
     public void testSetDate() throws Exception {
         Message m = new Message();
-        m.setDate(new Date(86400000), TimeZone.getTimeZone("GMT"));
 
+        m.setDate(new Date(86400000), TimeZone.getTimeZone("GMT"));
         assertEquals(new Date(86400000), m.getDate());
         assertEquals("Fri, 2 Jan 1970 00:00:00 +0000", m.getHeader().getField(
                 "Date").getBody());
+
+        m.setDate(null);
+        assertNull(m.getHeader().getField("Date"));
+    }
+
+    public void testGetSender() throws Exception {
+        Message m = new Message();
+        assertNull(m.getSender());
+
+        Header header = new Header();
+        header.setField(Field.parse("Sender", "john.doe@example.net"));
+        m.setHeader(header);
+
+        assertEquals("john.doe@example.net", m.getSender().getAddress());
+    }
+
+    public void testSetSender() throws Exception {
+        Message m = new Message();
+
+        m.setSender(Mailbox.parse("john.doe@example.net"));
+        assertEquals("john.doe@example.net", m.getHeader().getField("Sender")
+                .getBody());
+
+        m.setSender(null);
+        assertNull(m.getHeader().getField("Sender"));
+    }
+
+    public void testGetFrom() throws Exception {
+        Message m = new Message();
+        assertNull(m.getFrom());
+
+        Header header = new Header();
+        header.setField(Field.parse("From", "john.doe@example.net"));
+        m.setHeader(header);
+
+        assertEquals("john.doe@example.net", m.getFrom().get(0).getAddress());
+    }
+
+    public void testSetFrom() throws Exception {
+        Message m = new Message();
+
+        Mailbox mailbox1 = Mailbox.parse("john.doe@example.net");
+        Mailbox mailbox2 = Mailbox.parse("jane.doe@example.net");
+
+        m.setFrom(mailbox1);
+        assertEquals("john.doe@example.net", m.getHeader().getField("From")
+                .getBody());
+
+        m.setFrom(mailbox1, mailbox2);
+        assertEquals("john.doe@example.net, jane.doe@example.net", m
+                .getHeader().getField("From").getBody());
+
+        m.setFrom(Arrays.asList(mailbox1, mailbox2));
+        assertEquals("john.doe@example.net, jane.doe@example.net", m
+                .getHeader().getField("From").getBody());
+
+        m.setFrom((Mailbox) null);
+        assertNull(m.getHeader().getField("From"));
+    }
+
+    public void testGetTo() throws Exception {
+        Message m = new Message();
+        assertNull(m.getTo());
+
+        Header header = new Header();
+        header.setField(Field.parse("To", "john.doe@example.net"));
+        m.setHeader(header);
+
+        assertEquals("john.doe@example.net", ((Mailbox) m.getTo().get(0))
+                .getAddress());
+    }
+
+    public void testSetTo() throws Exception {
+        Message m = new Message();
+
+        Mailbox mailbox1 = Mailbox.parse("john.doe@example.net");
+        Mailbox mailbox2 = Mailbox.parse("jane.doe@example.net");
+        Group group = new Group("Does", mailbox1, mailbox2);
+        Mailbox mailbox3 = Mailbox.parse("Mary Smith <mary@example.net>");
+
+        m.setTo(group);
+        assertEquals("Does: john.doe@example.net, jane.doe@example.net;", m
+                .getHeader().getField("To").getBody());
+
+        m.setTo(group, mailbox3);
+        assertEquals("Does: john.doe@example.net, jane.doe@example.net;, "
+                + "Mary Smith <mary@example.net>", m.getHeader().getField("To")
+                .getBody());
+
+        m.setTo(Arrays.asList(group, mailbox3));
+        assertEquals("Does: john.doe@example.net, jane.doe@example.net;, "
+                + "Mary Smith <mary@example.net>", m.getHeader().getField("To")
+                .getBody());
+
+        m.setTo((Mailbox) null);
+        assertNull(m.getHeader().getField("To"));
+    }
+
+    public void testGetCc() throws Exception {
+        Message m = new Message();
+        assertNull(m.getCc());
+
+        Header header = new Header();
+        header.setField(Field.parse("Cc", "john.doe@example.net"));
+        m.setHeader(header);
+
+        assertEquals("john.doe@example.net", ((Mailbox) m.getCc().get(0))
+                .getAddress());
+    }
+
+    public void testSetCc() throws Exception {
+        Message m = new Message();
+
+        Mailbox mailbox1 = Mailbox.parse("john.doe@example.net");
+        Mailbox mailbox2 = Mailbox.parse("jane.doe@example.net");
+        Group group = new Group("Does", mailbox1, mailbox2);
+        Mailbox mailbox3 = Mailbox.parse("Mary Smith <mary@example.net>");
+
+        m.setCc(group);
+        assertEquals("Does: john.doe@example.net, jane.doe@example.net;", m
+                .getHeader().getField("Cc").getBody());
+
+        m.setCc(group, mailbox3);
+        assertEquals("Does: john.doe@example.net, jane.doe@example.net;, "
+                + "Mary Smith <mary@example.net>", m.getHeader().getField("Cc")
+                .getBody());
+
+        m.setCc(Arrays.asList(group, mailbox3));
+        assertEquals("Does: john.doe@example.net, jane.doe@example.net;, "
+                + "Mary Smith <mary@example.net>", m.getHeader().getField("Cc")
+                .getBody());
+
+        m.setCc((Mailbox) null);
+        assertNull(m.getHeader().getField("Cc"));
+    }
+
+    public void testGetBcc() throws Exception {
+        Message m = new Message();
+        assertNull(m.getBcc());
+
+        Header header = new Header();
+        header.setField(Field.parse("Bcc", "john.doe@example.net"));
+        m.setHeader(header);
+
+        assertEquals("john.doe@example.net", ((Mailbox) m.getBcc().get(0))
+                .getAddress());
+    }
+
+    public void testSetBcc() throws Exception {
+        Message m = new Message();
+
+        Mailbox mailbox1 = Mailbox.parse("john.doe@example.net");
+        Mailbox mailbox2 = Mailbox.parse("jane.doe@example.net");
+        Group group = new Group("Does", mailbox1, mailbox2);
+        Mailbox mailbox3 = Mailbox.parse("Mary Smith <mary@example.net>");
+
+        m.setBcc(group);
+        assertEquals("Does: john.doe@example.net, jane.doe@example.net;", m
+                .getHeader().getField("Bcc").getBody());
+
+        m.setBcc(group, mailbox3);
+        assertEquals("Does: john.doe@example.net, jane.doe@example.net;, "
+                + "Mary Smith <mary@example.net>", m.getHeader()
+                .getField("Bcc").getBody());
+
+        m.setBcc(Arrays.asList(group, mailbox3));
+        assertEquals("Does: john.doe@example.net, jane.doe@example.net;, "
+                + "Mary Smith <mary@example.net>", m.getHeader()
+                .getField("Bcc").getBody());
+
+        m.setBcc((Mailbox) null);
+        assertNull(m.getHeader().getField("Bcc"));
+    }
+
+    public void testGetReplyTo() throws Exception {
+        Message m = new Message();
+        assertNull(m.getReplyTo());
+
+        Header header = new Header();
+        header.setField(Field.parse("Reply-To", "john.doe@example.net"));
+        m.setHeader(header);
+
+        assertEquals("john.doe@example.net", ((Mailbox) m.getReplyTo().get(0))
+                .getAddress());
+    }
+
+    public void testSetReplyTo() throws Exception {
+        Message m = new Message();
+
+        Mailbox mailbox1 = Mailbox.parse("john.doe@example.net");
+        Mailbox mailbox2 = Mailbox.parse("jane.doe@example.net");
+        Group group = new Group("Does", mailbox1, mailbox2);
+        Mailbox mailbox3 = Mailbox.parse("Mary Smith <mary@example.net>");
+
+        m.setReplyTo(group);
+        assertEquals("Does: john.doe@example.net, jane.doe@example.net;", m
+                .getHeader().getField("Reply-To").getBody());
+
+        m.setReplyTo(group, mailbox3);
+        assertEquals("Does: john.doe@example.net, jane.doe@example.net;, "
+                + "Mary Smith <mary@example.net>", m.getHeader().getField(
+                "Reply-To").getBody());
+
+        m.setReplyTo(Arrays.asList(group, mailbox3));
+        assertEquals("Does: john.doe@example.net, jane.doe@example.net;, "
+                + "Mary Smith <mary@example.net>", m.getHeader().getField(
+                "Reply-To").getBody());
+
+        m.setReplyTo((Mailbox) null);
+        assertNull(m.getHeader().getField("Reply-To"));
     }
 
     public void testDisposeGetsPropagatedToBody() throws Exception {



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message