如何获得传入 &使用Apache CXF以简单的方式传出soap xml? [英] How to get incoming & outgoing soap xml in a simple way using Apache CXF?

查看:22
本文介绍了如何获得传入 &使用Apache CXF以简单的方式传出soap xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在 CXF 上摆弄服务器端拦截器.但是似乎实现简单的传入和传出拦截器并不是一项微不足道的任务,这些拦截器为我提供了一个包含 SOAP XML 的纯字符串.

I have been fiddling around with server side interceptors on CXF. But is seems that it is not a trivial task to implement simple incoming and outgoing interceptors that give me a plain string containing the SOAP XML.

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

I need to have the plain XML in the interceptor so that I can use them for specific logging tasks. The standard LogIn & LogOut interceptors are not up to the task. Is anyone willing to share some example on how I could implement a simple incoming interceptor that is able to get the incoming SOAP XML and a outgoing interceptor to again get the SOAP XML?

推荐答案

在这里找到传入拦截器的代码:使用 Apache CXF 作为 XML 记录请求/响应

Found the code for an incoming interceptor here: 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) {
            }
        }
    }
}

这篇关于如何获得传入 &使用Apache CXF以简单的方式传出soap xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆