From b0e1ed686da11fe411c469571866ae013d885134 Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Fri, 27 Aug 2021 11:54:57 +0200 Subject: [PATCH 1/5] Allow to override internal static fields Signed-off-by: Jorge Bescos Gascon --- api/pom.xml | 6 + .../jakarta/mail/internet/MimeBodyPart.java | 94 ++++++++---- .../jakarta/mail/internet/MimeMessage.java | 53 ++++--- .../jakarta/mail/internet/MimeMultipart.java | 8 +- .../mail/internet/MimePartDataSource.java | 20 ++- .../mail/internet/MimeBodyPartTest.java | 136 ++++++++++++++++++ .../mail/internet/NonAsciiFileNamesTest.java | 4 +- 7 files changed, 265 insertions(+), 56 deletions(-) create mode 100644 api/src/test/java/jakarta/mail/internet/MimeBodyPartTest.java diff --git a/api/pom.xml b/api/pom.xml index cc514e85..69fa8c61 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -106,6 +106,12 @@ junit test + + org.eclipse.angus + angus-mail + 1.0.0-SNAPSHOT + test + diff --git a/api/src/main/java/jakarta/mail/internet/MimeBodyPart.java b/api/src/main/java/jakarta/mail/internet/MimeBodyPart.java index 73550cb2..43f4a738 100644 --- a/api/src/main/java/jakarta/mail/internet/MimeBodyPart.java +++ b/api/src/main/java/jakarta/mail/internet/MimeBodyPart.java @@ -30,6 +30,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Properties; import jakarta.activation.DataHandler; import jakarta.activation.DataSource; @@ -87,29 +88,30 @@ public class MimeBodyPart extends BodyPart implements MimePart { // Paranoia: // allow this last minute change to be disabled if it causes problems - private static final boolean setDefaultTextCharset = - MimeUtility.getBooleanSystemProperty( - "mail.mime.setdefaulttextcharset", true); - - private static final boolean setContentTypeFileName = - MimeUtility.getBooleanSystemProperty( - "mail.mime.setcontenttypefilename", true); - - private static final boolean encodeFileName = - MimeUtility.getBooleanSystemProperty("mail.mime.encodefilename", false); - private static final boolean decodeFileName = - MimeUtility.getBooleanSystemProperty("mail.mime.decodefilename", false); - private static final boolean ignoreMultipartEncoding = - MimeUtility.getBooleanSystemProperty( - "mail.mime.ignoremultipartencoding", true); - private static final boolean allowutf8 = - MimeUtility.getBooleanSystemProperty("mail.mime.allowutf8", true); - + static final boolean SET_DEFAULT_TEXT_CHARSET = + MimeUtility.getBooleanSystemProperty("mail.mime.setdefaulttextcharset", true); + static final boolean SET_CONTENT_TYPE_FILE_NAME = + MimeUtility.getBooleanSystemProperty("mail.mime.setcontenttypefilename", true); + static final boolean ENCODE_FILE_NAME = + MimeUtility.getBooleanSystemProperty("mail.mime.encodefilename", false); + static final boolean DECODE_FILE_NAME = + MimeUtility.getBooleanSystemProperty("mail.mime.decodefilename", false); + static final boolean IGNORE_MULTIPART_ENCODING = + MimeUtility.getBooleanSystemProperty("mail.mime.ignoremultipartencoding", true); + static final boolean ALLOW_UTF8 = + MimeUtility.getBooleanSystemProperty("mail.mime.allowutf8", true); // Paranoia: // allow this last minute change to be disabled if it causes problems - static final boolean cacheMultipart = // accessed by MimeMessage - MimeUtility.getBooleanSystemProperty("mail.mime.cachemultipart", true); + static final boolean CACHE_MULTIPART = // accessed by MimeMessage + MimeUtility.getBooleanSystemProperty("mail.mime.cachemultipart", true); + protected boolean setDefaultTextCharset = SET_DEFAULT_TEXT_CHARSET; + protected boolean setContentTypeFileName = SET_CONTENT_TYPE_FILE_NAME; + protected boolean encodeFileName = ENCODE_FILE_NAME; + protected boolean decodeFileName = DECODE_FILE_NAME; + protected boolean ignoreMultipartEncoding = IGNORE_MULTIPART_ENCODING; + protected boolean allowutf8 = ALLOW_UTF8; + protected boolean cacheMultipart = CACHE_MULTIPART; /** * The DataHandler object representing this Part's content. @@ -216,6 +218,34 @@ public MimeBodyPart(InternetHeaders headers, byte[] content) this.content = content; } + /** + * Initializes MimeBodyPart from the InputStream is and it overwrites + * properties from session. + * + * @param session Session object for this message + * @param is the message input stream + * @exception MessagingException for failures + */ + public MimeBodyPart(Session session, InputStream is) throws MessagingException { + this(is); + initializeProperties(session); + } + + /** + * Set the values from session properties if exist, otherwise it keeps the previous value. + * @param session the not null session + */ + private void initializeProperties(Session session) { + Properties props = session.getProperties(); + setDefaultTextCharset = MimeUtility.getBooleanProperty(props, "mail.mime.setdefaulttextcharset", setDefaultTextCharset); + setContentTypeFileName = MimeUtility.getBooleanProperty(props, "mail.mime.setcontenttypefilename", setContentTypeFileName); + encodeFileName = MimeUtility.getBooleanProperty(props, "mail.mime.encodefilename", encodeFileName); + decodeFileName = MimeUtility.getBooleanProperty(props, "mail.mime.decodefilename", decodeFileName); + ignoreMultipartEncoding = MimeUtility.getBooleanProperty(props, "mail.mime.ignoremultipartencoding", ignoreMultipartEncoding); + allowutf8 = MimeUtility.getBooleanProperty(props, "mail.mime.allowutf8", allowutf8); + cacheMultipart = MimeUtility.getBooleanProperty(props, "mail.mime.cachemultipart", cacheMultipart); + } + /** * Return the size of the content of this body part in bytes. * Return -1 if the size cannot be determined.

@@ -546,7 +576,7 @@ public void setDescription(String description, String charset) */ @Override public String getFileName() throws MessagingException { - return getFileName(this); + return getFileName(this, decodeFileName); } /** @@ -574,7 +604,7 @@ public String getFileName() throws MessagingException { */ @Override public void setFileName(String filename) throws MessagingException { - setFileName(this, filename); + setFileName(this, filename, encodeFileName, setContentTypeFileName); } /** @@ -998,7 +1028,7 @@ public void saveFile(String file) throws IOException, MessagingException { @Override public void writeTo(OutputStream os) throws IOException, MessagingException { - writeTo(this, os, null); + writeTo(this, os, null, allowutf8, ignoreMultipartEncoding); } /** @@ -1174,7 +1204,7 @@ public Enumeration getNonMatchingHeaderLines(String[] names) * @exception MessagingException for failures */ protected void updateHeaders() throws MessagingException { - updateHeaders(this); + updateHeaders(this, setDefaultTextCharset, setContentTypeFileName, encodeFileName); /* * If we've cached a Multipart or Message object then * we're now committed to using this instance of the @@ -1290,7 +1320,7 @@ static String getDescription(MimePart part) } } - static String getFileName(MimePart part) throws MessagingException { + static String getFileName(MimePart part, boolean decodeFileName) throws MessagingException { String filename = null; String s = part.getHeader("Content-Disposition", null); @@ -1320,7 +1350,8 @@ static String getFileName(MimePart part) throws MessagingException { return filename; } - static void setFileName(MimePart part, String name) + static void setFileName(MimePart part, String name, boolean encodeFileName, + boolean setContentTypeFileName) throws MessagingException { if (encodeFileName && name != null) { try { @@ -1469,7 +1500,7 @@ static void setEncoding(MimePart part, String encoding) * Content-Type of the specified MimePart. Returns * either the original encoding or null. */ - static String restrictEncoding(MimePart part, String encoding) + static String restrictEncoding(MimePart part, String encoding, boolean ignoreMultipartEncoding) throws MessagingException { if (!ignoreMultipartEncoding || encoding == null) return encoding; @@ -1502,7 +1533,8 @@ static String restrictEncoding(MimePart part, String encoding) return encoding; } - static void updateHeaders(MimePart part) throws MessagingException { + static void updateHeaders(MimePart part, boolean setDefaultTextCharset, boolean setContentTypeFileName, + boolean encodeFileName) throws MessagingException { DataHandler dh = part.getDataHandler(); if (dh == null) // Huh ? return; @@ -1649,8 +1681,8 @@ static void invalidateContentHeaders(MimePart part) part.removeHeader("Content-Transfer-Encoding"); } - static void writeTo(MimePart part, OutputStream os, String[] ignoreList) - throws IOException, MessagingException { + static void writeTo(MimePart part, OutputStream os, String[] ignoreList, boolean allowutf8, + boolean ignoreMultipartEncoding) throws IOException, MessagingException { // see if we already have a LOS LineOutputStream los = null; @@ -1697,7 +1729,7 @@ static void writeTo(MimePart part, OutputStream os, String[] ignoreList) os.write(buf, 0, len); } else { os = MimeUtility.encode(os, - restrictEncoding(part, part.getEncoding())); + restrictEncoding(part, part.getEncoding(), ignoreMultipartEncoding)); part.getDataHandler().writeTo(os); } } finally { diff --git a/api/src/main/java/jakarta/mail/internet/MimeMessage.java b/api/src/main/java/jakarta/mail/internet/MimeMessage.java index a666abd0..22bc0388 100644 --- a/api/src/main/java/jakarta/mail/internet/MimeMessage.java +++ b/api/src/main/java/jakarta/mail/internet/MimeMessage.java @@ -45,6 +45,8 @@ import jakarta.mail.Session; import jakarta.mail.util.LineOutputStream; +import static jakarta.mail.internet.MimeBodyPart.*; + /** @@ -176,8 +178,20 @@ public class MimeMessage extends Message implements MimePart { // Should addresses in headers be parsed in "strict" mode? private boolean strict = true; + + protected boolean setDefaultTextCharset = SET_DEFAULT_TEXT_CHARSET; + protected boolean setContentTypeFileName = SET_CONTENT_TYPE_FILE_NAME; + protected boolean encodeFileName = ENCODE_FILE_NAME; + protected boolean decodeFileName = DECODE_FILE_NAME; + protected boolean ignoreMultipartEncoding = IGNORE_MULTIPART_ENCODING; + /* + * This is not a duplicate of allowutf8Headers. When mail.mime.allowutf8 + * is not defined, this value is 'true'. Meanwhile allowutf8Headers is 'false' + */ + protected boolean allowutf8 = ALLOW_UTF8; + protected boolean cacheMultipart = CACHE_MULTIPART; // Is UTF-8 allowed in headers? - private boolean allowutf8 = false; + private boolean allowutf8Headers = false; /** * Default constructor. An empty message object is created. @@ -312,14 +326,21 @@ protected MimeMessage(Folder folder, InternetHeaders headers, } /** - * Set the strict flag based on property. + * Set the properties from session if exists, otherwise it keeps the previous value. */ private void initStrict() { - if (session != null) { - Properties props = session.getProperties(); - strict = MimeUtility.getBooleanProperty(props, "mail.mime.address.strict", true); - allowutf8 = MimeUtility.getBooleanProperty(props, "mail.mime.allowutf8", false); - } + if (session != null) { + Properties props = session.getProperties(); + strict = MimeUtility.getBooleanProperty(props, "mail.mime.address.strict", true); + allowutf8Headers = MimeUtility.getBooleanProperty(props, "mail.mime.allowutf8", false); + setDefaultTextCharset = MimeUtility.getBooleanProperty(props, "mail.mime.setdefaulttextcharset", setDefaultTextCharset); + setContentTypeFileName = MimeUtility.getBooleanProperty(props, "mail.mime.setcontenttypefilename", setContentTypeFileName); + encodeFileName = MimeUtility.getBooleanProperty(props, "mail.mime.encodefilename", encodeFileName); + decodeFileName = MimeUtility.getBooleanProperty(props, "mail.mime.decodefilename", decodeFileName); + ignoreMultipartEncoding = MimeUtility.getBooleanProperty(props, "mail.mime.ignoremultipartencoding", ignoreMultipartEncoding); + allowutf8 = MimeUtility.getBooleanProperty(props, "mail.mime.allowutf8", allowutf8); + cacheMultipart = MimeUtility.getBooleanProperty(props, "mail.mime.cachemultipart", cacheMultipart); + } } /** @@ -750,7 +771,7 @@ private Address[] getAddressHeader(String name) private void setAddressHeader(String name, Address[] addresses) throws MessagingException { String s; - if (allowutf8) + if (allowutf8Headers) s = InternetAddress.toUnicodeString(addresses, name.length() + 2); else s = InternetAddress.toString(addresses, name.length() + 2); @@ -774,7 +795,7 @@ private void addAddressHeader(String name, Address[] addresses) System.arraycopy(addresses, 0, anew, a.length, addresses.length); } String s; - if (allowutf8) + if (allowutf8Headers) s = InternetAddress.toUnicodeString(anew, name.length() + 2); else s = InternetAddress.toString(anew, name.length() + 2); @@ -1318,7 +1339,7 @@ public String getMessageID() throws MessagingException { */ @Override public String getFileName() throws MessagingException { - return MimeBodyPart.getFileName(this); + return MimeBodyPart.getFileName(this, decodeFileName); } /** @@ -1343,7 +1364,7 @@ public String getFileName() throws MessagingException { */ @Override public void setFileName(String filename) throws MessagingException { - MimeBodyPart.setFileName(this, filename); + MimeBodyPart.setFileName(this, filename, encodeFileName, setContentTypeFileName); } private String getHeaderName(Message.RecipientType type) @@ -1506,7 +1527,7 @@ public Object getContent() throws IOException, MessagingException { throw e; } } - if (MimeBodyPart.cacheMultipart && + if (cacheMultipart && (c instanceof Multipart || c instanceof Message) && (content != null || contentStream != null)) { cachedContent = c; @@ -1904,14 +1925,14 @@ public void writeTo(OutputStream os, String[] ignoreList) saveChanges(); if (modified) { - MimeBodyPart.writeTo(this, os, ignoreList); + MimeBodyPart.writeTo(this, os, ignoreList, allowutf8, ignoreMultipartEncoding); return; } // Else, the content is untouched, so we can just output it // First, write out the header Enumeration hdrLines = getNonMatchingHeaderLines(ignoreList); - LineOutputStream los = Session.STREAM_PROVIDER.outputLineStream(os, allowutf8); + LineOutputStream los = Session.STREAM_PROVIDER.outputLineStream(os, allowutf8Headers); while (hdrLines.hasMoreElements()) los.writeln(hdrLines.nextElement()); @@ -2262,7 +2283,7 @@ protected void updateMessageID() throws MessagingException { * @exception MessagingException for other failures */ protected synchronized void updateHeaders() throws MessagingException { - MimeBodyPart.updateHeaders(this); + MimeBodyPart.updateHeaders(this, setDefaultTextCharset, setContentTypeFileName, encodeFileName); setHeader("MIME-Version", "1.0"); if (getHeader("Date") == null) setSentDate(new Date()); @@ -2295,7 +2316,7 @@ protected synchronized void updateHeaders() throws MessagingException { */ protected InternetHeaders createInternetHeaders(InputStream is) throws MessagingException { - return new InternetHeaders(is, allowutf8); + return new InternetHeaders(is, allowutf8Headers); } /** diff --git a/api/src/main/java/jakarta/mail/internet/MimeMultipart.java b/api/src/main/java/jakarta/mail/internet/MimeMultipart.java index 03f47293..48ae3e01 100644 --- a/api/src/main/java/jakarta/mail/internet/MimeMultipart.java +++ b/api/src/main/java/jakarta/mail/internet/MimeMultipart.java @@ -1005,7 +1005,13 @@ protected MimeBodyPart createMimeBodyPart(InternetHeaders headers, */ protected MimeBodyPart createMimeBodyPart(InputStream is) throws MessagingException { - return new MimeBodyPart(is); + if (ds != null && ds instanceof MimePartDataSource) { + Session session = ((MimePartDataSource)ds).getMessageContext().getSession(); + if (session != null) { + return new MimeBodyPart(session, is); + } + } + return new MimeBodyPart(is); } private MimeBodyPart createMimeBodyPartIs(InputStream is) diff --git a/api/src/main/java/jakarta/mail/internet/MimePartDataSource.java b/api/src/main/java/jakarta/mail/internet/MimePartDataSource.java index ba7a1cc0..93ae18c2 100644 --- a/api/src/main/java/jakarta/mail/internet/MimePartDataSource.java +++ b/api/src/main/java/jakarta/mail/internet/MimePartDataSource.java @@ -26,7 +26,9 @@ import jakarta.mail.MessageAware; import jakarta.mail.MessageContext; import jakarta.mail.MessagingException; +import jakarta.mail.Session; +import static jakarta.mail.internet.MimeBodyPart.*; /** @@ -39,6 +41,8 @@ */ public class MimePartDataSource implements DataSource, MessageAware { + + protected boolean ignoreMultipartEncoding = IGNORE_MULTIPART_ENCODING; /** * The MimePart that provides the data for this DataSource. * @@ -54,7 +58,13 @@ public class MimePartDataSource implements DataSource, MessageAware { * @param part the MimePart */ public MimePartDataSource(MimePart part) { - this.part = part; + this.part = part; + this.context = new MessageContext(part); + Session session = context.getSession(); + if (session != null) { + this.ignoreMultipartEncoding = MimeUtility.getBooleanProperty(session.getProperties(), + "mail.mime.ignoremultipartencoding", ignoreMultipartEncoding); + } } /** @@ -86,7 +96,7 @@ else if (part instanceof MimeMessage) throw new MessagingException("Unknown part"); String encoding = - MimeBodyPart.restrictEncoding(part, part.getEncoding()); + MimeBodyPart.restrictEncoding(part, part.getEncoding(), ignoreMultipartEncoding); if (encoding != null) return MimeUtility.decode(is, encoding); else @@ -149,9 +159,7 @@ public String getName() { * @since JavaMail 1.1 */ @Override - public synchronized MessageContext getMessageContext() { - if (context == null) - context = new MessageContext(part); - return context; + public MessageContext getMessageContext() { + return context; } } diff --git a/api/src/test/java/jakarta/mail/internet/MimeBodyPartTest.java b/api/src/test/java/jakarta/mail/internet/MimeBodyPartTest.java new file mode 100644 index 00000000..5a645722 --- /dev/null +++ b/api/src/test/java/jakarta/mail/internet/MimeBodyPartTest.java @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package jakarta.mail.internet; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +import org.junit.Test; + +import jakarta.mail.MessagingException; +import jakarta.mail.Session; + +public class MimeBodyPartTest { + + @Test + public void sessionProperties() throws MessagingException, IOException { + try { + Properties prop = new Properties(); + MimeMessage orig = buildFromProperties(prop); + MimeMultipart omp = (MimeMultipart) orig.getContent(); + MimeBodyPart obp = (MimeBodyPart) omp.getBodyPart(0); + // No properties added, so default will be checked + // MimeMessage + assertTrue(orig.setDefaultTextCharset); + assertTrue(orig.setContentTypeFileName); + assertFalse(orig.encodeFileName); + assertFalse(orig.decodeFileName); + assertTrue(orig.ignoreMultipartEncoding); + assertTrue(orig.allowutf8); + assertTrue(orig.cacheMultipart); + // MimeBodyPart + assertTrue(obp.setDefaultTextCharset); + assertTrue(obp.setContentTypeFileName); + assertFalse(obp.encodeFileName); + assertFalse(obp.decodeFileName); + assertTrue(obp.ignoreMultipartEncoding); + assertTrue(obp.allowutf8); + assertTrue(obp.cacheMultipart); + // Change the properties in opposite way + prop.put("mail.mime.setdefaulttextcharset", Boolean.FALSE); + prop.put("mail.mime.setcontenttypefilename", Boolean.FALSE); + prop.put("mail.mime.encodefilename", Boolean.TRUE); + prop.put("mail.mime.decodefilename", Boolean.TRUE); + prop.put("mail.mime.ignoremultipartencoding", Boolean.FALSE); + prop.put("mail.mime.allowutf8", Boolean.FALSE); + prop.put("mail.mime.cachemultipart", Boolean.FALSE); + orig = buildFromProperties(prop); + omp = (MimeMultipart) orig.getContent(); + obp = (MimeBodyPart) omp.getBodyPart(0); + // MimeMessage + assertFalse(orig.setDefaultTextCharset); + assertFalse(orig.setContentTypeFileName); + assertTrue(orig.encodeFileName); + assertTrue(orig.decodeFileName); + assertFalse(orig.ignoreMultipartEncoding); + assertFalse(orig.allowutf8); + assertFalse(orig.cacheMultipart); + // MimeBodyPart + assertFalse(obp.setDefaultTextCharset); + assertFalse(obp.setContentTypeFileName); + assertTrue(obp.encodeFileName); + assertTrue(obp.decodeFileName); + assertFalse(obp.ignoreMultipartEncoding); + assertFalse(obp.allowutf8); + assertFalse(obp.cacheMultipart); + } catch (Exception e) { + e.printStackTrace(); + throw new IllegalArgumentException(e); + } + } + + private MimeMessage buildFromProperties(Properties prop) throws MessagingException, IOException { + Session s = Session.getInstance(prop); + MimeMessage orig = createMessage(s); + return orig; + } + + private static MimeMessage createMessage(Session s) throws MessagingException { + String content = "Mime-Version: 1.0\n" + "Subject: Example\n" + + "Content-Type: multipart/mixed; boundary=\"-\"\n" + "\n" + "preamble\n" + "---\n" + + "Content-Type: text/x-test\n" + "Content-Transfer-Encoding: quoted-printable\n" + "\n" + "test part\n" + + "\n" + "-----\n"; + + InputStream input = new AsciiStringInputStream(content); + return new MimeMessage(s, input); + } + + private static class AsciiStringInputStream extends InputStream { + + private final String input; + private int position; + + public AsciiStringInputStream(String input) { + this(input, true); + } + + public AsciiStringInputStream(String input, boolean strict) { + if (strict) { + for (int i = 0; i < input.length(); i++) { + if (input.charAt(i) > 0x7F) { + throw new IllegalArgumentException("Not an ASCII string"); + } + } + } + this.input = input; + } + + @Override + public int read() { + if (position < input.length()) { + return input.charAt(position++) & 0xFF; + } else { + return -1; + } + } + + } +} diff --git a/api/src/test/java/jakarta/mail/internet/NonAsciiFileNamesTest.java b/api/src/test/java/jakarta/mail/internet/NonAsciiFileNamesTest.java index 1a74f9fb..410597b4 100644 --- a/api/src/test/java/jakarta/mail/internet/NonAsciiFileNamesTest.java +++ b/api/src/test/java/jakarta/mail/internet/NonAsciiFileNamesTest.java @@ -42,7 +42,7 @@ public void testNonAsciiFileName() throws Exception { MimeBodyPart mbp = new MimeBodyPart(); mbp.setText("test\n"); mbp.setFileName("test\u00a1\u00a2\u00a3"); - MimeBodyPart.updateHeaders(mbp); + MimeBodyPart.updateHeaders(mbp, true, true, false); String s = mbp.getHeader("Content-Disposition", null); assertTrue("Content-Disposition filename", s.indexOf("filename*") >= 0); @@ -60,7 +60,7 @@ public void testNonAsciiFileNameWithContentType() throws Exception { mbp.setText("test\n"); mbp.setHeader("Content-Type", "text/x-test"); mbp.setFileName("test\u00a1\u00a2\u00a3"); - MimeBodyPart.updateHeaders(mbp); + MimeBodyPart.updateHeaders(mbp, true, true, false); String s = mbp.getHeader("Content-Disposition", null); assertTrue("Content-Disposition filename", s.indexOf("filename*") >= 0); From 2f898d24cd00a068eed916422063e4147431f852 Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Fri, 27 Aug 2021 15:28:16 +0200 Subject: [PATCH 2/5] Remove impl test dependency Signed-off-by: Jorge Bescos Gascon --- api/pom.xml | 8 +- .../mail/internet/MimeBodyPartTest.java | 68 ++---------- .../mail/util/DummyStreamProvider.java | 100 ++++++++++++++++++ .../services/jakarta.mail.util.StreamProvider | 1 + 4 files changed, 113 insertions(+), 64 deletions(-) create mode 100644 api/src/test/java/jakarta/mail/util/DummyStreamProvider.java create mode 100644 api/src/test/resources/META-INF/services/jakarta.mail.util.StreamProvider diff --git a/api/pom.xml b/api/pom.xml index 69fa8c61..2a5493e0 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -106,12 +106,6 @@ junit test - - org.eclipse.angus - angus-mail - 1.0.0-SNAPSHOT - test - @@ -410,6 +404,8 @@ Use is subject to 2 false + + false diff --git a/api/src/test/java/jakarta/mail/internet/MimeBodyPartTest.java b/api/src/test/java/jakarta/mail/internet/MimeBodyPartTest.java index 5a645722..8987e05b 100644 --- a/api/src/test/java/jakarta/mail/internet/MimeBodyPartTest.java +++ b/api/src/test/java/jakarta/mail/internet/MimeBodyPartTest.java @@ -19,6 +19,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; @@ -32,11 +33,11 @@ public class MimeBodyPartTest { @Test public void sessionProperties() throws MessagingException, IOException { - try { + InputStream input = new ByteArrayInputStream(new byte[0]); Properties prop = new Properties(); - MimeMessage orig = buildFromProperties(prop); - MimeMultipart omp = (MimeMultipart) orig.getContent(); - MimeBodyPart obp = (MimeBodyPart) omp.getBodyPart(0); + Session session = Session.getInstance(prop); + MimeMessage orig = new MimeMessage(session, input); + MimeBodyPart obp = new MimeBodyPart(session, input); // No properties added, so default will be checked // MimeMessage assertTrue(orig.setDefaultTextCharset); @@ -62,9 +63,11 @@ public void sessionProperties() throws MessagingException, IOException { prop.put("mail.mime.ignoremultipartencoding", Boolean.FALSE); prop.put("mail.mime.allowutf8", Boolean.FALSE); prop.put("mail.mime.cachemultipart", Boolean.FALSE); - orig = buildFromProperties(prop); - omp = (MimeMultipart) orig.getContent(); - obp = (MimeBodyPart) omp.getBodyPart(0); + + session = Session.getInstance(prop); + orig = new MimeMessage(session, input); + obp = new MimeBodyPart(session, input); + // MimeMessage assertFalse(orig.setDefaultTextCharset); assertFalse(orig.setContentTypeFileName); @@ -81,56 +84,5 @@ public void sessionProperties() throws MessagingException, IOException { assertFalse(obp.ignoreMultipartEncoding); assertFalse(obp.allowutf8); assertFalse(obp.cacheMultipart); - } catch (Exception e) { - e.printStackTrace(); - throw new IllegalArgumentException(e); - } - } - - private MimeMessage buildFromProperties(Properties prop) throws MessagingException, IOException { - Session s = Session.getInstance(prop); - MimeMessage orig = createMessage(s); - return orig; - } - - private static MimeMessage createMessage(Session s) throws MessagingException { - String content = "Mime-Version: 1.0\n" + "Subject: Example\n" - + "Content-Type: multipart/mixed; boundary=\"-\"\n" + "\n" + "preamble\n" + "---\n" - + "Content-Type: text/x-test\n" + "Content-Transfer-Encoding: quoted-printable\n" + "\n" + "test part\n" - + "\n" + "-----\n"; - - InputStream input = new AsciiStringInputStream(content); - return new MimeMessage(s, input); - } - - private static class AsciiStringInputStream extends InputStream { - - private final String input; - private int position; - - public AsciiStringInputStream(String input) { - this(input, true); - } - - public AsciiStringInputStream(String input, boolean strict) { - if (strict) { - for (int i = 0; i < input.length(); i++) { - if (input.charAt(i) > 0x7F) { - throw new IllegalArgumentException("Not an ASCII string"); - } - } - } - this.input = input; - } - - @Override - public int read() { - if (position < input.length()) { - return input.charAt(position++) & 0xFF; - } else { - return -1; - } - } - } } diff --git a/api/src/test/java/jakarta/mail/util/DummyStreamProvider.java b/api/src/test/java/jakarta/mail/util/DummyStreamProvider.java new file mode 100644 index 00000000..4c9f2829 --- /dev/null +++ b/api/src/test/java/jakarta/mail/util/DummyStreamProvider.java @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package jakarta.mail.util; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class DummyStreamProvider implements StreamProvider { + + @Override + public InputStream inputBase64(InputStream in) { + return null; + } + + @Override + public OutputStream outputBase64(OutputStream out) { + return null; + } + + @Override + public InputStream inputBinary(InputStream in) { + return null; + } + + @Override + public OutputStream outputBinary(OutputStream out) { + return null; + } + + @Override + public OutputStream outputB(OutputStream out) { + return null; + } + + @Override + public InputStream inputQ(InputStream in) { + return null; + } + + @Override + public OutputStream outputQ(OutputStream out, boolean encodingWord) { + return null; + } + + @Override + public LineInputStream inputLineStream(InputStream in, boolean allowutf8) { + return new LineInputStream() { + @Override + public String readLine() throws IOException { + return null; + } + }; + } + + @Override + public LineOutputStream outputLineStream(OutputStream out, boolean allowutf8) { + return null; + } + + @Override + public InputStream inputQP(InputStream in) { + return null; + } + + @Override + public OutputStream outputQP(OutputStream out) { + return null; + } + + @Override + public InputStream inputSharedByteArray(byte[] buff) { + return null; + } + + @Override + public InputStream inputUU(InputStream in) { + return null; + } + + @Override + public OutputStream outputUU(OutputStream out, String filename) { + return null; + } + +} diff --git a/api/src/test/resources/META-INF/services/jakarta.mail.util.StreamProvider b/api/src/test/resources/META-INF/services/jakarta.mail.util.StreamProvider new file mode 100644 index 00000000..9b10701e --- /dev/null +++ b/api/src/test/resources/META-INF/services/jakarta.mail.util.StreamProvider @@ -0,0 +1 @@ +jakarta.mail.util.DummyStreamProvider \ No newline at end of file From 5387aee9d03aca54902a15783ba09c928694b04c Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Tue, 7 Sep 2021 06:59:00 +0200 Subject: [PATCH 3/5] Reviewer fixes Signed-off-by: Jorge Bescos Gascon --- api/src/test/java/module-info.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 api/src/test/java/module-info.java diff --git a/api/src/test/java/module-info.java b/api/src/test/java/module-info.java new file mode 100644 index 00000000..610496d6 --- /dev/null +++ b/api/src/test/java/module-info.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +module jakarta.mail { + + requires java.logging; + requires transitive jakarta.activation; + requires junit; + + uses jakarta.mail.Provider; + uses jakarta.mail.util.StreamProvider; +} From 98cb55e6b0bac482865209142fafe3ee391be5e4 Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Wed, 8 Sep 2021 09:02:40 +0200 Subject: [PATCH 4/5] Review fixes Signed-off-by: Jorge Bescos Gascon --- .../java/jakarta/mail/internet/MimeBodyPart.java | 2 ++ .../jakarta/mail/internet/MimeBodyPartTest.java | 14 +++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/api/src/main/java/jakarta/mail/internet/MimeBodyPart.java b/api/src/main/java/jakarta/mail/internet/MimeBodyPart.java index 43f4a738..8a4a955f 100644 --- a/api/src/main/java/jakarta/mail/internet/MimeBodyPart.java +++ b/api/src/main/java/jakarta/mail/internet/MimeBodyPart.java @@ -225,6 +225,8 @@ public MimeBodyPart(InternetHeaders headers, byte[] content) * @param session Session object for this message * @param is the message input stream * @exception MessagingException for failures + * + * @since JavaMail 2.1 */ public MimeBodyPart(Session session, InputStream is) throws MessagingException { this(is); diff --git a/api/src/test/java/jakarta/mail/internet/MimeBodyPartTest.java b/api/src/test/java/jakarta/mail/internet/MimeBodyPartTest.java index 8987e05b..b0917ec4 100644 --- a/api/src/test/java/jakarta/mail/internet/MimeBodyPartTest.java +++ b/api/src/test/java/jakarta/mail/internet/MimeBodyPartTest.java @@ -56,13 +56,13 @@ public void sessionProperties() throws MessagingException, IOException { assertTrue(obp.allowutf8); assertTrue(obp.cacheMultipart); // Change the properties in opposite way - prop.put("mail.mime.setdefaulttextcharset", Boolean.FALSE); - prop.put("mail.mime.setcontenttypefilename", Boolean.FALSE); - prop.put("mail.mime.encodefilename", Boolean.TRUE); - prop.put("mail.mime.decodefilename", Boolean.TRUE); - prop.put("mail.mime.ignoremultipartencoding", Boolean.FALSE); - prop.put("mail.mime.allowutf8", Boolean.FALSE); - prop.put("mail.mime.cachemultipart", Boolean.FALSE); + prop.put("mail.mime.setdefaulttextcharset", Boolean.FALSE.toString()); + prop.put("mail.mime.setcontenttypefilename", Boolean.FALSE.toString()); + prop.put("mail.mime.encodefilename", Boolean.TRUE.toString()); + prop.put("mail.mime.decodefilename", Boolean.TRUE.toString()); + prop.put("mail.mime.ignoremultipartencoding", Boolean.FALSE.toString()); + prop.put("mail.mime.allowutf8", Boolean.FALSE.toString()); + prop.put("mail.mime.cachemultipart", Boolean.FALSE.toString()); session = Session.getInstance(prop); orig = new MimeMessage(session, input); From 9b1e97b045b284357fce96b36765fdc2294ba28e Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Tue, 3 Jan 2023 10:45:37 +0100 Subject: [PATCH 5/5] Review comment Signed-off-by: Jorge Bescos Gascon --- api/src/main/java/jakarta/mail/internet/MimeMultipart.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/jakarta/mail/internet/MimeMultipart.java b/api/src/main/java/jakarta/mail/internet/MimeMultipart.java index a602983c..9737b2ec 100644 --- a/api/src/main/java/jakarta/mail/internet/MimeMultipart.java +++ b/api/src/main/java/jakarta/mail/internet/MimeMultipart.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -1003,8 +1003,8 @@ protected MimeBodyPart createMimeBodyPart(InternetHeaders headers, */ protected MimeBodyPart createMimeBodyPart(InputStream is) throws MessagingException { - if (ds != null && ds instanceof MimePartDataSource) { - Session session = ((MimePartDataSource)ds).getMessageContext().getSession(); + if (ds instanceof MessageAware) { + Session session = ((MessageAware)ds).getMessageContext().getSession(); if (session != null) { return new MimeBodyPart(session, is); }