Mule+CXF struggle with quoted encodings

After upgrading Java to version 1.6.0.17 our CXF based webservices running on Mule ESB did not work anymore.

Caused by: javax.xml.stream.XMLStreamException: java.io.UnsupportedEncodingException: "utf-8"
	at com.ctc.wstx.stax.WstxOutputFactory.createSW(WstxOutputFactory.java:257)
	at com.ctc.wstx.stax.WstxOutputFactory.createXMLStreamWriter(WstxOutputFactory.java:124)
	at org.apache.cxf.interceptor.StaxOutInterceptor.handleMessage(StaxOutInterceptor.java:67)
	... 25 more
Caused by: java.io.UnsupportedEncodingException: "utf-8"
	at sun.nio.cs.StreamEncoder.forOutputStreamWriter(StreamEncoder.java:42)
	at java.io.OutputStreamWriter.<init>(OutputStreamWriter.java:83)
	at com.ctc.wstx.stax.WstxOutputFactory.createSW(WstxOutputFactory.java:253)
	... 27 more

That looks strange, because utf-8 should be a supported encoding. But a closer look reveals that the encoding passed contains the quotes. And that does not work. According to the specification quotes are allowed around the charset, but that is not very common.
The quotes were introduced in JAX-WS 2.1.2.

luckily the problem is easy to patch when you know this:

if (encoding != null && encoding.startsWith("\"") && encoding.endsWith("\"")) {
  encoding = encoding.substring(1, encoding.length() - 1);
}

insert this code at two places:

org.apache.cxf.interceptor.StaxInInterceptor#handleMessage()
after
String encoding = (String)message.get(Message.ENCODING);
 
and
org.apache.cxf.interceptor.StaxInInterceptor#getEncoding()
before
return encoding;

The patched class needs to be on the classpath before the original one. When doing so, the webservices worked again. A small unit test verifies this behavior and ensures that after a potential Mule/CXF upgrade the quoted encodings work.

  • Facebook
  • Delicious
  • Digg
  • StumbleUpon
  • Reddit
  • Blogger
  • LinkedIn
Fabian Lange

One Response to Mule+CXF struggle with quoted encodings

  1. John Prystash says:

    Thanks a lot for this info! This did the trick for me. We were using Mule 2.2.6 which pulls in CXF 2.1.9. I re-built cxf-rt-core from source, updated the version number, and depended on the new version, things work great.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>