转至: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:
消息处理类:
关于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.
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);} }