יום שישי, 24 במאי 2013

CXF server calls interceptor

In order to apply an interceptor to CXF web service calls an interceptor class that extend the AbstractSoapInterceptor abstract class may be used:

public
class WebServiceCallsInterceptor extends  AbstractSoapInterceptor{

    public WebServiceCallsInterceptor()
    {
        super(Phase.PRE_INVOKE);
        System.out.print("WebServiceCallsInterceptor ctor was called");
    }
   
    public void handleMessage(SoapMessage t) throws Fault {
    
        String theSoupMessage = t.toString();
        System.out.print(theSoupMessage);
    }
}

Note that in the constructor the location of the interceptor in the interceptors calls chain is specified. if the call is done before invoking the method or after and the relevant location in the chain of other interceptors.(using the AddAfter method for an example ).

The SoapMessage object expose a dictionary containing the message meda data for example the request uri can be retreive by calling the request uri:

public
void handleMessage(SoapMessage t) throws Fault {
        String theRequeseURI = t.get("org.apache.cxf.request.uri").toString();

        System.out.print(theRequeseURI);
    }

The return value is "/cxfServer/HelloWorld"
In order to register the interceptor the interceptor class should be added to the service attributes :
@org.apache.cxf.interceptor.InInterceptors (interceptors = {"com.mycompany.cxfserver.WebServiceCallsInterceptor" })
@WebService(endpointInterface = "com.mycompany.cxfserver.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

public String sayHi(String text) {
return "Hello " + text;
}


}

אין תגובות:

הוסף רשומת תגובה