Skip to content

Commit fc55203

Browse files
committed
UniqueValue should use UUID::randomUUID for boundary and messageid #460
Signed-off-by: jmehrens <[email protected]>
1 parent db4f348 commit fc55203

File tree

6 files changed

+307
-21
lines changed

6 files changed

+307
-21
lines changed

mail/src/main/java/com/sun/mail/util/PropUtil.java

Lines changed: 32 additions & 1 deletion
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
@@ -115,6 +115,37 @@ public static boolean getBooleanSystemProperty(String name, boolean def) {
115115
}
116116
}
117117

118+
/**
119+
* Get a string valued System property.
120+
*
121+
* @param name the property name
122+
* @param def default value if property not found
123+
* @return the property value
124+
*/
125+
public static String getSystemProperty(String name, String def) {
126+
try {
127+
Object v = getProp(System.getProperties(), name);
128+
if (v != null) { //found entry
129+
return v instanceof String ? (String) v : def;
130+
}
131+
} catch (ClassCastException useDefault) {
132+
return def;
133+
} catch (SecurityException sex) {
134+
// fall through...
135+
}
136+
137+
/*
138+
* If we can't get the entire System Properties object because
139+
* of a SecurityException, just ask for the specific property.
140+
*/
141+
try {
142+
String value = System.getProperty(name);
143+
return value == null ? def : value;
144+
} catch (ClassCastException | SecurityException useDefault) {
145+
return def;
146+
}
147+
}
148+
118149
/**
119150
* Get the value of the specified property.
120151
* If the "get" method returns null, use the getProperty method,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,6 +2213,7 @@ public void saveChanges() throws MessagingException {
22132213
*
22142214
* @exception MessagingException for failures
22152215
* @since JavaMail 1.4
2216+
* @see jakarta.mail.internet.InternetAddress#getLocalAddress
22162217
*/
22172218
protected void updateMessageID() throws MessagingException {
22182219
setHeader("Message-ID",

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

Lines changed: 47 additions & 18 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,7 +16,8 @@
1616

1717
package jakarta.mail.internet;
1818

19-
import java.net.*;
19+
import com.sun.mail.util.PropUtil;
20+
import java.util.UUID;
2021
import java.util.concurrent.atomic.AtomicInteger;
2122
import jakarta.mail.Session;
2223

@@ -36,7 +37,7 @@ class UniqueValue {
3637
/**
3738
* A global unique number, to ensure uniqueness of generated strings.
3839
*/
39-
private static AtomicInteger id = new AtomicInteger();
40+
private static final AtomicInteger id = new AtomicInteger();
4041

4142
/**
4243
* Get a unique value for use in a multipart boundary string.
@@ -47,12 +48,19 @@ class UniqueValue {
4748
*/
4849
public static String getUniqueBoundaryValue() {
4950
StringBuilder s = new StringBuilder();
50-
long hash = s.hashCode();
51-
51+
s.append("----=_Part_");
52+
String p = PropUtil.getSystemProperty(
53+
"mail.mime.multipart.boundary.factory", null);
54+
if (UniqueValue.class.getName().equals(p)) {
5255
// Unique string is ----=_Part_<part>_<hashcode>.<currentTime>
53-
s.append("----=_Part_").append(id.getAndIncrement()).append("_").
54-
append(hash).append('.').
56+
s.append(id.getAndIncrement()).append("_").
57+
append(System.identityHashCode(s)).append('.').
5558
append(System.currentTimeMillis());
59+
} else if (UUID.class.getName().equals(p)) {
60+
s.append(UUID.randomUUID());
61+
} else { //Not defined or malformed property.
62+
s.append(UUID.randomUUID());
63+
}
5664
return s.toString();
5765
}
5866

@@ -71,25 +79,46 @@ public static String getUniqueBoundaryValue() {
7179
* @see jakarta.mail.internet.InternetAddress
7280
*/
7381
public static String getUniqueMessageIDValue(Session ssn) {
74-
String suffix = null;
82+
StringBuilder s = new StringBuilder();
83+
String p = messageIdSupplier(ssn);
84+
if (UniqueValue.class.getName().equals(p)) {
85+
// Unique string is <hashcode>.<id>.<currentTime><suffix>
86+
s.append(System.identityHashCode(s)).append('.').
87+
append(id.getAndIncrement()).append('.').
88+
append(System.currentTimeMillis());
89+
} else if (UUID.class.getName().equals(p)) {
90+
s.append(UUID.randomUUID());
91+
} else { //Not defined or malformed property.
92+
s.append(UUID.randomUUID());
93+
}
7594

95+
String suffix;
7696
InternetAddress addr = InternetAddress.getLocalAddress(ssn);
7797
if (addr != null)
7898
suffix = addr.getAddress();
7999
else {
80100
suffix = "jakartamailuser@localhost"; // worst-case default
81101
}
82-
int at = suffix.lastIndexOf('@');
83-
if (at >= 0)
84-
suffix = suffix.substring(at);
85-
86-
StringBuilder s = new StringBuilder();
87102

88-
// Unique string is <hashcode>.<id>.<currentTime><suffix>
89-
s.append(s.hashCode()).append('.').
90-
append(id.getAndIncrement()).append('.').
91-
append(System.currentTimeMillis()).
92-
append(suffix);
103+
int at = suffix.lastIndexOf('@');
104+
if (at >= 0) {
105+
s.append(suffix, at, suffix.length());
106+
} else {
107+
s.append(suffix);
108+
}
93109
return s.toString();
94110
}
111+
112+
private static String messageIdSupplier(Session ssn) {
113+
String k = "mail.mime.messageid.factory";
114+
if (ssn != null) {
115+
return ssn.getProperty(k);
116+
} else {//Act like default default session without creating it.
117+
return PropUtil.getSystemProperty(k, null);
118+
}
119+
}
120+
121+
private UniqueValue() throws IllegalAccessException {
122+
throw new IllegalAccessException(UniqueValue.class.getName());
123+
}
95124
}

mail/src/main/java/jakarta/mail/internet/package.html

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
55
<!--
66
7-
Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
7+
Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
88
99
This program and the accompanying materials are made available under the
1010
terms of the Eclipse Public License v. 2.0, which is available at
@@ -290,6 +290,20 @@
290290
</TD>
291291
</TR>
292292

293+
<TR>
294+
<TD><A ID="mail.mime.messageid.factory">mail.mime.messageid.factory</A></TD>
295+
<TD>String</TD>
296+
<TD>
297+
Sets the format of the MimeMessage message id. If set to
298+
<code>"javax.mail.internet.UniqueValue"</code>, for format compatible with older
299+
versions of JakartaMail. If set to <code>"java.util.UUID"</code> the message id
300+
will contain a randomly generator UUID.
301+
The default is <code>"java.util.UUID"</code> if not specified or format is
302+
invalid. The message id suffix can be modified by using <code>mail.from</code>
303+
or <code>mail.host</code> session properties.
304+
</TD>
305+
</TR>
306+
293307
<TR>
294308
<TD><A ID="mail.replyallcc">mail.replyallcc</A></TD>
295309
<TD>boolean</TD>
@@ -344,6 +358,19 @@
344358
</TD>
345359
</TR>
346360

361+
<TR>
362+
<TD><A ID="mail.mime.multipart.boundary.factory">mail.mime.multipart.boundary.factory</A></TD>
363+
<TD>String</TD>
364+
<TD>
365+
Sets the format of the MimeMultipart boundary line. If set to
366+
<code>"javax.mail.internet.UniqueValue"</code>, for format compatible with older
367+
versions of JakartaMail. If set to <code>"java.util.UUID"</code> the boundary
368+
line will contain a randomly generator UUID.
369+
The default is <code>"java.util.UUID"</code> if not specified or format is
370+
invalid.
371+
</TD>
372+
</TR>
373+
347374
<TR>
348375
<TD><A ID="mail.mime.setcontenttypefilename">mail.mime.setcontenttypefilename</A></TD>
349376
<TD>boolean</TD>

mail/src/test/java/com/sun/mail/util/logging/MailHandlerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class MailHandlerTest extends AbstractLogging {
6262
/**
6363
* A host name that can not be resolved.
6464
*/
65-
private static final String UNKNOWN_HOST = "bad-host-name";
65+
private static final String UNKNOWN_HOST = MailHandlerTest.class.getName();
6666
/**
6767
* Stores a writable directory that is in the class path and visible to the
6868
* context class loader.

0 commit comments

Comments
 (0)