简体   繁体   English

JAX-WS RI:实现方法拦截器的最佳方式

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

I want to provide my own method interceptor for my webservice invocations.我想为我的 web 服务调用提供我自己的方法拦截器。 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.这个想法是每次调用 web 服务时,都会通过我的拦截器进行调度。 I've looked at hooking up my own InstanceResolver , but it is getting out of control.我看过连接我自己的InstanceResolver ,但它已经失控了。 I know how to do this in CXF and with JAX-RS (Jersey) + Guice.我知道如何在 CXF 和 JAX-RS (Jersey) + Guice 中做到这一点。

JAX-WS provides handler-chains , but these handlers get invoked way too early (ie, much before the method invocation), so I do not have the needed information at this point. JAX-WS 提供了handler-chains ,但是这些处理程序被调用得太早了(即,在方法调用之前很久),所以我现在没有所需的信息。

What is the best way to do this with the Referene Implementation of JAX-WS?使用 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?在 jax-ws 处理程序中,您就在真正的事物之前,您可以访问整个 SOAP 消息的内容,您需要什么还不可用?

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM