41.10. Configuring Endpoints to Use Handlers
41.10.1. Programmatic Configuration
41.10.1.1. Adding a Handler Chain to a Consumer
Overview
Binding object.
Procedure
- Create a
List<Handler>object to hold the handler chain. - Create an instance of each handler that will be added to the chain.
- Add each of the instantiated handler objects to the list in the order they are to be invoked by the runtime.
- Get the
Bindingobject from the service proxy.TipApache CXF provides an implementation of theBindinginterface calledorg.apache.cxf.jaxws.binding.DefaultBindingImpl. - Set the handler chain on the proxy using the
Bindingobject'ssetHandlerChain()method.
Example
Example 41.14. Adding a Handler Chain to a Consumer
import javax.xml.ws.BindingProvider; import javax.xml.ws.handler.Handler; import java.util.ArrayList; import java.util.List; import org.apache.cxf.jaxws.binding.DefaultBindingImpl; ... SmallNumberHandler sh = new SmallNumberHandler(); 1 List<Handler> handlerChain = new ArrayList<Handler>(); 2 handlerChain.add(sh); 3 DefaultBindingImpl binding = ((BindingProvider)proxy).getBinding(); 4 binding.getBinding().setHandlerChain(handlerChain); 5
41.10.1.2. Adding a Handler Chain to a Service Provider
Overview
@HandlerChain annotation. The annotation points to a meta-data file defining the handler chain used by the service provider.
Procedure
- Decorate the provider's implementation class with the
@HandlerChainannotation. - Create a handler configuration file that defines the handler chain.
The @HandlerChain annotation
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.
- a URL
- a relative path name
handlers.xml. handlers.xml must be located in the directory from which the service provider is run.
Example 41.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
{
...
}Handler configuration file
http://java.sun.com/xml/ns/javaee.
handler-chains element. The handler-chains element has one or more handler-chain elements.
handler-chain element define a handler chain. Table 41.1, “Elements Used to Define a Server-Side Handler Chain” describes the handler-chain element's children.
Table 41.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:
##SOAP11_HTTP, ##SOAP11_HTTP_MTOM, ##SOAP12_HTTP, ##SOAP12_HTTP_MTOM, or ##XML_HTTP.
For more information about message binding URIs see Appendix C, Apache CXF Binding IDs.
|
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.
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.
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 41.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>41.10.2. Spring Configuration
Overview
jaxwxs:handlers child to the element configuring the endpoint.
Procedure
- If the endpoint does not already have a configuration element, add one.For more information on configuring Apache CXF endpoints see Chapter 15, Configuring JAX-WS Endpoints.
- Add a
jaxws:handlerschild element to the endpoint's configuration element. - For each handler in the chain, add a
beanelement specifying the class that implements the handler.TipIf your handler implementation is used in more than one place you can reference abeanelement using therefelement.
The handlers element
jaxws:handlers element defines a handler chain in an endpoint's configuration. It can appear as a child to all of the JAX-WS endpoint configuration elements. These are:
jaxws:endpointconfigures a service provider.jaxws:serveralso configures a service provider.jaxws:clientconfigures a service consumer.
- add a
beanelement defining the implementation class - use a
refelement to refer to a namedbeanelement from elsewhere in the configuration file
Example
Example 41.17. Configuring an Endpoint to Use a Handler Chain In Spring
<beans ...
xmlns:jaxws="http://cxf.apache.org/jaxws"
...
schemaLocation="...
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
...">
<jaxws:endpoint id="HandlerExample"
implementor="org.apache.cxf.example.DemoImpl"
address="http://localhost:8080/demo">
<jaxws:handlers>
<bean class="demo.handlers.common.LoggingHandler" />
<bean class="demo.handlers.common.AddHeaderHandler" />
</jaxws:handlers>
</jaws:endpoint>
</beans>
Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.