LibraryLink ToToggle FramesPrintFeedback

Using the InterceptorProvider API

Interceptors can be registered with any component that implements the InterceptorProvider interface shown in Example 5.3.


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:

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); 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 = ClientProxy.getClient(proxy); 4

    ValidateInterceptor validInterceptor = new ValidateInterceptor(); 5
    cxfClient.getInInterceptor().add(validInterceptor); 6

    ...
  } 
}

The code in Example 5.4 does the following:

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

Gets the FUSE Services Framework Client object associated with the proxy.

5

Creates an instance of the interceptor.

6

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.


The code in Example 5.5 does the following:

1

Creates a ServerFactoryBean object that will provide access to the underlying FUSE Services Framework objects.

2

Gets the Server object that FUSE Services Framework uses to represent the endpoint.

3

Gets the FUSE Services Framework EndpointImpl object for the service provider.

4

Creates an instance of the interceptor.

5

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.

Example 5.6. Attaching an Interceptor to a Bus

import org.apache.cxf.BusFactory;
org.apache.cxf.Bus;

...

Bus bus = BusFactory.getDefaultBus(); 1

WatchInterceptor watchInterceptor = new WatchInterceptor(); 2

bus..getInInterceptor().add(watchInterceptor); 3

...

The code in Example 5.6 does the following:

1

Gets the default bus for the runtime instance.

2

Creates an instance of the interceptor.

3

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.