|
1 | 1 | /* |
2 | | - * Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * |
4 | 4 | * This program and the accompanying materials are made available under the |
5 | 5 | * terms of the Eclipse Public License v. 2.0, which is available at |
@@ -63,29 +63,30 @@ public class MimeBodyPart extends BodyPart implements MimePart { |
63 | 63 |
|
64 | 64 | // Paranoia: |
65 | 65 | // allow this last minute change to be disabled if it causes problems |
66 | | - private static final boolean setDefaultTextCharset = |
67 | | - PropUtil.getBooleanSystemProperty( |
68 | | - "mail.mime.setdefaulttextcharset", true); |
69 | | - |
70 | | - private static final boolean setContentTypeFileName = |
71 | | - PropUtil.getBooleanSystemProperty( |
72 | | - "mail.mime.setcontenttypefilename", true); |
73 | | - |
74 | | - private static final boolean encodeFileName = |
75 | | - PropUtil.getBooleanSystemProperty("mail.mime.encodefilename", false); |
76 | | - private static final boolean decodeFileName = |
77 | | - PropUtil.getBooleanSystemProperty("mail.mime.decodefilename", false); |
78 | | - private static final boolean ignoreMultipartEncoding = |
79 | | - PropUtil.getBooleanSystemProperty( |
80 | | - "mail.mime.ignoremultipartencoding", true); |
81 | | - private static final boolean allowutf8 = |
82 | | - PropUtil.getBooleanSystemProperty("mail.mime.allowutf8", true); |
83 | | - |
| 66 | + static final boolean SET_DEFAULT_TEXT_CHARSET = |
| 67 | + PropUtil.getBooleanSystemProperty("mail.mime.setdefaulttextcharset", true); |
| 68 | + static final boolean SET_CONTENT_TYPE_FILE_NAME = |
| 69 | + PropUtil.getBooleanSystemProperty("mail.mime.setcontenttypefilename", true); |
| 70 | + static final boolean ENCODE_FILE_NAME = |
| 71 | + PropUtil.getBooleanSystemProperty("mail.mime.encodefilename", false); |
| 72 | + static final boolean DECODE_FILE_NAME = |
| 73 | + PropUtil.getBooleanSystemProperty("mail.mime.decodefilename", false); |
| 74 | + static final boolean IGNORE_MULTIPART_ENCODING = |
| 75 | + PropUtil.getBooleanSystemProperty("mail.mime.ignoremultipartencoding", true); |
| 76 | + static final boolean ALLOW_UTF8 = |
| 77 | + PropUtil.getBooleanSystemProperty("mail.mime.allowutf8", true); |
84 | 78 | // Paranoia: |
85 | 79 | // allow this last minute change to be disabled if it causes problems |
86 | | - static final boolean cacheMultipart = // accessed by MimeMessage |
87 | | - PropUtil.getBooleanSystemProperty("mail.mime.cachemultipart", true); |
| 80 | + static final boolean CACHE_MULTIPART = // accessed by MimeMessage |
| 81 | + PropUtil.getBooleanSystemProperty("mail.mime.cachemultipart", true); |
88 | 82 |
|
| 83 | + private boolean setDefaultTextCharset = SET_DEFAULT_TEXT_CHARSET; |
| 84 | + private boolean setContentTypeFileName = SET_CONTENT_TYPE_FILE_NAME; |
| 85 | + private boolean encodeFileName = ENCODE_FILE_NAME; |
| 86 | + private boolean decodeFileName = DECODE_FILE_NAME; |
| 87 | + private boolean ignoreMultipartEncoding = IGNORE_MULTIPART_ENCODING; |
| 88 | + private boolean allowutf8 = ALLOW_UTF8; |
| 89 | + private boolean cacheMultipart = CACHE_MULTIPART; |
89 | 90 |
|
90 | 91 | /** |
91 | 92 | * The DataHandler object representing this Part's content. |
@@ -522,7 +523,7 @@ public void setDescription(String description, String charset) |
522 | 523 | */ |
523 | 524 | @Override |
524 | 525 | public String getFileName() throws MessagingException { |
525 | | - return getFileName(this); |
| 526 | + return getFileName(this, decodeFileName); |
526 | 527 | } |
527 | 528 |
|
528 | 529 | /** |
@@ -550,7 +551,7 @@ public String getFileName() throws MessagingException { |
550 | 551 | */ |
551 | 552 | @Override |
552 | 553 | public void setFileName(String filename) throws MessagingException { |
553 | | - setFileName(this, filename); |
| 554 | + setFileName(this, filename, encodeFileName, setContentTypeFileName); |
554 | 555 | } |
555 | 556 |
|
556 | 557 | /** |
@@ -969,7 +970,7 @@ public void saveFile(String file) throws IOException, MessagingException { |
969 | 970 | @Override |
970 | 971 | public void writeTo(OutputStream os) |
971 | 972 | throws IOException, MessagingException { |
972 | | - writeTo(this, os, null); |
| 973 | + writeTo(this, os, null, allowutf8, ignoreMultipartEncoding); |
973 | 974 | } |
974 | 975 |
|
975 | 976 | /** |
@@ -1145,7 +1146,7 @@ public Enumeration<String> getNonMatchingHeaderLines(String[] names) |
1145 | 1146 | * @exception MessagingException for failures |
1146 | 1147 | */ |
1147 | 1148 | protected void updateHeaders() throws MessagingException { |
1148 | | - updateHeaders(this); |
| 1149 | + updateHeaders(this, setDefaultTextCharset, setContentTypeFileName, encodeFileName); |
1149 | 1150 | /* |
1150 | 1151 | * If we've cached a Multipart or Message object then |
1151 | 1152 | * we're now committed to using this instance of the |
@@ -1261,7 +1262,7 @@ static String getDescription(MimePart part) |
1261 | 1262 | } |
1262 | 1263 | } |
1263 | 1264 |
|
1264 | | - static String getFileName(MimePart part) throws MessagingException { |
| 1265 | + static String getFileName(MimePart part, boolean decodeFileName) throws MessagingException { |
1265 | 1266 | String filename = null; |
1266 | 1267 | String s = part.getHeader("Content-Disposition", null); |
1267 | 1268 |
|
@@ -1291,7 +1292,8 @@ static String getFileName(MimePart part) throws MessagingException { |
1291 | 1292 | return filename; |
1292 | 1293 | } |
1293 | 1294 |
|
1294 | | - static void setFileName(MimePart part, String name) |
| 1295 | + static void setFileName(MimePart part, String name, boolean encodeFileName, |
| 1296 | + boolean setContentTypeFileName) |
1295 | 1297 | throws MessagingException { |
1296 | 1298 | if (encodeFileName && name != null) { |
1297 | 1299 | try { |
@@ -1440,7 +1442,7 @@ static void setEncoding(MimePart part, String encoding) |
1440 | 1442 | * Content-Type of the specified MimePart. Returns |
1441 | 1443 | * either the original encoding or null. |
1442 | 1444 | */ |
1443 | | - static String restrictEncoding(MimePart part, String encoding) |
| 1445 | + static String restrictEncoding(MimePart part, String encoding, boolean ignoreMultipartEncoding) |
1444 | 1446 | throws MessagingException { |
1445 | 1447 | if (!ignoreMultipartEncoding || encoding == null) |
1446 | 1448 | return encoding; |
@@ -1473,7 +1475,8 @@ static String restrictEncoding(MimePart part, String encoding) |
1473 | 1475 | return encoding; |
1474 | 1476 | } |
1475 | 1477 |
|
1476 | | - static void updateHeaders(MimePart part) throws MessagingException { |
| 1478 | + static void updateHeaders(MimePart part, boolean setDefaultTextCharset, boolean setContentTypeFileName, |
| 1479 | + boolean encodeFileName) throws MessagingException { |
1477 | 1480 | DataHandler dh = part.getDataHandler(); |
1478 | 1481 | if (dh == null) // Huh ? |
1479 | 1482 | return; |
@@ -1620,8 +1623,8 @@ static void invalidateContentHeaders(MimePart part) |
1620 | 1623 | part.removeHeader("Content-Transfer-Encoding"); |
1621 | 1624 | } |
1622 | 1625 |
|
1623 | | - static void writeTo(MimePart part, OutputStream os, String[] ignoreList) |
1624 | | - throws IOException, MessagingException { |
| 1626 | + static void writeTo(MimePart part, OutputStream os, String[] ignoreList, boolean allowutf8, |
| 1627 | + boolean ignoreMultipartEncoding) throws IOException, MessagingException { |
1625 | 1628 |
|
1626 | 1629 | // see if we already have a LOS |
1627 | 1630 | LineOutputStream los = null; |
@@ -1666,7 +1669,7 @@ static void writeTo(MimePart part, OutputStream os, String[] ignoreList) |
1666 | 1669 | os.write(buf, 0, len); |
1667 | 1670 | } else { |
1668 | 1671 | os = MimeUtility.encode(os, |
1669 | | - restrictEncoding(part, part.getEncoding())); |
| 1672 | + restrictEncoding(part, part.getEncoding(), ignoreMultipartEncoding)); |
1670 | 1673 | part.getDataHandler().writeTo(os); |
1671 | 1674 | } |
1672 | 1675 | } finally { |
@@ -1709,4 +1712,61 @@ MimePart getPart() { |
1709 | 1712 | return part; |
1710 | 1713 | } |
1711 | 1714 | } |
| 1715 | + |
| 1716 | + /** |
| 1717 | + * Overrides the property of mail.mime.setdefaulttextcharset. |
| 1718 | + * @param setDefaultTextCharset Enable/disable setDefaultTextCharset |
| 1719 | + */ |
| 1720 | + public void setDefaultTextCharset(boolean setDefaultTextCharset) { |
| 1721 | + this.setDefaultTextCharset = setDefaultTextCharset; |
| 1722 | + } |
| 1723 | + |
| 1724 | + /** |
| 1725 | + * Overrides the property of mail.mime.setContentTypeFileName. |
| 1726 | + * @param setContentTypeFileName Enable/disable setContentTypeFileName |
| 1727 | + */ |
| 1728 | + public void setContentTypeFileName(boolean setContentTypeFileName) { |
| 1729 | + this.setContentTypeFileName = setContentTypeFileName; |
| 1730 | + } |
| 1731 | + |
| 1732 | + /** |
| 1733 | + * Overrides the property of mail.mime.encodeFileName. |
| 1734 | + * @param encodeFileName Enable/disable encodeFileName |
| 1735 | + */ |
| 1736 | + public void setEncodeFileName(boolean encodeFileName) { |
| 1737 | + this.encodeFileName = encodeFileName; |
| 1738 | + } |
| 1739 | + |
| 1740 | + /** |
| 1741 | + * Overrides the property of mail.mime.decodeFileName. |
| 1742 | + * @param decodeFileName Enable/disable decodeFileName |
| 1743 | + */ |
| 1744 | + public void setDecodeFileName(boolean decodeFileName) { |
| 1745 | + this.decodeFileName = decodeFileName; |
| 1746 | + } |
| 1747 | + |
| 1748 | + /** |
| 1749 | + * Overrides the property of mail.mime.ignoreMultipartEncoding. |
| 1750 | + * @param ignoreMultipartEncoding Enable/disable ignoreMultipartEncoding |
| 1751 | + */ |
| 1752 | + public void setIgnoreMultipartEncoding(boolean ignoreMultipartEncoding) { |
| 1753 | + this.ignoreMultipartEncoding = ignoreMultipartEncoding; |
| 1754 | + } |
| 1755 | + |
| 1756 | + /** |
| 1757 | + * Overrides the property of mail.mime.cacheMultipart. |
| 1758 | + * @param cacheMultipart Enable/disable cacheMultipart |
| 1759 | + */ |
| 1760 | + public void setCacheMultipart(boolean cacheMultipart) { |
| 1761 | + this.cacheMultipart = cacheMultipart; |
| 1762 | + } |
| 1763 | + |
| 1764 | + /** |
| 1765 | + * Overrides the property of mail.mime.cachedContent. |
| 1766 | + * @param cachedContent Enable/disable cachedContent |
| 1767 | + */ |
| 1768 | + public void setCachedContent(Object cachedContent) { |
| 1769 | + this.cachedContent = cachedContent; |
| 1770 | + } |
| 1771 | + |
1712 | 1772 | } |
0 commit comments