【问题标题】:CXF logging InterceptorsCXF 日志记录拦截器
【发布时间】:2011-12-29 08:24:52
【问题描述】:

我目前正在使用 CXF 用户指南中提到的 log4j 记录 CXF。但是日志文件正在泛滥并且变得无法管理所有 IN/OUT 有效负载日志。

仅当某些错误/异常作为输出生成时,我才需要记录传入的 SOAP 有效负载。我知道它需要编写自定义拦截器,但这有可能实现吗?

谁能给我一些链接/提示或者可能是一些示例工作代码?

提前致谢 提尔坦卡

【问题讨论】:

  • 有人请建议.......我还是卡住了!!祝大家新年快乐。

标签: logging soap cxf interceptor


【解决方案1】:

此链接可能会有所帮助:http://www.madbit.org/blog/programming/942/how-to-log-apache-cxf-soap-request-and-response-using-log4j/#sthash.SOlB7sx6.CaTMsv3I.dpbs

您可以通过编写自定义拦截器并在您的 cxf.xml 文件中添加 bean ref 来做到这一点:

<bean id="customIncomingSoapFaultInterceptor"     class="com.tirtha.CustomIncomingSoapFaultInterceptor" />

<cxf:bus>

    <cxf:inFaultInterceptors>
        <ref bean="customIncomingSoapFaultInterceptor" />

    </cxf:inFaultInterceptors>

</cxf:bus>

示例自定义拦截器:

import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.cxf.binding.soap.Soap12;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.transport.http.AbstractHTTPDestination;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class CustomIncomingSoapFaultInterceptor extends AbstractSoapInterceptor  {

private static Log s_logger =    LogFactory.getLog(CustomIncomingSoapFaultInterceptor.class);


public CustomIncomingSoapFaultInterceptor(){

    // set phase here
    //super(Phase.PRE_PROTOCOL);
    super(Phase.RECEIVE);
}
@Override
public void handleMessage(SoapMessage message) throws Fault {
    Fault fault = null;
    String soapMessage = null;

        StringBuilder strMessage = null;    
        HttpServletRequest httpRequest = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
        if (httpRequest != null) {


             InputStream ist = message.getContent(InputStream.class);
                if (ist != null) {
                    CachedOutputStream bos = new CachedOutputStream();
                    try {
                        IOUtils.copy(ist, bos);

                        bos.flush();
                        ist.close();
                        message.setContent(InputStream.class, bos.getInputStream());
                        soapMessage = new String(bos.getBytes());//this soap message is what you want
                        bos.close();

                        s_logger.debug("Soap Message: ---------->" + soapMessage==null?"null":soapMessage);
                        s_logger.debug("String Request: ---------->" + soapMessage);


                    } catch (IOException e) {
                        throw new Fault(e);
                    }
                }



        }
}

【讨论】:

    【解决方案2】:

    您需要自定义 Feature 来仅记录 SOAP 错误/异常。

    以下是LoggingFeature.initializeProvider()方法的源码。如您所见,此方法中添加了故障拦截器。

    @Override
    protected void initializeProvider(InterceptorProvider provider, Bus bus) {
        if (limit == DEFAULT_LIMIT && inLocation == null 
            && outLocation == null && !prettyLogging) {
            provider.getInInterceptors().add(IN);
        >>> provider.getInFaultInterceptors().add(IN);
            provider.getOutInterceptors().add(OUT);
        >>> provider.getOutFaultInterceptors().add(OUT);
        } else {
            LoggingInInterceptor in = new LoggingInInterceptor(limit);
            in.setOutputLocation(inLocation);
            in.setPrettyLogging(prettyLogging);
            in.setShowBinaryContent(showBinary);
            LoggingOutInterceptor out = new LoggingOutInterceptor(limit);
            out.setOutputLocation(outLocation);
            out.setPrettyLogging(prettyLogging);
            out.setShowBinaryContent(showBinary);
    
            provider.getInInterceptors().add(in);
            provider.getInFaultInterceptors().add(in);
            provider.getOutInterceptors().add(out);
            provider.getOutFaultInterceptors().add(out);
        }
    }
    

    您可以编写自己的LoggingFeature 并覆盖initializeProvider,如下所示:

    public class CustomLoggingFeature extends LoggingFeature {
        @Override
        protected void initializeProvider(InterceptorProvider provider, Bus bus) {
            provider.getInFaultInterceptors().add(new LoggingInInterceptor(getLimit()));
            provider.getOutFaultInterceptors().add(new LoggingOutInterceptor(getLimit()));
        }
    }
    

    然后你可以激活CustomLoggingFeature如下:

    @WebService
    @Features(classes = {CustomLoggingFeature.class})
    public interface AssetServices {
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      • 2017-08-02
      • 2023-04-07
      • 2010-10-09
      相关资源
      最近更新 更多