2011-12-29 101 views
2

我目前使用log4j來記錄CXF,如CXF用戶指南中所述。但是日誌文件氾濫,並且對於所有IN/OUT負載日誌變得難以管理。CXF日誌記錄攔截器

只有當生成一些故障/異常作爲輸出時,我需要記錄傳入的SOAP有效負載。我知道這將需要編寫自定義攔截器,但這是可能實現的嗎?

任何人都可以提供一些鏈接/提示或可能是一些示例工作代碼?

由於提前 教Tirthankar

+0

有人請建議......我仍然卡住!祝大家新年快樂。 – Tirtha 2012-01-01 05:30:59

回答

3

此鏈接可以幫助: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 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); 
        } 
       } 



     } 
} 
0

您需要自定義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 { 
}