mail-api
mail-api copied to clipboard
Headers order for InternetHeaders read from stream are not always correct
(Originally submitted to Sun bug system, transferred to Oracle bug system, and finally here for resolution.)
A DESCRIPTION OF THE PROBLEM : InternetHeaders created from stream do not initialize the internal array with placeholders for default headers.
public InternetHeaders(InputStream is) throws MessagingException { headers = new ArrayList(40); load(is); }
should be better written as:
public InternetHeaders(InputStream is) throws MessagingException { this(); load(is); }
This way adding further headers to an InternetHeaders created from stream will keep the correct order.
Furthermore if the stream that we used to create the internet headers have a Return-Path in the wrong position (not as first line) any further call to addHeader/setHeader for Return-Path will put the header in the wrong place.
REPRODUCIBILITY : This bug can be reproduced always.
CUSTOMER SUBMITTED WORKAROUND : Override the methods in a custom InternetHeaders extension:
public class MyInternetHeaders extends InternetHeaders {
/**
* Constructor that takes an InputStream containing the contents
* of the set of mail headers.
*
* @param in the InputStream containing the header data
*
* @throws MessagingException if the super class cannot be properly instantiated
* based on the stream
*/
public MyInternetHeaders(InputStream in) throws MessagingException {
super();
load(in);
}
/**
* If the new header is a Return-Path we get sure that we add it to the top
* Javamail, at least until 1.4.0 does the wrong thing if it loaded a stream with
* a return-path in the middle.
*
* @see javax.mail.internet.InternetHeaders#addHeader(java.lang.String, java.lang.String)
*/
public void addHeader(String arg0, String arg1) {
if (RFC2822Headers.RETURN_PATH.equalsIgnoreCase(arg0)) {
headers.add(0, new InternetHeader(arg0, arg1));
} else {
super.addHeader(arg0, arg1);
}
}
/**
* If the new header is a Return-Path we get sure that we add it to the top
* Javamail, at least until 1.4.0 does the wrong thing if it loaded a stream with
* a return-path in the middle.
*
* @see javax.mail.internet.InternetHeaders#setHeader(java.lang.String, java.lang.String)
*/
public void setHeader(String arg0, String arg1) {
if (RFC2822Headers.RETURN_PATH.equalsIgnoreCase(arg0)) {
super.removeHeader(arg0);
}
super.setHeader(arg0, arg1);
}
....
Affected Versions
1.4.3
Environment
All All
- Issue Imported From: https://github.com/javaee/javamail/issues/98
- Original Issue Raised By:@glassfishrobot
- Original Issue Assigned To: @bshannon
@glassfishrobot Commented @bshannon said: (Original evaluation from Sun bug system:)
It's not clear to me what the right solution is. It would definitely be wrong to reorder the headers when reading in the message. If the original headers are in some random order, it's not clear where to put new headers that you add to the message. The only reasonable solution may be for you to subclass InternetHeaders and make it behave the way that's appropriate for your application.
@glassfishrobot Commented Reported by @bshannon
@glassfishrobot Commented This issue was imported from Bugzilla JAVAMAIL-6275