JAX-WS RI:实现方法拦截器的最佳方式 [英] JAX-WS RI: Best way to implement a Method Interceptor

查看:26
本文介绍了JAX-WS RI:实现方法拦截器的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的网络服务调用提供我自己的方法拦截器.基本上,这个方法拦截器应该在调用真正的方法之前调用...见下面的片段:

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

这个想法是每次调用 webservice 时,它​​都会通过我的拦截器调度.我已经考虑过连接我自己的 InstanceResolver,但它已经失控了.我知道如何在 CXF 和 JAX-RS (Jersey) + Guice 中执行此操作.

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 提供了 handler-chains,但是这些处理程序被调用太早(即,在方法调用之前),所以我没有需要此时的信息.

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.

使用 JAX-WS 的 Referene 实现实现此目的的最佳方法是什么?

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

推荐答案

在 jax-ws 处理程序中,您就在真正的事情之前,您可以访问整个 SOAP 消息的内容,而您需要的是不可用的内容还没有?

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?


在处理程序中使用的一些示例:


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

这篇关于JAX-WS RI:实现方法拦截器的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆