十二、用Axis操作 Header头部信息

Axis中操作Header的信息是通过Handler来完成的,在读取Header的信息的时候,可以完成Header的操作的Handler有 JWSHandler、SOAPMonitorHandler、JAXRPCHandler。JWSHandler是完成jws发布模式的 WebService的Header的读取操作,

SOAPMonitorHandler是完成SOAPMonitor模式的,JAXRPCHandler是java rpc模式的。

下面将会用JAXRPCHandler读取客户端的Header信息,然后用BasicHandler将客户端请求的Header写入客户端的响应的Header中。

1、 首先编写一个方法,这个方法什么也不做。就是在这个方法请求和响应的时候分别完成读取Header的信息和写入响应的Header内容。代码如下: 

 package com.hoo.service; /** * <b>function:</b>读取客户端头部Header信息的WebService * @author hoojo * @createDate Dec 19, 2010 11:33:25 AM * @file ReadHeaderService.java * @package com.hoo.service * @project AxisWebService * @blog http://blog.csdn.net/IBM_hoojo * @email [email protected] * @version 1.0 */ public class OperaterHeaderService { /** * <b>function:</b>提供一个空方法,在调用这个方法的时候 * 触发ReadHeaderHandler读取Header的信息 * @author hoojo * @createDate Dec 19, 2010 11:33:54 AM */ public void readHeader() { } }  

 

 

2、 客户端的Header中的内容是下面这样的xml内容

 <ns1:Header soapenv:mustUnderstand="0" xmlns:ns1="soap"> <ServiceName>ReadHeaderService</ServiceName> <CreateDate>2010-12-19T06:22:59.931Z</CreateDate> <ns2:Version xmlns:ns2="service">1.0</ns2:Version> <author> <name>axis</name> <age>14</age> </author> <copyRight> <year>2010</year> <number>123123123</number> </copyRight> <Tel>555666442</Tel> </ns1:Header>  

 

 

下面将用客户端代码将上面的xml元素添加的Header中,代码如下:

/** 添加请求的header */ //父节点,根节点 SOAPHeaderElement el = new SOAPHeaderElement(new QName("soap", "Header")); //文本节点 MessageElement msgEl = new MessageElement(new QName("ServiceName"), "ReadHeaderService"); el.addChild(msgEl); //文本节点 el.addChild(new MessageElement(new QName("CreateDate"), new Date())); el.addChild(new MessageElement(new QName("service", "Version"), "1.0")); //子元素 msgEl = new MessageElement(new QName("author")); msgEl.addChild(new MessageElement(new QName("name"), "axis")); msgEl.addChild(new MessageElement(new QName("age"), "14")); el.addChild(msgEl); //子元素 msgEl = new MessageElement(new QName("copyRight")); msgEl.addChild(new MessageElement(new QName("year"), "2010")); msgEl.addChild(new MessageElement(new QName("number"), "123123123")); el.addChild(msgEl); el.addChild(new MessageElement(new QName("Tel"), "555666442")); System.out.println(el.toString()); //设置请求的header call.addHeader(el);

 

 

上面的就完成了xml中的内容,并且添加到请求的Header中。

3、 根据上面的Header的内容,我们现在编写一个Handler用来读取客户端请求信息中的Header的内容,这个Handler需要继承JAXRPCHandler。代码如下:

package com.hoo.service.handler; import java.util.Iterator; import java.util.List; import javax.xml.soap.SOAPHeader; import org.apache.axis.AxisFault; import org.apache.axis.MessageContext; import org.apache.axis.handlers.JAXRPCHandler; import org.apache.axis.message.MessageElement; import org.apache.axis.message.NodeImpl; import org.apache.axis.message.SOAPHeaderElement; import org.apache.axis.message.Text; /** * <b>function:</b>读取Header信息的Handler * @author hoojo * @createDate Dec 19, 2010 11:34:56 AM * @file ReadHeaderHandler.java * @package com.hoo.service.handler * @project AxisWebService * @blog http://blog.csdn.net/IBM_hoojo * @email [email protected] * @version 1.0 */ public class ReadHeaderHandler extends JAXRPCHandler { private static final long serialVersionUID = -546314372478633248L; @SuppressWarnings("unchecked") @Override public void invoke(MessageContext msgContext) throws AxisFault { try { //头对象 SOAPHeader header = msgContext.getCurrentMessage().getSOAPHeader(); Iterator<SOAPHeaderElement> iter = header.getChildElements(); while (iter.hasNext()) { //子元素 SOAPHeaderElement el = iter.next(); //继续子元素 List<NodeImpl> list = el.getChildren(); for (NodeImpl node : list) { if (node.hasChildNodes()) { MessageElement msg = (MessageElement) node; if (msg.hasChildNodes()) { List<NodeImpl> msgNode = msg.getChildren(); for (NodeImpl n : msgNode) { if (!n.hasChildNodes()) { Text text = (Text) n; System.out.println("父节点:" + el.getName() + ", 节点:" + msg.getName() + ", 值:" + text.getValue()); } else { MessageElement child = (MessageElement) n; if (child.hasChildNodes()) { List<NodeImpl> childNode = child.getChildren(); for (NodeImpl n2 : childNode) { if (!n2.hasChildNodes()) { Text text = (Text) n2; System.out.print("父父节点:" + el.getName() + ", 父节点:" + msg.getName()); System.out.println(", 节点:" + child.getName() + "##" + child.getNodeName() + ", 值:" + text.getValue() + "##" + child.getValue()); } } } } } } } } } } catch (Exception e) { e.printStackTrace(); } } }  

 

 

上面读取Header内容的代码有点小长,不过比较简单。就是用循环遍历Header的内容,Header的内容是一个xml的文档。所以用循环一步步向里面遍历,不过感觉写个递归会更简单。

4、 下面将编写一个Handler来向客户端的Response中写入Header内容,写入的Header的内容就是客户端请求的Header内容。代码如下:

 package com.hoo.service.handler; import java.util.Iterator; import javax.xml.soap.SOAPException; import org.apache.axis.AxisFault; import org.apache.axis.Message; import org.apache.axis.MessageContext; import org.apache.axis.handlers.BasicHandler; import org.apache.axis.message.SOAPEnvelope; import org.apache.axis.message.SOAPHeader; import org.apache.axis.message.SOAPHeaderElement; /** * <b>function:</b>用handler向客户端写入Response 的Header信息 * @author hoojo * @createDate Dec 19, 2010 2:08:35 PM * @file WriteHeaderHandler.java * @package com.hoo.service.handler * @project AxisWebService * @blog http://blog.csdn.net/IBM_hoojo * @email [email protected] * @version 1.0 */ public class WriteHeaderHandler extends BasicHandler { private static final long serialVersionUID = 1L; @SuppressWarnings("unchecked") public void invoke(MessageContext msgContext) throws AxisFault { Message msg = msgContext.getCurrentMessage(); SOAPEnvelope se = msg.getSOAPEnvelope(); //request Header的部分头信息 Message reqMsg = msgContext.getRequestMessage(); SOAPEnvelope reqSe = reqMsg.getSOAPEnvelope(); SOAPHeader reqHeader = null; try { reqHeader = (SOAPHeader) reqSe.getHeader(); } catch (SOAPException e) { e.printStackTrace(); } //response Header部分头信息 Message respMsg = msgContext.getResponseMessage(); SOAPEnvelope respSe = respMsg.getSOAPEnvelope(); SOAPHeader respHeader = null; try { respHeader = (SOAPHeader) respSe.getHeader(); } catch (SOAPException e) { e.printStackTrace(); } Iterator<SOAPHeaderElement> iter = reqHeader.examineAllHeaderElements(); while (iter.hasNext()) { SOAPHeaderElement she = iter.next(); try { //添加请求信息到响应的Header中 respHeader.addChildElement(she); } catch (SOAPException e) { e.printStackTrace(); } System.out.println(she.getNodeName()); } //设置响应信息 msgContext.setResponseMessage(respMsg); try { System.out.println("header Info:" + se.getHeader()); } catch (SOAPException e) { e.printStackTrace(); } } }  

 

 

上面的代码是取出respHeader = (SOAPHeader) respSe.getHeader();

然后循环遍历Header的元素写入到respHeader.addChildElement(she);

5、 好了,上面已经写好了WebService的方法,以及请求时读取Header内容的Handler和在响应时写入Header内容的Handler。下面开始写wsdd发布当前WebService。

<?xml version="1.0" encoding="UTF-8"?> <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <handler name="ReadHeaderHandler" type="java:com.hoo.service.handler.ReadHeaderHandler"/> <handler name="WriteHeaderHandler" type="java:com.hoo.service.handler.WriteHeaderHandler"/> <service name="OperaterHeader" provider="java:RPC"> <requestFlow> <handler type="ReadHeaderHandler"/> </requestFlow> <parameter name="className" value="com.hoo.service.OperaterHeaderService" /> <parameter name="allowedMethods" value="*" /> <parameter name="scope" value="request" /> <responseFlow> <handler type="WriteHeaderHandler"/> </responseFlow> </service> </deployment>

 

 

还是用doc命令发布当前WebService,命令如下:

C:/SoftWare/tomcat-5.0.28/tomcat-5.0.28/webapps/AxisWebService/WEB-INF>java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://localhost:8080/AxisWebService/services/AdminService deployHeader.wsdd

发布完成后,你会看的

Processing file deployHeader.wsdd

<Admin>Done processing</Admin>

的字样就表示发布成功了,然后在浏览器地址栏输入:

http://localhost:8080/AxisWebService/servlet/AxisServlet

你就可以看到OperaterHandler这个WebService了

6、 OperaterHandler这个WebService成功发布,现在就是要编写客户端代码调用这个WebService,客户端代码比较简单,完整代码如下:

package com.hoo.client; import java.rmi.RemoteException; import java.util.Date; import javax.xml.namespace.QName; import javax.xml.rpc.ServiceException; import javax.xml.soap.SOAPException; import org.apache.axis.Message; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.message.MessageElement; import org.apache.axis.message.SOAPEnvelope; import org.apache.axis.message.SOAPHeaderElement; /** * <b>function:</b>Header操作WebService客户端 * @author hoojo * @createDate Dec 19, 2010 2:18:51 PM * @file OperaterHeaderClient.java * @package com.hoo.client * @project AxisWebService * @blog http://blog.csdn.net/IBM_hoojo * @email [email protected] * @version 1.0 */ public class OperaterHeaderClient { public static void main(String[] args) throws SOAPException, ServiceException, RemoteException { String url = "http://localhost:8080/AxisWebService/services/OperaterHeader"; //创建服务 Service service = new Service(); //创建调用句柄 Call call = (Call) service.createCall(); //设置请求地址 call.setTargetEndpointAddress(url); call.setOperationName(new QName("readHeader")); /** 添加请求的header */ //父节点,根节点 SOAPHeaderElement el = new SOAPHeaderElement(new QName("soap", "Header")); //文本节点 MessageElement msgEl = new MessageElement(new QName("ServiceName"), "ReadHeaderService"); el.addChild(msgEl); //文本节点 el.addChild(new MessageElement(new QName("CreateDate"), new Date())); el.addChild(new MessageElement(new QName("service", "Version"), "1.0")); //子元素 msgEl = new MessageElement(new QName("author")); msgEl.addChild(new MessageElement(new QName("name"), "axis")); msgEl.addChild(new MessageElement(new QName("age"), "14")); el.addChild(msgEl); //子元素 msgEl = new MessageElement(new QName("copyRight")); msgEl.addChild(new MessageElement(new QName("year"), "2010")); msgEl.addChild(new MessageElement(new QName("number"), "123123123")); el.addChild(msgEl); el.addChild(new MessageElement(new QName("Tel"), "555666442")); System.out.println(el.toString()); //设置请求的header call.addHeader(el); /** * 注意:在请求readHeader方法的时候,也就是invoke执行的时候。会调用ReadHeaderHandler这个handler * 在请求完成后,准备返回值的时候或是说函数在运行完成了后。就会触发WriteHeaderHandler */ call.invoke(new Object[] {}); /** 响应的header */ Message msg = call.getResponseMessage(); SOAPEnvelope se = msg.getSOAPEnvelope(); System.out.println("responseMessage:" + se.getHeader()); } }  

 

 

 

call.getResponseMessage();可以得到服务器响应的Header的信息

运行后你可以看到客户端控制台会输出:

 

<ns1:Header soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns:ns1="soap" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><ServiceName xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">ReadHeaderService</ServiceName><CreateDate xsi:type="xsd:dateTime" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">2010-12-19T06:22:59.931Z</CreateDate><ns2:Version xsi:type="xsd:string" xmlns:ns2="service" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">1.0</ns2:Version><author><name xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">axis</name><age xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">14</age></author><copyRight><year xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">2010</year><number xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">123123123</number></copyRight><Tel xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">555666442</Tel></ns1:Header>

responseMessage:<soapenv:Header xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><ns1:Header soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns:ns1="soap"><ServiceName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">ReadHeaderService</ServiceName><CreateDate xsi:type="xsd:dateTime" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2010-12-19T06:22:59.931Z</CreateDate><ns2:Version xsi:type="soapenc:string" xmlns:ns2="service" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">1.0</ns2:Version><author><name xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">axis</name><age xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">14</age></author><copyRight><year xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2010</year><number xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">123123123</number></copyRight><Tel xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">555666442</Tel></ns1:Header></soapenv:Header>

responseMessage前面的内容是客户端将会写入到Header中的xml内容,而responseMessage后面的内容就是响应的Header的内容,这段内容是WriteHeaderHandler写入到ResponseMessage中的。

再看看服务器端控制台的内容:

父节点:Header, 节点:ServiceName, 值:ReadHeaderService

父节点:Header, 节点:CreateDate, 值:2010-12-19T06:22:59.931Z

父节点:Header, 节点:Version, 值:1.0

父父节点:Header, 父节点:author, 节点:name##name, 值:axis##axis

父父节点:Header, 父节点:author, 节点:age##age, 值:14##14

父父节点:Header, 父节点:copyRight, 节点:year##year, 值:2010##2010

父父节点:Header, 父节点:copyRight, 节点:number##number, 值:123123123##123123123

父节点:Header, 节点:Tel, 值:555666442

ns1:Header

header Info:<soapenv:Header xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><ns1:Header soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns:ns1="soap"><ServiceName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">ReadHeaderService</ServiceName><CreateDate xsi:type="xsd:dateTime" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2010-12-19T06:22:59.931Z</CreateDate><ns2:Version xsi:type="soapenc:string" xmlns:ns2="service" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">1.0</ns2:Version><author><name xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">axis</name><age xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">14</age></author><copyRight><year xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2010</year><number xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">123123123</number></copyRight><Tel xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">555666442</Tel></ns1:Header></soapenv:Header>

上面的文本内容是ReadHeaderHandler读取客户端的Header的内容和我们定制的xml的内容是一致的。而后面的xml能入是WriteHeaderHandler中输出的内容。

作者:hoojo
出处: http://www.cnblogs.com/hoojo/archive/2010/12/20/1911396.html
blog:http://blog.csdn.net/IBM_hoojo

你可能感兴趣的:(webservice,String,header,service,SOAP,2010)