This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git


The following commit(s) were added to refs/heads/master by this push:
     new 5973a7ede2 JAMES-4112 Avoid unneeded mime message content updates when 
header is modified (#2625)
5973a7ede2 is described below

commit 5973a7ede2b945ed1b380424f34fa3e169546cfc
Author: Benoit TELLIER <btell...@linagora.com>
AuthorDate: Fri Feb 7 17:56:22 2025 +0100

    JAMES-4112 Avoid unneeded mime message content updates when header is 
modified (#2625)
---
 .../james/server/core/MimeMessageWrapper.java      |   3 +-
 .../org/apache/james/server/core/MailImplTest.java |  68 +++++++++++
 .../container/core/src/test/resources/invalid.eml  | 132 +++++++++++++++++++++
 3 files changed, 201 insertions(+), 2 deletions(-)

diff --git 
a/server/container/core/src/main/java/org/apache/james/server/core/MimeMessageWrapper.java
 
b/server/container/core/src/main/java/org/apache/james/server/core/MimeMessageWrapper.java
index 4743939dfe..e9d922d6f9 100644
--- 
a/server/container/core/src/main/java/org/apache/james/server/core/MimeMessageWrapper.java
+++ 
b/server/container/core/src/main/java/org/apache/james/server/core/MimeMessageWrapper.java
@@ -163,6 +163,7 @@ public class MimeMessageWrapper extends MimeMessage 
implements Disposable {
                     original.writeTo(out);
                     out.close();
                     source = src;
+                    saved = true;
                 }
 
             } catch (IOException ex) {
@@ -516,8 +517,6 @@ public class MimeMessageWrapper extends MimeMessage 
implements Disposable {
         if (headers == null) {
             loadHeaders();
         }
-        modified = true;
-        saved = false;
         headersModified = true;
     }
 
diff --git 
a/server/container/core/src/test/java/org/apache/james/server/core/MailImplTest.java
 
b/server/container/core/src/test/java/org/apache/james/server/core/MailImplTest.java
index 4972b21b67..bb78345026 100644
--- 
a/server/container/core/src/test/java/org/apache/james/server/core/MailImplTest.java
+++ 
b/server/container/core/src/test/java/org/apache/james/server/core/MailImplTest.java
@@ -25,6 +25,7 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.Date;
 import java.util.EnumSet;
 import java.util.Optional;
@@ -34,9 +35,12 @@ import java.util.stream.IntStream;
 import jakarta.mail.MessagingException;
 import jakarta.mail.internet.MimeMessage;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.james.core.MailAddress;
 import org.apache.james.core.MaybeSender;
 import org.apache.james.core.builder.MimeMessageBuilder;
+import org.apache.james.util.ClassLoaderUtils;
+import org.apache.james.util.MimeMessageUtil;
 import org.apache.mailet.AttributeName;
 import org.apache.mailet.AttributeValue;
 import org.apache.mailet.ContractMailTest;
@@ -178,6 +182,70 @@ public class MailImplTest extends ContractMailTest {
         
assertThat(mail.getPerRecipientSpecificHeaders()).isEqualTo(duplicate.getPerRecipientSpecificHeaders());
     }
 
+    @Test
+    void duplicateInvalid() throws Exception {
+        String name = MailUtil.newId();
+        MailImpl mail = MailImpl.builder()
+            .name(name)
+            .sender("sender@localhost")
+            .mimeMessage(new 
MimeMessageWrapper(MimeMessageUtil.mimeMessageFromStream(ClassLoaderUtils.getSystemResourceAsSharedStream("invalid.eml"))))
+            .build();
+
+        mail.getMessage().addHeader("abc", "def");
+
+        MailImpl duplicate = MailImpl.duplicate(mail);
+
+        ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
+        duplicate.getMessage().writeTo(bos1);
+
+        ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
+        duplicate.getMessage().writeTo(bos2);
+        assertThat(new ByteArrayInputStream(bos1.toByteArray()))
+            .hasSameContentAs(new ByteArrayInputStream(bos2.toByteArray()));
+    }
+
+    @Test
+    void addHeaderInvalid() throws Exception {
+        String name = MailUtil.newId();
+        MailImpl mail = MailImpl.builder()
+            .name(name)
+            .sender("sender@localhost")
+            .mimeMessage(new 
MimeMessageWrapper(MimeMessageUtil.mimeMessageFromStream(ClassLoaderUtils.getSystemResourceAsSharedStream("invalid.eml"))))
+            .build();
+
+        mail.getMessage().addHeader("abc", "def");
+
+        MailImpl duplicate = MailImpl.duplicate(mail);
+
+        ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
+        duplicate.getMessage().writeTo(bos1);
+        assertThat(new String(bos1.toByteArray(), StandardCharsets.US_ASCII))
+            .contains("abc: def");
+    }
+
+
+    @Test
+    void addHeader() throws Exception {
+        String name = MailUtil.newId();
+        MailImpl mail = MailImpl.builder()
+            .name(name)
+            .sender("sender@localhost")
+            .mimeMessage(new 
MimeMessageWrapper(MimeMessageUtil.mimeMessageFromStream(ClassLoaderUtils.getSystemResourceAsSharedStream("JAMES-1593.eml"))))
+            .build();
+
+        mail.getMessage().addHeader("abc", "def");
+
+        MailImpl duplicate = MailImpl.duplicate(mail);
+
+        ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
+        duplicate.getMessage().writeTo(bos1);
+        assertThat(new String(bos1.toByteArray(), StandardCharsets.US_ASCII))
+            .contains("abc: def");
+        MimeMessageWrapper wrapper = (MimeMessageWrapper) mail.getMessage();
+        assertThat(IOUtils.toString(new MimeMessageInputStream(wrapper), 
StandardCharsets.US_ASCII))
+            .contains("abc: def");
+    }
+
     @Test
     void setAttributeShouldThrowOnNullAttributeName() {
         MailImpl mail = newMail();
diff --git a/server/container/core/src/test/resources/invalid.eml 
b/server/container/core/src/test/resources/invalid.eml
new file mode 100644
index 0000000000..bf027b28c4
--- /dev/null
+++ b/server/container/core/src/test/resources/invalid.eml
@@ -0,0 +1,132 @@
+From: XXXX <x...@govmu.org>
+Message-Id: <343a4de5-9329-4487-a570-29a91e669...@govmu.org>
+Content-Type: multipart/alternative;
+       boundary="Apple-Mail=_F2D44D4D-8BA8-476D-B874-23E3A91AB142"
+Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.400.131.1.6\))
+Subject: YYYY
+Date: Wed, 5 Feb 2025 10:28:42 +0400
+X-Mailer: Apple Mail (2.3826.400.131.1.6)
+
+
+--Apple-Mail=_F2D44D4D-8BA8-476D-B874-23E3A91AB142
+Content-Transfer-Encoding: quoted-printable
+Content-Type: text/plain;
+       charset=utf-8
+
+This email (including any attachments) is intended for the person to =
+whom it is addressed. If you are not the addressee or have received this =
+email in error, please notify the sender immediately, delete it from =
+your system and do not copy, disclose or otherwise act upon it. Click =
+Here <https://www.sicom.mu/en/email-disclaimer> to consult the full =
+disclaimer.
+
+
+--Apple-Mail=_F2D44D4D-8BA8-476D-B874-23E3A91AB142
+Content-Type: multipart/related;
+       type="text/html";
+       boundary="Apple-Mail=_E197F722-9914-4A5D-854C-81A1C1BED428"
+
+
+--Apple-Mail=_E197F722-9914-4A5D-854C-81A1C1BED428
+Content-Transfer-Encoding: quoted-printable
+Content-Type: text/html;
+       charset=utf-8
+href=3D"https://www.sicom.mu/en/email-disclaimer"; target=3D"_blank"><span =
+style=3D"color: rgb(51, 153, 255);">Click Here</span></a></u><span =
+class=3D"Apple-converted-space">&nbsp;</span>to consult the full =
+disclaimer.</span></div></div></blockquote></div><br></div></div></body></=
+html>=
+
+--Apple-Mail=_E197F722-9914-4A5D-854C-81A1C1BED428
+Content-Transfer-Encoding: base64
+Content-Disposition: inline;
+       filename=Outlook-vehhhgpe.png
+Content-Type: image/png;
+       x-unix-mode=0666;
+       name="Outlook-vehhhgpe.png"
+Content-Id: <8175aa72-15e0-4f9b-b5d4-fe005f7490ad>
+
+iVBORw
+
+--Apple-Mail=_E197F722-9914-4A5D-854C-81A1C1BED428
+Content-Transfer-Encoding: base64
+Content-Disposition: inline;
+       filename*=us-ascii''Outlook%2DIcon%0A%0ADesc.png
+Content-Type: image/png;
+       x-unix-mode=0666;
+       name="Outlook-Icon
+
+Desc.png"
+Content-Id: <725d49dc-c591-460d-a04c-ad75842587aa>
+
+iVBORw0KG
+
+--Apple-Mail=_E197F722-9914-4A5D-854C-81A1C1BED428
+Content-Transfer-Encoding: base64
+Content-Disposition: inline;
+       filename=Outlook-iqu0ddn0.png
+Content-Type: image/png;
+       x-unix-mode=0666;
+       name="Outlook-iqu0ddn0.png"
+Content-Id: <1bab6c3b-1ade-406d-abd5-6177fdc1c060>
+
+iVBORw0KGgoAAAANSUhEUgAAAIs
+
+--Apple-Mail=_E197F722-9914-4A5D-854C-81A1C1BED428
+Content-Transfer-Encoding: base64
+Content-Disposition: inline;
+       filename=Outlook-nmbydsiy.png
+Content-Type: image/png;
+       x-unix-mode=0666;
+       name="Outlook-nmbydsiy.png"
+Content-Id: <dc75ef2d-67ff-4e8e-9466-12136bb05d2c>
+
+iVBORw0KGgoAAAANSUhEUgA
+
+--Apple-Mail=_E197F722-9914-4A5D-854C-81A1C1BED428
+Content-Transfer-Encoding: base64
+Content-Disposition: inline;
+       filename=Outlook-otqcvrrz.png
+Content-Type: image/png;
+       x-unix-mode=0666;
+       name="Outlook-otqcvrrz.png"
+Content-Id: <581175a5-c5ec-49d2-97dd-893f6547a24c>
+
+iVBORw0KGgoAAAANSUhE
+
+--Apple-Mail=_E197F722-9914-4A5D-854C-81A1C1BED428
+Content-Transfer-Encoding: base64
+Content-Disposition: inline;
+       filename=Outlook-zcc22hef.png
+Content-Type: image/png;
+       x-unix-mode=0666;
+       name="Outlook-zcc22hef.png"
+Content-Id: <3aec91c3-13af-44e4-ad9e-9f10c393da9a>
+
+iVBORw0KGgoAAAANSUh
+
+--Apple-Mail=_E197F722-9914-4A5D-854C-81A1C1BED428
+Content-Transfer-Encoding: base64
+Content-Disposition: inline;
+       filename=Outlook-rvl3vy1c.png
+Content-Type: image/png;
+       x-unix-mode=0666;
+       name="Outlook-rvl3vy1c.png"
+Content-Id: <2f7019f0-abef-42ef-a1a3-7cbcf574d296>
+
+iVBORw0KGgoAAAANS
+
+--Apple-Mail=_E197F722-9914-4A5D-854C-81A1C1BED428
+Content-Transfer-Encoding: base64
+Content-Disposition: inline;
+       filename=Outlook-jp2kz4q4.png
+Content-Type: image/png;
+       x-unix-mode=0666;
+       name="Outlook-jp2kz4q4.png"
+Content-Id: <d0483e84-9dd2-472b-a115-7581ef8cdc06>
+
+iVBORw0KGgoAAAANSUhEUgAA
+
+--Apple-Mail=_E197F722-9914-4A5D-854C-81A1C1BED428--
+
+--Apple-Mail=_F2D44D4D-8BA8-476D-B874-23E3A91AB142--


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org
For additional commands, e-mail: notifications-h...@james.apache.org

Reply via email to