在OutFaultInterceptor for CXF Web Service上记录错误时记录请求xml

在OutFaultInterceptor for CXF Web Service上记录错误时记录请求xml,第1张

概述当我遇到诸如失败模式验证之类的错误时,是否可以在OutFaultInterceptor中检索请求 XML并将其记录到文件中? 我试过搜索网络,但似乎没有找到与此相关的很多内容. 是的,这是可能的.我编写了CxfOutInterceptor来获取消息的XML.这是代码: import org.apache.cxf.common.util.StringUtils;import org.apache. 当我遇到诸如失败模式验证之类的错误时,是否可以在OutFaultInterceptor中检索请求 XML并将其记录到文件中?

我试过搜索网络,但似乎没有找到与此相关的很多内容.

解决方法 是的,这是可能的.我编写了CxfOutInterceptor来获取消息的XML.这是代码:

import org.apache.cxf.common.util.StringUtils;import org.apache.cxf.interceptor.Fault;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.AbstractPhaseInterceptor;import org.apache.cxf.phase.Phase;import org.springframework.beans.factory.annotation.autowired;import java.io.IOException;import java.io.OutputStream;import java.io.Writer;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class CxfOutInterceptor extends AbstractPhaseInterceptor<Message> {    private static final Logger LOGGER = LoggerFactory.getLogger(CxfInInterceptor.class);    public CxfOutInterceptor() {        super(Phase.PRE_STREAM);    }    public static final String SINGLE_KEY = CxfOutInterceptor.class.getname() + ".Processed";    private static final int liMIT = 10 * 1024 * 1024;    @OverrIDe    public voID handleFault(Message message) {        LOGGER.trace("handleFault");        try {            internalHandleMessage(message);        } catch (Throwable ex) {            LOGGER.error("Exception thrown by internalHandleMessage: ",ex);        } finally {            LOGGER.trace("handleFault - end");        }    }    @OverrIDe    public voID handleMessage(Message message) throws Fault {        LOGGER.trace("handleMessage");        try {            if (onceOnly(message)) {                LOGGER.deBUG("handled message prevIoUsly");                return;            }            internalHandleMessage(message);        } finally {            LOGGER.trace("handleMessage - end");        }    }    private class LogCallback implements CachedOutputStreamCallback {        private final Message message;        private final OutputStream origStream;        public LogCallback(final Message msg,final OutputStream os) {            this.message = msg;            this.origStream = os;        }        @OverrIDe        public voID onFlush(CachedOutputStream cos) {        }        @OverrIDe        public voID onClose(CachedOutputStream cos) {            StringBuilder requestBuilder = new StringBuilder();            String enCoding = (String) message.get(Message.ENCoding);            try {                writePayload(requestBuilder,cos,enCoding);                //requestBuilder - is your actuall body of the message.            } catch (IOException ex) {                LOGGER.trace("Unable to write output stream to StringBuilder:\n" + ex.toString());            }            try {                cos.lockOutputStream();                cos.resetOut(null,false);            } catch (Exception ex) {                LOGGER.info("Ignoring exception");            }            message.setContent(OutputStream.class,origStream);        }    }    private voID internalHandleMessage(Message message) {        final OutputStream os = message.getContent(OutputStream.class);        final Writer writer = message.getContent(Writer.class);        if (os == null && writer == null) {            return;        }        if (os == null) {            message.setContent(Writer.class,writer);        } else {            final CacheAnDWriteOutputStream newOut = new CacheAnDWriteOutputStream(os);            message.setContent(OutputStream.class,newOut);            newOut.registerCallback(new LogCallback(message,os));        }    }    private static boolean onceOnly(Message message) {        if (message.getExchange().containsKey(SINGLE_KEY)) {            return true;        } else {            message.getExchange().put(SINGLE_KEY,Boolean.TRUE);            return false;        }    }    private static voID writePayload(StringBuilder builder,CachedOutputStream cos,String enCoding)            throws IOException {        if (StringUtils.isEmpty(enCoding)) {            cos.writeCacheto(builder,liMIT);        } else {            cos.writeCacheto(builder,enCoding,liMIT);        }    }}

您将在onClose方法中获取消息的XML.请参阅此评论:// requestBuilder – 是您的消息的实际XML.

总结

以上是内存溢出为你收集整理的在OutFaultInterceptor for CXF Web Service上记录错误时记录请求xml全部内容,希望文章能够帮你解决在OutFaultInterceptor for CXF Web Service上记录错误时记录请求xml所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1054949.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存