Mule+CXF struggle with quoted encodings

25.11.2009 | 1 minutes of reading time

After upgrading Java to version 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);

insert this code at two places:

3String encoding = (String)message.get(Message.ENCODING);
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\n

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


Gemeinsam bessere Projekte umsetzen

Wir helfen Deinem Unternehmen

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.