Ce contenu n'est pas disponible dans la langue sélectionnée.
56.3. Adding interceptors programmatically
56.3.1. Approaches to Adding Interceptors
- the
InterceptorProvider
API - Java annotations
InterceptorProvider
API allows the developer to attach interceptors to any of the runtime components that have interceptor chains, but it requires working with the underlying Apache CXF classes. The Java annotations can only be added to service interfaces or service implementations, but they allow developers to stay within the JAX-WS API or the JAX-RS API.
56.3.2. Using the interceptor provider API
Overview
InterceptorProvider
interface shown in Example 56.3, “The interceptor provider interface”.
Example 56.3. The interceptor provider interface
package org.apache.cxf.interceptor; import java.util.List; public interface InterceptorProvider { List<Interceptor<? extends Message>> getInInterceptors(); List<Interceptor<? extends Message>> getOutInterceptors(); List<Interceptor<? extends Message>> getInFaultInterceptors(); List<Interceptor<? extends Message>> getOutFaultInterceptors(); }
List
object. Using the methods offered by the Java List
object, developers can add and remove interceptors to any of the chains.
Procedure
InterceptorProvider
API to attach an interceptor to a runtime component's interceptor chain, you must:
- Get access to the runtime component with the chain to which the interceptor is being attached.Developers must use Apache CXF specific APIs to access the runtime components from standard Java application code. The runtime components are usually accessible by casting the JAX-WS or JAX-RS artifacts into the underlying Apache CXF objects.
- Create an instance of the interceptor.
- Use the proper get method to retrieve the desired interceptor chain.
- Use the
List
object'sadd()
method to attach the interceptor to the interceptor chain.TipThis step is usually combined with retrieving the interceptor chain.
Attaching an interceptor to a consumer
Example 56.4. Attaching an interceptor to a consumer programmatically
package com.fusesource.demo; import java.io.File; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import org.apache.cxf.endpoint.Client; public class Client { public static void main(String args[]) { QName serviceName = new QName("http://demo.eric.org", "stockQuoteReporter"); Service s = Service.create(serviceName); 1 QName portName = new QName("http://demo.eric.org", "stockQuoteReporterPort"); s.addPort(portName, "http://schemas.xmlsoap.org/soap/", "http://localhost:9000/EricStockQuote"); 2 quoteReporter proxy = s.getPort(portName, quoteReporter.class); 3 Client cxfClient = (Client) proxy; 4 ValidateInterceptor validInterceptor = new ValidateInterceptor(); 5 cxfClient.getInInterceptor().add(validInterceptor); 6 ... } }
- 1
- Creates a JAX-WS
Service
object for the consumer. - 2
- Adds a port to the
Service
object that provides the consumer's target address. - 3
- Creates the proxy used to invoke methods on the service provider.
- 4
- Casts the proxy to the
org.apache.cxf.endpoint.Client
type. - 5
- Creates an instance of the interceptor.
- 6
- Attaches the interceptor to the inbound interceptor chain.
Attaching an interceptor to a service provider
Example 56.5. Attaching an interceptor to a service provider programmatically
package com.fusesource.demo; import java.util.*; import org.apache.cxf.endpoint.Server; import org.apache.cxf.frontend.ServerFactoryBean; import org.apache.cxf.frontend.EndpointImpl; public class stockQuoteReporter implements quoteReporter { ... public stockQuoteReporter() { ServerFactoryBean sfb = new ServerFactoryBean(); 1 Server server = sfb.create(); 2 EndpointImpl endpt = server.getEndpoint(); 3 AuthTokenInterceptor authInterceptor = new AuthTokenInterceptor(); 4 endpt.getOutInterceptor().add(authInterceptor); 5 } }
- 1
- Creates a
ServerFactoryBean
object that will provide access to the underlying Apache CXF objects. - 2
- Gets the
Server
object that Apache CXF uses to represent the endpoint. - 3
- Gets the Apache CXF
EndpointImpl
object for the service provider. - 4
- Creates an instance of the interceptor.
- 5
- Attaches the interceptor to the endpoint;s outbound interceptor chain.
Attaching an interceptor to a bus
Example 56.6. Attaching an interceptor to a bus
WatchInterceptor
will be attached to the inbound interceptor chain of all endpoints created by the runtime instance.
56.3.3. Using Java annotations
Overview
Where to place the annotations
- the service endpoint interface(SEI) defining the endpointIf the annotations are placed on an SEI, all of the service providers that implement the interface and all of the consumers that use the SEI to create proxies will be affected.
- a service implementation classIf the annotations are placed on an implementation class, all of the service providers using the implementation class will be affected.
The annotations
Listing the interceptors
Example 56.7. Syntax for listing interceptors in a chain annotation
interceptors={"interceptor1", "interceptor2", ..., "interceptorN"}
Example
SayHiImpl
.
Example 56.8. Attaching interceptors to a service implementation
import org.apache.cxf.interceptor.InInterceptors;
@InInterceptors(interceptors={"com.sayhi.interceptors.FirstLast", "com.sayhi.interceptors.LogName"})
public class SayHiImpl implements SayHi
{
...
}