Skip to content

Commit a2aba7c

Browse files
committed
Adding setters for system properties
Signed-off-by: Jorge Bescos Gascon <[email protected]> Move methods from jakarta to com.sun.mail Signed-off-by: Jorge Bescos Gascon <[email protected]> Use session properties instead of setters Signed-off-by: Jorge Bescos Gascon <[email protected]>
1 parent 226d2d6 commit a2aba7c

File tree

7 files changed

+270
-83
lines changed

7 files changed

+270
-83
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ nbproject/private/
44
.m2/
55
webrev
66
mbox/.ccls-cache/
7+
/bin/
8+
.classpath
9+
.project
10+
.settings/

mail/src/main/java/jakarta/mail/internet/MimeBodyPart.java

Lines changed: 96 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -16,16 +16,42 @@
1616

1717
package jakarta.mail.internet;
1818

19-
import jakarta.mail.*;
20-
import jakarta.activation.*;
21-
import java.io.*;
22-
import java.util.*;
23-
import com.sun.mail.util.PropUtil;
19+
import jakarta.activation.DataHandler;
20+
import jakarta.activation.DataSource;
21+
import jakarta.activation.FileDataSource;
22+
import jakarta.mail.BodyPart;
23+
import jakarta.mail.EncodingAware;
24+
import jakarta.mail.FolderClosedException;
25+
import jakarta.mail.Header;
26+
import jakarta.mail.IllegalWriteException;
27+
import jakarta.mail.Message;
28+
import jakarta.mail.MessageContext;
29+
import jakarta.mail.MessageRemovedException;
30+
import jakarta.mail.MessagingException;
31+
import jakarta.mail.Multipart;
32+
import jakarta.mail.Part;
33+
import jakarta.mail.Session;
34+
35+
import java.io.BufferedInputStream;
36+
import java.io.BufferedOutputStream;
37+
import java.io.ByteArrayInputStream;
38+
import java.io.File;
39+
import java.io.FileOutputStream;
40+
import java.io.IOException;
41+
import java.io.InputStream;
42+
import java.io.OutputStream;
43+
import java.io.UnsupportedEncodingException;
44+
import java.util.ArrayList;
45+
import java.util.Enumeration;
46+
import java.util.List;
47+
import java.util.Properties;
48+
2449
import com.sun.mail.util.ASCIIUtility;
25-
import com.sun.mail.util.MimeUtil;
26-
import com.sun.mail.util.MessageRemovedIOException;
2750
import com.sun.mail.util.FolderClosedIOException;
2851
import com.sun.mail.util.LineOutputStream;
52+
import com.sun.mail.util.MessageRemovedIOException;
53+
import com.sun.mail.util.MimeUtil;
54+
import com.sun.mail.util.PropUtil;
2955

3056
/**
3157
* This class represents a MIME body part. It implements the
@@ -63,29 +89,30 @@ public class MimeBodyPart extends BodyPart implements MimePart {
6389

6490
// Paranoia:
6591
// 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-
92+
static final boolean SET_DEFAULT_TEXT_CHARSET =
93+
PropUtil.getBooleanSystemProperty("mail.mime.setdefaulttextcharset", true);
94+
static final boolean SET_CONTENT_TYPE_FILE_NAME =
95+
PropUtil.getBooleanSystemProperty("mail.mime.setcontenttypefilename", true);
96+
static final boolean ENCODE_FILE_NAME =
97+
PropUtil.getBooleanSystemProperty("mail.mime.encodefilename", false);
98+
static final boolean DECODE_FILE_NAME =
99+
PropUtil.getBooleanSystemProperty("mail.mime.decodefilename", false);
100+
static final boolean IGNORE_MULTIPART_ENCODING =
101+
PropUtil.getBooleanSystemProperty("mail.mime.ignoremultipartencoding", true);
102+
static final boolean ALLOW_UTF8 =
103+
PropUtil.getBooleanSystemProperty("mail.mime.allowutf8", true);
84104
// Paranoia:
85105
// 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);
106+
static final boolean CACHE_MULTIPART = // accessed by MimeMessage
107+
PropUtil.getBooleanSystemProperty("mail.mime.cachemultipart", true);
88108

109+
protected boolean setDefaultTextCharset = SET_DEFAULT_TEXT_CHARSET;
110+
protected boolean setContentTypeFileName = SET_CONTENT_TYPE_FILE_NAME;
111+
protected boolean encodeFileName = ENCODE_FILE_NAME;
112+
protected boolean decodeFileName = DECODE_FILE_NAME;
113+
protected boolean ignoreMultipartEncoding = IGNORE_MULTIPART_ENCODING;
114+
protected boolean allowutf8 = ALLOW_UTF8;
115+
protected boolean cacheMultipart = CACHE_MULTIPART;
89116

90117
/**
91118
* The DataHandler object representing this Part's content.
@@ -192,6 +219,34 @@ public MimeBodyPart(InternetHeaders headers, byte[] content)
192219
this.content = content;
193220
}
194221

222+
/**
223+
* Initializes MimeBodyPart from the InputStream is and it overwrites
224+
* properties from session.
225+
*
226+
* @param session Session object for this message
227+
* @param is the message input stream
228+
* @exception MessagingException for failures
229+
*/
230+
public MimeBodyPart(Session session, InputStream is) throws MessagingException {
231+
this(is);
232+
initializeProperties(session);
233+
}
234+
235+
/**
236+
* Set the values from session properties if exist, otherwise it keeps the previous value.
237+
* @param session the not null session
238+
*/
239+
private void initializeProperties(Session session) {
240+
Properties props = session.getProperties();
241+
setDefaultTextCharset = PropUtil.getBooleanProperty(props, "mail.mime.setdefaulttextcharset", setDefaultTextCharset);
242+
setContentTypeFileName = PropUtil.getBooleanProperty(props, "mail.mime.setcontenttypefilename", setContentTypeFileName);
243+
encodeFileName = PropUtil.getBooleanProperty(props, "mail.mime.encodefilename", encodeFileName);
244+
decodeFileName = PropUtil.getBooleanProperty(props, "mail.mime.decodefilename", decodeFileName);
245+
ignoreMultipartEncoding = PropUtil.getBooleanProperty(props, "mail.mime.ignoremultipartencoding", ignoreMultipartEncoding);
246+
allowutf8 = PropUtil.getBooleanProperty(props, "mail.mime.allowutf8", allowutf8);
247+
cacheMultipart = PropUtil.getBooleanProperty(props, "mail.mime.cachemultipart", cacheMultipart);
248+
}
249+
195250
/**
196251
* Return the size of the content of this body part in bytes.
197252
* Return -1 if the size cannot be determined. <p>
@@ -522,7 +577,7 @@ public void setDescription(String description, String charset)
522577
*/
523578
@Override
524579
public String getFileName() throws MessagingException {
525-
return getFileName(this);
580+
return getFileName(this, decodeFileName);
526581
}
527582

528583
/**
@@ -550,7 +605,7 @@ public String getFileName() throws MessagingException {
550605
*/
551606
@Override
552607
public void setFileName(String filename) throws MessagingException {
553-
setFileName(this, filename);
608+
setFileName(this, filename, encodeFileName, setContentTypeFileName);
554609
}
555610

556611
/**
@@ -969,7 +1024,7 @@ public void saveFile(String file) throws IOException, MessagingException {
9691024
@Override
9701025
public void writeTo(OutputStream os)
9711026
throws IOException, MessagingException {
972-
writeTo(this, os, null);
1027+
writeTo(this, os, null, allowutf8, ignoreMultipartEncoding);
9731028
}
9741029

9751030
/**
@@ -1145,7 +1200,7 @@ public Enumeration<String> getNonMatchingHeaderLines(String[] names)
11451200
* @exception MessagingException for failures
11461201
*/
11471202
protected void updateHeaders() throws MessagingException {
1148-
updateHeaders(this);
1203+
updateHeaders(this, setDefaultTextCharset, setContentTypeFileName, encodeFileName);
11491204
/*
11501205
* If we've cached a Multipart or Message object then
11511206
* we're now committed to using this instance of the
@@ -1261,7 +1316,7 @@ static String getDescription(MimePart part)
12611316
}
12621317
}
12631318

1264-
static String getFileName(MimePart part) throws MessagingException {
1319+
static String getFileName(MimePart part, boolean decodeFileName) throws MessagingException {
12651320
String filename = null;
12661321
String s = part.getHeader("Content-Disposition", null);
12671322

@@ -1291,7 +1346,8 @@ static String getFileName(MimePart part) throws MessagingException {
12911346
return filename;
12921347
}
12931348

1294-
static void setFileName(MimePart part, String name)
1349+
static void setFileName(MimePart part, String name, boolean encodeFileName,
1350+
boolean setContentTypeFileName)
12951351
throws MessagingException {
12961352
if (encodeFileName && name != null) {
12971353
try {
@@ -1440,7 +1496,7 @@ static void setEncoding(MimePart part, String encoding)
14401496
* Content-Type of the specified MimePart. Returns
14411497
* either the original encoding or null.
14421498
*/
1443-
static String restrictEncoding(MimePart part, String encoding)
1499+
static String restrictEncoding(MimePart part, String encoding, boolean ignoreMultipartEncoding)
14441500
throws MessagingException {
14451501
if (!ignoreMultipartEncoding || encoding == null)
14461502
return encoding;
@@ -1473,7 +1529,8 @@ static String restrictEncoding(MimePart part, String encoding)
14731529
return encoding;
14741530
}
14751531

1476-
static void updateHeaders(MimePart part) throws MessagingException {
1532+
static void updateHeaders(MimePart part, boolean setDefaultTextCharset, boolean setContentTypeFileName,
1533+
boolean encodeFileName) throws MessagingException {
14771534
DataHandler dh = part.getDataHandler();
14781535
if (dh == null) // Huh ?
14791536
return;
@@ -1620,8 +1677,8 @@ static void invalidateContentHeaders(MimePart part)
16201677
part.removeHeader("Content-Transfer-Encoding");
16211678
}
16221679

1623-
static void writeTo(MimePart part, OutputStream os, String[] ignoreList)
1624-
throws IOException, MessagingException {
1680+
static void writeTo(MimePart part, OutputStream os, String[] ignoreList, boolean allowutf8,
1681+
boolean ignoreMultipartEncoding) throws IOException, MessagingException {
16251682

16261683
// see if we already have a LOS
16271684
LineOutputStream los = null;
@@ -1666,7 +1723,7 @@ static void writeTo(MimePart part, OutputStream os, String[] ignoreList)
16661723
os.write(buf, 0, len);
16671724
} else {
16681725
os = MimeUtility.encode(os,
1669-
restrictEncoding(part, part.getEncoding()));
1726+
restrictEncoding(part, part.getEncoding(), ignoreMultipartEncoding));
16701727
part.getDataHandler().writeTo(os);
16711728
}
16721729
} finally {

0 commit comments

Comments
 (0)