Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging request/response with Apache CXF as XML

Is it possible to log the request/response as XML using CXF, ideally to a separate file so I can monitor what an application is doing?

like image 271
irishguy Avatar asked Jan 04 '11 10:01

irishguy


People also ask

Where do I put CXF XML?

For both web service clients and servers, the default location that CXF will look for a configuration for is "/cxf. xml" on the class path. For example, when running your application in a servlet container, this file is expected to be located in a /WEB-INF/classes folder of your web application.

What is Apache CXF used for?

Apache CXF™ is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI.

What is CXF endpoint?

In Apache Camel, the Camel CXF component is the key to integrating routes with Web services. You can use the Camel CXF component to create a CXF endpoint, which can be used in either of the following ways: Consumer — (at the start of a route) represents a Web service instance, which integrates with the route.


3 Answers

So, I tried a little more with this. To get the XML Request and Replies logged, and if you are using Log4J, you need to set the Log-level of CXF in the log4j.xml file like this (>= INFO):

<logger name="org.apache.cxf" >
    <level value="INFO" />
</logger>

And the cxf.xml file should contains this:

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

Both files should be in the CLASSPATH.

To display the soap message add this to your code:

Client client = ClientProxy.getClient(service);
client.getInInterceptors().add(new LoggingInInterceptor());
client.getOutInterceptors().add(new LoggingOutInterceptor());
like image 63
Asturio Avatar answered Oct 17 '22 00:10

Asturio


The request soap xml can be logged easily by an custom In interceptor. Say, we have an interceptor named "wsLoggingInInterceptor", So in the context file it will be like the following :

<bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
<bean id="logOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
<bean id="wsLoggingInInterceptor" class="org.jinouts.webservice.logging.WSLoggingInInterceptor"/>


   <cxf:bus>
        <cxf:inInterceptors>
            <ref bean="loggingInInterceptor"/>
            <ref bean="wsLoggingInInterceptor"/>
        </cxf:inInterceptors>
        <cxf:outInterceptors>
            <ref bean="logOutInterceptor"/>            
       </cxf:outInterceptors>
    </cxf:bus>

In the class we can get the request xml as follows:

public class WSLoggingInInterceptor extends AbstractSoapInterceptor
{

    public WSLoggingInInterceptor ()
    {
        super(Phase.RECEIVE);
    }
    
    @Override
    public void handleMessage ( SoapMessage message ) throws Fault
    {
        //get the remote address
        HttpServletRequest httpRequest = (HttpServletRequest) message.get ( AbstractHTTPDestination.HTTP_REQUEST );
        System.out.println ("Request From the address : " + httpRequest.getRemoteAddr ( ) );
        
        try
        {
            // now get the request xml
            InputStream is = message.getContent ( InputStream.class );
            CachedOutputStream os = new CachedOutputStream ( );
            IOUtils.copy ( is, os );
            os.flush ( );
            message.setContent (  InputStream.class, os.getInputStream ( ) );
            is.close ( );
            
            System.out.println ("The request is: " + IOUtils.toString ( os.getInputStream ( ) ));
            os.close ( );
        }
        
        catch ( Exception ex )
        {
            ex.printStackTrace ( );
        }
        
    }

}

Look, here I have also log the address from where the request is coming. You can also get some more information from the "HttpServletRequest" object. you can have more from : http://cxf.apache.org/docs/interceptors.html

To log response xml you can have a look at this thread

like image 23
Asraful Haque Avatar answered Oct 16 '22 22:10

Asraful Haque


Add the following to your endpoints and clients:

<jaxws:features>
    <bean class="org.apache.cxf.feature.LoggingFeature" />
</jaxws:features>

This will log everything to the server log.

If you want to log them elsewhere, then look at the source code of the built-in CXF LoggingInInterceptor and LoggingOutInterceptor. You can follow the pattern they use to grab the messages on their way in/out and do with them what you like.

Add your own interceptors to the chain with something like this:

<jaxws:inInterceptors>
    <ref bean="myLoggingInInterceptor" />
</jaxws:inInterceptors>
like image 18
BPS Avatar answered Oct 16 '22 22:10

BPS