james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwiederk...@apache.org
Subject svn commit: r736123 - in /james/mime4j/trunk: examples/src/java/org/apache/james/mime4j/samples/transform/ src/main/java/org/apache/james/mime4j/message/ src/test/java/org/apache/james/mime4j/message/
Date Tue, 20 Jan 2009 21:56:31 GMT
Author: mwiederkehr
Date: Tue Jan 20 13:56:31 2009
New Revision: 736123

URL: http://svn.apache.org/viewvc?rev=736123&view=rev
Log:
MIME4J-100: getters and setters for Message-ID, Subject and Date in class Message.

Modified:
    james/mime4j/trunk/examples/src/java/org/apache/james/mime4j/samples/transform/TransformMessage.java
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java
    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/examples/src/java/org/apache/james/mime4j/samples/transform/TransformMessage.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/examples/src/java/org/apache/james/mime4j/samples/transform/TransformMessage.java?rev=736123&r1=736122&r2=736123&view=diff
==============================================================================
--- james/mime4j/trunk/examples/src/java/org/apache/james/mime4j/samples/transform/TransformMessage.java
(original)
+++ james/mime4j/trunk/examples/src/java/org/apache/james/mime4j/samples/transform/TransformMessage.java
Tue Jan 20 13:56:31 2009
@@ -21,18 +21,16 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.util.Date;
 import java.util.Random;
 
-import org.apache.james.mime4j.field.Field;
 import org.apache.james.mime4j.message.Body;
 import org.apache.james.mime4j.message.BodyFactory;
 import org.apache.james.mime4j.message.BodyPart;
-import org.apache.james.mime4j.message.Header;
 import org.apache.james.mime4j.message.Message;
 import org.apache.james.mime4j.message.Mode;
 import org.apache.james.mime4j.message.Multipart;
 import org.apache.james.mime4j.message.TextBody;
-import org.apache.james.mime4j.util.MimeUtil;
 import org.apache.james.mime4j.storage.DefaultStorageProvider;
 import org.apache.james.mime4j.storage.StorageProvider;
 import org.apache.james.mime4j.storage.TempFileStorageProvider;
@@ -110,14 +108,10 @@
         // it should be disposed of.
         removed.dispose();
 
-        // Create a unique message ID for the new message.
-        Header header = message.getHeader();
-        Field messageId = Field.parse("Message-ID", MimeUtil
-                .createUniqueMessageId(HOSTNAME));
-        header.setField(messageId);
-
-        // Set a new subject; note the usage of setField instead of addField
-        header.setField(Field.parse("Subject", "Transformed message"));
+        // Set some headers on the transformed message
+        message.createMessageId(HOSTNAME);
+        message.setSubject("Transformed message");
+        message.setDate(new Date());
 
         return message;
     }
@@ -127,9 +121,6 @@
      * two binary).
      */
     private static Message createTemplate() throws IOException {
-        Header header = new Header();
-        header.addField(Field.parse("Subject", "Template message"));
-
         Multipart multipart = new Multipart("mixed");
 
         BodyPart part1 = createTextPart("This is the first part of the template..");
@@ -142,9 +133,10 @@
         multipart.addBodyPart(part3);
 
         Message message = new Message();
-        message.setHeader(header);
         message.setMultipart(multipart);
 
+        message.setSubject("Template message");
+
         return message;
     }
 

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java?rev=736123&r1=736122&r2=736123&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java Tue Jan 20
13:56:31 2009
@@ -390,7 +390,7 @@
      * 
      * @return the header of this entity; never <code>null</code>.
      */
-    protected Header obtainHeader() {
+    Header obtainHeader() {
         if (header == null) {
             header = new Header();
         }

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=736123&r1=736122&r2=736123&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 Tue Jan
20 13:56:31 2009
@@ -19,19 +19,22 @@
 
 package org.apache.james.mime4j.message;
 
+import java.io.IOException;
+import java.io.InputStream;
+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.DateTimeField;
 import org.apache.james.mime4j.field.Field;
+import org.apache.james.mime4j.field.Fields;
 import org.apache.james.mime4j.field.UnstructuredField;
 import org.apache.james.mime4j.parser.MimeEntityConfig;
 import org.apache.james.mime4j.parser.MimeStreamParser;
 import org.apache.james.mime4j.storage.DefaultStorageProvider;
 import org.apache.james.mime4j.storage.StorageProvider;
 
-import java.io.IOException;
-import java.io.InputStream;
-
-
 /**
  * Represents a MIME message. The following code parses a stream into a 
  * <code>Message</code> object.
@@ -109,7 +112,7 @@
      * @throws IOException on I/O errors.
      * @throws MimeIOException on MIME protocol violations.
      */
-     public Message(InputStream is, MimeEntityConfig config,
+    public Message(InputStream is, MimeEntityConfig config,
             StorageProvider storageProvider) throws IOException,
             MimeIOException {
         try {
@@ -122,13 +125,121 @@
     }
     
     /**
-     * Gets the <code>Subject</code> field.
+     * Returns the value of the <i>Message-ID</i> header field of this message
+     * or <code>null</code> if it is not present.
      * 
-     * @return the <code>Subject</code> field or <code>null</code>
if it
-     *         doesn't exist.
+     * @return the identifier of this message.
      */
-    public UnstructuredField getSubject() {
-        return (UnstructuredField) getHeader().getField(Field.SUBJECT);
+    public String getMessageId() {
+        Field field = obtainField(Field.MESSAGE_ID);
+        if (field == null)
+            return null;
+
+        return field.getBody();
     }
-    
+
+    /**
+     * Creates and sets a new <i>Message-ID</i> header field for this message.
+     * A <code>Header</code> is created if this message does not already have
+     * one.
+     * 
+     * @param hostName
+     *            host name to be included in the identifier or
+     *            <code>null</code> if no host name should be included.
+     */
+    public void createMessageId(String hostname) {
+        Header header = obtainHeader();
+
+        header.setField(Fields.messageId(hostname));
+    }
+
+    /**
+     * Returns the (decoded) value of the <i>Subject</i> header field of this
+     * message or <code>null</code> if it is not present.
+     * 
+     * @return the subject of this message.
+     */
+    public String getSubject() {
+        UnstructuredField field = obtainField(Field.SUBJECT);
+        if (field == null)
+            return null;
+
+        return field.getValue();
+    }
+
+    /**
+     * Sets the <i>Subject</i> header field for this message. The specified
+     * string may contain non-ASCII characters, in which case it gets encoded as
+     * an 'encoded-word' automatically. A <code>Header</code> is created if
+     * this message does not already have one.
+     * 
+     * @param subject
+     *            subject to set.
+     */
+    public void setSubject(String subject) {
+        Header header = obtainHeader();
+
+        if (subject == null) {
+            header.removeFields(Field.SUBJECT);
+        } else {
+            header.setField(Fields.subject(subject));
+        }
+    }
+
+    /**
+     * Returns the value of the <i>Date</i> header field of this message as
+     * <code>Date</code> object or <code>null</code> if it is not
present.
+     * 
+     * @return the date of this message.
+     */
+    public Date getDate() {
+        DateTimeField dateField = obtainField(Field.DATE);
+        if (dateField == null)
+            return null;
+
+        return dateField.getDate();
+    }
+
+    /**
+     * Sets the <i>Date</i> header field for this message. This method uses the
+     * default <code>TimeZone</code> of this host to encode the specified
+     * <code>Date</code> object into a string.
+     * 
+     * @param date
+     *            date to set.
+     */
+    public void setDate(Date date) {
+        setDate(date, null);
+    }
+
+    /**
+     * Sets the <i>Date</i> header field for this message. The specified
+     * <code>TimeZone</code> is used to encode the specified <code>Date</code>
+     * object into a string.
+     * 
+     * @param date
+     *            date to set.
+     * @param zone
+     *            a time zone.
+     */
+    public void setDate(Date date, TimeZone zone) {
+        Header header = obtainHeader();
+
+        if (date == null) {
+            header.removeFields(Field.DATE);
+        } else {
+            header.setField(Fields.date(Field.DATE, date, zone));
+        }
+    }
+
+    private <F extends Field> F obtainField(String fieldName) {
+        Header header = getHeader();
+        if (header == null)
+            return null;
+
+        @SuppressWarnings("unchecked")
+        F field = (F) header.getField(fieldName);
+        return field;
+    }
+
 }

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=736123&r1=736122&r2=736123&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 Tue
Jan 20 13:56:31 2009
@@ -26,7 +26,9 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
+import java.util.Date;
 import java.util.List;
+import java.util.TimeZone;
 
 import junit.framework.TestCase;
 
@@ -165,6 +167,70 @@
         assertTrue("header added", lines.contains(testheader));
     }
 
+    public void testGetMessageId() throws Exception {
+        Message m = new Message();
+        assertNull(m.getMessageId());
+
+        String id = "<msg17@localhost>";
+        Header header = new Header();
+        header.setField(Field.parse("Message-ID", id));
+        m.setHeader(header);
+        assertEquals(id, m.getMessageId());
+    }
+
+    public void testCreateMessageId() throws Exception {
+        Message m = new Message();
+        m.createMessageId("hostname");
+
+        String id = m.getMessageId();
+        assertNotNull(id);
+        assertTrue(id.startsWith("<Mime4j."));
+        assertTrue(id.endsWith("@hostname>"));
+    }
+
+    public void testGetSubject() throws Exception {
+        Message m = new Message();
+        assertNull(m.getSubject());
+
+        String subject = "testing 1 2";
+        Header header = new Header();
+        header.setField(Field.parse("Subject", subject));
+        m.setHeader(header);
+        assertEquals(subject, m.getSubject());
+
+        header.setField(Field.parse("Subject", "=?windows-1252?Q?99_=80?="));
+        assertEquals("99 \u20ac", m.getSubject());
+    }
+
+    public void testSetSubject() throws Exception {
+        Message m = new Message();
+        m.setSubject("Semmelbr\366sel");
+
+        assertEquals("Semmelbr\366sel", m.getSubject());
+        assertEquals("=?ISO-8859-1?Q?Semmelbr=F6sel?=", m.getHeader().getField(
+                "Subject").getBody());
+    }
+
+    public void testGetDate() throws Exception {
+        Message m = new Message();
+        assertNull(m.getDate());
+
+        Header header = new Header();
+        header.setField(Field.parse("Date", "Thu, 1 Jan 1970 05:30:00 +0530"));
+        m.setHeader(header);
+
+        assertEquals(new Date(0), m.getDate());
+    }
+
+    public void testSetDate() throws Exception {
+        Message m = new Message();
+        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());
+    }
+
     public void testDisposeGetsPropagatedToBody() throws Exception {
         DummyBody body1 = new DummyBody();
         BodyPart part1 = new BodyPart();



---------------------------------------------------------------------
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