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

2019-08-13 05:31发布

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 the 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?

1条回答
姐就是有狂的资本
2楼-- · 2019-08-13 06:12

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();
}
查看更多
登录 后发表回答