使用JAX-WS:如何设置用户代理属性 [英] Using JAX-WS: How can I set the user agent property

查看:105
本文介绍了使用JAX-WS:如何设置用户代理属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此进行了搜索,发现了一些近乎未命中的情况。我创建了一个使用JAX-WS来使用Web服务的java客户端。使用JAX设置HTTP_USER_AGENT值有没有办法?我希望在特定客户(我的)访问它时拥有我的Web服务日志,所以我想要一个自定义的值。

I've searched on this and found a few near misses. I've created a java client to consume a web service using JAX-WS. Is there a way when using JAX to set the HTTP_USER_AGENT value? I would like to have my web service log when specific clients (mine) access it so I wanted a customized value.

我看过你在系统属性中设置它的选项,但这似乎不起作用。生成的JAX类似乎没有对连接对象的直接引用,所以我看不出如何操作这些类。

I've seen options where you set it in the system properties but this doesn't seem to work. The generated JAX classes don't seem to have a direct reference to the connection object so I don't see how I can manipulate those classes.

任何帮助都会很棒。
谢谢
ST

Any help would be great. Thanks ST

推荐答案

JAX-WS中解决这类问题的方法是实现一个SoapMessage Handler(接口:SOAPHandler< SOAPMessageContext>)。
在该处理程序中,您将HTTP标头插入到可能已经存在的标头中,然后将控制权交给处理程序链中的下一个处理程序。

The solution to this kind of problem in JAX-WS is to implement a SoapMessage Handler (Interface: SOAPHandler< SOAPMessageContext >). Within that handler you insert your HTTP header into maybe already existing headers, then you give control to the next handler in the handler chain.

这个概念处理程序链是很好的,你可以有一个非常特定的小类(安全,记录等)。

The concept of this handler chain is kind of nice, you can have small classes for a very specific purpose (Security, Logging etc.).

在你的客户端你配置处理程序链之前发送任何请求:

In your client you configure the handler chain prior to sending any request:

// HandlerChain installieren
Binding binding = ((BindingProvider) port).getBinding();
List hchain = binding.getHandlerChain();
if (hchain == null) {
  hchain = new ArrayList();
}
hchain.add(new HTTPUserAgentHandler());
binding.setHandlerChain(hchain);

这是HTTPUserAgentHandler的代码:

And here is the code for the HTTPUserAgentHandler:

public class HTTPUserAgentHandler implements SOAPHandler<SOAPMessageContext> {

  @Override
  public boolean handleMessage(SOAPMessageContext context) {
      boolean request = ((Boolean) context.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY)).booleanValue();

      if (request) {
          @SuppressWarnings("unchecked")
          Map<String, List<String>> headers = (Map<String, List<String>>) context
                  .get(MessageContext.HTTP_REQUEST_HEADERS);
          if (null == headers) {
              headers = new HashMap<String, List<String>>();
          }
          headers.put("HTTP_USER_AGENT", Collections.singletonList("user_agent"));
          context.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
      }
      return true;
  }

  @Override
  public boolean handleFault(SOAPMessageContext context) {
      return true;
  }

  @Override
  public void close(MessageContext context) {}

  @Override
  public Set<QName> getHeaders() {
      return null;
  }

}

这篇关于使用JAX-WS:如何设置用户代理属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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