Reputation: 1518
I have created a Rest service using CXF 3.0.0 milestone1 and I am trying to get the body of the HTTP response in an Out Interceptor in phase SEND and put it in a String variable without having the logging feature enabled in the xml configuration file.
The question is very similar to this question "How do I get the response payload of a REST service from the Message of an outgoing Interceptor?" which I asked a while ago. The difference here is that in my old question I had enabled the logging feature of Apache CXF
<jaxrs:features>
<cxf:logging/>
</jaxrs:features>
When I remove the <cxf:logging/>
from my xml
file then I get a ClassCastException
.
Here is my interceptor code:
public void handleMessage(Message message) throws Fault {
BAprintHouse.setFunction("MyLoginOutInterceptor.handleMessage()");
OutputStream os = message.getContent(OutputStream.class);
StringBuilder responsePayload = new StringBuilder();
CachedOutputStream cos = (CachedOutputStream) os;
Exchange ex = message.getExchange();
try {
cos.writeCacheTo(responsePayload);
} catch (IOException e) {
BAprintHouse.log("ERROR", "IO Error:" + e.getMessage());
}
}
The exception occurs in the line CachedOutputStream cos = (CachedOutputStream) os;
java.lang.ClassCastException: org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStream cannot be cast to org.apache.cxf.io.CachedOutputStream
at com.test.interceptors.MyLoginOutInterceptor.handleMessage(MyLoginOutInterceptor.java:35)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
at org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:77)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:239)
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:223)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:203)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:137)
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:158)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:243)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:168)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:219)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:722)
Is there any way to get the response body from the message and that it will work with or without the <cxf:logging/>
in place?
Thanks
Upvotes: 3
Views: 5533
Reputation: 2122
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.
Upvotes: 4