如何在没有启用日志记录功能的情况下在Out Interceptor中获取http响应正文?

时间:2014-03-31 12:53:46

标签: java rest cxf jax-rs interceptor

我使用 CXF 3.0.0里程碑1 创建了 Rest 服务,我正在努力获取 HTTP 响应的正文 Out Interceptor 阶段 SEND 并将其放入String变量中,而不在xml配置文件中启用日志记录功能。

这个问题非常类似于我刚才问过的这个问题“How do I get the response payload of a REST service from the Message of an outgoing Interceptor?”。这里的区别在于,在我的旧问题中,我启用了 Apache CXF

的日志记录功能
<jaxrs:features>
    <cxf:logging/>
</jaxrs:features>

当我从<cxf:logging/>文件中删除xml后,我得到ClassCastException

这是我的拦截器代码:

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

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)

有没有办法从邮件中获取响应正文,并且无论是否有<cxf:logging/>它都能正常工作?

谢谢

1 个答案:

答案 0 :(得分:4)

CXF LoggingOutInterceptor(由日志记录功能启用)创建CachedOutputStream(作为CacheAndWriteOutputStream),这就是您获得ClassCastException的原因。它从未被创造过。如果消息中不存在CachedOutputStream,则需要编写一个创建CachedOutputStream的拦截器。例如:

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

    }

}

您还应该查看org.apache.cxf.interceptor.LoggingOutInterceptor.handleMessage(Message)的源代码,如果您启用了日志记录功能,则会创建CacheAndWriteOutputStream。