Adding Interceptors Programmatically

Interceptors can be attached to endpoints programmatically using either one of two approaches:

  • the InterceptorProvider API

  • Java annotations

Using the InterceptorProvider API allows the developer to attach interceptors to any of the FUSE Services Framework runtime components that have interceptor chains, but it requires working with the underlying FUSE Services Framework 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.

Example 5.4, “Attaching an Interceptor to a Consumer Programmatically” 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, “Attaching an Interceptor to a Consumer Programmatically” 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, “Attaching an Interceptor to a Service Provider Programmatically” shows code for attaching an interceptor to a service provider's outbound interceptor chain.


The code in Example 5.5, “Attaching an Interceptor to a Service Provider Programmatically” 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, “Attaching an Interceptor to a Bus” 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, “Attaching an Interceptor to a Bus” 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.