且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Java中使用Web服务时如何触发处理程序类

更新时间:2023-08-31 17:39:16

***的开始是查看Axis指南:《 Apache轴参考指南》 ,其中将概述工作流程.

要配置从客户端触发的处理程序,您需要执行以下操作:

1-基本创建类似于以下内容的处理程序类:

package mypackge;

import javax.xml.soap.SOAPException;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;
import org.apache.axis.message.SOAPHeader;
import org.apache.axis.message.SOAPHeaderElement;

public class SoapHeaderConsumerHandler
  extends BasicHandler
{
  public void invoke(MessageContext messageContext)
    throws AxisFault
  {
    // Your logic for request or response handling goes here. 
    // Basically you need to make use of the parameter
   // messageContext where you can access the soap header and body tags.
  }
}

2-创建client-config.wsdd文件.它将如下所示:

<deployment
    xmlns="http://xml.apache.org/axis/wsdd/"
    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    <globalConfiguration>
        <responseFlow>
            <handler name="log" type="java:mypackge.SoapHeaderConsumerHandler"/>
        </responseFlow>
    </globalConfiguration>
    <transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender"/>
</deployment>

您可以看到我仅对来自服务器端的传入响应使用处理程序.因此,无论何时客户端应用程序收到服务器的响应,处理程序类SoapHeaderConsumerHandler都会被触发,默认情况下将调用方法调用.

注意::如果要在将传出请求发送到服务器之前访问传出请求,则需要为<requestFlow>添加额外标签以添加请求处理程序.

检查来自Axis指南的Deployment(WSDD)参考:

3- client-config.wsdd文件的放置位置?

您应该将.wsdd文件放置在工作目录中.您可以使用:

轻松找到工作目录的位置
System.out.println("Working Directory = " + System.getProperty("user.dir"));

来源: 在以下位置获取当前工作目录Java

更新:

我发现没有必要将client-config.wsdd文件放在工作目录中.您可以在代码中指定此文件的路径,如下所示:

System.setProperty("axis.ClientConfigFile", "[Path goes here]\\client-config.wsdd");

您只需要将.wsdd文件放在此处.

其他有用链接:

V轴处理程序这是服务器端的示例处理程序.

在Axis中处理SOAP标头

Using Axis 1.4 I built client application that will consume external server services.

The server application response with soap message that include header tag along with body tag.

My problem with the header tag, I am trying to find away to get the header element.

What is done so far:

I found that I need to use a handler that extends BasicHandler using this class I can get the header tag. source: Dealing with SOAP Headers in Axis

But how to make this handler work when consuming web-service? I mean how to invoke this handler when ever I am receiving response from server to get its header.

Some blogs suggest I need to use .wsdd file. I am using Jdeveloper 11g with weblogic 10.3.6 environment where I am only aware of web.xml file for configuration.

Question: How to link those information(handler class, .wsdd file and web.xml) to gather and make the handler works to get the header tags?

The best start was to check Axis guide on: Apache-Axis Reference Guide where you will have an overview of the work flow.

To configure handlers to be trigger from the client side you need to do the following:

1- Create handler class basically something similar to the following:

package mypackge;

import javax.xml.soap.SOAPException;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;
import org.apache.axis.message.SOAPHeader;
import org.apache.axis.message.SOAPHeaderElement;

public class SoapHeaderConsumerHandler
  extends BasicHandler
{
  public void invoke(MessageContext messageContext)
    throws AxisFault
  {
    // Your logic for request or response handling goes here. 
    // Basically you need to make use of the parameter
   // messageContext where you can access the soap header and body tags.
  }
}

2- Create the client-config.wsdd file. it will look like the following:

<deployment
    xmlns="http://xml.apache.org/axis/wsdd/"
    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    <globalConfiguration>
        <responseFlow>
            <handler name="log" type="java:mypackge.SoapHeaderConsumerHandler"/>
        </responseFlow>
    </globalConfiguration>
    <transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender"/>
</deployment>

You can see that I am using only handlers for the incoming response from the server side. So when ever the client application receive a response from the server the handler class SoapHeaderConsumerHandler will be triggered and the method invoke will be called by default.

Note: if you want to access the outgoing request before send it to the server you need to add extra tag for <requestFlow> to add request handler.

Check Deployment(WSDD) Reference from Axis guide:

3- Where to place the client-config.wsdd file ?

You should place the .wsdd file in the working directory. You can easily find out the working directory location using :

System.out.println("Working Directory = " + System.getProperty("user.dir"));

Source: Getting the Current Working Directory in Java

UPDATE:

I found out that it is not necessary to put the client-config.wsdd file in the working directory. You can specify the path of this file in your code as follow:

System.setProperty("axis.ClientConfigFile", "[Path goes here]\\client-config.wsdd");

You just need to place the .wsdd file there.

Extra Useful Links:

Where to place the client-config.wsdd file in Railo

V Axis handler This is an example for the server side handlers.

Dealing with SOAP Headers in Axis