Chapter 7. Handler Framework

Table of Contents

7.1. Overview
7.2. Handler Chain Execution Order
7.3. Handler Class Organization
7.3.1. Declaring Handler Classes
7.4. Configuring a Handler Class
7.4.1. Creating a Web service from Java Class
7.4.2. Creating a Web Service from WSDL
7.4.3. Creating a Client
7.5. Example of a Web Service that Use a Handler Chain
7.6. Executing Handler Framework in Web Services

This chapter describes the basic concepts and components of the handler framework with examples.

JAX-WS Web services provide a plug-in style framework, which is more convenient for the handlers and also improves the runtime system functionality of JEUS 7 web services.

The following are two types of handlers.


Use the following criteria to select a handler.

  • Use the SOAP handler if the entire SOAP message is needed.

  • Use the logical handler if only the XML document payload of the SOAP message is needed.

In other cases, web services configure the endpoint class to process messages. In a special case where the Java object is required, the Interceptor class supported by JEUS EJB is used. For more information about the JEUS EJB Interceptor class, refer to "JEUS EJB Guide".

For an outbound message, all logical handlers in the handler chain are processed before SOAP handlers. For an inbound message, all SOAP handlers are processed before logical handlers.

Caution

In programming client or service-endpoint interfaces, even though logical handlers are configured before SOAP handlers, all logical handlers are processed before SOAP handlers in creating or calling the services.


This section describes how to organize a handler class and discusses about the MessageContext class.

To organize a handler class, a user creates a class that implements a logical handler or SOAP handler interface.

The following is an example of each handler class.



As shown in the previous example, both logical and SOAP handlers implement a handler interface, and the handler interface implements two methods, handlerMessage( ) and handleFault( ).

Each method receives an instance that inherits the MessageContext class as a parameter, and the instance is used to determine whether the message is inbound or outbound. The @PostConstruct and @PreDestroy annotations can be used in a user handler class as shown in the following example.


In the previous MyLogicalHandler class, methodA, declared with the @PostConstruct annotation, is invoked after the handler is created and methodB, declared with the @PreDestroy annotation, is invoked before the handler is destroyed.

This section describes how to attach a user handler to a web service.

This section shows a simple example of implementing a user SOAP handler, that is used to output the log, and using it for a web service.

The javax.xml.ws.handler.LogicalHandler class, a logical handler class, or the javax.xml.ws.handler.SOAPHandler class, a SOAP handler class, inherits the abstract interface, javax.xml.ws.handler.Handler. A user can create a handler, as needed, by implementing the two classes.

The aforementioned handler class, LoggingHandler, that will be implemented in the example is a class that implements the SOAP handler class, javax.xml.ws.handler.soap.SOAPHandler.


As shown in the previous example, the handler checks whether the message coming into the handler is coming into the server or going out to the client.

Service Class

The following is the service block from the example Java class. In the service block, the @HandlerChain annotation is used to set the handler class of the server.


The following example shows the handlers.xml file, which is registered as the @HandlerChain annotation.


Client Class

The following shows the client block of the example Java class. In the client block, binding user declaration is added when using the wsimport tool to create a client through the WSDL document.


The following example shows the binding user declaration.


This section describes how to execute the handler framework using the implemented classes and other configuration files from previous examples. Other service endpoint interface implementation classes and configuration files are the same as those from the previous examples.

Create a service by configuring the handler framework and deploy it to JEUS by executing the following command.

$ ant build deploy

Once the previous step is completed successfully, build and call the client. Since the client is processed by using the wsimport tool, it can only be built after the service has been deployed.

As shown in the following example, create a service with the configured handler framework and then call it. In the console, the LoggingHandler outputs the service-client message exchanges on both the service and client screens.

$ ant run

...

run:

     [java] ##############################################
     [java] ### JAX-WS Webservices examples - handler  ###
     [java] ##############################################

     [java] Client message:
     [java] <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body><ns2:addNumbers xmlns:ns2="http://server.fromjavahandler/"><arg0>10</arg0>
<arg1>20</arg1></ns2:addNumbers></S:Body></S:Envelope>
     [java] ##############################################
     [java] ### JAX-WS Webservices examples - handler  ###
     [java] ##############################################

     [java] Server message:
     [java] <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header/><S:Body><ns2:addNumbersResponse xmlns:ns2="http://server.fromjavahandler/">
<return>30</return></ns2:addNumbersResponse></S:Body></S:Envelope>

...

BUILD SUCCESSFUL