LibraryLink ToToggle FramesPrintFeedback

Chapter 6. Manipulating Interceptor Chains on the Fly

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.

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.

[Tip]Tip

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.

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 6.1. The interceptor chain is returned as a org.apache.cxf.interceptor.InterceptorChain object.


The InterceptorChain object has two methods, shown in Example 6.2, 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 6.3 shows code for adding a single interceptor to a message's interceptor chain.


The code in Example 6.3 does the following:

1

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

[Important]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.

2

Gets the interceptor chain for the current message.

3

Adds the new interceptor to the chain.

The InterceptorChain object has one method, shown in Example 6.4, for removing an interceptor from an interceptor chain.


Example 6.5 shows code for removing an interceptor from a message's interceptor chain.


The code in Example 6.5 does the following:

1

Instantiates a copy of the interceptor to be removed from the chain.

[Important]Important

The interceptor being removed from 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

Removes the interceptor from the chain.