You add a handler chain to a service provider by decorating either the SEI or the implementation class with
the @HandlerChain
annotation. The annotation points to a meta-data file defining the
handler chain used by the service provider.
To add handler chain to a service provider you do the following:
Decorate the provider's implementation class with the @HandlerChain
annotation.
Create a handler configuration file that defines the handler chain.
The javax.jws.HandlerChain
annotation decorates service provider's implementation
class. It instructs the runtime to load the handler chain configuration file specified by its file
property.
The annotation's file property supports two methods for identifying the handler configuration file to load:
a URL
a relative path name
Example 21.15 shows a service provider implementation that will
use the handler chain defined in a file called handlers.xml
. handlers.xml
must be located in the directory from which the service provider is run.
Example 21.15. Service Implementation that Loads a Handler Chain
import javax.jws.HandlerChain;
import javax.jws.WebService;
...
@WebService(name = "AddNumbers",
targetNamespace = "http://apache.org/handlers",
portName = "AddNumbersPort",
endpointInterface = "org.apache.handlers.AddNumbers",
serviceName = "AddNumbersService")
@HandlerChain(file = "handlers.xml")
public class AddNumbersImpl implements AddNumbers
{
...
}
The handler configuration file defines a handler chain using the XML grammar that accompanies JSR 109(Web Services for Java
EE, Version 1.2). This grammar is defined in the http://java.sun.com/xml/ns/javaee
.
The root element of the handler configuration file is the handler-chains
element. The
handler-chains
element has one or more handler-chain
elements.
The handler-chain
element define a handler chain. Table 21.1
describes the handler-chain
element's children.
Table 21.1. Elements Used to Define a Server-Side Handler Chain
Element | Description |
---|---|
handler
| Contains the elements that describe a handler. |
service-name-pattern
| Specifies the QName of the WSDL service element defining the service to
which the handler chain is bound. You can use * as a wildcard when defining the QName. |
port-name-pattern
| Specifies the QName of the WSDL port element defining the endpoint to
which the handler chain is bound. You can use * as a wildcard when defining the QName. |
protocol-binding
|
Specifies the message binding for which the handler chain is used. The binding is specified as a URI or using one
of the following aliases: For more information about message binding URIs see
Appendix A in |
The handler-chain
element is only required to have a single handler
element as a child. It can, however, support as many handler
elements as needed to define
the complete handler chain. The handlers in the chain are executed in the order they specified in the handler chain definition.
![]() | Important |
---|---|
The final order of execution will be determined by sorting the specified handlers into logical handlers and protocol handlers. Within the groupings, the order specified in the configuration will be used. |
The other children, such as protocol-binding
, are used to limit the scope of the defined handler chain.
For example, if you use the service-name-pattern
element, the handler chain will only be attached to
service providers whose WSDL port
element is a child of the specified WSDL
service
element. You can only use one of these limiting children in a
handler
element.
The handler
element defines an individual handler in a handler chain. Its
handler-class
child element specifies the fully qualified name of the class implementing the
handler. The handler
element can also have an optional
handler-name
element that specifies a unique name for the handler.
Example 21.16 shows a handler configuration file that defines a single handler chain. The chain is made up of two handlers.
Example 21.16. Handler Configuration File
<handler-chains xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee"> <handler-chain> <handler> <handler-name>LoggingHandler</handler-name> <handler-class>demo.handlers.common.LoggingHandler</handler-class> </handler> <handler> <handler-name>AddHeaderHandler</handler-name> <handler-class>demo.handlers.common.AddHeaderHandler</handler-class> </handler> </handler-chain> </handler-chains>