Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Headers order for InternetHeaders read from stream are not always correct #98

Open
glassfishrobot opened this issue Feb 4, 2014 · 4 comments

Comments

Projects
None yet
2 participants
@glassfishrobot
Copy link

commented Feb 4, 2014

(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

@glassfishrobot

This comment has been minimized.

Copy link
Author

commented Feb 4, 2014

@glassfishrobot Commented
Reported by @bshannon

@glassfishrobot

This comment has been minimized.

Copy link
Author

commented Feb 4, 2014

@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

This comment has been minimized.

Copy link
Author

commented May 2, 2017

@glassfishrobot Commented
This issue was imported from Bugzilla JAVAMAIL-6275

@glassfishrobot

This comment has been minimized.

Copy link
Author

commented Sep 10, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.