I need to to have the requests and responses in the logs of the application but the requests sent by Apache CXF are in FastInfoset (Content-Type: application/fastinfoset) which results in the log of the request and response being unreadable (since it's binary). Is there a way around that so that I keep FastInfoset messages (for performance reasons) but I get proper XML in the logs?
Here is the CXF configuration I have right now, if that helps:
<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>
Thank you in advance for any help.
I have taken a look at LoggingInInterceptor.logInputStream and it seems that it does not support fastinfoset. But you can use a custom interceptor instead of LoggingInInterceptor
and LoggingOutInterceptor
to extract the payload, decode it, and log the original message.
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);
}
}
Replace in the XML file
<bean id="logInbound" class="test.CustomInterceptor" />
<bean id="logOutbound" class="test.CustomInterceptor" />
Finding an example of how to decode a FastInfoset has not been easy. Try this using DOM and FastInfoset-1.2.12.jar. In this repo you have several examples using sTAX and 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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With