Rechercher

Ce contenu n'est pas disponible dans la langue sélectionnée.

Chapter 60. Manipulating Interceptor Chains on the Fly

download PDF

Abstract

Interceptors can reconfigure an endpoint’s interceptor chain as part of its message processing logic. It can add new interceptors, remove interceptors, reorder interceptors, and even suspend the interceptor chain. Any on-the-fly manipulation is invocation-specific, so the original chain is used each time an endpoint is involved in a message exchange.

Overview

Interceptor chains only live as long as the message exchange that sparked their creation. Each message contains a reference to the interceptor chain responsible for processing it. Developers can use this reference to alter the message’s interceptor chain. Because the chain is per-exchange, any changes made to a message’s interceptor chain will not effect other message exchanges.

Chain life-cycle

Interceptor chains and the interceptors in the chain are instantiated on a per-invocation basis. When an endpoint is invoked to participate in a message exchange, the required interceptor chains are instantiated along with instances of its interceptors. When the message exchange that caused the creation of the interceptor chain is completed, the chain and its interceptor instances are destroyed.

This means that any changes you make to the interceptor chain or to the fields of an interceptor do not persist across message exchanges. So, if an interceptor places another interceptor in the active chain only the active chain is effected. Any future message exchanges will be created from a pristine state as determined by the endpoint’s configuration. It also means that a developer cannot set flags in an interceptor that will alter future message processing.

If an interceptor needs to pass information along to future instances, it can set a property in the message context. The context does persist across message exchanges.

Getting the interceptor chain

The first step in changing a message’s interceptor chain is getting the interceptor chain. This is done using the Message.getInterceptorChain() method shown in Example 60.1, “Method for getting an interceptor chain”. The interceptor chain is returned as a org.apache.cxf.interceptor.InterceptorChain object.

Example 60.1. Method for getting an interceptor chain

InterceptorChaingetInterceptorChain

Adding interceptors

The InterceptorChain object has two methods, shown in Example 60.2, “Methods for adding interceptors to an interceptor chain”, for adding interceptors to an interceptor chain. One allows you to add a single interceptor and the other allows you to add multiple interceptors.

Example 60.2. Methods for adding interceptors to an interceptor chain

addInterceptor<? extends Message>iaddCollection<Interceptor<? extends Message>>i

Example 60.3, “Adding an interceptor to an interceptor chain on-the-fly” shows code for adding a single interceptor to a message’s interceptor chain.

Example 60.3. Adding an interceptor to an interceptor chain on-the-fly

void handleMessage(Message message)
{
  ...
  AddledIntereptor addled = new AddledIntereptor();
  InterceptorChain chain = message.getInterceptorChain();
  chain.add(addled);
  ...
}

The code in Example 60.3, “Adding an interceptor to an interceptor chain on-the-fly” does the following:

Instantiates a copy of the interceptor to be added to the chain.

Important

The interceptor being added to the chain should be in either the same phase as the current interceptor or a latter phase than the current interceptor.

Gets the interceptor chain for the current message.

Adds the new interceptor to the chain.

Removing interceptors

The InterceptorChain object has one method, shown in Example 60.4, “Methods for removing interceptors from an interceptor chain”, for removing an interceptor from an interceptor chain.

Example 60.4. Methods for removing interceptors from an interceptor chain

removeInterceptor<? extends Message>i

Example 60.5, “Removing an interceptor from an interceptor chain on-the-fly” shows code for removing an interceptor from a message’s interceptor chain.

Example 60.5. Removing an interceptor from an interceptor chain on-the-fly

void handleMessage(Message message)
{
  ...
  Iterator<Interceptor<? extends Message>> iterator =
            message.getInterceptorChain().iterator();
  Interceptor<?> removeInterceptor =  null;
  for (; iterator.hasNext(); ) {
    Interceptor<?> interceptor = iterator.next();
    if (interceptor.getClass().getName().equals("InterceptorClassName")) {
      removeInterceptor = interceptor;
      break;
    }
  }

  if (removeInterceptor !=  null) {
    log.debug("Removing interceptor {}",removeInterceptor.getClass().getName());
    message.getInterceptorChain().remove(removeInterceptor);
  }
  ...
}

Where InterceptorClassName is the class name of the interceptor you want to remove from the chain.

Red Hat logoGithubRedditYoutubeTwitter

Apprendre

Essayez, achetez et vendez

Communautés

À propos de la documentation Red Hat

Nous aidons les utilisateurs de Red Hat à innover et à atteindre leurs objectifs grâce à nos produits et services avec un contenu auquel ils peuvent faire confiance.

Rendre l’open source plus inclusif

Red Hat s'engage à remplacer le langage problématique dans notre code, notre documentation et nos propriétés Web. Pour plus de détails, consultez leBlog Red Hat.

À propos de Red Hat

Nous proposons des solutions renforcées qui facilitent le travail des entreprises sur plusieurs plates-formes et environnements, du centre de données central à la périphérie du réseau.

© 2024 Red Hat, Inc.