Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ nbproject/private/
.m2/
webrev
mbox/.ccls-cache/
/bin/
.classpath
.project
.settings/
135 changes: 96 additions & 39 deletions mail/src/main/java/jakarta/mail/internet/MimeBodyPart.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 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
Expand All @@ -16,16 +16,42 @@

package jakarta.mail.internet;

import jakarta.mail.*;
import jakarta.activation.*;
import java.io.*;
import java.util.*;
import com.sun.mail.util.PropUtil;
import jakarta.activation.DataHandler;
import jakarta.activation.DataSource;
import jakarta.activation.FileDataSource;
import jakarta.mail.BodyPart;
import jakarta.mail.EncodingAware;
import jakarta.mail.FolderClosedException;
import jakarta.mail.Header;
import jakarta.mail.IllegalWriteException;
import jakarta.mail.Message;
import jakarta.mail.MessageContext;
import jakarta.mail.MessageRemovedException;
import jakarta.mail.MessagingException;
import jakarta.mail.Multipart;
import jakarta.mail.Part;
import jakarta.mail.Session;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import com.sun.mail.util.ASCIIUtility;
import com.sun.mail.util.MimeUtil;
import com.sun.mail.util.MessageRemovedIOException;
import com.sun.mail.util.FolderClosedIOException;
import com.sun.mail.util.LineOutputStream;
import com.sun.mail.util.MessageRemovedIOException;
import com.sun.mail.util.MimeUtil;
import com.sun.mail.util.PropUtil;

/**
* This class represents a MIME body part. It implements the
Expand Down Expand Up @@ -63,29 +89,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 =
PropUtil.getBooleanSystemProperty(
"mail.mime.setdefaulttextcharset", true);

private static final boolean setContentTypeFileName =
PropUtil.getBooleanSystemProperty(
"mail.mime.setcontenttypefilename", true);

private static final boolean encodeFileName =
PropUtil.getBooleanSystemProperty("mail.mime.encodefilename", false);
private static final boolean decodeFileName =
PropUtil.getBooleanSystemProperty("mail.mime.decodefilename", false);
private static final boolean ignoreMultipartEncoding =
PropUtil.getBooleanSystemProperty(
"mail.mime.ignoremultipartencoding", true);
private static final boolean allowutf8 =
PropUtil.getBooleanSystemProperty("mail.mime.allowutf8", true);

static final boolean SET_DEFAULT_TEXT_CHARSET =
PropUtil.getBooleanSystemProperty("mail.mime.setdefaulttextcharset", true);
static final boolean SET_CONTENT_TYPE_FILE_NAME =
PropUtil.getBooleanSystemProperty("mail.mime.setcontenttypefilename", true);
static final boolean ENCODE_FILE_NAME =
PropUtil.getBooleanSystemProperty("mail.mime.encodefilename", false);
static final boolean DECODE_FILE_NAME =
PropUtil.getBooleanSystemProperty("mail.mime.decodefilename", false);
static final boolean IGNORE_MULTIPART_ENCODING =
PropUtil.getBooleanSystemProperty("mail.mime.ignoremultipartencoding", true);
static final boolean ALLOW_UTF8 =
PropUtil.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
PropUtil.getBooleanSystemProperty("mail.mime.cachemultipart", true);
static final boolean CACHE_MULTIPART = // accessed by MimeMessage
PropUtil.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.
Expand Down Expand Up @@ -192,6 +219,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 = PropUtil.getBooleanProperty(props, "mail.mime.setdefaulttextcharset", setDefaultTextCharset);
setContentTypeFileName = PropUtil.getBooleanProperty(props, "mail.mime.setcontenttypefilename", setContentTypeFileName);
encodeFileName = PropUtil.getBooleanProperty(props, "mail.mime.encodefilename", encodeFileName);
decodeFileName = PropUtil.getBooleanProperty(props, "mail.mime.decodefilename", decodeFileName);
ignoreMultipartEncoding = PropUtil.getBooleanProperty(props, "mail.mime.ignoremultipartencoding", ignoreMultipartEncoding);
allowutf8 = PropUtil.getBooleanProperty(props, "mail.mime.allowutf8", allowutf8);
cacheMultipart = PropUtil.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. <p>
Expand Down Expand Up @@ -522,7 +577,7 @@ public void setDescription(String description, String charset)
*/
@Override
public String getFileName() throws MessagingException {
return getFileName(this);
return getFileName(this, decodeFileName);
}

/**
Expand Down Expand Up @@ -550,7 +605,7 @@ public String getFileName() throws MessagingException {
*/
@Override
public void setFileName(String filename) throws MessagingException {
setFileName(this, filename);
setFileName(this, filename, encodeFileName, setContentTypeFileName);
}

/**
Expand Down Expand Up @@ -969,7 +1024,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);
}

/**
Expand Down Expand Up @@ -1145,7 +1200,7 @@ public Enumeration<String> 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
Expand Down Expand Up @@ -1261,7 +1316,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);

Expand Down Expand Up @@ -1291,7 +1346,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 {
Expand Down Expand Up @@ -1440,7 +1496,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;
Expand Down Expand Up @@ -1473,7 +1529,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;
Expand Down Expand Up @@ -1620,8 +1677,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;
Expand Down Expand Up @@ -1666,7 +1723,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 {
Expand Down
Loading