Arsnael commented on code in PR #2005:
URL: https://github.com/apache/james-project/pull/2005#discussion_r1492216066


##########
server/data/data-jmap/src/main/java/org/apache/james/jmap/mime4j/JamesBodyFactory.java:
##########
@@ -0,0 +1,366 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.mime4j;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
+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.message.BasicBodyFactory;
+import org.apache.james.mime4j.message.BodyFactory;
+import org.apache.james.mime4j.util.ByteArrayOutputStreamRecycler;
+import org.apache.james.mime4j.util.ContentUtil;
+
+import com.google.common.io.CountingOutputStream;
+
+/**
+ * Factory for creating message bodies.
+ */
+public class JamesBodyFactory implements BodyFactory {
+
+    public static final BasicBodyFactory INSTANCE = new BasicBodyFactory();
+
+    private final Charset defaultCharset;
+
+    public JamesBodyFactory() {
+        this(true);
+    }
+
+    public JamesBodyFactory(final Charset defaultCharset) {
+        this.defaultCharset = defaultCharset;
+    }
+
+    public JamesBodyFactory(final boolean lenient) {
+        this(lenient ? Charset.defaultCharset() : null);
+    }
+
+    /**
+     * @return the defaultCharset
+     */
+    public Charset getDefaultCharset() {
+        return defaultCharset;
+    }
+
+    /**
+     * <p>
+     * Select the Charset for the given <code>mimeCharset</code> string.
+     * </p>
+     * <p>
+     * If you need support for non standard or invalid 
<code>mimeCharset</code> specifications you might want to
+     * create your own derived {@link BodyFactory} extending {@link 
BasicBodyFactory} and overriding this method as
+     * suggested by <a 
href="https://issues.apache.org/jira/browse/MIME4J-218";>MIME4J-218</a>
+     * </p>
+     * <p>
+     * The default behavior is lenient, invalid <code>mimeCharset</code> 
specifications will return the
+     * <code>defaultCharset</code>.
+     * </p>
+     *
+     * @param mimeCharset - the string specification for a Charset e.g. "UTF-8"
+     * @throws UnsupportedEncodingException if the mimeCharset is invalid
+     */
+    protected Charset resolveCharset(final String mimeCharset) throws 
UnsupportedEncodingException {
+        if (mimeCharset != null) {
+            try {
+                return Charset.forName(mimeCharset);
+            } catch (UnsupportedCharsetException ex) {
+                if (defaultCharset == null) {
+                    throw new UnsupportedEncodingException(mimeCharset);
+                }
+            } catch (IllegalCharsetNameException ex) {
+                if (defaultCharset == null) {
+                    throw new UnsupportedEncodingException(mimeCharset);
+                }
+            }
+        }
+        return defaultCharset;
+    }
+
+    public TextBody textBody(final String text, final String mimeCharset) 
throws UnsupportedEncodingException {
+        if (text == null) {
+            throw new IllegalArgumentException("Text may not be null");
+        }
+        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 StringBody3(ContentUtil.bufferEfficient(content), 
resolveCharset(mimeCharset));
+    }
+
+    public TextBody textBody(final String text, final Charset charset) {
+        if (text == null) {
+            throw new IllegalArgumentException("Text may not be null");
+        }
+        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 {
+        CountingOutputStream out = new 
CountingOutputStream(OutputStream.nullOutputStream());
+        is.transferTo(out);
+        return new FakeBinaryBody(out.getCount());
+    }
+
+    public BinaryBody binaryBody(final byte[] buf) {
+        return new BinaryBody1(buf);
+    }
+
+    static class StringBody1 extends TextBody {

Review Comment:
   Copied from BasicBodyFactory form mime4j, makes kinda sense, forget this 
comment



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to