How to get incoming & outgoing soap xml in a simple way using Apache CXF?

后端 未结 4 994
走了就别回头了
走了就别回头了 2020-12-03 00:24

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

4条回答
  •  爱一瞬间的悲伤
    2020-12-03 00:38

    Found the code for an incoming interceptor here: Logging request/response with Apache CXF as XML

    My outgoing interceptor:

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

提交回复
热议问题