The CXF LoggingOutInterceptor (enabled by the logging feature) creates the CachedOutputStream (as a CacheAndWriteOutputStream), and that is why you get the ClassCastException. It was never created. You need to write an interceptor that creates a CachedOutputStream if it is not present in the Message. For example:
public class CacheOutputInterceptor extends AbstractPhaseInterceptor<Message> {
public CacheOutputInterceptor() {
super(Phase.PRE_STREAM);
addAfter(LoggingOutInterceptor.class.getName());
}
@Override
public void handleMessage(Message message) throws Fault {
final OutputStream os = message.getContent(OutputStream.class);
if (os != null && !(os instanceof CachedOutputStream) ) {
final CacheAndWriteOutputStream newOut = new CacheAndWriteOutputStream(os);
message.setContent(OutputStream.class, newOut);
}
}
}
You should also take a look at the source for org.apache.cxf.interceptor.LoggingOutInterceptor.handleMessage(Message), which is where the CacheAndWriteOutputStream is created if you have the logging feature enabled.