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 :
Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


4 + = twelve



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books