Popular searches
//

Mule+CXF struggle with quoted encodings

25.11.2009 | 1 minutes reading time

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

1Caused by: javax.xml.stream.XMLStreamException: java.io.UnsupportedEncodingException: "utf-8"
2    at com.ctc.wstx.stax.WstxOutputFactory.createSW(WstxOutputFactory.java:257)
3    at com.ctc.wstx.stax.WstxOutputFactory.createXMLStreamWriter(WstxOutputFactory.java:124)
4    at org.apache.cxf.interceptor.StaxOutInterceptor.handleMessage(StaxOutInterceptor.java:67)
5    ... 25 more
6Caused by: java.io.UnsupportedEncodingException: "utf-8"
7    at sun.nio.cs.StreamEncoder.forOutputStreamWriter(StreamEncoder.java:42)
8    at java.io.OutputStreamWriter.<init>(OutputStreamWriter.java:83)
9    at com.ctc.wstx.stax.WstxOutputFactory.createSW(WstxOutputFactory.java:253)
10    ... 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:

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

insert this code at two places:

1org.apache.cxf.interceptor.StaxInInterceptor#handleMessage()
2after
3String encoding = (String)message.get(Message.ENCODING);
4 
5and
6org.apache.cxf.interceptor.StaxInInterceptor#getEncoding()
7before
8return 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.

share post

//

More articles in this subject area

Discover exciting further topics and let the codecentric world inspire you.