Chapter 59. Manipulating Interceptor Chains on the Fly
Abstract
Overview
Chain life-cycle
Getting the interceptor chain
Message.getInterceptorChain()
method shown in Example 59.1, “Method for getting an interceptor chain”. The interceptor chain is returned as a org.apache.cxf.interceptor.InterceptorChain
object.
Example 59.1. Method for getting an interceptor chain
InterceptorChain getInterceptorChain();
Adding interceptors
InterceptorChain
object has two methods, shown in Example 59.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 59.2. Methods for adding interceptors to an interceptor chain
void add(Interceptor<? extends Message> i);
void add(Collection<Interceptor<? extends Message>> i);
Example 59.3. Adding an interceptor to an interceptor chain on-the-fly
- 1
- Instantiates a copy of the interceptor to be added to the chain.ImportantThe 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.
- 2
- Gets the interceptor chain for the current message.
- 3
- Adds the new interceptor to the chain.
Removing interceptors
InterceptorChain
object has one method, shown in Example 59.4, “Methods for removing interceptors from an interceptor chain”, for removing an interceptor from an interceptor chain.
Example 59.4. Methods for removing interceptors from an interceptor chain
void remove(Interceptor<? extends Message> i);
Example 59.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); } ... }
InterceptorClassName
is the class name of the interceptor you want to remove from the chain.