SOAP消息传输工具类

来源:互联网 发布:招聘seo主管 编辑:程序博客网 时间:2024/05/17 03:27
package com.sdmc.xmediatv.drm.util;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.URL;import java.net.URLConnection;import java.util.Iterator;import javax.xml.soap.MessageFactory;import javax.xml.soap.MimeHeaders;import javax.xml.soap.SOAPBody;import javax.xml.soap.SOAPElement;import javax.xml.soap.SOAPMessage;import com.sdmc.xmediatv.drm.xmlbean.CommonAcRes;import com.sdmc.xmediatv.drm.xmlbean.CommonRes;import com.sdmc.xmediatv.drm.xmlbean.RegisterDeviceRes;import com.sdmc.xmediatv.util.ConfigUtil;public class SoapUtil {private final static String address = ConfigUtil.config.get("yong.xin.drm.portal.url")+"";public static String getResXml(String reqStr) {OutputStreamWriter out = null;StringBuilder sTotalString = new StringBuilder();try {URL urlTemp = new URL(address);URLConnection connection = urlTemp.openConnection();connection.setDoOutput(true);out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");StringBuffer sb = new StringBuffer();sb.append(reqStr);out.write(sb.toString());out.flush();String sCurrentLine;sCurrentLine = "";InputStream l_urlStream;l_urlStream = connection.getInputStream();// 请求BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream));while ((sCurrentLine = l_reader.readLine()) != null) {sTotalString.append(sCurrentLine);}} catch (Exception e) {e.printStackTrace();} finally {if (null != out) {try {out.close();} catch (IOException e) {e.printStackTrace();}}}return sTotalString.toString();}/** * 把soap字符串格式化为SOAPMessage *  * @param soapString * @return * @see [类、类#方法、类#成员] */@SuppressWarnings("restriction")public static SOAPMessage formatSoapString(String soapString) {MessageFactory msgFactory;try {msgFactory = MessageFactory.newInstance();SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(),new ByteArrayInputStream(soapString.getBytes("UTF-8")));reqMsg.saveChanges();return reqMsg;} catch (Exception e) {e.printStackTrace();return null;}}/** * 解析soap xml *  * @param iterator * @param resultBean */@SuppressWarnings("unchecked")public static CommonRes parseComResp(Iterator<SOAPElement> iterator, CommonRes resultBean) {while (iterator.hasNext()) {SOAPElement element = iterator.next();//System.out.println("Node Name:" + element.getNodeName());//System.out.println("Value:" + element.getValue());if ("ComResp".equals(element.getNodeName())) {Iterator<SOAPElement> it = element.getChildElements();SOAPElement el = null;while (it.hasNext()) {el = it.next();if ("RetCode".equals(el.getLocalName())) {resultBean.setRetCode(el.getValue());//System.out.println("#### " + el.getLocalName() + " ==== " + el.getValue());}}} else if (null == element.getValue() && element.getChildElements().hasNext()) {parseComResp(element.getChildElements(), resultBean);}}return resultBean;}/** * 解析soap xml *  * @param iterator * @param resultBean */@SuppressWarnings("unchecked")public static CommonAcRes parseComAcResp(Iterator<SOAPElement> iterator, CommonAcRes resultBean) {while (iterator.hasNext()) {SOAPElement element = iterator.next();if ("ComResp".equals(element.getNodeName())) {Iterator<SOAPElement> it = element.getChildElements();SOAPElement el = null;while (it.hasNext()) {el = it.next();if ("RetCode".equals(el.getLocalName())) {resultBean.setRetCode(el.getValue());}else if ("AC".equals(el.getLocalName())) {resultBean.setAc(el.getValue());}}} else if (null == element.getValue() && element.getChildElements().hasNext()) {parseComAcResp(element.getChildElements(), resultBean);}}return resultBean;}/** * 解析soap xml *  * @param iterator * @param resultBean */@SuppressWarnings("unchecked")public static RegisterDeviceRes parseRegisterDeviceResp(Iterator<SOAPElement> iterator, RegisterDeviceRes resultBean) {while (iterator.hasNext()) {SOAPElement element = iterator.next();if ("ComResp".equals(element.getNodeName())) {Iterator<SOAPElement> it = element.getChildElements();SOAPElement el = null;while (it.hasNext()) {el = it.next();if ("RetCode".equals(el.getLocalName())) {resultBean.setRetCode(el.getValue());}else if ("SN".equals(el.getLocalName())) {resultBean.setSn(el.getValue());}else if ("ResData".equals(el.getLocalName())) {resultBean.setResData(el.getValue());}}} else if (null == element.getValue() && element.getChildElements().hasNext()) {parseRegisterDeviceResp(element.getChildElements(), resultBean);}}return resultBean;}@SuppressWarnings("unchecked")public static void main(String[] args) {System.out.println("开始解析soap...");String deptXML = "<?xml version=\"1.0\"  encoding=\"UTF-8\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:ns=\"urn:stv-securemax\"><SOAP-ENV:Body SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><ns:DeleteContentResponse><ComResp><RetCode>0</RetCode></ComResp></ns:DeleteContentResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>";CommonRes ret = new CommonRes();try {SOAPMessage msg = formatSoapString(deptXML);SOAPBody body = msg.getSOAPBody();Iterator<SOAPElement> iterator = body.getChildElements();parseComResp(iterator, ret);} catch (Exception e) {e.printStackTrace();}System.out.println("解析soap结束...");}}