chibenwa commented on a change in pull request #515:
URL: https://github.com/apache/james-project/pull/515#discussion_r661913863



##########
File path: 
mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/utils/MimeMessageHeadersUtil.java
##########
@@ -21,38 +21,39 @@
 
 import java.util.List;
 import java.util.Optional;
-import java.util.stream.Collectors;
 
 import org.apache.james.mailbox.store.mail.model.MimeMessageId;
 import org.apache.james.mailbox.store.mail.model.Subject;
+import org.apache.james.mime4j.codec.DecodeMonitor;
+import org.apache.james.mime4j.field.UnstructuredFieldImpl;
 import org.apache.james.mime4j.message.HeaderImpl;
 import org.apache.james.mime4j.stream.Field;
 
+import com.github.steveash.guavate.Guavate;
+
 public class MimeMessageHeadersUtil {
-    public static MimeMessageId parseMimeMessageId(HeaderImpl headers) {
-        return new MimeMessageId(headers.getField("Message-ID").getBody());
+    public static Optional<MimeMessageId> parseMimeMessageId(HeaderImpl 
headers) {
+        return Optional.ofNullable(headers.getField("Message-ID")).map(field 
-> new MimeMessageId(field.getBody()));
     }
 
     public static Optional<MimeMessageId> parseInReplyTo(HeaderImpl headers) {
-        Field inReplyToField = headers.getField("In-Reply-To");
-        if (inReplyToField != null) {
-            return Optional.of(new MimeMessageId(inReplyToField.getBody()));
-        }
-        return Optional.empty();
+        return Optional.ofNullable(headers.getField("In-Reply-To")).map(field 
-> new MimeMessageId(field.getBody()));
     }
 
     public static Optional<List<MimeMessageId>> parseReferences(HeaderImpl 
headers) {
         List<Field> mimeMessageIdFields = headers.getFields("References");
         if (!mimeMessageIdFields.isEmpty()) {
             List<MimeMessageId> mimeMessageIdList = 
mimeMessageIdFields.stream()
                 .map(mimeMessageIdField -> new 
MimeMessageId(mimeMessageIdField.getBody()))
-                .collect(Collectors.toList());
+                .collect(Guavate.toImmutableList());
             return Optional.of(mimeMessageIdList);
         }
         return Optional.empty();
     }
 
-    public static Subject parseSubject(HeaderImpl headers) {
-        return new Subject(headers.getField("Subject").getBody());
+    public static Optional<Subject> parseSubject(HeaderImpl headers) {
+        return Optional.ofNullable(headers.getField("Subject"))
+            .map(field -> UnstructuredFieldImpl.PARSER.parse(field, 
DecodeMonitor.SILENT))

Review comment:
       We do not need to parse the field if it is already a 
`UnstructuredFieldImpl`




-- 
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