如何使用 Apache CXF 以简单的方式获取传入和传出的soap xml?

2024-03-19

我一直在 CXF 上摆弄服务器端拦截器。但实现简单的传入和传出拦截器(为我提供包含 SOAP XML 的纯字符串)似乎并不是一项简单的任务。

我需要在拦截器中包含纯 XML,以便我可以将它们用于特定的日志记录任务。标准的 LogIn 和 LogOut 拦截器无法胜任这项任务。有人愿意分享一些关于如何实现一个简单的传入拦截器(能够获取传入 SOAP XML)和一个传出拦截器(能够再次获取 SOAP XML)的示例吗?


在这里找到传入拦截器的代码:使用 Apache CXF 将请求/响应记录为 XML https://stackoverflow.com/questions/4592422/logging-request-response-with-apache-cxf-as-xml

我的传出拦截器:

import java.io.OutputStream;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.io.CacheAndWriteOutputStream;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.io.CachedOutputStreamCallback;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.Phase;

public class MyLogInterceptor extends LoggingOutInterceptor {

    public MyLogInterceptor() {
        super(Phase.PRE_STREAM);
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        OutputStream out = message.getContent(OutputStream.class);
        final CacheAndWriteOutputStream newOut = new CacheAndWriteOutputStream(out);
        message.setContent(OutputStream.class, newOut);
        newOut.registerCallback(new LoggingCallback());
    }

    public class LoggingCallback implements CachedOutputStreamCallback {
        public void onFlush(CachedOutputStream cos) {
        }

        public void onClose(CachedOutputStream cos) {
            try {
                StringBuilder builder = new StringBuilder();
                cos.writeCacheTo(builder, limit);
                // here comes my xml:
                String soapXml = builder.toString();
            } catch (Exception e) {
            }
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Apache CXF 以简单的方式获取传入和传出的soap xml? 的相关文章

随机推荐