Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5d98c15b authored by cketti's avatar cketti
Browse files

Use MimeParameterEncoder and MimeParameterDecoder in MimeMessageHelper

parent 5f7dac45
Loading
Loading
Loading
Loading
+8 −4
Original line number Diff line number Diff line
@@ -84,7 +84,8 @@ public class MessageBuilderTest extends RobolectricTest {
            "text =E2=98=AD";

    private static final String MESSAGE_CONTENT_WITH_ATTACH = "" +
            "Content-Type: multipart/mixed; boundary=\"" + BOUNDARY_1 + "\"\r\n" +
            "Content-Type: multipart/mixed;\r\n" +
            " boundary=" + BOUNDARY_1 + "\r\n" +
            "Content-Transfer-Encoding: 7bit\r\n" +
            "\r\n" +
            "--" + BOUNDARY_1 + "\r\n" +
@@ -107,7 +108,8 @@ public class MessageBuilderTest extends RobolectricTest {
            "--" + BOUNDARY_1 + "--\r\n";

    private static final String MESSAGE_CONTENT_WITH_LONG_FILE_NAME =
            "Content-Type: multipart/mixed; boundary=\"" + BOUNDARY_1 + "\"\r\n" +
            "Content-Type: multipart/mixed;\r\n" +
            " boundary=" + BOUNDARY_1 + "\r\n" +
            "Content-Transfer-Encoding: 7bit\r\n" +
            "\r\n" +
            "--" + BOUNDARY_1 + "\r\n" +
@@ -133,7 +135,8 @@ public class MessageBuilderTest extends RobolectricTest {

    private static final String ATTACHMENT_FILENAME_NON_ASCII = "テスト文書.txt";
    private static final String MESSAGE_CONTENT_WITH_ATTACH_NON_ASCII_FILENAME = "" +
            "Content-Type: multipart/mixed; boundary=\"" + BOUNDARY_1 + "\"\r\n" +
            "Content-Type: multipart/mixed;\r\n" +
            " boundary=" + BOUNDARY_1 + "\r\n" +
            "Content-Transfer-Encoding: 7bit\r\n" +
            "\r\n" +
            "--" + BOUNDARY_1 + "\r\n" +
@@ -156,7 +159,8 @@ public class MessageBuilderTest extends RobolectricTest {
            "--" + BOUNDARY_1 + "--\r\n";

    private static final String MESSAGE_CONTENT_WITH_MESSAGE_ATTACH = "" +
            "Content-Type: multipart/mixed; boundary=\"" + BOUNDARY_1 + "\"\r\n" +
            "Content-Type: multipart/mixed;\r\n" +
            " boundary=" + BOUNDARY_1 + "\r\n" +
            "Content-Transfer-Encoding: 7bit\r\n" +
            "\r\n" +
            "--" + BOUNDARY_1 + "\r\n" +
+16 −0
Original line number Diff line number Diff line
@@ -6,6 +6,22 @@ object Headers {
        return MimeParameterEncoder.encode(mimeType, mapOf("name" to name))
    }

    @JvmStatic
    fun contentType(mimeType: String, charset: String, name: String?): String {
        val parameters = if (name == null) {
            mapOf("charset" to charset)
        } else {
            mapOf("charset" to charset, "name" to name)
        }

        return MimeParameterEncoder.encode(mimeType, parameters)
    }

    @JvmStatic
    fun contentTypeForMultipart(mimeType: String, boundary: String): String {
        return MimeParameterEncoder.encode(mimeType, mapOf("boundary" to boundary))
    }

    @JvmStatic
    @JvmOverloads
    fun contentDisposition(disposition: String, fileName: String, size: Long? = null): String {
+9 −11
Original line number Diff line number Diff line
@@ -23,23 +23,21 @@ public class MimeMessageHelper {
        if (body instanceof Multipart) {
            Multipart multipart = ((Multipart) body);
            multipart.setParent(part);
            String mimeType = multipart.getMimeType();
            String contentType = String.format("%s; boundary=\"%s\"", mimeType, multipart.getBoundary());
            String contentType = Headers.contentTypeForMultipart(multipart.getMimeType(), multipart.getBoundary());
            part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType);
            // note: if this is ever changed to 8bit, multipart/signed parts must always be 7bit!
            setEncoding(part, MimeUtil.ENC_7BIT);
        } else if (body instanceof TextBody) {
            String contentType;
            if (MimeUtility.mimeTypeMatches(part.getMimeType(), "text/*")) {
                contentType = String.format("%s;\r\n charset=utf-8", part.getMimeType());
                String name = MimeUtility.getHeaderParameter(part.getContentType(), "name");
                if (name != null) {
                    contentType += String.format(";\r\n name=\"%s\"", name);
                }
            MimeValue contentTypeHeader = MimeParameterDecoder.decode(part.getContentType());
            String mimeType = contentTypeHeader.getValue();
            if (MimeUtility.mimeTypeMatches(mimeType, "text/*")) {
                String name = contentTypeHeader.getParameters().get("name");

                String contentType = Headers.contentType(mimeType, "utf-8", name);
                part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType);
            } else {
                contentType = part.getMimeType();
                part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, mimeType);
            }
            part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType);

            setEncoding(part, MimeUtil.ENC_QUOTED_PRINTABLE);
        } else if (body instanceof RawDataBody) {
+12 −6
Original line number Diff line number Diff line
@@ -67,7 +67,8 @@ class MessageTest {
            Subject: Test Message
            Date: Wed, 28 Aug 2013 08:51:09 -0400
            MIME-Version: 1.0
            Content-Type: multipart/mixed; boundary="----Boundary103"
            Content-Type: multipart/mixed;
             boundary=----Boundary103
            Content-Transfer-Encoding: 7bit

            ------Boundary103
@@ -96,7 +97,8 @@ class MessageTest {
            Subject: Test Message
            Date: Wed, 28 Aug 2013 08:51:09 -0400
            MIME-Version: 1.0
            Content-Type: multipart/mixed; boundary="----Boundary102"
            Content-Type: multipart/mixed;
             boundary=----Boundary102
            Content-Transfer-Encoding: 7bit

            ------Boundary102
@@ -125,7 +127,8 @@ class MessageTest {
            Subject: Test Message
            Date: Wed, 28 Aug 2013 08:51:09 -0400
            MIME-Version: 1.0
            Content-Type: multipart/mixed; boundary="----Boundary101"
            Content-Type: multipart/mixed;
             boundary=----Boundary101
            Content-Transfer-Encoding: 7bit

            ------Boundary101
@@ -163,7 +166,8 @@ class MessageTest {
        bodyPart.writeTo(out)

        assertThat(out.toString()).isEqualTo("""
            Content-Type: multipart/mixed; boundary="----Boundary103"
            Content-Type: multipart/mixed;
             boundary=----Boundary103
            Content-Transfer-Encoding: 7bit

            ------Boundary103
@@ -192,7 +196,8 @@ class MessageTest {
            Subject: Test Message
            Date: Wed, 28 Aug 2013 08:51:09 -0400
            MIME-Version: 1.0
            Content-Type: multipart/mixed; boundary="----Boundary102"
            Content-Type: multipart/mixed;
             boundary=----Boundary102
            Content-Transfer-Encoding: 7bit

            ------Boundary102
@@ -221,7 +226,8 @@ class MessageTest {
            Subject: Test Message
            Date: Wed, 28 Aug 2013 08:51:09 -0400
            MIME-Version: 1.0
            Content-Type: multipart/mixed; boundary="----Boundary101"
            Content-Type: multipart/mixed;
             boundary=----Boundary101
            Content-Transfer-Encoding: 7bit

            ------Boundary101