用Java来输出soap报文

来源:互联网 发布:西岐网络新三板 编辑:程序博客网 时间:2024/05/16 00:28

前段时间开发acs,是基于soap协议的通信,一次通信过程包含多个soap报文,而且也不想普通的webserivice那样,

 soap报文是自动生成的。acs的通信的报文是硬编码编出来的,虽然能正确运行,但是实在是不雅,最近闲来无事,

 想着如何用像webservice那样,用java对象设置参数后,将对象转换成string格式的soap报文,这样以后程序的维护

 问题就得到解决了。

  然后在google中baidu了一把,找了个例子,然后开始试验了。其实程序并不难,写这个文章,主要是帮助那些直接使用soap

协议进行开发的朋友们参考,也给自己留个备注,呵呵。

package com.seahigh.acs.soap;import java.util.arraylist;import java.util.list;import javax.xml.namespace.qname;import javax.xml.parsers.documentbuilderfactory;import javax.xml.soap.messagefactory;import javax.xml.soap.soapbody;import javax.xml.soap.soapelement;import javax.xml.soap.soapenvelope;import javax.xml.soap.soapexception;import javax.xml.soap.soapfactory;import javax.xml.soap.soapheader;import javax.xml.soap.soapmessage;import javax.xml.soap.soappart;import javax.xml.transform.outputkeys;import javax.xml.transform.source;import javax.xml.transform.transformer;import javax.xml.transform.transformerfactory;import javax.xml.transform.dom.domsource;import javax.xml.transform.sax.saxsource;import javax.xml.transform.stream.streamresult;import org.w3c.dom.document;import org.w3c.dom.node;import org.xml.sax.inputsource;/** *  * @author 汪心利 * date 2010-3-30下午02:51:30 * (c)copyright seahigh 2010 */public class soaputil {public soappart initsoappart() throws soapexception {soapmessage soapmessage = messagefactory.newinstance().createmessage();soappart soappart = soapmessage.getsoappart();soapenvelope soapenvelope = soappart.getenvelope();soapheader soapheader = soapenvelope.getheader();soapelement cwmp = soapenvelope.addnamespacedeclaration("cwmp","urn:dslforum-org:cwmp-1-0");soapelement xsi = soapenvelope.addnamespacedeclaration("xsi","http://www.w3.org/2001/xmlschema-instance");soapelement xsd = soapenvelope.addnamespacedeclaration("xsd","http://www.w3.org/2001/xmlschema");soapelement enc = soapenvelope.addnamespacedeclaration("soap-enc","http://schemas.xmlsoap.org/soap/encoding/");soapelement id = soapheader.addchildelement("id", "cwmp");id.settextcontent("1");return soappart;}public void soap2string(source source) throws exception {if (source != null) {node root = null;if (source instanceof domsource) {root = ((domsource) source).getnode();} else if (source instanceof saxsource) {inputsource insource = ((saxsource) source).getinputsource();documentbuilderfactory dbf = documentbuilderfactory.newinstance();dbf.setnamespaceaware(true);document doc = dbf.newdocumentbuilder().parse(insource);root = (node) doc.getdocumentelement();}transformer transformer = transformerfactory.newinstance().newtransformer();transformer.setoutputproperty(outputkeys.indent, "yes");transformer.transform(new domsource(root), new streamresult(system.out));}}public source informresponse(soappart part) throws exception {soapenvelope soapenvelope = part.getenvelope();soapbody soapbody = soapenvelope.getbody();soapelement informres = soapbody.addchildelement("informresponse","cwmp");soapelement max = soapfactory.newinstance().createelement("maxenvelopes", "", "");max.settextcontent("1");informres.addchildelement(max);return part.getcontent();}public source getparametervalues(soappart part, list list) throws exception {soapenvelope soapenvelope = part.getenvelope();soapbody soapbody = soapenvelope.getbody();soapelement informres = soapbody.addchildelement("getparametervalues","cwmp");soapfactory soapfactory = soapfactory.newinstance();soapelement names = soapfactory.createelement("parameternames", "", "");names.addattribute(new qname("soap-enc:arraytype"), "xsd:string["+ list.size() + "]");soapelement nameelement = null;for (int i = 0; i < list.size(); i++) {nameelement = soapfactory.createelement("string", "", "");nameelement.settextcontent((string) list.get(i));names.addchildelement(nameelement);}informres.addchildelement(names);return part.getcontent();}public static void main(string[] args) throws exception {soaputil util = new soaputil();soappart part = util.initsoappart();source inform = util.getparametervalues(part, util.gettestnames());util.soap2string(inform);}public list<string> gettestnames() {list<string> list = new arraylist<string>();list.add("internetgatewaydevice.deviceinfo.x_ct-com_cpu");list.add("internetgatewaydevice.deviceinfo.x_ct-com_worktime");list.add("internetgatewaydevice.deviceinfo.softwareversion");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.ssid");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_receivenoise");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalbytesreceived");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalbytessent");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalbytesreceived");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalbytessent");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalpacketsreceived");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalpacketssent");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalpacketsreceived");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalpacketssent");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.responsepass");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.askpass");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.successpass");list.add("internetgatewaydevice.deviceinfo.x_ct-com_temp");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.lan-packetserror");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.lan-totalbytesreceived");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.lan-totalbytessent");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.wan-totalbytesreceived");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.wan-packetserror");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.wan-totalbytessent");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.associateddevice.1.x_ct-com_receiverate");list.add("internetgatewaydevice.landevice.1.wlanconfiguration.1.associateddevice.1.x_ct-com_sendrate");list.add("internetgatewaydevice.deviceinfo.serialnumber");list.add("internetgatewaydevice.deviceinfo.manufactureroui");return list;}}

 

这就是研究的成果了,看下结果:

对应tr069协议中的rcp method:getparametervalues

<?xml version="1.0" encoding="utf-8" standalone="no"?><soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:cwmp="urn:dslforum-org:cwmp-1-0" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"><soap-env:header><cwmp:id>1</cwmp:id></soap-env:header><soap-env:body><cwmp:getparametervalues><parameternames soap-enc:arraytype="xsd:string[27]"><string>internetgatewaydevice.deviceinfo.x_ct-com_cpu</string><string>internetgatewaydevice.deviceinfo.x_ct-com_worktime</string><string>internetgatewaydevice.deviceinfo.softwareversion</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.ssid</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_receivenoise</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalbytesreceived</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalbytessent</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalbytesreceived</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalbytessent</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalpacketsreceived</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_lan-totalpacketssent</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalpacketsreceived</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_wan-totalpacketssent</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.responsepass</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.askpass</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.successpass</string><string>internetgatewaydevice.deviceinfo.x_ct-com_temp</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.lan-packetserror</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.lan-totalbytesreceived</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.lan-totalbytessent</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.wan-totalbytesreceived</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.wan-packetserror</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.x_ct-com_stat.wan-totalbytessent</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.associateddevice.1.x_ct-com_receiverate</string><string>internetgatewaydevice.landevice.1.wlanconfiguration.1.associateddevice.1.x_ct-com_sendrate</string><string>internetgatewaydevice.deviceinfo.serialnumber</string><string>internetgatewaydevice.deviceinfo.manufactureroui</string></parameternames></cwmp:getparametervalues></soap-env:body></soap-env:envelope>

 cpe informresponse

<?xml version="1.0" encoding="utf-8" standalone="no"?><soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:cwmp="urn:dslforum-org:cwmp-1-0" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"><soap-env:header><cwmp:id>1</cwmp:id></soap-env:header><soap-env:body><cwmp:informresponse><maxenvelopes>1</maxenvelopes></cwmp:informresponse></soap-env:body></soap-env:envelope>
0 0