Enterprise Java

Web-service learnings with Apache CXF

In my last couple of projects I worked with web-services, creating them at some places and consuming them at others. I felt that the standard tasks like creating a client, creating a web service etc are quite straight forward and there are enough resources if you get stuck. But with web services, it is some trivial task that can take a lot of your time and you feel quite frustrated when you can not get that simple thing right.

Logging

In one of the projects, we created web-services using Apache CXF and things were working fine. There was just one issue Apache CXF uses java.util logger for logging and used to print all sorts of logs. Our application was using Log4J, so task was simple make use of Log4J for Cxf and control logging.

Configuring the logger to use Log4J is a starightfoward task as mentioned on Apache CXF site, here is what the page says:

Add the file META-INF/cxf/org.apache.cxf.Logger to the classpath and make sure it contains the following content: org.apache.cxf.common.logging.Log4jLogger

Simple you need add a file to the META-INF directory and it will be done. We were having a maven project to generate war, so I created the file in the META-INF folder that gets generated for the war i.e. at src/main/webapp/META-INF.

Now, the file is there but the logging was still out of control, CXF was still using java logger. I spent some more time on it to figure out what I have done wrong. Some more effort was required to realize that I have missed and important instruction “in the classpath“. The META-INF folder that gets generated besides the WEB-INF is not in the classpath and a META-INF folder is required in the classes folder of the war.

After words looking back at this issue, it was silly for me to miss the classpath part, but also I could not understand why the CXF guys choose the META-INF folder for their configuration files. META-INF is there on Java platform for services and class loaders. It becomes quite ambiguous when frameworks start using it for their configurations and then there are multiple folders to play around, which can make things quite unclear.

Soap Handlers

At another place we had a requirement where we need to intercept the incoming / outgoing soap requests and then do something with it. Apache CXF provides interceptors that can be used to accomplish this task. You can have some inbound and out bound and they will do as asked. But we were using the jaxws implementation that gets shipped with java. Not intending to shift our existing clients to CXF , as we were having quite a few clients, it took us quite some time to figure it out how you can accomplish this with the jaxws.

Basically, according to the JAX-WS specification you can have some SoapHandlers that can be configured with the client. The handler will be called every time there is a SOAP message exchange. In the handler you can determine if this is an in-bound message or an out-bound and then do your logic. These are the steps required to configure a handler :

Create a class extending the SoapHandler interface and implement the handle message method:

class CustomHandler implements SOAPHandler<SOAPMessageContext>{
 // TODO: implement other methods
 public boolean handleMessage(SOAPMessageContext context) {
  // Check for message status
  Boolean outboundProperty = (Boolean)
   context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
  if (null == outboundProperty || !outboundProperty) {
      log.debug("not an outbound message");
      return true;
  }
 }
}

Handlers are like servlet filters. They are in a chain and gets called one by one. So we need to create a XML file in which we can configure all the handlers we want.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<javaee:handler-chains
     xmlns:javaee="http://java.sun.com/xml/ns/javaee"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <javaee:handler-chain>
    <javaee:handler>
      <javaee:handler-class>pkg.CustomHandler</javaee:handler-class>
    </javaee:handler>
  </javaee:handler-chain>
</javaee:handler-chains>

Also the configuration file needs to be in the classpath as this will be imported in the web service clients that we create. After this is done, we need to enable this chain of handlers on a web service client.

@WebServiceClient(.....)
@HandlerChain(file="handlerFile.xml")
public class SampleServiceImpl extends Service{
}

The handler concept is from the jaxws specification and would work on all of its implementations like metro, CXF etc.

Reference: Web-service learnings from our JCG partner Rahul Sharma at the “The road so far….” blog.

Related Articles :
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button