vttranlina commented on code in PR #2490:
URL: https://github.com/apache/james-project/pull/2490#discussion_r1830538711


##########
mailet/standard/src/main/java/org/apache/james/transport/mailets/FoldLongLines.java:
##########
@@ -0,0 +1,56 @@
+/****************************************************************
+ * 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.transport.mailets;
+
+import java.util.List;
+
+import jakarta.mail.Header;
+import jakarta.mail.MessagingException;
+
+import org.apache.commons.lang3.stream.Streams;
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMailet;
+
+import com.github.fge.lambdas.Throwing;
+
+public class FoldLongLines extends GenericMailet {
+    public static final String MAX_CHARACTERS_PARAMETER_NAME = "maxCharacters";
+
+    private static final int MAX_CHARACTERS = 998;
+
+    private Integer maxCharacters;
+
+    @Override
+    public void init() throws MessagingException {
+        maxCharacters = 
getInitParameterAsOptional(MAX_CHARACTERS_PARAMETER_NAME).map(Integer::parseInt).orElse(MAX_CHARACTERS);
+    }
+
+    @Override
+    public void service(Mail mail) throws MessagingException {
+        List<Header> foldedHeaders = 
Streams.of(mail.getMessage().getAllHeaders().asIterator()).map(this::fold).toList();

Review Comment:
   IMO, filter `maxCharacters` here is well
   Then we don't need care the logic of `MimeUtil`



##########
mailet/standard/src/main/java/org/apache/james/transport/mailets/FoldLongLines.java:
##########
@@ -0,0 +1,56 @@
+/****************************************************************
+ * 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.transport.mailets;
+
+import java.util.List;
+
+import jakarta.mail.Header;
+import jakarta.mail.MessagingException;
+
+import org.apache.commons.lang3.stream.Streams;
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMailet;
+
+import com.github.fge.lambdas.Throwing;
+
+public class FoldLongLines extends GenericMailet {
+    public static final String MAX_CHARACTERS_PARAMETER_NAME = "maxCharacters";
+
+    private static final int MAX_CHARACTERS = 998;
+
+    private Integer maxCharacters;
+
+    @Override
+    public void init() throws MessagingException {
+        maxCharacters = 
getInitParameterAsOptional(MAX_CHARACTERS_PARAMETER_NAME).map(Integer::parseInt).orElse(MAX_CHARACTERS);
+    }
+
+    @Override
+    public void service(Mail mail) throws MessagingException {
+        List<Header> foldedHeaders = 
Streams.of(mail.getMessage().getAllHeaders().asIterator()).map(this::fold).toList();
+        foldedHeaders.forEach(Throwing.consumer(header -> 
mail.getMessage().setHeader(header.getName(), header.getValue())));
+    }
+
+    private Header fold(Header header) {
+        // TODO After new release of mime4j, update to use MimeUtil of mime4j 
and remove MimeUtil class file
+        String folded = MimeUtil.fold(header.getValue(), 
header.getName().length() + 2, maxCharacters);

Review Comment:
   Why `2` ? 
   this is for ": " ?



##########
mailet/standard/src/test/java/org/apache/james/transport/mailets/FoldLongLinesTest.java:
##########
@@ -0,0 +1,98 @@
+/****************************************************************
+ * 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.transport.mailets;
+
+import static 
org.apache.james.transport.mailets.FoldLongLines.MAX_CHARACTERS_PARAMETER_NAME;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+
+import jakarta.mail.MessagingException;
+
+import org.apache.commons.lang3.stream.Streams;
+import org.apache.james.core.builder.MimeMessageBuilder;
+import org.apache.mailet.Mail;
+import org.apache.mailet.Mailet;
+import org.apache.mailet.MailetContext;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMailContext;
+import org.apache.mailet.base.test.FakeMailetConfig;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+
+public class FoldLongLinesTest {
+    static final String HEADER_NAME = "References";
+    static final String HEADER_VALUE = "<a1@gmailcom> <a2@gmailcom> 
<a3@gmailcom>";
+    static final String FOLDED_LINE = "<a1@gmailcom>\r\n" +
+        " <a2@gmailcom> <a3@gmailcom>";
+
+    private Mailet foldMailet;
+    private MailetContext mailetContext;
+
+    @BeforeEach
+    void beforeEach() throws MessagingException {
+        foldMailet = new FoldLongLines();
+        mailetContext = FakeMailContext.builder()
+            .logger(mock(Logger.class))
+            .build();
+        FakeMailetConfig mailetConfig = FakeMailetConfig.builder()
+            .mailetName("Test")
+            .mailetContext(mailetContext)
+            .setProperty(MAX_CHARACTERS_PARAMETER_NAME, "30")
+            .build();
+        foldMailet.init(mailetConfig);
+    }
+
+    @Test
+    void serviceShouldFoldLinesWhenTheyExceedMaxCharacters() throws 
MessagingException {
+        FakeMailetConfig mailetConfig = FakeMailetConfig.builder()
+            .mailetName("Test")
+            .mailetContext(mailetContext)
+            .setProperty(MAX_CHARACTERS_PARAMETER_NAME, "30")
+            .build();
+        foldMailet.init(mailetConfig);
+
+        Mail mail = FakeMail.builder()
+            
.name("mail").mimeMessage(MimeMessageBuilder.mimeMessageBuilder().addHeader(HEADER_NAME,
 HEADER_VALUE).build())
+            .build();
+        foldMailet.service(mail);
+
+        String actual = 
Streams.of(mail.getMessage().getAllHeaders()).filter(header -> 
header.getName().equals(HEADER_NAME)).findFirst().get().getValue();

Review Comment:
   verify 
   `mail.getMessage().getAllHeaders()).filter(header -> 
header.getName().equals(HEADER_NAME)` has size = `1` 
   it is more strong, 
   This strengthens the validation and ensures we replace the header value 
instead of appending a new one.
   



-- 
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: notifications-unsubscr...@james.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


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

Reply via email to