Red Hat DocumentationFuse ESBToggle FramesPrintFeedback

Using the interceptor provider API

Overview

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

Example 15. 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.

Procedure

To use the InterceptorProvider API to attach an interceptor to a runtime component's interceptor chain, you must:

  1. 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.

  2. Create an instance of the interceptor.

  3. Use the proper get method to retrieve the desired interceptor chain.

  4. Use the List object's add() method to attach the interceptor to the interceptor chain.

    Tip

    This step is usually combined with retrieving the interceptor chain.

Attaching an interceptor to a consumer

Example 16 shows code for attaching an interceptor to the inbound interceptor chain of a JAX-WS consumer.

Example 16. 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 16 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.

Attaching an interceptor to a service provider

Example 17 shows code for attaching an interceptor to a service provider's outbound interceptor chain.

Example 17. 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
  }
}

The code in Example 17 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.

Attaching an interceptor to a bus

Example 18 shows code for attaching an interceptor to a bus' inbound interceptor chain.

Example 18. 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 18 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.

Comments powered by Disqus