处理程序将HTTP标头添加到使用Axis Client API时未调用的HTTP请求 [英] Handler to add HTTP headers to HTTP request not invoked when using Axis Client API

查看:625
本文介绍了处理程序将HTTP标头添加到使用Axis Client API时未调用的HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Axis API访问Axis HTTP服务器。
可以在此处找到API的文档。

I am using the Axis API to access Axis HTTP server. The documentation of the API can be found here.

我使用以下代码将处理程序添加到服务器。 service 的类型为java.xml.rpc.Service

I am using the following code to add handlers to the server. service is of type java.xml.rpc.Service

    HandlerRegistry registry = service.getHandlerRegistry();
    QName serviceName = new QName(url, "MyServiceClass");

    List<HandlerInfo> handlerChain = new ArrayList<HandlerInfo>();
    HandlerInfo handlerInfo = new HandlerInfo(MyHandler.class, null, null);
    handlerChain.add(handlerInfo);
    registry.setHandlerChain(serviceName, handlerChain);

我知道服务名称是正确的,因为我在后续服务调用中获得了正确的输出对象。

I know that the service name is correct as I am getting the correct output in subsequent calls to the service object.

不知何故没有调用处理程序。这是Handler类。
我的目的是在将请求转发到服务器之前向HTTP请求添加自定义标头

Somehow the handler is not being invoked. Here is the Handler class. My intention is to add custom headers to the HTTP request before forwarding the request to the server.

import javax.xml.namespace.QName;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;

public class MyHandler extends BasicHandler {

    @Override
    public void init() {
        System.out.println("init called");
        super.init();
        System.out.println("init called");
    }

    @Override
    public void cleanup() {
        super.cleanup();
        System.out.println("cleanup called");
    }

    @Override
    public void invoke(MessageContext mc) throws AxisFault {
        System.out.println("invoke called");
    }

    public QName[] getHeaders() {
        System.out.println("getHeaders");
        return new QName[1];
    }
}

以上代码有什么问题?

有没有其他方法可以使用Apache Axis API修改HTTP标头?

Is there any other way to modify HTTP Headers using Apache Axis API?

推荐答案

Okie。这应该可以解决这个问题:

Okie. This should do the trick :

1 - 创建一个包含wsdd文件(比如 /tmp/test.wsdd )这个:

1 - Create a wsdd file (say /tmp/test.wsdd) containing this :

<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
 <handler name="test" type="java:axistest.TestHandler" />
 <transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender">
   <requestFlow>
    <handler type="test"/>
   </requestFlow>
 </transport>
</deployment>

2 - 确保所有轴库都在您的类路径中然后运行:

2 - Ensure all axis libs are in your class path and then run :

java org.apache.axis.utils.Admin client /tmp/test.wsdd

3 - 步骤2将生成client-config.wsdd。将其复制到项目中并确保它在项目运行时位于类路径中。

3 - Step 2 will generate a client-config.wsdd. Copy this to your project and ensure it will be in the class path when the project is run.

4 - 所有Web服务调用(通过Http传输)将通过TestHandler1路由class

4 - ALL webservice calls (via Http transport) will route via the TestHandler1 class

这是我的TestHandler1类(稍微修改了访问MIME头的ur处理程序):

Here is my TestHandler1 class (a slight modification of ur handler to access the MIME headers) :

package axistest;

import javax.xml.namespace.QName;
import javax.xml.soap.MimeHeaders;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;

public class TestHandler1 extends BasicHandler {

@Override
public void init() {
    System.out.println("init called");
    super.init();
    System.out.println("init called");
}

@Override
public void cleanup() {
    super.cleanup();
    System.out.println("cleanup called");
}

@Override
public void invoke(MessageContext mc) throws AxisFault {
    System.out.println("invoke called");
    System.out.println("=----------------------------------=");
    MimeHeaders mimeHeaders = mc.getMessage().getMimeHeaders();
    mimeHeaders.addHeader("X-Test", "Hello");
    System.out.println("Headers : \n " + mimeHeaders);
}

public QName[] getHeaders() {
    System.out.println("getHeaders");
    return new QName[1];
}

}

当我在我的盒子上运行时,我看到正在调用这些处理程序方法:

when I run this on my box, I see that these handler methods are being invoked :

- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
init called
init called
invoke called
=----------------------------------=
Headers : 
 org.apache.axis.message.MimeHeaders@761eec35
.
.
.

这篇关于处理程序将HTTP标头添加到使用Axis Client API时未调用的HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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