views:

98

answers:

2

When a certain user tries to view our web page, a NullPointerException with the message 'charsetName' is thrown when we call response.getWriter(). I decompiled our web server's response class (JRun 3.1) and found that this error is being thrown when it does this:

s = getCharacterEncoding(); // returns 'x-mac-roman' I believe
try
{
    outWriter.exchangeWriter(new OutputStreamWriter(bufStream, s));
}
catch(UnsupportedEncodingException unsupportedencodingexception)
{
    s = MIME2Java.convert(s); // looks like this returns null
    outWriter.exchangeWriter(new OutputStreamWriter(bufStream, s)); // NPE!!!
}

I was finally able to reproduce this bug when I forced my browser to send a request header of 'Accept-Charset=x-mac-roman,utf-8', which is what the user's browser seems to do.

This is webserver code so I can't make any changes here, but this there something we can do on our end to ensure this never happens. Can we explicitly force the webserver to use a certain encoding and not leave it up to the requests?

A: 

You can create a filter and a new Request (using a request wrapper) that always responds a "valid" character encoding, for assorted values of "valid". Effectively, that's what they're trying to do with the MIME2Java.convert() call, but you would have to do that "early" and intercept that to ensure that you have better control over the encoding.

Will Hartung
+1  A: 

MacRoman is an "international character set" which is not always installed by the Sun Java installer, and hence not available to the programs.

According to http://java.sun.com/javase/6/docs/technotes/guides/intl/encoding.doc.html it is not done if the installer determines it is an "European" operating system.

If you reinstall your Sun Java and request support for Non-European languages in a customized installation, this should be corrected.

Thorbjørn Ravn Andersen