簡體   English   中英

使用Apache CXF發送FastInfoset請求時,我可以在日志中使用XML嗎?

[英]Can I have the XML in the log when sending FastInfoset request with Apache CXF?

我需要在應用程序的日志中提供請求和響應,但Apache CXF發送的請求在FastInfoset(Content-Type:application / fastinfoset)中,這導致請求和響應的日志不可讀(因為它是二進制的) )。 有沒有辦法解決這個問題,以便我保留FastInfoset消息(出於性能原因)但是我在日志中得到了正確的XML?

這是我現在擁有的CXF配置,如果有幫助的話:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/core"
    xsi:schemaLocation="
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="logInbound" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
    <bean id="logOutbound" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />

    <cxf:bus>
        <cxf:inInterceptors>
            <ref bean="logInbound" />
        </cxf:inInterceptors>
        <cxf:outInterceptors>
            <ref bean="logOutbound" />
        </cxf:outInterceptors>
        <cxf:outFaultInterceptors>
            <ref bean="logOutbound" />
        </cxf:outFaultInterceptors>
        <cxf:inFaultInterceptors>
            <ref bean="logInbound" />
        </cxf:inFaultInterceptors>
    </cxf:bus>
</beans>

預先感謝您的任何幫助。

我看了一下LoggingInInterceptor.logInputStream ,它似乎不支持fastinfoset。 但是您可以使用自定義攔截器而不是LoggingInInterceptorLoggingOutInterceptor來提取有效負載,對其進行解碼並記錄原始消息。

public class CustomInterceptor extends AbstractPhaseInterceptor<Message> {
    public CustomInterceptor () {
        super(Phase.RECEIVE);
    }

    public void handleMessage(Message message) {
        //Get the message body into payload[] and set a new non-consumed  inputStream into Message
        InputStream in = message.getContent(InputStream.class);
        byte payload[] = IOUtils.readBytesFromStream(in);
        ByteArrayInputStream bin = new ByteArrayInputStream(payload);
        message.setContent(InputStream.class, bin);

        //Decode from FastInfoset and write the payload in your preferred log system
        OutputStream out = System.out 
        decodeFI(in,out);

    }

    public void handleFault(Message messageParam) {
        //Invoked when interceptor fails
        //Exception e = message.getContent(Exception.class);
    }
}

替換XML文件

<bean id="logInbound" class="test.CustomInterceptor" />
<bean id="logOutbound" class="test.CustomInterceptor" />

找到如何解碼FastInfoset的示例並不容易。 使用DOM和FastInfoset-1.2.12.jar嘗試此操作。 在這個回購中,你有幾個使用sTAX和SAX的例子

public void decodeFI(InputStream in, OutputStream out) throws Exception{
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.newDocument();

    DOMDocumentParser parser = new DOMDocumentParser();
    parser.parse(doc, in);

    // write the content into xml file
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(out);
    transformer.transform(source, result);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM