|
1 | 1 | package org.simplejavamail.api.internal.general;
|
2 | 2 |
|
3 |
| -import java.util.ArrayList; |
| 3 | +import java.util.Arrays; |
4 | 4 | import java.util.List;
|
5 | 5 |
|
| 6 | +import static java.util.Collections.singletonList; |
| 7 | + |
6 | 8 | public class HeadersToIgnoreWhenParsingExternalEmails {
|
7 | 9 |
|
8 | 10 | @SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
9 | 11 | public static boolean shouldIgnoreHeader(final String headerName) {
|
10 |
| - return HEADERS_TO_IGNORE.contains(headerName); |
| 12 | + return HEADERS_TO_IGNORE_CASE_INSENSITIVE.stream().map(MessageHeader::getName).anyMatch(headerName::equalsIgnoreCase) || |
| 13 | + HEADERS_TO_IGNORE_CASE_SENSITIVE.stream().map(MessageHeader::getName).anyMatch(headerName::equals); |
11 | 14 | }
|
12 | 15 |
|
13 | 16 | /**
|
14 |
| - * Contains the headers we will ignore, because either we set the information differently (such as Subject) or we recognize the header as |
15 |
| - * interfering or obsolete for new emails. |
| 17 | + * Contains the headers we will ignore, because either we set the information differently (such as Subject) or we |
| 18 | + * recognize the header as interfering or obsolete for new emails. |
16 | 19 | */
|
17 |
| - private static final List<String> HEADERS_TO_IGNORE = new ArrayList<>(); |
| 20 | + private static final List<MessageHeader> HEADERS_TO_IGNORE_CASE_INSENSITIVE = Arrays.asList( |
| 21 | + MessageHeader.RECEIVED, |
| 22 | + MessageHeader.RESENT_DATE, |
| 23 | + MessageHeader.RESENT_FROM, |
| 24 | + MessageHeader.RESENT_SENDER, |
| 25 | + MessageHeader.RESENT_TO, |
| 26 | + MessageHeader.RESENT_CC, |
| 27 | + MessageHeader.RESENT_BCC, |
| 28 | + MessageHeader.DATE, |
| 29 | + MessageHeader.FROM, |
| 30 | + MessageHeader.SENDER, |
| 31 | + MessageHeader.TO, |
| 32 | + MessageHeader.CC, |
| 33 | + MessageHeader.BCC, |
| 34 | + MessageHeader.SUBJECT, |
| 35 | + MessageHeader.CONTENT_MD5, |
| 36 | + MessageHeader.CONTENT_LENGTH, |
| 37 | + MessageHeader.COLON, |
| 38 | + MessageHeader.STATUS, |
| 39 | + MessageHeader.CONTENT_DISPOSITION, |
| 40 | + MessageHeader.SIZE, |
| 41 | + MessageHeader.FILENAME, |
| 42 | + MessageHeader.CONTENT_ID, |
| 43 | + MessageHeader.NAME, |
| 44 | + MessageHeader.RESENT_MESSAGE_ID, |
| 45 | + MessageHeader.COMMENTS, |
| 46 | + MessageHeader.KEYWORDS, |
| 47 | + MessageHeader.ERRORS_TO, |
| 48 | + MessageHeader.MIME_VERSION, |
| 49 | + MessageHeader.CONTENT_TYPE, |
| 50 | + MessageHeader.CONTENT_TRANSFER_ENCODING, |
| 51 | + MessageHeader.RESENT_MESSAGE_ID, |
| 52 | + MessageHeader.REPLY_TO |
| 53 | + ); |
18 | 54 |
|
19 |
| - static { |
20 |
| - // taken from: protected jakarta.mail.internet.InternetHeaders constructor |
21 |
| - /* |
22 |
| - * When extracting information to create an Email, we're NOT interested in the following headers: |
23 |
| - */ |
24 |
| - // HEADERS_TO_IGNORE.add("Return-Path"); // bounceTo address |
25 |
| - HEADERS_TO_IGNORE.add("Received"); |
26 |
| - HEADERS_TO_IGNORE.add("Resent-Date"); |
27 |
| - HEADERS_TO_IGNORE.add("Resent-From"); |
28 |
| - HEADERS_TO_IGNORE.add("Resent-Sender"); |
29 |
| - HEADERS_TO_IGNORE.add("Resent-To"); |
30 |
| - HEADERS_TO_IGNORE.add("Resent-Cc"); |
31 |
| - HEADERS_TO_IGNORE.add("Resent-Bcc"); |
32 |
| - HEADERS_TO_IGNORE.add("Resent-Message-Id"); |
33 |
| - HEADERS_TO_IGNORE.add("Date"); |
34 |
| - HEADERS_TO_IGNORE.add("From"); |
35 |
| - HEADERS_TO_IGNORE.add("Sender"); |
36 |
| - HEADERS_TO_IGNORE.add("Reply-To"); |
37 |
| - HEADERS_TO_IGNORE.add("To"); |
38 |
| - HEADERS_TO_IGNORE.add("Cc"); |
39 |
| - HEADERS_TO_IGNORE.add("Bcc"); |
40 |
| - HEADERS_TO_IGNORE.add("Message-Id"); |
41 |
| - // The next two are needed for replying to |
42 |
| - // HEADERS_TO_IGNORE.add("In-Reply-To"); |
43 |
| - // HEADERS_TO_IGNORE.add("References"); |
44 |
| - HEADERS_TO_IGNORE.add("Subject"); |
45 |
| - HEADERS_TO_IGNORE.add("Comments"); |
46 |
| - HEADERS_TO_IGNORE.add("Keywords"); |
47 |
| - HEADERS_TO_IGNORE.add("Errors-To"); |
48 |
| - HEADERS_TO_IGNORE.add("MIME-Version"); |
49 |
| - HEADERS_TO_IGNORE.add("Content-Type"); |
50 |
| - HEADERS_TO_IGNORE.add("Content-Transfer-Encoding"); |
51 |
| - HEADERS_TO_IGNORE.add("Content-MD5"); |
52 |
| - HEADERS_TO_IGNORE.add(":"); |
53 |
| - HEADERS_TO_IGNORE.add("Content-Length"); |
54 |
| - HEADERS_TO_IGNORE.add("Status"); |
55 |
| - // extra headers that should be ignored, which may originate from nested attachments |
56 |
| - HEADERS_TO_IGNORE.add("Content-Disposition"); |
57 |
| - HEADERS_TO_IGNORE.add("size"); |
58 |
| - HEADERS_TO_IGNORE.add("filename"); |
59 |
| - HEADERS_TO_IGNORE.add("Content-ID"); |
60 |
| - HEADERS_TO_IGNORE.add("name"); |
61 |
| - HEADERS_TO_IGNORE.add("From"); |
62 |
| - } |
| 55 | + /** |
| 56 | + * Similar to {@link #HEADERS_TO_IGNORE_CASE_INSENSITIVE}, but case-sensitive. Why? Well, that's a little |
| 57 | + * complicated. These headers cause issues due to legacy code that is not case-insensitive. So we need to keep |
| 58 | + * track of these headers separately to avoid issues. |
| 59 | + * <p> |
| 60 | + * In practice, this should not cause any issues for real-world usage / use cases. |
| 61 | + */ |
| 62 | + private static final List<MessageHeader> HEADERS_TO_IGNORE_CASE_SENSITIVE = singletonList( |
| 63 | + MessageHeader.MESSAGE_ID |
| 64 | + ); |
63 | 65 | }
|
0 commit comments