Author: mwiederkehr
Date: Sat Jan 10 13:51:31 2009
New Revision: 733372

URL: http://svn.apache.org/viewvc?rev=733372&view=rev
Log:
MIME4J-99: convenience methods in Entity to set the message body along with the 
content type header field. Also introduces getMimeCharset() in TextBody.

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/StorageTextBody.java
    
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/StringTextBody.java
    
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/TextBody.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=733372&r1=733371&r2=733372&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
 Sat Jan 10 13:51:31 2009
@@ -31,6 +31,7 @@
 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.message.storage.DefaultStorageProvider;
 import org.apache.james.mime4j.message.storage.StorageProvider;
 import org.apache.james.mime4j.message.storage.TempFileStorageProvider;
@@ -127,9 +128,6 @@
      */
     private static Message createTemplate() throws IOException {
         Header header = new Header();
-        header.addField(Field.parse("Content-Type",
-                "multipart/mixed;\r\n\tboundary=\""
-                        + MimeUtil.createUniqueBoundary() + "\""));
         header.addField(Field.parse("Subject", "Template message"));
 
         Multipart multipart = new Multipart("mixed");
@@ -145,7 +143,7 @@
 
         Message message = new Message();
         message.setHeader(header);
-        message.setBody(multipart);
+        message.setMultipart(multipart);
 
         return message;
     }
@@ -154,14 +152,10 @@
      * Creates a text part from the specified string.
      */
     private static BodyPart createTextPart(String text) {
-        Header header = new Header();
-        header.addField(Field.parse("Content-Type", "text/plain"));
-
-        Body body = new BodyFactory().textBody(text);
+        TextBody body = new BodyFactory().textBody(text);
 
         BodyPart bodyPart = new BodyPart();
-        bodyPart.setHeader(header);
-        bodyPart.setBody(body);
+        bodyPart.setText(body);
 
         return bodyPart;
     }
@@ -172,7 +166,6 @@
     private static BodyPart createRandomBinaryPart(int numberOfBytes)
             throws IOException {
         Header header = new Header();
-        header.addField(Field.parse("Content-Type", 
"application/octet-stream"));
         header.addField(Field.parse("Content-Transfer-Encoding", "base64"));
 
         byte[] data = new byte[numberOfBytes];
@@ -182,7 +175,7 @@
 
         BodyPart bodyPart = new BodyPart();
         bodyPart.setHeader(header);
-        bodyPart.setBody(body);
+        bodyPart.setBody(body, "application/octet-stream");
 
         return bodyPart;
     }

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=733372&r1=733371&r2=733372&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 
Sat Jan 10 13:51:31 2009
@@ -131,7 +131,83 @@
         this.body = body;
         body.setParent(this);
     }
-    
+
+    /**
+     * Sets the specified message as body of this entity and the content type 
to
+     * &quot;message/rfc822&quot;. A <code>Header</code> is created if this
+     * entity does not already have one.
+     * 
+     * @param message
+     *            the message to set as body.
+     */
+    public void setMessage(Message message) {
+        setBody(message, "message/rfc822");
+    }
+
+    /**
+     * Sets the specified multipart as body of this entity. Also sets the
+     * content type accordingly and creates a message boundary string. A
+     * <code>Header</code> is created if this message does not already have
+     * one.
+     * 
+     * @param multipart
+     *            the multipart to set as body.
+     */
+    public void setMultipart(Multipart multipart) {
+        String contentType = "multipart/" + multipart.getSubType()
+                + ";\r\n\tboundary=\"" + MimeUtil.createUniqueBoundary() + 
"\"";
+
+        setBody(multipart, contentType);
+    }
+
+    /**
+     * Sets the specified <code>TextBody</code> as body of this entity and the
+     * content type to &quot;text/plain&quot;. A <code>Header</code> is
+     * created if this message does not already have one.
+     * 
+     * @param textBody
+     *            the <code>TextBody</code> to set as body.
+     * @see BodyFactory#textBody(String)
+     */
+    public void setText(TextBody textBody) {
+        setText(textBody, "plain");
+    }
+
+    /**
+     * Sets the specified <code>TextBody</code> as body of this entity. Also
+     * sets the content type according to the specified sub-type. A
+     * <code>Header</code> is created if this message does not already have
+     * one.
+     * 
+     * @param textBody
+     *            the <code>TextBody</code> to set as body.
+     * @see BodyFactory#textBody(String)
+     */
+    public void setText(TextBody textBody, String subType) {
+        String contentType = "text/" + subType;
+        String mimeCharset = textBody.getMimeCharset();
+        if (mimeCharset != null && !mimeCharset.equalsIgnoreCase("us-ascii")) {
+            contentType += "; charset=\"" + mimeCharset + "\"";
+        }
+
+        setBody(textBody, contentType);
+    }
+
+    /**
+     * Sets the body of this entity and sets the content-type to the specified
+     * value. A <code>Header</code> is created if this entity does not already
+     * have one.
+     * 
+     * @param body
+     *            the body.
+     */
+    public void setBody(Body body, String contentType) {
+        setBody(body);
+
+        Header header = obtainHeader();
+        header.setField(Field.parse("Content-Type", contentType));
+    }
+
     /**
      * Determines the MIME type of this <code>Entity</code>. The MIME type
      * is derived by looking at the parent's Content-Type field if no
@@ -244,4 +320,17 @@
         }
     }
 
+    /**
+     * Obtains the header of this entity. Creates and sets a new header if this
+     * entity's header is currently <code>null</code>.
+     * 
+     * @return the header of this entity; never <code>null</code>.
+     */
+    protected Header obtainHeader() {
+        if (header == null) {
+            header = new Header();
+        }
+        return header;
+    }
+
 }

Modified: 
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/StorageTextBody.java
URL: 
http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/StorageTextBody.java?rev=733372&r1=733371&r2=733372&view=diff
==============================================================================
--- 
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/StorageTextBody.java
 (original)
+++ 
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/StorageTextBody.java
 Sat Jan 10 13:51:31 2009
@@ -28,6 +28,7 @@
 
 import org.apache.james.mime4j.decoder.CodecUtil;
 import org.apache.james.mime4j.message.storage.MultiReferenceStorage;
+import org.apache.james.mime4j.util.CharsetUtil;
 
 /**
  * Text body backed by a {...@link 
org.apache.james.mime4j.message.storage.Storage}.
@@ -43,6 +44,13 @@
     }
 
     /**
+     * @see org.apache.james.mime4j.message.TextBody#getMimeCharset()
+     */
+    public String getMimeCharset() {
+        return CharsetUtil.toMimeCharset(charset.name());
+    }
+
+    /**
      * @see org.apache.james.mime4j.message.TextBody#getReader()
      */
     public Reader getReader() throws IOException {
@@ -67,7 +75,7 @@
         storage.addReference();
         return new StorageTextBody(storage, charset);
     }
-    
+
     /**
      * Deletes the Storage that holds the content of this text body.
      * 

Modified: 
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/StringTextBody.java
URL: 
http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/StringTextBody.java?rev=733372&r1=733371&r2=733372&view=diff
==============================================================================
--- 
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/StringTextBody.java
 (original)
+++ 
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/StringTextBody.java
 Sat Jan 10 13:51:31 2009
@@ -27,6 +27,8 @@
 import java.io.Writer;
 import java.nio.charset.Charset;
 
+import org.apache.james.mime4j.util.CharsetUtil;
+
 /**
  * Text body backed by a <code>String</code>.
  */
@@ -41,6 +43,13 @@
     }
 
     /**
+     * @see org.apache.james.mime4j.message.TextBody#getMimeCharset()
+     */
+    public String getMimeCharset() {
+        return CharsetUtil.toMimeCharset(charset.name());
+    }
+
+    /**
      * @see org.apache.james.mime4j.message.TextBody#getReader()
      */
     public Reader getReader() throws IOException {

Modified: 
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/TextBody.java
URL: 
http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/TextBody.java?rev=733372&r1=733371&r2=733372&view=diff
==============================================================================
--- 
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/TextBody.java 
(original)
+++ 
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/TextBody.java 
Sat Jan 10 13:51:31 2009
@@ -30,7 +30,13 @@
  * @version $Id: TextBody.java,v 1.3 2004/10/02 12:41:11 ntherning Exp $
  */
 public interface TextBody extends Body {
-    
+    /**
+     * Returns the MIME charset of this text body.
+     * 
+     * @return the MIME charset.
+     */
+    String getMimeCharset();
+
     /**
      * Gets a <code>Reader</code> which may be used to read out the contents
      * of this body.



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to