soap报头与处理

本文深入探讨了SOAP报头的作用及其在消息传递中的重要性,特别是如何通过jax-rpc客户API和SAAJAPI进行SOAP消息头的处理。文章详细介绍了如何创建并实现SOAP消息处理程序,包括如何获取、解析SOAP头元素和SOAP消息体,以及如何修改SOAP应答消息的头部信息。此外,还展示了如何使用JBossWS工具配置SOAP消息头处理类,以及通过客户端代码实现代理调用的案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转至:http://www.blogjava.net/fool/archive/2006/11/13/80901.html
soap报头与处理
soapheaderelement 对象中的属性决定了接收者怎样处理消息,可以认为header属性提供了扩展消息的方法,给出了像身份认证,支付,转发消息这样的相关的事情。jax-rpc客户api(占位程序,动态代理,dii)均没有提供对soap文件头的支持,saaj api提供了支持。所有消息处理程序都必须实现javax.xml.rpc.handler.handler接口,该接口有一系列的方法用以处理soap消息。javax.xml.rpc.handler.generichandler是一个缺省适配器类,创建我们自己的消息处理程序的时候从该类继承覆写我们自己感兴趣的方法就可以了。但每一个handler必须实现 getheader()方法跟 handlerequest()方法.具体实现:
服务类handlerservic:
package handler; public class handlerservice implements handlerserviceinterface1 {  public void testinoutheader(string bodymsg, string headermsg)   {      system.out.print("testinheader: " + bodymsg + "," + headermsg);    } } 

消息处理类:
package handler; import javax.xml.rpc.handler.*;import javax.xml.soap.*;import javax.xml.namespace.qname;import javax.xml.rpc.jaxrpcexception;import javax.xml.rpc.handler.soap.soapmessagecontext;import java.util.iterator; public class cutomerhandler extends generichandler {    protected qname[] headers = new qname[] {                                new qname("http://handler", "headermsg")    };    public qname[] getheaders() {        return headers;    }// 返回handler要处理的文件头元素名的素组.     // 处理接收到的soap消息.如果返回值为false时jax-rpc会立即中断对soap消息处理并将soap消息回传。关于saaj api使用方法见j2ee web service开发(三)    public boolean handlerequest(messagecontext msgcontext) {         try {            soapmessage soapmessage = ((soapmessagecontext) msgcontext).                                      getmessage();            soapheader soapheader = soapmessage.getsoapheader();            iterator it = soapheader.extractallheaderelements();            while (it.hasnext()) {                soapheaderelement headerelement = (soapheaderelement) it.next();                name headername = headerelement.getelementname();                system.out.println(" header name is: " +                                   headername.getqualifiedname());                system.out.println(" header value is: " +                                   headerelement.getvalue());            }            soapbody soapbody = soapmessage.getsoapbody();            iterator bodyiterator = soapbody.getchildelements();            while (bodyiterator.hasnext()) {                soapbodyelement soapbodyelement = (soapbodyelement)                                                  bodyiterator.next();                system.out.println("soapbodyelement print: " +                                   soapbodyelement.getnodename());            }            soapbodyelement nextsoapbodyelement = (soapbodyelement) soapbody.                                                  getchildelements().next();            soapelement soapelement = (soapelement) nextsoapbodyelement.                                      getchildelements().next();            system.out.println("soapelement print: " + soapelement.getlocalname() +                               "  " + soapelement.getvalue());        } catch (soapexception e) {            throw new jaxrpcexception(e);        }         return true;    } 

// 返回soap应答消息,本例修改soap文件头的值再返传给客户端.    public boolean handleresponse(messagecontext msgcontext) {         try {            soapmessage soapmessage = ((soapmessagecontext) msgcontext).                                      getmessage();            soapheader soapheader = soapmessage.getsoapheader();             soapbody soapbody = soapmessage.getsoapbody();            soapbodyelement soapbodyelement = (soapbodyelement) soapbody.                                              getchildelements().next();            string rpcname = soapbodyelement.getelementname().getlocalname();                 soapfactory soapfactory = soapfactory.newinstance();                name headername = soapfactory.createname("headermsg", "ns","http://handler");                soapheaderelement she = soapheader.addheaderelement(headername);                she.setvalue("return header message");         } catch (soapexception e) {            throw new jaxrpcexception(e);        }         return true;    }  }

用于生成布署描述符的wstools工具的配置文件:<configuration xmlns="http://www.jboss.org/jbossws-tools"   xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"   xsi:schemalocation="http://www.jboss.org/jbossws-tools http://www.jboss.org/jbossws-tools/schema/jbossws-tool_1_0.xsd">  <java-wsdl>    <service name="testservice" style="rpc" endpoint="handler.handlerserviceinterface1">       <operation name="testinoutheader">        <parameter type="java.lang.string"/>        <parameter type="java.lang.string"/>      </operation>     </service>    <namespaces target-namespace="http://handler" type-namespace="http://handler"/>    <mapping file="handlerserviceinterface1.xml"/>    <webservices servlet-link="handlerserviceinterface1"/>  </java-wsdl></configuration>

关于wstools工具的使用见j2ee web service开发(一).
在生成的webservice描述符文件中手工加入以下代码:
<handler>
<handler-name>cutomerhandler</handler-name>
<handler-class>handler.cutomerhandler</handler-class>
</handler> 用以配置处理soap消息头的类.

客户端代码:利用j2ee web serive开发(三)介绍的api不难写出saaj客户端代码就不再重复劳动了。这里只介绍jboss ws利用dii实现soap消息头的处理,它扩展了标准jax-rpc api.
package handler;import javax.xml.namespace.qname;import javax.xml.rpc.call;import javax.xml.rpc.parametermode;import javax.xml.rpc.service;import org.jboss.ws.constants;import org.jboss.ws.jaxrpc.callimpl;import javax.xml.rpc.servicefactory;import java.util.*;public class handlerclient {    public handlerclient() {    }     public static void main(string[] args) throws exception{        handlerclient handlerclient = new handlerclient();        handlerclient.testunboundinoutheader();    }    public void testunboundinoutheader() throws exception{        service service = servicefactory.newinstance().createservice(new qname("testservice"));        callimpl call = (callimpl)service.createcall();        call.setoperationname(new qname("http://handler", "testinoutheader"));        call.addparameter("string_1", constants.type_literal_string, parametermode.in);        call.addparameter("string_2", constants.type_literal_string, parametermode.in);        call.settargetendpointaddress("http://hechang:8082/customer-handler/services/handlerserviceinterface1");        qname xmlname = new qname("http://handler", "headermsg");        // xmlname = new qname("http://otherns", "headervalue");         call.addunboundheader(xmlname, constants.type_literal_string, string.class, parametermode.in);        call.setunboundheadervalue(xmlname, " in header message");// 设置消息头 可以设定多组        object retobj = call.invoke(new object[]{"hello world!", "in header message"});               string unboundret = (string)call.getunboundheadervalue(xmlname);       // 处理返回的消息头的值        system.out.println(" unboundreturn: "+unboundret);} } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值