Skip to content

Commit 6058cea

Browse files
committed
Refactor header ignore list as HeadersToIgnoreWhenParsingExternalEmails in the code module, so both main module and Outlook module can use it when parsing external emails.
Also made `DecodedHeader` public because it was a return type already in a public interface (MimeMessageParser). Simplified `MimeMessageParser` codebase by replacing getters with @Getter
1 parent 5ade471 commit 6058cea

File tree

4 files changed

+36
-153
lines changed

4 files changed

+36
-153
lines changed
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
package org.simplejavamail.internal.outlooksupport.converter;
1+
package org.simplejavamail.api.internal.general;
22

33
import java.util.ArrayList;
44
import java.util.List;
55

6-
class HeadersToIgnore {
6+
public class HeadersToIgnoreWhenParsingExternalEmails {
7+
8+
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
9+
public static boolean shouldIgnoreHeader(final String headerName) {
10+
return HEADERS_TO_IGNORE.contains(headerName);
11+
}
712

813
/**
914
* Contains the headers we will ignore, because either we set the information differently (such as Subject) or we recognize the header as
10-
* interfering or obsolete for new emails).
15+
* interfering or obsolete for new emails.
1116
*/
12-
static final List<String> HEADERS_TO_IGNORE = new ArrayList<>();
17+
private static final List<String> HEADERS_TO_IGNORE = new ArrayList<>();
1318

1419
static {
1520
// taken from: protected jakarta.mail.internet.InternetHeaders constructor

modules/outlook-module/src/main/java/org/simplejavamail/internal/outlooksupport/converter/OutlookEmailConverter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
import org.simplejavamail.api.email.EmailPopulatingBuilder;
1010
import org.simplejavamail.api.email.EmailStartingBuilder;
1111
import org.simplejavamail.api.internal.general.EmailPopulatingBuilderFactory;
12+
import org.simplejavamail.api.internal.general.HeadersToIgnoreWhenParsingExternalEmails;
1213
import org.simplejavamail.api.internal.outlooksupport.model.EmailFromOutlookMessage;
1314
import org.simplejavamail.internal.modules.OutlookModule;
1415
import org.simplejavamail.internal.outlooksupport.internal.model.OutlookMessageProxy;
1516
import org.simplejavamail.internal.util.InternalEmailConverter;
16-
import org.simplejavamail.internal.util.MiscUtil;
1717
import org.simplejavamail.outlookmessageparser.model.OutlookAttachment;
1818
import org.simplejavamail.outlookmessageparser.model.OutlookFileAttachment;
1919
import org.simplejavamail.outlookmessageparser.model.OutlookMessage;
@@ -27,7 +27,6 @@
2727
import java.util.Map;
2828

2929
import static java.util.Optional.ofNullable;
30-
import static org.simplejavamail.internal.outlooksupport.converter.HeadersToIgnore.HEADERS_TO_IGNORE;
3130
import static org.simplejavamail.internal.util.MiscUtil.extractCID;
3231
import static org.simplejavamail.internal.util.MiscUtil.valueNullOrEmpty;
3332
import static org.simplejavamail.internal.util.Preconditions.checkNonEmptyArgument;
@@ -140,7 +139,7 @@ private static void parseHeader(final String headerName, final String headerValu
140139
builder.withReturnReceiptTo(headerValue);
141140
} else if (isEmailHeader(headerName, headerValue, "Return-Path")) {
142141
builder.withBounceTo(headerValue);
143-
} else if (!HEADERS_TO_IGNORE.contains(headerName)) {
142+
} else if (!HeadersToIgnoreWhenParsingExternalEmails.shouldIgnoreHeader(headerName)) {
144143
builder.withHeader(headerName, headerValue);
145144
} else {
146145
// header recognized, but not relevant (see #HEADERS_TO_IGNORE)

modules/simple-java-mail/src/main/java/org/simplejavamail/converter/internal/mimemessage/DecodedHeader.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
import lombok.val;
66

77
@Value
8-
class DecodedHeader {
8+
public class DecodedHeader {
99

1010
String name;
1111
String value;
1212

1313
public static DecodedHeader of(Header h) {
14-
val decodedName = MimeMessageParser.decodeText(h.getName());
15-
val decodedValue = MimeMessageParser.decodeText(h.getValue());
16-
return new DecodedHeader(decodedName, decodedValue);
14+
return new DecodedHeader(
15+
MimeMessageParser.decodeText(h.getName()),
16+
MimeMessageParser.decodeText(h.getValue())
17+
);
1718
}
18-
}
19+
}

modules/simple-java-mail/src/main/java/org/simplejavamail/converter/internal/mimemessage/MimeMessageParser.java

Lines changed: 19 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package org.simplejavamail.converter.internal.mimemessage;
22

3+
import jakarta.activation.CommandMap;
4+
import jakarta.activation.MailcapCommandMap;
5+
import lombok.Getter;
36
import org.eclipse.angus.mail.handlers.text_plain;
47
import jakarta.activation.ActivationDataFlavor;
5-
import jakarta.activation.CommandMap;
68
import jakarta.activation.DataHandler;
79
import jakarta.activation.DataSource;
8-
import jakarta.activation.MailcapCommandMap;
910
import jakarta.mail.Address;
1011
import jakarta.mail.Message.RecipientType;
1112
import jakarta.mail.MessagingException;
@@ -23,6 +24,7 @@
2324
import lombok.val;
2425
import org.jetbrains.annotations.NotNull;
2526
import org.jetbrains.annotations.Nullable;
27+
import org.simplejavamail.api.internal.general.HeadersToIgnoreWhenParsingExternalEmails;
2628
import org.simplejavamail.internal.util.MiscUtil;
2729
import org.simplejavamail.internal.util.NamedDataSource;
2830
import org.simplejavamail.internal.util.Preconditions;
@@ -60,57 +62,8 @@
6062
*/
6163
public final class MimeMessageParser {
6264

63-
/**
64-
* Contains the headers we will ignore, because either we set the information differently (such as Subject) or we recognize the header as
65-
* interfering or obsolete for new emails).
66-
*/
67-
private static final List<String> HEADERS_TO_IGNORE = new ArrayList<>();
68-
6965
static {
70-
// taken from: protected jakarta.mail.internet.InternetHeaders constructor
71-
/*
72-
* When extracting information to create an Email, we're NOT interested in the following headers:
73-
*/
74-
// HEADERS_TO_IGNORE.add("Return-Path"); // bounceTo address
75-
HEADERS_TO_IGNORE.add("Received");
76-
HEADERS_TO_IGNORE.add("Resent-Date");
77-
HEADERS_TO_IGNORE.add("Resent-From");
78-
HEADERS_TO_IGNORE.add("Resent-Sender");
79-
HEADERS_TO_IGNORE.add("Resent-To");
80-
HEADERS_TO_IGNORE.add("Resent-Cc");
81-
HEADERS_TO_IGNORE.add("Resent-Bcc");
82-
HEADERS_TO_IGNORE.add("Resent-Message-Id");
83-
HEADERS_TO_IGNORE.add("Date");
84-
HEADERS_TO_IGNORE.add("From");
85-
HEADERS_TO_IGNORE.add("Sender");
86-
HEADERS_TO_IGNORE.add("Reply-To");
87-
HEADERS_TO_IGNORE.add("To");
88-
HEADERS_TO_IGNORE.add("Cc");
89-
HEADERS_TO_IGNORE.add("Bcc");
90-
HEADERS_TO_IGNORE.add("Message-Id");
91-
// The next two are needed for replying to
92-
// HEADERS_TO_IGNORE.add("In-Reply-To");
93-
// HEADERS_TO_IGNORE.add("References");
94-
HEADERS_TO_IGNORE.add("Subject");
95-
HEADERS_TO_IGNORE.add("Comments");
96-
HEADERS_TO_IGNORE.add("Keywords");
97-
HEADERS_TO_IGNORE.add("Errors-To");
98-
HEADERS_TO_IGNORE.add("MIME-Version");
99-
HEADERS_TO_IGNORE.add("Content-Type");
100-
HEADERS_TO_IGNORE.add("Content-Transfer-Encoding");
101-
HEADERS_TO_IGNORE.add("Content-MD5");
102-
HEADERS_TO_IGNORE.add(":");
103-
HEADERS_TO_IGNORE.add("Content-Length");
104-
HEADERS_TO_IGNORE.add("Status");
105-
// extra headers that should be ignored, which may originate from nested attachments
106-
HEADERS_TO_IGNORE.add("Content-Disposition");
107-
HEADERS_TO_IGNORE.add("size");
108-
HEADERS_TO_IGNORE.add("filename");
109-
HEADERS_TO_IGNORE.add("Content-ID");
110-
HEADERS_TO_IGNORE.add("name");
111-
HEADERS_TO_IGNORE.add("From");
112-
113-
MailcapCommandMap mc = (MailcapCommandMap)CommandMap.getDefaultCommandMap();
66+
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
11467
mc.addMailcap("text/calendar;; x-java-content-handler=" + text_calendar.class.getName());
11568
CommandMap.setDefaultCommandMap(mc);
11669
}
@@ -210,7 +163,7 @@ private static void parseHeader(final DecodedHeader header, @NotNull final Parse
210163
parsedComponents.returnReceiptTo = createAddress(headerValue, "Return-Receipt-To");
211164
} else if (isEmailHeader(header, "Return-Path")) {
212165
parsedComponents.bounceToAddress = createAddress(headerValue, "Return-Path");
213-
} else if (!HEADERS_TO_IGNORE.contains(headerName)) {
166+
} else if (!HeadersToIgnoreWhenParsingExternalEmails.shouldIgnoreHeader(headerName)) {
214167
if (!parsedComponents.headers.containsKey(headerName)) {
215168
parsedComponents.headers.put(headerName, new ArrayList<>());
216169
}
@@ -439,8 +392,7 @@ private static String parseDataSourceName(@NotNull final Part part, @NotNull fin
439392
return !valueNullOrEmpty(result) ? decodeText(result) : null;
440393
}
441394

442-
@NotNull
443-
private static byte[] readContent(@NotNull final InputStream is) {
395+
private static byte @NotNull [] readContent(@NotNull final InputStream is) {
444396
try {
445397
return MiscUtil.readInputStreamToBytes(is);
446398
} catch (final IOException e) {
@@ -606,86 +558,27 @@ static void moveInvalidEmbeddedResourcesToAttachments(ParsedMimeMessageComponent
606558
}
607559
}
608560

561+
@Getter
609562
public static class ParsedMimeMessageComponents {
610-
@SuppressWarnings("unchecked")
611563
final Set<MimeDataSource> attachmentList = new TreeSet<>();
612564
final Map<String, DataSource> cidMap = new TreeMap<>();
613565
private final Map<String, Collection<Object>> headers = new HashMap<>();
614566
private final List<InternetAddress> toAddresses = new ArrayList<>();
615567
private final List<InternetAddress> ccAddresses = new ArrayList<>();
616568
private final List<InternetAddress> bccAddresses = new ArrayList<>();
617-
private String messageId;
618-
private String subject;
619-
private InternetAddress fromAddress;
620-
private InternetAddress replyToAddresses;
621-
private InternetAddress dispositionNotificationTo;
622-
private InternetAddress returnReceiptTo;
623-
private InternetAddress bounceToAddress;
624-
private String contentTransferEncoding;
569+
@Nullable private String messageId;
570+
@Nullable private String subject;
571+
@Nullable private InternetAddress fromAddress;
572+
@Nullable private InternetAddress replyToAddresses;
573+
@Nullable private InternetAddress dispositionNotificationTo;
574+
@Nullable private InternetAddress returnReceiptTo;
575+
@Nullable private InternetAddress bounceToAddress;
576+
@Nullable private String contentTransferEncoding;
625577
private final StringBuilder plainContent = new StringBuilder();
626578
final StringBuilder htmlContent = new StringBuilder();
627-
private String calendarMethod;
628-
private String calendarContent;
629-
private Date sentDate;
630-
631-
@Nullable
632-
public String getMessageId() {
633-
return messageId;
634-
}
635-
636-
public Set<MimeDataSource> getAttachmentList() {
637-
return attachmentList;
638-
}
639-
640-
public Map<String, DataSource> getCidMap() {
641-
return cidMap;
642-
}
643-
644-
public Map<String, Collection<Object>> getHeaders() {
645-
return headers;
646-
}
647-
648-
public List<InternetAddress> getToAddresses() {
649-
return toAddresses;
650-
}
651-
652-
public List<InternetAddress> getCcAddresses() {
653-
return ccAddresses;
654-
}
655-
656-
public List<InternetAddress> getBccAddresses() {
657-
return bccAddresses;
658-
}
659-
660-
@Nullable
661-
public String getSubject() {
662-
return subject;
663-
}
664-
665-
@Nullable
666-
public InternetAddress getFromAddress() {
667-
return fromAddress;
668-
}
669-
670-
@Nullable
671-
public InternetAddress getReplyToAddresses() {
672-
return replyToAddresses;
673-
}
674-
675-
@Nullable
676-
public InternetAddress getDispositionNotificationTo() {
677-
return dispositionNotificationTo;
678-
}
679-
680-
@Nullable
681-
public InternetAddress getReturnReceiptTo() {
682-
return returnReceiptTo;
683-
}
684-
685-
@Nullable
686-
public InternetAddress getBounceToAddress() {
687-
return bounceToAddress;
688-
}
579+
@Nullable private String calendarMethod;
580+
@Nullable private String calendarContent;
581+
@Nullable private Date sentDate;
689582

690583
@Nullable
691584
public String getPlainContent() {
@@ -697,21 +590,6 @@ public String getHtmlContent() {
697590
return htmlContent.length() == 0 ? null : htmlContent.toString();
698591
}
699592

700-
@Nullable
701-
public String getCalendarContent() {
702-
return calendarContent;
703-
}
704-
705-
@Nullable
706-
public String getContentTransferEncoding() {
707-
return contentTransferEncoding;
708-
}
709-
710-
@Nullable
711-
public String getCalendarMethod() {
712-
return calendarMethod;
713-
}
714-
715593
@Nullable
716594
public Date getSentDate() {
717595
return sentDate != null ? new Date(sentDate.getTime()) : null;

0 commit comments

Comments
 (0)