Web-service learnings

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


About these ads

4 responses

  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.

    Jacek

    1. Rahul Sharma | Reply

      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 ?

      Rahul

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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: