The road so far….

April 23, 2011

Web-service learnings

Filed under: java — Tags: — Rahul Sharma @ 11:23 pm

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.


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 jaxws 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"?>

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.

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.



  1. “I could not understand why the CXF guys choose the META-INF folder for their configuration files.” – the explanation’s quite simple when the Java Servlet specification’s conculted – the classpath root begins at /WEB-INF/classes so searching for META-INF/cxf/org.apache.cxf.Logger requires to place it accordingly. Simple, isn’t it? Ah, it does not have much to do with CXF.


    Comment by Jacek Laskowski — May 2, 2011 @ 10:31 pm

    • What I mean here was why they choose a file name org.apache.cxf.Logger in META-INF/cxf folder ? Why they could not settle for org.apache.cxf.Logger file in the classpath rather /META-INF/CXF/org.apache.cxf.Logger in the classpath ? Is there some benefit of using /META-INF/cxf folder ?


      Comment by Rahul Sharma — May 3, 2011 @ 9:55 pm

      • That’s the question I don’t know an answer to either. I also noticed Maven places its files in META-INF/maven.

        Comment by Jacek Laskowski — May 4, 2011 @ 12:37 am

  2. […] Web-service learnings – […]

    Pingback by Bookmarks for May 15th – [ ] — May 15, 2012 @ 3:34 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: