Can I have XML in the log when sending a FastInfoset request with Apache CXF?

I need to have requests and responses in the application logs, but requests sent by Apache CXF are in FastInfoset (Content-Type: application / fastinfoset), which makes the request and response log unreadable (since it is binary). Is there a way around this so that I save FastInfoset messages (for performance reasons), but I get the correct 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> 

Thanks in advance for any help.

+5
source share
1 answer

I took a look at LoggingInInterceptor.logInputStream and it seems that it does not support fastinfoset. But you can use the interceptor instead of the LoggingInInterceptor and LoggingOutInterceptor to extract the payload, decode it, and register 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 XML file

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

Finding an example of how to decode FastInfoset is not easy. Try this with the DOM and FastInfoset-1.2.12.jar. In this repo you have some 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); } 
+3
source

Source: https://habr.com/ru/post/1274901/


All Articles