O'Reilly logo

Java SOA Cookbook by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing Server-Side Handler Chains

Problem

You need to specify the handlers you want your service to invoke against incoming and outgoing messages, and you want your chain to be defined externally in a configuration file rather than have it embedded in the code.

Solution

Write an XML file that defines the chain in accordance with the Java EE rules, and then add a javax.jws.HandlerChain annotation to your web service to specify the name of the handler chain file. This file will be an XML file that you put in the WEB-INF/classes directory or elsewhere on the application’s classpath.

Let’s look at how to do this.

If you are starting from Java, create a WAR project in whatever way you like and add a web service POJO to it. Then add the @HandlerChain annotation to the class, and use the file attribute to indicate the name of the XML file that lists your handlers. This is shown in Example 7-13.

Example 7-13. HandlerService.java

package com.soacookbook;

import javax.jws.HandlerChain;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * Uses a handler chain on the server.
 */
@WebService
@HandlerChain(file="myHandlers.xml")
public class HandlerWebService {
    
    @WebMethod(operationName = "doWork")
    public String doWork(
        @WebParam(name = "msg") String msg) {
        
        System.out.println("doing work");
        return "Processed work for: " + msg;
    }
}

Other than the new annotation, there’s nothing noteworthy about this web service. You then need to write the actual handler implementation ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required