Hi, We currently register a number of interceptors against the endpoint when our Rest service starts up. I have a specific scenario whereby when I call a backend system I need to create a new “session” and ensure this session is deleted when the response is returned to the client. The session is only created on some GETs (the first GET goes to the backend and subsequent ones go to a cache) so I want to dynamically register an interceptor for a specific request/response combination.
I came across https://issues.apache.org/jira/browse/CXF-2015 <https://issues.apache.org/jira/browse/CXF-2015> which helped me build the following code to achieve what I wanted:- Interceptor interceptor = new GenericOutboundInterceptor(); Collection<Interceptor> interceptorAsList = new ArrayList<>(1); interceptorAsList.add(interceptor); // register the specified CallbackAction only on the out & fault interceptors for this request/response // to ensure no impact on other messages Message message = PhaseInterceptorChain.getCurrentMessage(); Collection<Interceptor> outInterceptors = (Collection<Interceptor>)message.get(Message.OUT_INTERCEPTORS); if (null!=outInterceptors) { outInterceptors.add(interceptor); } else { message.put(Message.OUT_INTERCEPTORS, interceptorAsList); } Collection<Interceptor> faultInterceptors = (Collection<Interceptor>)message.get(Message.FAULT_OUT_INTERCEPTORS); if (null!=faultInterceptors) { faultInterceptors.add(interceptor); } else { message.put(Message.FAULT_OUT_INTERCEPTORS, interceptorAsList); } } It all works but I just had a few questions: 1. Am I using the correct way to access the message i.e. via PhaseInterceptorChain.getCurrentMessage()? 2. I added the null checks in as the message seemed to have no existing interceptors which surprised me as I had assumed it would have referenced the ones registered on the endpoint but I guess they are different? 3. Do I need to register the new interceptor on the fault chain too? 4. Is there any need to manually remove the interceptor? Many thanks Mandy
