Skip to content

Commit

Permalink
UniqueValue should use UUID::randomUUID for boundary and messageid #460
Browse files Browse the repository at this point in the history
Signed-off-by: jmehrens <[email protected]>
  • Loading branch information
jmehrens committed Jan 25, 2021
1 parent db4f348 commit fc55203
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 21 deletions.
33 changes: 32 additions & 1 deletion mail/src/main/java/com/sun/mail/util/PropUtil.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 Down Expand Up @@ -115,6 +115,37 @@ public static boolean getBooleanSystemProperty(String name, boolean def) {
}
}

/**
* Get a string valued System property.
*
* @param name the property name
* @param def default value if property not found
* @return the property value
*/
public static String getSystemProperty(String name, String def) {
try {
Object v = getProp(System.getProperties(), name);
if (v != null) { //found entry
return v instanceof String ? (String) v : def;
}
} catch (ClassCastException useDefault) {
return def;
} catch (SecurityException sex) {
// fall through...
}

/*
* If we can't get the entire System Properties object because
* of a SecurityException, just ask for the specific property.
*/
try {
String value = System.getProperty(name);
return value == null ? def : value;
} catch (ClassCastException | SecurityException useDefault) {
return def;
}
}

/**
* Get the value of the specified property.
* If the "get" method returns null, use the getProperty method,
Expand Down
1 change: 1 addition & 0 deletions mail/src/main/java/jakarta/mail/internet/MimeMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2213,6 +2213,7 @@ public void saveChanges() throws MessagingException {
*
* @exception MessagingException for failures
* @since JavaMail 1.4
* @see jakarta.mail.internet.InternetAddress#getLocalAddress
*/
protected void updateMessageID() throws MessagingException {
setHeader("Message-ID",
Expand Down
65 changes: 47 additions & 18 deletions mail/src/main/java/jakarta/mail/internet/UniqueValue.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,7 +16,8 @@

package jakarta.mail.internet;

import java.net.*;
import com.sun.mail.util.PropUtil;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import jakarta.mail.Session;

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

/**
* Get a unique value for use in a multipart boundary string.
Expand All @@ -47,12 +48,19 @@ class UniqueValue {
*/
public static String getUniqueBoundaryValue() {
StringBuilder s = new StringBuilder();
long hash = s.hashCode();

s.append("----=_Part_");
String p = PropUtil.getSystemProperty(
"mail.mime.multipart.boundary.factory", null);
if (UniqueValue.class.getName().equals(p)) {
// Unique string is ----=_Part_<part>_<hashcode>.<currentTime>
s.append("----=_Part_").append(id.getAndIncrement()).append("_").
append(hash).append('.').
s.append(id.getAndIncrement()).append("_").
append(System.identityHashCode(s)).append('.').
append(System.currentTimeMillis());
} else if (UUID.class.getName().equals(p)) {
s.append(UUID.randomUUID());
} else { //Not defined or malformed property.
s.append(UUID.randomUUID());
}
return s.toString();
}

Expand All @@ -71,25 +79,46 @@ public static String getUniqueBoundaryValue() {
* @see jakarta.mail.internet.InternetAddress
*/
public static String getUniqueMessageIDValue(Session ssn) {
String suffix = null;
StringBuilder s = new StringBuilder();
String p = messageIdSupplier(ssn);
if (UniqueValue.class.getName().equals(p)) {
// Unique string is <hashcode>.<id>.<currentTime><suffix>
s.append(System.identityHashCode(s)).append('.').
append(id.getAndIncrement()).append('.').
append(System.currentTimeMillis());
} else if (UUID.class.getName().equals(p)) {
s.append(UUID.randomUUID());
} else { //Not defined or malformed property.
s.append(UUID.randomUUID());
}

String suffix;
InternetAddress addr = InternetAddress.getLocalAddress(ssn);
if (addr != null)
suffix = addr.getAddress();
else {
suffix = "jakartamailuser@localhost"; // worst-case default
}
int at = suffix.lastIndexOf('@');
if (at >= 0)
suffix = suffix.substring(at);

StringBuilder s = new StringBuilder();

// Unique string is <hashcode>.<id>.<currentTime><suffix>
s.append(s.hashCode()).append('.').
append(id.getAndIncrement()).append('.').
append(System.currentTimeMillis()).
append(suffix);
int at = suffix.lastIndexOf('@');
if (at >= 0) {
s.append(suffix, at, suffix.length());
} else {
s.append(suffix);
}
return s.toString();
}

private static String messageIdSupplier(Session ssn) {
String k = "mail.mime.messageid.factory";
if (ssn != null) {
return ssn.getProperty(k);
} else {//Act like default default session without creating it.
return PropUtil.getSystemProperty(k, null);
}
}

private UniqueValue() throws IllegalAccessException {
throw new IllegalAccessException(UniqueValue.class.getName());
}
}
29 changes: 28 additions & 1 deletion mail/src/main/java/jakarta/mail/internet/package.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!--
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 Down Expand Up @@ -290,6 +290,20 @@
</TD>
</TR>

<TR>
<TD><A ID="mail.mime.messageid.factory">mail.mime.messageid.factory</A></TD>
<TD>String</TD>
<TD>
Sets the format of the MimeMessage message id. If set to
<code>"javax.mail.internet.UniqueValue"</code>, for format compatible with older
versions of JakartaMail. If set to <code>"java.util.UUID"</code> the message id
will contain a randomly generator UUID.
The default is <code>"java.util.UUID"</code> if not specified or format is
invalid. The message id suffix can be modified by using <code>mail.from</code>
or <code>mail.host</code> session properties.
</TD>
</TR>

<TR>
<TD><A ID="mail.replyallcc">mail.replyallcc</A></TD>
<TD>boolean</TD>
Expand Down Expand Up @@ -344,6 +358,19 @@
</TD>
</TR>

<TR>
<TD><A ID="mail.mime.multipart.boundary.factory">mail.mime.multipart.boundary.factory</A></TD>
<TD>String</TD>
<TD>
Sets the format of the MimeMultipart boundary line. If set to
<code>"javax.mail.internet.UniqueValue"</code>, for format compatible with older
versions of JakartaMail. If set to <code>"java.util.UUID"</code> the boundary
line will contain a randomly generator UUID.
The default is <code>"java.util.UUID"</code> if not specified or format is
invalid.
</TD>
</TR>

<TR>
<TD><A ID="mail.mime.setcontenttypefilename">mail.mime.setcontenttypefilename</A></TD>
<TD>boolean</TD>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class MailHandlerTest extends AbstractLogging {
/**
* A host name that can not be resolved.
*/
private static final String UNKNOWN_HOST = "bad-host-name";
private static final String UNKNOWN_HOST = MailHandlerTest.class.getName();
/**
* Stores a writable directory that is in the class path and visible to the
* context class loader.
Expand Down
Loading

0 comments on commit fc55203

Please sign in to comment.