java 使用 Axis 客户端 API 时未调用的用于将 HTTP 标头添加到 HTTP 请求的处理程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3925272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:59:05  来源:igfitidea点击:

Handler to add HTTP headers to HTTP request not invoked when using Axis Client API

javahttphttphandleraxis

提问by Rohit Banga

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

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

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

我正在使用以下代码向服务器添加处理程序。service是 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.

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

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

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

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

What is wrong with the above code?

上面的代码有什么问题?

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

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

采纳答案by madhurtanwani

Okie. This should do the trick :

好的。这应该可以解决问题:

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

1 - 创建一个/tmp/test.wsdd包含以下内容的 wsdd 文件(比如):

<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 - Ensure all axis libs are in your class path and then run :

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

java org.apache.axis.utils.Admin client /tmp/test.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.

3 - 第 2 步将生成一个 client-config.wsdd。将此复制到您的项目中,并确保在项目运行时它将位于类路径中。

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

4 - 所有网络服务调用(通过 Http 传输)将通过 TestHandler1 类路由

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

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

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
.
.
.

回答by madhurtanwani

BTW reading on the Handlers usage, I found this page. See if it helps : http://soa.sys-con.com/node/39721

顺便说一下,在阅读 Handlers 用法时,我找到了这个页面。看看它是否有帮助:http: //soa.sys-con.com/node/39721

回答by madhurtanwani

We are adding custom headers to a SOAP request. However, we have implemented this by injecting the headers into the Axis Stub object for the webservice at runtime. Thus, they dont change for every request, but the headers injected are used for the entire run of our test cases

我们正在向 SOAP 请求添加自定义标头。但是,我们通过在运行时将标头注入 Web 服务的 Axis Stub 对象来实现这一点。因此,它们不会因每个请求而改变,但注入的标头用于我们测试用例的整个运行

If you think that's your use case as well, I can find that code and update with what we did.

如果您认为这也是您的用例,我可以找到该代码并更新我们所做的。