简体   繁体   English

Apache CXF - 在 In 和 Out 拦截器之间共享数据

[英]Apache CXF - share data between In and Out interceptors

I am using Apach CXF as REST provider.我使用 Apache CXF 作为 REST 提供者。

I want to gather data when I enter the webservice, gather data before I enter the resposne and add some calculation to the response.我想在进入webservice时收集数据,在进入resposne之前收集数据并在响应中添加一些计算。 For this question and for simplicity, lets assume I want to get the starting time on entering, the finishing time before the response is sent and add the total time to the response.对于这个问题,为了简单起见,假设我想获得输入的开始时间,发送响应之前的完成时间,并将总时间添加到响应中。

Now, how do I do that?现在,我该怎么做? I created In and Out interceptors that works fine alone, but how do I use the data from the In interceptor in the Out interceptor?我创建了单独工作正常的 In 和 Out 拦截器,但是如何在 Out 拦截器中使用来自 In 拦截器的数据?

Thanks Idob谢谢伊多布



UPDATE:更新:

I tried to set the data as contextual parameter with我尝试将数据设置为上下文参数

message.setContextualProperty(key,value);

but I am getteing NULL on但我得到了 NULL

message.getContextualProperty(key);

I also tried the same but just with我也尝试过同样的但只是与

message.put(key,value) and message.get(key)

didn't work.没有用。

Idea's anyone?想法有人吗?

Thank you, Idob谢谢你,伊多布

You can store values on the Exchange .您可以在Exchange上存储值。 CXF creates an Exchange object for each request to act as a container for the in and out messages for the request/response pair and makes it accessible as message.getExchange() from both. CXF 为每个请求创建一个Exchange对象,作为请求/响应对的message.getExchange()和传出消息的容器,并使其可作为message.getExchange()从两者访问。

In interceptor:在拦截器中:

public void handleMessage(Message inMessage) throws Fault {
  inMessage.getExchange().put("com.example.myKey", myCustomObject);
}

Out interceptor出拦截器

public void handleMessage(Message outMessage) throws Fault {
  MyCustomObject obj = (MyCustomObject)outMessage.getExchange().get("com.example.myKey");
}

(or vice-versa for client-side interceptors, where the out would store values and the in would retrieve them). (对于客户端拦截器,反之亦然,其中 out 将存储值,而 in 将检索它们)。 Choose a key that you know won't be used by other interceptors - a package-qualified name is a good choice.选择一个您知道不会被其他拦截器使用的密钥 - 包限定名称是一个不错的选择。 Note that, like Message , Exchange is a StringMap and has generic put/get methods taking a Class as the key that give you compile-time type safety and save you having to cast:请注意,与Message一样, Exchange是一个StringMap并且具有通用的 put/get 方法,将Class作为键,为您提供编译时类型安全并节省您必须强制转换:

theExchange.put(MyCustomObject.class, new MyCustomObject());
MyCustomObject myObj = theExchange.get(MyCustomObject.class);

Your interceptors have access to javax.xml.ws.handler.MessageContext .您的拦截器可以访问javax.xml.ws.handler.MessageContext This extends Map<String,Object> , so you can put whatever you want into the context and access it later on in the request:这扩展了Map<String,Object> ,因此您可以将任何您想要的内容放入上下文并稍后在请求中访问它:

public boolean handleMessage(final MessageContext context) {
    context.put("my-key", myCustomObject);
            // do whatever else your interceptor does
}

Later on:稍后的:

public boolean handleMessage(final MessageContext context) {
    MyCustomObject myCustomObject = context.get("my-key");
            // do whatever else your interceptor does
}

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

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