james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1606167 - in /james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message: BasicBodyFactory.java BodyBuilder.java MessageBuilder.java SingleBodyBuilder.java
Date Fri, 27 Jun 2014 17:59:52 GMT
Author: olegk
Date: Fri Jun 27 17:59:51 2014
New Revision: 1606167

URL: http://svn.apache.org/r1606167
Log:
Minor code refactoring and cleanup in MessageBuilder; SingleBodyBuilder to use BodyFactory
to instantiate TextBody and BinaryBody

Removed:
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BodyBuilder.java
Modified:
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageBuilder.java
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/SingleBodyBuilder.java

Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java?rev=1606167&r1=1606166&r2=1606167&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java
(original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/BasicBodyFactory.java
Fri Jun 27 17:59:51 2014
@@ -21,26 +21,28 @@ package org.apache.james.mime4j.message;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.StringReader;
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.Charset;
 import java.nio.charset.UnsupportedCharsetException;
 
 import org.apache.james.mime4j.Charsets;
 import org.apache.james.mime4j.dom.BinaryBody;
+import org.apache.james.mime4j.dom.SingleBody;
 import org.apache.james.mime4j.dom.TextBody;
+import org.apache.james.mime4j.io.InputStreams;
+import org.apache.james.mime4j.util.ContentUtil;
 
 /**
  * Factory for creating message bodies.
  */
 public class BasicBodyFactory implements BodyFactory {
 
-    public BinaryBody binaryBody(final InputStream is) throws IOException {
-        return SingleBodyBuilder.create()
-                .readFrom(is)
-                .buildBinary();
-    }
+    public static final BasicBodyFactory INSTANCE = new BasicBodyFactory();
 
-    protected Charset resolveCharset(final String mimeCharset) throws UnsupportedEncodingException
{
+    private static Charset resolveCharset(final String mimeCharset) throws UnsupportedEncodingException
{
         try {
             return mimeCharset != null ? Charset.forName(mimeCharset) : null;
         } catch (UnsupportedCharsetException ex) {
@@ -48,41 +50,179 @@ public class BasicBodyFactory implements
         }
     }
 
-    public TextBody textBody(final InputStream is, final String mimeCharset) throws IOException
{
-        return SingleBodyBuilder.create()
-                .readFrom(is)
-                .setCharset(resolveCharset(mimeCharset))
-                .buildText();
-    }
-
     public TextBody textBody(final String text, final String mimeCharset) throws UnsupportedEncodingException
{
         if (text == null) {
             throw new IllegalArgumentException("Text may not be null");
         }
-        return SingleBodyBuilder.create()
-                .setText(text)
-                .setCharset(resolveCharset(mimeCharset))
-                .buildText();
+        return new StringBody1(text, resolveCharset(mimeCharset));
+    }
+
+    public TextBody textBody(final byte[] content, final Charset charset) {
+        if (content == null) {
+            throw new IllegalArgumentException("Content may not be null");
+        }
+        return new StringBody2(content, charset);
+    }
+
+    public TextBody textBody(final InputStream content, final String mimeCharset) throws
IOException {
+        if (content == null) {
+            throw new IllegalArgumentException("Input stream may not be null");
+        }
+        return new StringBody2(ContentUtil.buffer(content), resolveCharset(mimeCharset));
     }
 
     public TextBody textBody(final String text, final Charset charset) {
         if (text == null) {
             throw new IllegalArgumentException("Text may not be null");
         }
-        return SingleBodyBuilder.create()
-                .setText(text)
-                .setCharset(charset)
-                .buildText();
+        return new StringBody1(text, charset);
     }
 
     public TextBody textBody(final String text) {
         return textBody(text, Charsets.DEFAULT_CHARSET);
     }
 
+    public BinaryBody binaryBody(final String content, final Charset charset) {
+        if (content == null) {
+            throw new IllegalArgumentException("Content may not be null");
+        }
+        return new BinaryBody2(content, charset);
+    }
+
+    public BinaryBody binaryBody(final InputStream is) throws IOException {
+        return new BinaryBody1(ContentUtil.buffer(is));
+    }
+
     public BinaryBody binaryBody(final byte[] buf) {
-        return SingleBodyBuilder.create()
-                .setByteArray(buf)
-                .buildBinary();
+        return new BinaryBody1(buf);
+    }
+
+    static class StringBody1 extends TextBody {
+
+        private final String content;
+        private final Charset charset;
+
+        StringBody1(final String content, final Charset charset) {
+            super();
+            this.content = content;
+            this.charset = charset;
+        }
+
+        @Override
+        public String getMimeCharset() {
+            return this.charset != null ? this.charset.name() : null;
+        }
+
+        @Override
+        public Reader getReader() throws IOException {
+            return new StringReader(this.content);
+        }
+
+        @Override
+        public InputStream getInputStream() throws IOException {
+            return InputStreams.create(this.content,
+                    this.charset != null ? this.charset : Charsets.DEFAULT_CHARSET);
+        }
+
+        @Override
+        public void dispose() {
+        }
+
+        @Override
+        public SingleBody copy() {
+            return new StringBody1(this.content, this.charset);
+        }
+
+    }
+
+    static class StringBody2 extends TextBody {
+
+        private final byte[] content;
+        private final Charset charset;
+
+        StringBody2(final byte[] content, final Charset charset) {
+            super();
+            this.content = content;
+            this.charset = charset;
+        }
+
+        @Override
+        public String getMimeCharset() {
+            return this.charset != null ? this.charset.name() : null;
+        }
+
+        @Override
+        public Reader getReader() throws IOException {
+            return new InputStreamReader(InputStreams.create(this.content), this.charset);
+        }
+
+        @Override
+        public InputStream getInputStream() throws IOException {
+            return InputStreams.create(this.content);
+        }
+
+        @Override
+        public void dispose() {
+        }
+
+        @Override
+        public SingleBody copy() {
+            return new StringBody2(this.content, this.charset);
+        }
+
+    }
+
+    static class BinaryBody1 extends BinaryBody {
+
+        private final byte[] content;
+
+        BinaryBody1(final byte[] content) {
+            super();
+            this.content = content;
+        }
+
+        @Override
+        public InputStream getInputStream() throws IOException {
+            return InputStreams.create(this.content);
+        }
+
+        @Override
+        public void dispose() {
+        }
+
+        @Override
+        public SingleBody copy() {
+            return new BinaryBody1(this.content);
+        }
+
+    }
+
+    static class BinaryBody2 extends BinaryBody {
+
+        private final String content;
+        private final Charset charset;
+
+        BinaryBody2(final String content, final Charset charset) {
+            super();
+            this.content = content;
+            this.charset = charset;
+        }
+
+        @Override
+        public InputStream getInputStream() throws IOException {
+            return InputStreams.create(this.content,
+                    this.charset != null ? this.charset : Charsets.DEFAULT_CHARSET);
+        }
+
+        @Override
+        public void dispose() {
+        }
+
+        @Override
+        public SingleBody copy() {
+            return new BinaryBody2(this.content, this.charset);
+        }
+
     }
 
 }

Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageBuilder.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageBuilder.java?rev=1606167&r1=1606166&r2=1606167&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageBuilder.java
(original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageBuilder.java
Fri Jun 27 17:59:51 2014
@@ -238,8 +238,9 @@ public class MessageBuilder {
     /**
      * Sets transfer encoding of this message.
      *
-     * @param MIME type of this message
+     * @param mimeType MIME type of this message
      *            the MIME type to use.
+     * @param parameters content type parameters to use.
      */
     public void setContentType(String mimeType, NameValuePair... parameters) {
         if (mimeType == null) {
@@ -461,11 +462,10 @@ public class MessageBuilder {
     }
 
     /**
-     * Generates and sets message ID for this message.
+     * Sets message ID for this message.
      *
-     * @param hostname
-     *            host name to be included in the identifier or
-     *            <code>null</code> if no host name should be included.
+     * @param messageId
+     *            the message ID.
      */
     public MessageBuilder setMessageId(final String messageId) {
         if (messageId == null) {
@@ -975,45 +975,20 @@ public class MessageBuilder {
     }
 
     /**
-     * Sets body of this message.
+     * Sets body of this message.  Also sets the content type based on properties of
+     * the given {@link Body}.
      *
      * @param body
      *            the body.
      */
     public MessageBuilder setBody(Body body) {
         this.body = body;
-        return this;
-    }
-
-    /**
-     * Sets body of this message.
-     *
-     * @param body
-     *            the body.
-     */
-    public MessageBuilder setBody(String body, Charset charset) {
-        return setBody(SingleBodyBuilder.create()
-                .setText(body)
-                .setCharset(charset)
-                .build());
-    }
-
-    public Message build() {
-        Message message = new MessageImpl();
-        final Header header = message.getHeader();
-        if (!containsField(FieldName.MIME_VERSION)) {
-            header.setField(Fields.version("1.0"));
-        }
-        for (final Field field : fields) {
-            header.addField(field);
-        }
-
         if (!containsField(FieldName.CONTENT_TYPE) && body != null) {
             if (body instanceof Message) {
-                header.setField(Fields.contentType("message/rfc822"));
+                setField(Fields.contentType("message/rfc822"));
             } else if (body instanceof Multipart) {
                 Multipart multipart = (Multipart) body;
-                header.setField(Fields.contentType("multipart/" + multipart.getSubType(),
+                setField(Fields.contentType("multipart/" + multipart.getSubType(),
                         new NameValuePair("boundary", MimeUtil.createUniqueBoundary())));
             } else if (body instanceof TextBody) {
                 TextBody textBody = (TextBody) body;
@@ -1022,13 +997,58 @@ public class MessageBuilder {
                     mimeCharset = null;
                 }
                 if (mimeCharset != null) {
-                    header.setField(Fields.contentType("text/plain", new NameValuePair("charset",
mimeCharset)));
+                    setField(Fields.contentType("text/plain", new NameValuePair("charset",
mimeCharset)));
                 } else {
-                    header.setField(Fields.contentType("text/plain"));
+                    setField(Fields.contentType("text/plain"));
                 }
             }
         }
+        return this;
+    }
+
+    /**
+     * Sets text of this message with the charset.
+     *
+     * @param text
+     *            the text.
+     * @param charset
+     *            the charset of the text.
+     */
+    public MessageBuilder setBody(String text, Charset charset) {
+        return setBody(text, null, charset);
+    }
+
+    /**
+     * Sets text of this message with the given MIME subtype and charset.
+     *
+     * @param text
+     *            the text.
+     * @param charset
+     *            the charset of the text.
+     * @param subtype
+     *            the text subtype (e.g. &quot;plain&quot;, &quot;html&quot;
or
+     *            &quot;xml&quot;).
+     */
+    public MessageBuilder setBody(String text, String subtype, Charset charset) {
+        if (subtype != null) {
+            if (charset != null) {
+                setField(Fields.contentType("text/" + subtype, new NameValuePair("charset",
charset.name())));
+            } else {
+                setField(Fields.contentType("text/" + subtype));
+            }
+        }
+        return setBody(BasicBodyFactory.INSTANCE.textBody(text, charset));
+    }
 
+    public Message build() {
+        Message message = new MessageImpl();
+        final Header header = message.getHeader();
+        if (!containsField(FieldName.MIME_VERSION)) {
+            header.setField(Fields.version("1.0"));
+        }
+        for (final Field field : fields) {
+            header.addField(field);
+        }
         if (!containsField(FieldName.DATE)) {
             header.setField(Fields.date(new Date()));
         }

Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/SingleBodyBuilder.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/SingleBodyBuilder.java?rev=1606167&r1=1606166&r2=1606167&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/SingleBodyBuilder.java
(original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/SingleBodyBuilder.java
Fri Jun 27 17:59:51 2014
@@ -21,9 +21,7 @@ package org.apache.james.mime4j.message;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.io.Reader;
-import java.io.StringReader;
 import java.nio.charset.Charset;
 
 import org.apache.james.mime4j.Charsets;
@@ -42,6 +40,8 @@ public class SingleBodyBuilder {
         return new SingleBodyBuilder();
     }
 
+    private BodyFactory bodyFactory;
+
     private String text;
     private byte[] bin;
     private Charset charset;
@@ -50,6 +50,11 @@ public class SingleBodyBuilder {
         super();
     }
 
+    public SingleBodyBuilder withFactory(final BodyFactory bodyFactory) {
+        this.bodyFactory = bodyFactory;
+        return this;
+    }
+
     public SingleBodyBuilder setText(final String text) {
         this.text = text;
         this.bin = null;
@@ -79,28 +84,49 @@ public class SingleBodyBuilder {
         return this;
     }
 
-    public TextBody buildText() {
+    public TextBody buildText() throws IOException {
         Charset cs = this.charset != null ? this.charset : Charsets.DEFAULT_CHARSET;
-        if (this.text != null) {
-            return new StringBody1(this.text, cs);
-        } else if (this.bin != null) {
-            return new StringBody2(this.bin, cs);
+        if (this.bodyFactory != null) {
+            if (this.text != null) {
+                return this.bodyFactory.textBody(InputStreams.create(this.text, cs), cs.name());
+            } else if (this.bin != null) {
+                return this.bodyFactory.textBody(InputStreams.create(this.bin), cs.name());
+            } else {
+                return this.bodyFactory.textBody(InputStreams.create(new byte [] {}), cs.name());
+            }
         } else {
-            return new StringBody2(new byte [] {}, cs);
+            if (this.text != null) {
+                return BasicBodyFactory.INSTANCE.textBody(this.text, cs);
+            } else if (this.bin != null) {
+                return BasicBodyFactory.INSTANCE.textBody(this.bin, cs);
+            } else {
+                return BasicBodyFactory.INSTANCE.textBody(new byte [] {}, cs);
+            }
         }
     }
 
-    public BinaryBody buildBinary() {
-        if (this.bin != null) {
-            return new BinaryBody1(this.bin);
-        } else if (this.text != null) {
-            return new BinaryBody2(this.text, this.charset);
+    public BinaryBody buildBinary() throws IOException {
+        Charset cs = this.charset != null ? this.charset : Charsets.DEFAULT_CHARSET;
+        if (this.bodyFactory != null) {
+            if (this.text != null) {
+                return this.bodyFactory.binaryBody(InputStreams.create(this.text, cs));
+            } else if (this.bin != null) {
+                return this.bodyFactory.binaryBody(InputStreams.create(this.bin));
+            } else {
+                return this.bodyFactory.binaryBody((InputStreams.create(new byte[]{})));
+            }
         } else {
-            return new BinaryBody1(new byte [] {});
+            if (this.bin != null) {
+                return BasicBodyFactory.INSTANCE.binaryBody(this.bin);
+            } else if (this.text != null) {
+                return BasicBodyFactory.INSTANCE.binaryBody(this.text, cs);
+            } else {
+                return BasicBodyFactory.INSTANCE.binaryBody(new byte [] {});
+            }
         }
     }
 
-    public SingleBody build() {
+    public SingleBody build() throws IOException {
         if (this.charset != null) {
             return buildText();
         } else {
@@ -108,132 +134,4 @@ public class SingleBodyBuilder {
         }
     }
 
-    static class StringBody1 extends TextBody {
-
-        private final String content;
-        private final Charset charset;
-
-        StringBody1(final String content, final Charset charset) {
-            super();
-            this.content = content;
-            this.charset = charset;
-        }
-
-        @Override
-        public String getMimeCharset() {
-            return this.charset != null ? this.charset.name() : null;
-        }
-
-        @Override
-        public Reader getReader() throws IOException {
-            return new StringReader(this.content);
-        }
-
-        @Override
-        public InputStream getInputStream() throws IOException {
-            return InputStreams.create(this.content,
-                    this.charset != null ? this.charset : Charsets.DEFAULT_CHARSET);
-        }
-
-        @Override
-        public void dispose() {
-        }
-
-        @Override
-        public SingleBody copy() {
-            return new StringBody1(this.content, this.charset);
-        }
-
-    }
-
-    static class StringBody2 extends TextBody {
-
-        private final byte[] content;
-        private final Charset charset;
-
-        StringBody2(final byte[] content, final Charset charset) {
-            super();
-            this.content = content;
-            this.charset = charset;
-        }
-
-        @Override
-        public String getMimeCharset() {
-            return this.charset != null ? this.charset.name() : null;
-        }
-
-        @Override
-        public Reader getReader() throws IOException {
-            return new InputStreamReader(InputStreams.create(this.content), this.charset);
-        }
-
-        @Override
-        public InputStream getInputStream() throws IOException {
-            return InputStreams.create(this.content);
-        }
-
-        @Override
-        public void dispose() {
-        }
-
-        @Override
-        public SingleBody copy() {
-            return new StringBody2(this.content, this.charset);
-        }
-
-    }
-
-    static class BinaryBody1 extends BinaryBody {
-
-        private final byte[] content;
-
-        BinaryBody1(final byte[] content) {
-            super();
-            this.content = content;
-        }
-
-        @Override
-        public InputStream getInputStream() throws IOException {
-            return InputStreams.create(this.content);
-        }
-
-        @Override
-        public void dispose() {
-        }
-
-        @Override
-        public SingleBody copy() {
-            return new BinaryBody1(this.content);
-        }
-
-    }
-
-    static class BinaryBody2 extends BinaryBody {
-
-        private final String content;
-        private final Charset charset;
-
-        BinaryBody2(final String content, final Charset charset) {
-            super();
-            this.content = content;
-            this.charset = charset;
-        }
-
-        @Override
-        public InputStream getInputStream() throws IOException {
-            return InputStreams.create(this.content,
-                    this.charset != null ? this.charset : Charsets.DEFAULT_CHARSET);
-        }
-
-        @Override
-        public void dispose() {
-        }
-
-        @Override
-        public SingleBody copy() {
-            return new BinaryBody2(this.content, this.charset);
-        }
-
-    }
-
 }



Mime
View raw message