You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by mw...@apache.org on 2009/01/26 21:25:52 UTC
svn commit: r737820 - in /james/mime4j/trunk/src:
main/java/org/apache/james/mime4j/field/Fields.java
main/java/org/apache/james/mime4j/field/address/Group.java
test/java/org/apache/james/mime4j/field/FieldsTest.java
Author: mwiederkehr
Date: Mon Jan 26 20:25:52 2009
New Revision: 737820
URL: http://svn.apache.org/viewvc?rev=737820&view=rev
Log:
MIME4J-100: methods for creating mailbox fields, mailbox-list fields and address-list fields
Modified:
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Group.java
james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldsTest.java
Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java?rev=737820&r1=737819&r2=737820&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java Mon Jan 26 20:25:52 2009
@@ -22,6 +22,8 @@
import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
@@ -29,6 +31,8 @@
import java.util.TimeZone;
import org.apache.james.mime4j.codec.EncoderUtil;
+import org.apache.james.mime4j.field.address.Address;
+import org.apache.james.mime4j.field.address.Mailbox;
import org.apache.james.mime4j.util.MimeUtil;
public class Fields {
@@ -105,6 +109,133 @@
return parse(UnstructuredField.class, Field.SUBJECT, rawValue);
}
+ public static MailboxField sender(Mailbox mailbox) {
+ return mailbox(Field.SENDER, mailbox);
+ }
+
+ public static MailboxListField from(Mailbox mailbox) {
+ return mailboxList(Field.FROM, Collections.singleton(mailbox));
+ }
+
+ public static MailboxListField from(Mailbox... mailboxes) {
+ return mailboxList(Field.FROM, Arrays.asList(mailboxes));
+ }
+
+ public static MailboxListField from(Iterable<Mailbox> mailboxes) {
+ return mailboxList(Field.FROM, mailboxes);
+ }
+
+ public static AddressListField to(Address address) {
+ return addressList(Field.TO, Collections.singleton(address));
+ }
+
+ public static AddressListField to(Address... addresses) {
+ return addressList(Field.TO, Arrays.asList(addresses));
+ }
+
+ public static AddressListField to(Iterable<Address> addresses) {
+ return addressList(Field.TO, addresses);
+ }
+
+ public static AddressListField cc(Address address) {
+ return addressList(Field.CC, Collections.singleton(address));
+ }
+
+ public static AddressListField cc(Address... addresses) {
+ return addressList(Field.CC, Arrays.asList(addresses));
+ }
+
+ public static AddressListField cc(Iterable<Address> addresses) {
+ return addressList(Field.CC, addresses);
+ }
+
+ public static AddressListField bcc(Address address) {
+ return addressList(Field.BCC, Collections.singleton(address));
+ }
+
+ public static AddressListField bcc(Address... addresses) {
+ return addressList(Field.BCC, Arrays.asList(addresses));
+ }
+
+ public static AddressListField bcc(Iterable<Address> addresses) {
+ return addressList(Field.BCC, addresses);
+ }
+
+ public static AddressListField replyTo(Address address) {
+ return addressList(Field.REPLY_TO, Collections.singleton(address));
+ }
+
+ public static AddressListField replyTo(Address... addresses) {
+ return addressList(Field.REPLY_TO, Arrays.asList(addresses));
+ }
+
+ public static AddressListField replyTo(Iterable<Address> addresses) {
+ return addressList(Field.REPLY_TO, addresses);
+ }
+
+ /**
+ * Creates a mailbox field from the specified field name and mailbox
+ * address. Valid field names are <code>Sender</code> and
+ * <code>Resent-Sender</code>.
+ *
+ * @param fieldName
+ * the name of the mailbox field (<code>Sender</code> or
+ * <code>Resent-Sender</code>).
+ * @param mailbox
+ * mailbox address for the field value.
+ * @return the newly created mailbox field.
+ */
+ public static MailboxField mailbox(String fieldName, Mailbox mailbox) {
+ String value = encodeAddresses(Collections.singleton(mailbox));
+
+ String folded = MimeUtil.fold(value, fieldName.length() + 2);
+ return parse(MailboxField.class, fieldName, folded);
+ }
+
+ /**
+ * Creates a mailbox-list field from the specified field name and mailbox
+ * addresses. Valid field names are <code>From</code> and
+ * <code>Resent-From</code>.
+ *
+ * @param fieldName
+ * the name of the mailbox field (<code>From</code> or
+ * <code>Resent-From</code>).
+ * @param mailboxes
+ * mailbox addresses for the field value.
+ * @return the newly created mailbox-list field.
+ */
+ public static MailboxListField mailboxList(String fieldName,
+ Iterable<Mailbox> mailboxes) {
+ String value = encodeAddresses(mailboxes);
+
+ String folded = MimeUtil.fold(value, fieldName.length() + 2);
+ return parse(MailboxListField.class, fieldName, folded);
+ }
+
+ /**
+ * Creates an address-list field from the specified field name and mailbox
+ * or group addresses. Valid field names are <code>To</code>,
+ * <code>Cc</code>, <code>Bcc</code>, <code>Reply-To</code>,
+ * <code>Resent-To</code>, <code>Resent-Cc</code> and
+ * <code>Resent-Bcc</code>.
+ *
+ * @param fieldName
+ * the name of the mailbox field (<code>To</code>,
+ * <code>Cc</code>, <code>Bcc</code>, <code>Reply-To</code>,
+ * <code>Resent-To</code>, <code>Resent-Cc</code> or
+ * <code>Resent-Bcc</code>).
+ * @param addresses
+ * mailbox or group addresses for the field value.
+ * @return the newly created address-list field.
+ */
+ public static AddressListField addressList(String fieldName,
+ Iterable<Address> addresses) {
+ String value = encodeAddresses(addresses);
+
+ String folded = MimeUtil.fold(value, fieldName.length() + 2);
+ return parse(AddressListField.class, fieldName, folded);
+ }
+
private static <F extends Field> F parse(Class<F> fieldClass,
String fieldName, String fieldBody) {
Field field = Field.parse(fieldName, fieldBody);
@@ -116,6 +247,19 @@
return fieldClass.cast(field);
}
+ private static String encodeAddresses(Iterable<? extends Address> addresses) {
+ StringBuilder sb = new StringBuilder();
+
+ for (Address address : addresses) {
+ if (sb.length() > 0) {
+ sb.append(", ");
+ }
+ sb.append(address.getEncodedString());
+ }
+
+ return sb.toString();
+ }
+
private static final ThreadLocal<DateFormat> RFC822_DATE_FORMAT = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Group.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Group.java?rev=737820&r1=737819&r2=737820&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Group.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/address/Group.java Mon Jan 26 20:25:52 2009
@@ -19,6 +19,9 @@
package org.apache.james.mime4j.field.address;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.List;
import org.apache.james.mime4j.codec.EncoderUtil;
@@ -40,6 +43,26 @@
* @param mailboxes
* The mailboxes in this group.
*/
+ public Group(String name, Mailbox... mailboxes) {
+ this(name, new MailboxList(Arrays.asList(mailboxes), true));
+ }
+
+ /**
+ * @param name
+ * The group name.
+ * @param mailboxes
+ * The mailboxes in this group.
+ */
+ public Group(String name, Collection<Mailbox> mailboxes) {
+ this(name, new MailboxList(new ArrayList<Mailbox>(mailboxes), true));
+ }
+
+ /**
+ * @param name
+ * The group name.
+ * @param mailboxes
+ * The mailboxes in this group.
+ */
public Group(String name, MailboxList mailboxes) {
if (name == null)
throw new IllegalArgumentException();
Modified: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldsTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldsTest.java?rev=737820&r1=737819&r2=737820&view=diff
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldsTest.java (original)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldsTest.java Mon Jan 26 20:25:52 2009
@@ -19,6 +19,7 @@
package org.apache.james.mime4j.field;
+import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@@ -26,6 +27,9 @@
import junit.framework.TestCase;
+import org.apache.james.mime4j.field.address.Group;
+import org.apache.james.mime4j.field.address.Mailbox;
+
public class FieldsTest extends TestCase {
public void testContentTypeString() throws Exception {
@@ -165,4 +169,139 @@
assertEquals(expected, Fields.subject(eighty).getRaw());
}
+ public void testSender() throws Exception {
+ MailboxField field = Fields.sender(Mailbox
+ .parse("JD <jo...@acme.org>"));
+ assertEquals("Sender: JD <jo...@acme.org>", field.getRaw());
+ }
+
+ public void testFrom() throws Exception {
+ Mailbox mailbox1 = Mailbox.parse("JD <jo...@acme.org>");
+ Mailbox mailbox2 = Mailbox.parse("Mary Smith <ma...@example.net>");
+
+ MailboxListField field = Fields.from(mailbox1);
+ assertEquals("From: JD <jo...@acme.org>", field.getRaw());
+
+ field = Fields.from(mailbox1, mailbox2);
+ assertEquals("From: JD <jo...@acme.org>, "
+ + "Mary Smith <ma...@example.net>", field.getRaw());
+
+ field = Fields.from(Arrays.asList(mailbox1, mailbox2));
+ assertEquals("From: JD <jo...@acme.org>, "
+ + "Mary Smith <ma...@example.net>", field.getRaw());
+ }
+
+ public void testTo() throws Exception {
+ Mailbox mailbox1 = Mailbox.parse("JD <jo...@acme.org>");
+ Mailbox mailbox2 = Mailbox.parse("jane.doe@example.org");
+ Mailbox mailbox3 = Mailbox.parse("Mary Smith <ma...@example.net>");
+ Group group = new Group("The Does", mailbox1, mailbox2);
+
+ AddressListField field = Fields.to(group);
+ assertEquals("To: The Does: JD <jo...@acme.org>, "
+ + "jane.doe@example.org;", field.getRaw());
+
+ field = Fields.to(group, mailbox3);
+ assertEquals("To: The Does: JD <jo...@acme.org>, "
+ + "jane.doe@example.org;, Mary Smith\r\n <ma...@example.net>",
+ field.getRaw());
+
+ field = Fields.to(Arrays.asList(group, mailbox3));
+ assertEquals("To: The Does: JD <jo...@acme.org>, "
+ + "jane.doe@example.org;, Mary Smith\r\n <ma...@example.net>",
+ field.getRaw());
+ }
+
+ public void testCc() throws Exception {
+ Mailbox mailbox1 = Mailbox.parse("JD <jo...@acme.org>");
+ Mailbox mailbox2 = Mailbox.parse("jane.doe@example.org");
+ Mailbox mailbox3 = Mailbox.parse("Mary Smith <ma...@example.net>");
+ Group group = new Group("The Does", mailbox1, mailbox2);
+
+ AddressListField field = Fields.cc(group);
+ assertEquals("Cc: The Does: JD <jo...@acme.org>, "
+ + "jane.doe@example.org;", field.getRaw());
+
+ field = Fields.cc(group, mailbox3);
+ assertEquals("Cc: The Does: JD <jo...@acme.org>, "
+ + "jane.doe@example.org;, Mary Smith\r\n <ma...@example.net>",
+ field.getRaw());
+
+ field = Fields.cc(Arrays.asList(group, mailbox3));
+ assertEquals("Cc: The Does: JD <jo...@acme.org>, "
+ + "jane.doe@example.org;, Mary Smith\r\n <ma...@example.net>",
+ field.getRaw());
+ }
+
+ public void testBcc() throws Exception {
+ Mailbox mailbox1 = Mailbox.parse("JD <jo...@acme.org>");
+ Mailbox mailbox2 = Mailbox.parse("jane.doe@example.org");
+ Mailbox mailbox3 = Mailbox.parse("Mary Smith <ma...@example.net>");
+ Group group = new Group("The Does", mailbox1, mailbox2);
+
+ AddressListField field = Fields.bcc(group);
+ assertEquals("Bcc: The Does: JD <jo...@acme.org>, "
+ + "jane.doe@example.org;", field.getRaw());
+
+ field = Fields.bcc(group, mailbox3);
+ assertEquals("Bcc: The Does: JD <jo...@acme.org>, "
+ + "jane.doe@example.org;, Mary Smith\r\n <ma...@example.net>",
+ field.getRaw());
+
+ field = Fields.bcc(Arrays.asList(group, mailbox3));
+ assertEquals("Bcc: The Does: JD <jo...@acme.org>, "
+ + "jane.doe@example.org;, Mary Smith\r\n <ma...@example.net>",
+ field.getRaw());
+ }
+
+ public void testReplyTo() throws Exception {
+ Mailbox mailbox1 = Mailbox.parse("JD <jo...@acme.org>");
+ Mailbox mailbox2 = Mailbox.parse("jane.doe@example.org");
+ Mailbox mailbox3 = Mailbox.parse("Mary Smith <ma...@example.net>");
+ Group group = new Group("The Does", mailbox1, mailbox2);
+
+ AddressListField field = Fields.replyTo(group);
+ assertEquals("Reply-To: The Does: JD <jo...@acme.org>, "
+ + "jane.doe@example.org;", field.getRaw());
+
+ field = Fields.replyTo(group, mailbox3);
+ assertEquals("Reply-To: The Does: JD <jo...@acme.org>, "
+ + "jane.doe@example.org;, Mary\r\n Smith <ma...@example.net>",
+ field.getRaw());
+
+ field = Fields.replyTo(Arrays.asList(group, mailbox3));
+ assertEquals("Reply-To: The Does: JD <jo...@acme.org>, "
+ + "jane.doe@example.org;, Mary\r\n Smith <ma...@example.net>",
+ field.getRaw());
+ }
+
+ public void testMailbox() throws Exception {
+ MailboxField field = Fields.mailbox("Resent-Sender", Mailbox
+ .parse("JD <jo...@acme.org>"));
+ assertEquals("Resent-Sender: JD <jo...@acme.org>", field.getRaw());
+ }
+
+ public void testMailboxList() throws Exception {
+ Mailbox mailbox1 = Mailbox.parse("JD <jo...@acme.org>");
+ Mailbox mailbox2 = Mailbox.parse("Mary Smith <ma...@example.net>");
+
+ MailboxListField field = Fields.mailboxList("Resent-From", Arrays
+ .asList(mailbox1, mailbox2));
+ assertEquals("Resent-From: JD <jo...@acme.org>, "
+ + "Mary Smith <ma...@example.net>", field.getRaw());
+ }
+
+ public void testAddressList() throws Exception {
+ Mailbox mailbox1 = Mailbox.parse("JD <jo...@acme.org>");
+ Mailbox mailbox2 = Mailbox.parse("jane.doe@example.org");
+ Mailbox mailbox3 = Mailbox.parse("Mary Smith <ma...@example.net>");
+ Group group = new Group("The Does", mailbox1, mailbox2);
+
+ AddressListField field = Fields.addressList("Resent-To", Arrays.asList(
+ group, mailbox3));
+ assertEquals("Resent-To: The Does: JD <jo...@acme.org>, "
+ + "jane.doe@example.org;, Mary\r\n Smith <ma...@example.net>",
+ field.getRaw());
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org