2

I am having trouble logging an outgoing SOAP message from the server. The handleMessage method does not overwrite the message content as expected. How would I store the outgoing SOAP to the message?

public class OutgoingSoapInterceptor extends AbstractPhaseInterceptor<Message> {
    private static final Logger logger = LoggerFactory.getLogger(OutgoingSoapInterceptor.class.getName());

    public OutgoingSoapInterceptor ()
    {
        super(Phase.PRE_STREAM);
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        logger.debug("outbound soap handleMessage");

        OutputStream os = message.getContent ( OutputStream.class );
        CacheAndWriteOutputStream cwos = new CacheAndWriteOutputStream ( os);
        message.setContent ( OutputStream.class, cwos );

        cwos.registerCallback ( new LoggingOutCallBack ( ) );
    }
}
ABC123
  • 1,037
  • 2
  • 20
  • 44

1 Answers1

1

There is a simpler way to log the SOAP messages using CXF LoggingInInterceptor and LoggingOutInterceptor

  LogUtils.setLoggerClass(org.apache.cxf.common.logging.Log4jLogger.class);

  yourService = new YourService(wsdlURL, SERVICE_NAME);
  port = yourService.getServicePort(); 

  Client client = ClientProxy.getClient(port);
  client.getInInterceptors().add(new LoggingInInterceptor());
  client.getOutInterceptors().add(new LoggingOutInterceptor());

Or configuring interceptors in <cxf:bus> with spring

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

    <import resource="classpath:META-INF/cxf/cxf.xml" />

    <cxf:bus>
        <cxf:features>
            <cxf:logging />
        </cxf:features>
    </cxf:bus>  

    <jaxws:endpoint ... />        
</beans>

See more examples in How to log Apache CXF Soap Request and Soap Response using Log4j

Community
  • 1
  • 1
pedrofb
  • 37,271
  • 5
  • 94
  • 142
  • This put me on the right path. In the process I ran across this system property: https://docs.jboss.org/author/display/WFLY8/Advanced+User+Guide#AdvancedUserGuide-Systemproperty – ABC123 Jun 15 '16 at 22:06
  • I did not know the property. I do not know if it is standard of cxf or own jboss – pedrofb Jun 16 '16 at 05:32
  • The log output gets truncated. How can we log the complete xml in/out message regardless of message size ? – Abdul Fatah Jun 19 '20 at 13:22
  • Some of the API is now deprecated. Should be `org.apache.cxf.common.logging.Slf4jLogger.class`now and `org.apache.cxf.ext.logging.LoggingOutInterceptor` from `org.apache.cxf:cxf-rt-features-logging`. – Volker Seibt Feb 28 '23 at 08:26