gpt4 book ai didi

java - 使用 Apache CXF 发送 FastInfoset 请求时,我可以在日志中包含 XML 吗?

转载 作者:搜寻专家 更新时间:2023-10-31 20:17:12 26 4
gpt4 key购买 nike

我需要在应用程序的日志中包含请求和响应,但 Apache CXF 发送的请求位于 FastInfoset(内容类型: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。但是您可以使用自定义 interceptor代替 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 试试这个。在这个repo你有几个使用 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);
}

关于java - 使用 Apache CXF 发送 FastInfoset 请求时,我可以在日志中包含 XML 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48408034/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com