首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

用Java回输出soap报文

2012-11-18 
用Java来输出soap报文?? 前段时间开发ACS,是基于SOAP协议的通信,一次通信过程包含多个soap报文,而且也不想

用Java来输出soap报文

?? 前段时间开发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>

?

热点排行