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 |
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);
} |
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; |
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.