V5
package com.isoftstone.core.service.impl; import com.isoftstone.core.service.common.impl.ConnectToV5ByXmlImpl; import com.isoftstone.core.util.AESTool; import com.isoftstone.core.util.FileTool; import com.web.CommRemoteCall; import java.io.PrintStream; import java.util.List; import org.apache.log4j.Logger; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; public class TestTruePolicyImpl extends ConnectToV5ByXmlImpl { static Logger log = Logger.getLogger(TestTruePolicyImpl.class.getName()); private static final String KEY = "BDYZ_ZBX"; public String invoke(String request) { String decryptStr = ""; String zbxNeedStr = ""; String coreRetStr = ""; try { Document docReq = DocumentHelper.parseText(request); decryptNodes(docReq.getRootElement()); decryptStr = docReq.asXML(); String sendstr = addRouteToXml(decryptStr); coreRetStr = CommRemoteCall.RemoteCallIo(sendstr); Document docRet = null; try { docRet = DocumentHelper.parseText(coreRetStr); } catch (Exception e) { e.printStackTrace(); zbxNeedStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><respData><errorCode>" + AESTool.encrypt("01", "BDYZ_ZBX") + "</errorCode>" + "<errorReason>" + AESTool.encrypt(new StringBuilder("核心返回报文如下:").append(coreRetStr).append(",解析时发生异常,异常如下:\t").append(e.getMessage()).toString(), "BDYZ_ZBX") + "</errorReason>" + "</respData>"; } if (docRet != null) { encryptNodes(docRet.getRootElement()); zbxNeedStr = docRet.asXML(); } } catch (Exception e) { e.printStackTrace(); zbxNeedStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><respData><errorCode>" + AESTool.encrypt("01", "BDYZ_ZBX") + "</errorCode>" + "<errorReason>" + AESTool.encrypt(e.getMessage(), "BDYZ_ZBX") + "</errorReason>" + "</respData>"; } finally { log.info("【中保信请求v5核心原始报文】:\n" + request); log.info("【ESB解密后请求v5核心的真实报文】:\n" + decryptStr); log.info("【核心返回的原始报文】:\n" + coreRetStr); log.info("【核心返回的原始报文经过ESB加密后,中保信真正需要的报文】:\n" + zbxNeedStr); } return zbxNeedStr; } public static void decryptNodes(Element node) throws Exception { System.out.println("--------------------"); if (node.isTextOnly()) { node.setText(AESTool.linuxDecrypt(node.getTextTrim(), "BDYZ_ZBX")); } List<Element> listElement = node.elements(); for (Element e : listElement) decryptNodes(e); } public static void encryptNodes(Element node) throws Exception { System.out.println("--------------------"); if (node.isTextOnly()) { node.setText(AESTool.linuxEncrypt(node.getTextTrim(), "BDYZ_ZBX")); } List<Element> listElement = node.elements(); for (Element e : listElement) encryptNodes(e); } public static void main(String[] args) throws Exception { String ssss = FileTool.readStringFromFile("d:/c.txt", "gbk"); Document docReq = DocumentHelper.parseText(ssss); encryptNodes(docReq.getRootElement()); String decryptStr = docReq.asXML(); System.out.println(decryptStr); } }
V7
package com.isoftstone.core.service.impl; import java.util.List; import org.apache.log4j.Logger; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import com.isoftstone.core.service.intf.ServiceOfStringPara; import com.isoftstone.core.util.AESTool; import com.isoftstone.core.util.PatternTool; import com.isoftstone.core.util.WebserviceTool; public class V7TestTruePolicyImpl implements ServiceOfStringPara{ static Logger log = Logger.getLogger(V7TestTruePolicyImpl.class.getName()); private String serviceUrl;//http://pcistest.zsins.com/pcis/services/PolicyValidateTrueService private static final String KEY = "BDYZ_ZBX"; @Override public String invoke(String request) { String decryptStr = ""; String zbxNeedStr = ""; String retStr = ""; StringBuffer soapRequestData = new StringBuffer(); try { Document docReq = DocumentHelper.parseText(request); decryptNodes(docReq.getRootElement()); decryptStr = docReq.asXML(); soapRequestData.append("<soapenv:Envelope"); soapRequestData.append(" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" "); soapRequestData.append(" xmlns:zbx=\"http://zbx.interfaces.webservice.pcis.isoftstone.com/\">"); soapRequestData.append(" <soapenv:Header/> "); soapRequestData.append("<soapenv:Body>"); soapRequestData.append("<zbx:validate>"); soapRequestData.append("<arg0>"); soapRequestData.append("<![CDATA[" + decryptStr + "]]>"); soapRequestData.append("</arg0>"); soapRequestData.append("</zbx:validate>"); soapRequestData.append(" </soapenv:Body>"); soapRequestData.append("</soapenv:Envelope>"); retStr = WebserviceTool.send(soapRequestData.toString(), serviceUrl); if (retStr != null) { zbxNeedStr = PatternTool.parsePattern("<return>([\\s\\S]*)</return>", retStr); if("".equals(zbxNeedStr)){ zbxNeedStr = retStr; } } } catch (Exception e) { e.printStackTrace(); zbxNeedStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"+ "<respData>"+ "<errorCode>"+AESTool.encrypt("01",KEY)+"</errorCode>"+//根据中保信接口文档,接口处理过程中程序发生异常,必须返回01 "<errorReason>"+AESTool.encrypt(e.getMessage(),KEY)+"</errorReason>"+ "</respData>"; } finally { log.info("【中保信请求v7核心原始报文】:\n" + request); log.info("【ESB解密后请求v7核心的真实报文】:\n" + soapRequestData.toString()); log.info("【v7核心返回的原始报文】:\n" + retStr); log.info("【v7核心返回的原始报文经过ESB加密后,中保信真正需要的报文】:\n" + zbxNeedStr); } return zbxNeedStr; } /** * 从指定节点开始,递归遍历所有子节点 * * @author chenleixing */ public static void decryptNodes(Element node) throws Exception{ if(node.isTextOnly()){ node.setText(AESTool.linuxDecrypt(node.getTextTrim(), KEY)); } // 递归遍历当前节点所有的子节点 List<Element> listElement = node.elements();// 所有一级子节点的list for (Element e : listElement) {// 遍历所有一级子节点 decryptNodes(e);// 递归 } } /** * 从指定节点开始,递归遍历所有子节点 * * @author chenleixing */ public static void encryptNodes(Element node) throws Exception{ if(node.isTextOnly()){ node.setText(AESTool.linuxEncrypt(node.getTextTrim(), KEY)); } // 递归遍历当前节点所有的子节点 List<Element> listElement = node.elements();// 所有一级子节点的list for (Element e : listElement) {// 遍历所有一级子节点 encryptNodes(e);// 递归 } } public String getServiceUrl() { return serviceUrl; } public void setServiceUrl(String serviceUrl) { this.serviceUrl = serviceUrl; } /*public static void main(String[] args) throws Exception{ // System.out.println(AESTool.linuxEncrypt("验真", KEY)); String ssss = FileTool.readStringFromFile("d:/c.txt", "gbk"); // Document docReq = DocumentHelper.parseText(ssss); // encryptNodes(docReq.getRootElement()); // String decryptStr = docReq.asXML(); // System.out.println(decryptStr); // String str = FileTool.readStringFromFile("D:/a.txt", "UTF-8"); new TestTruePolicyImpl().invoke(ssss); }*/ }
时间: 2024-08-29 01:00:28