开发者

JAX-WS RI: Best way to implement a Method Interceptor

I want to provide my own method interceptor for my webservice invocations. Basically, this method interceptor should be called right before the real method is called... See the snippet below:

public class MyMethodInterceptor {
  public Object invoke(Object t, Method m, Object[] args) throws Throwable {
    // do some magic, such as tracing, authorise, etc...
    return m.invoke(t, args);
  }     
}

// ....    

public class MyWebServiceImpl implements MyWebServiceInterface {
  public String greet(final String name) {
    return "Hi there, " + name;
  }
}

The idea is that everytime that the webservice gets invoked, it will be dispatched through my interceptor. I've looked at hooking up my own InstanceResolver, but it is getting out of control. I know how to do this in CXF and with JAX-RS (Jersey) + Guice.

JAX-WS provides handler-chains, but these handlers get invoked way too early (i.e., much before t开发者_StackOverflowhe method invocation), so I do not have the needed information at this point.

What is the best way to do this with the Referene Implementation of JAX-WS?


In a jax-ws handler you are just before the real thing, you have access to the content of entire SOAP message, what you need that isn't available yet?

EDIT:
Some examples, to use in the handler:

public String getMessage(SOAPMessageContext smc) {
    SOAPMessage message = smc.getMessage();
    ByteArrayOutputStream soapEnvelope = new ByteArrayOutputStream();
    message.writeTo(soapEnvelope);
    soapEnvelope.close();
    return new String(soapEnvelope.toByteArray());
}

public String getMethod(SOAPMessageContext smc) {
    SOAPMessage message = smc.getMessage();
    SOAPBody body = message.getSOAPBody();
    return body.getFirstChild().getLocalName();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜