Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine target service/method from CXF Interceptor

Tags:

java

cxf

I'd like to write an interceptor for the Apache CXF JAX-RS implementation that inspects the target service/method for a particular annotation and does some special processing for that annotation.

I can't seem to find anything in the interceptor documentation that describes how to do this. Does anyone have any ideas?

Thanks!

like image 935
Nick Spacek Avatar asked Nov 18 '10 20:11

Nick Spacek


2 Answers

If the interceptor runs fairly late in the chain (like the USER_LOGICAL phase), you should be able to do something like:


Exchange exchange = msg.getExchange();
BindingOperationInfo bop = exchange.get(BindingOperationInfo.class);
MethodDispatcher md = (MethodDispatcher) 
                exchange.get(Service.class).get(MethodDispatcher.class.getName());
Method meth = md.getMethod(bop);

That should give you the Method that was bound in so you can get the declared class or the annotations, etc...

like image 142
Daniel Kulp Avatar answered Sep 21 '22 20:09

Daniel Kulp


Ah. I didn't specify that I was using the JAX-RS part of CXF; not sure if that impacts Daniel Kulp's answer but his solution didn't actually work for me. I believe it is because CXF does things differently when handling JAX-RS.

I came across the source for CXF's [JAXRSInInterceptor][1] and I saw in that code that this interceptor is putting the method info into the Exchange object like so:

message.getExchange().put(OperationResourceInfo.class, ori);

...during the UNMARSHAL phase, which according to the CXF interceptor docs happens before the *_LOGICAL phase. So by writing an Interceptor that handles the USER_LOGICAL phase I can do:

message.getExchange().get(OperationResourceInfo.class)

...to get access in there to the Method and Class<?> of the Service handling the call!

like image 26
Nick Spacek Avatar answered Sep 21 '22 20:09

Nick Spacek