Search code examples
web-servicescxfjax-ws

CXF In interceptor get reference to service object


I am extending AbstractPhaseInterceptor, and I want to get hold of the JAXWS web service object, in INVOKE or PRE_INVOKE phase. How do I do this?

To be clear, I need to get a reference to the object that implements the web service methods, so:

@WebService(...)
public class ExampleWebService
{
   @WebMethod(...)
   public void doSomething(...)
   {
   }
}

public class MyInterceptor
    extends AbstractPhaseInterceptor<Message>
{
    public MyInterceptor()
    {
        super(Phase.INVOKE);
    }

    @Override
    public void handleMessage(Message message)
            throws Fault
    {
        ExampleWebService serviceObject = getServiceObject(message);
    }

    private static ExampleWebService getServiceObject(Message messsage)
    {
        // how do I implement this?
    }
}

Solution

  • I do not test the code but something like that probably works.

    import org.apache.cxf.endpoint.Server;
    import org.apache.cxf.frontend.ServerFactoryBean;
    ...
    
    Server server = serverFactoryBean.create();
    MyInterceptor myInterceptor = new MyInterceptor(server.getEndpoint());
    server.getEndpoint().getInInterceptor().add(myInterceptor);