Interceptors can be registered with any component that implements the
InterceptorProvider
interface shown in Example 5.3.
Example 5.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(); }
The four methods in the interface allow you to retrieve each of an endpoint's interceptor chains as a Java
List
object. Using the methods offered by the Java List
object, developers can
add and remove interceptors to any of the chains.
To use the 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 Fuse Services Framework 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 Fuse Services Framework 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.Tip This step is usually combined with retrieving the interceptor chain.
Example 5.4 shows code for attaching an interceptor to the inbound interceptor chain of a JAX-WS consumer.
Example 5.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.ClientProxy; import org.apache.cxf.endpoint.ClientProxy; public class Client { public static void main(String args[]) { QName serviceName = new QName("http://demo.eric.org", "stockQuoteReporter"); Service s = Service.create(serviceName);QName portName = new QName("http://demo.eric.org", "stockQuoteReporterPort"); s.addPort(portName, "http://schemas.xmlsoap.org/soap/", "http://localhost:9000/EricStockQuote");
quoteReporter proxy = s.getPort(portName, quoteReporter.class);
Client cxfClient = ClientProxy.getClient(proxy);
ValidateInterceptor validInterceptor = new ValidateInterceptor();
cxfClient.getInInterceptor().add(validInterceptor);
... } }
The code in Example 5.4 does the following:
Creates a JAX-WS | |
Adds a port to the | |
Creates the proxy used to invoke methods on the service provider. | |
Gets the Fuse Services Framework | |
Creates an instance of the interceptor. | |
Attaches the interceptor to the inbound interceptor chain. |
Example 5.5 shows code for attaching an interceptor to a service provider's outbound interceptor chain.
Example 5.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();Server server = sfb.create();
EndpointImpl endpt = server.getEndpoint();
AuthTokenInterceptor authInterceptor = new AuthTokenInterceptor();
endpt.getOutInterceptor().add(authInterceptor);
} }
The code in Example 5.5 does the following:
Creates a | |
Gets the | |
Gets the Fuse Services Framework | |
Creates an instance of the interceptor. | |
Attaches the interceptor to the endpoint;s outbound interceptor chain. |
Example 5.6 shows code for attaching an interceptor to a bus' inbound interceptor chain.
The code in Example 5.6 does the following:
Gets the default bus for the runtime instance. | |
Creates an instance of the interceptor. | |
Attaches the interceptor to the inbound interceptor chain. |
The WatchInterceptor
will be attached to the inbound interceptor chain of all endpoints created by
the runtime instance.