SoapUtil.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package com.hgwebservice.util;
  2. import javax.xml.parsers.DocumentBuilder;
  3. import javax.xml.parsers.DocumentBuilderFactory;
  4. import javax.xml.soap.*;
  5. import java.io.StringReader;
  6. import java.io.ByteArrayOutputStream;
  7. import java.io.StringReader;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10. import javax.xml.namespace.QName;
  11. import javax.xml.parsers.DocumentBuilder;
  12. import javax.xml.parsers.DocumentBuilderFactory;
  13. import javax.xml.soap.MessageFactory;
  14. import javax.xml.soap.MimeHeaders;
  15. import javax.xml.soap.SOAPBody;
  16. import javax.xml.soap.SOAPBodyElement;
  17. import javax.xml.soap.SOAPConnection;
  18. import javax.xml.soap.SOAPConnectionFactory;
  19. import javax.xml.soap.SOAPEnvelope;
  20. import javax.xml.soap.SOAPHeader;
  21. import javax.xml.soap.SOAPMessage;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import org.w3c.dom.Document;
  25. import org.w3c.dom.Element;
  26. import org.w3c.dom.NodeList;
  27. import org.xml.sax.InputSource;
  28. public class SoapUtil {
  29. private static final Logger log = LoggerFactory.getLogger(SoapUtil.class);
  30. private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyyMMddHHmmss");
  31. public SoapUtil() {
  32. }
  33. public static Document generatorWebserviceBySoap(String soapPrefix, String endPointURL, String action, String cdata) throws Exception {
  34. log.info("Soap util");
  35. Document doc = null;
  36. SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
  37. SOAPConnection soapConnection = soapConnectionFactory.createConnection();
  38. MessageFactory messageFactory = MessageFactory.newInstance();
  39. SOAPMessage soapRequest = messageFactory.createMessage();
  40. soapRequest.setProperty("javax.xml.soap.write-xml-declaration", "true");
  41. SOAPEnvelope reqEnvelope = soapRequest.getSOAPPart().getEnvelope();
  42. reqEnvelope.removeNamespaceDeclaration(reqEnvelope.getPrefix());
  43. reqEnvelope.addNamespaceDeclaration("soap", "http://www.w3.org/2003/05/soap-envelope");
  44. reqEnvelope.addNamespaceDeclaration("urn", "urn:hl7-org:v3");
  45. reqEnvelope.setPrefix(soapPrefix);
  46. SOAPHeader reqHeader = soapRequest.getSOAPHeader();
  47. reqHeader.setPrefix(soapPrefix);
  48. SOAPBody reqBody = soapRequest.getSOAPBody();
  49. reqBody.setPrefix(soapPrefix);
  50. SOAPBodyElement reqBodyAccountVerity = reqBody.addBodyElement(new QName("HIPMessageServer"));
  51. reqBodyAccountVerity.setPrefix("urn");
  52. reqBodyAccountVerity.addChildElement("action").addTextNode(action);
  53. reqBodyAccountVerity.addChildElement("message").addTextNode(cdata);
  54. soapRequest.saveChanges();
  55. ByteArrayOutputStream outReq = new ByteArrayOutputStream();
  56. ByteArrayOutputStream outRes = new ByteArrayOutputStream();
  57. soapRequest.writeTo(outReq);
  58. // log.info("SOAP Request:");
  59. // log.info(outReq.toString());
  60. SOAPMessage soapResponse = soapConnection.call(soapRequest, endPointURL);
  61. soapResponse.writeTo(outRes);
  62. soapConnection.close();
  63. // log.info("SOAP Response:");
  64. // log.info(outRes.toString());
  65. return ConstantUtil.convertStringToDocument(outRes.toString());
  66. }
  67. public static String getToken() throws Exception {
  68. Document document = generatorWebserviceBySoap("soap", "http://192.168.5.247:9280/cdxtHIP-service/webservices/IHIPMessageService", "XT00004", buildRequestXml());
  69. NodeList nodeList = document.getElementsByTagName("return");
  70. Element item = (Element)nodeList.item(0);
  71. String temValue = item.getFirstChild().getNodeValue();
  72. Document parse = ConstantUtil.convertStringToDocument(temValue);
  73. return parse != null ? parse.getElementsByTagName("accessToken").item(0).getFirstChild().getNodeValue() : null;
  74. }
  75. public static String buildRequestXml() {
  76. StringBuilder sb = new StringBuilder();
  77. sb.append("<![CDATA[");
  78. sb.append("<CUSTOM_IN00000 xmlns=\"urn:hl7-org:v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ITSVersion=\"XML_1.0\" xsi:schemaLocation=\"urn:hl7-org:v3 ../multicacheschemas/CUSTOM_IN00000.xsd\"> ");
  79. sb.append("<id root=\"2.16.156.10011.0\" extension=\"040CD76AED0E400B9FD360387BCDE0EB\"/><creationTime value=\"");
  80. sb.append(SDF.format(new Date()));
  81. sb.append("\"/> ");
  82. sb.append("<interactionId root=\"2.16.840.1.113883.1.6\" extension=\"");
  83. sb.append("XT00004");
  84. sb.append("\"/>");
  85. sb.append("<processingCode code=\"P\"/><processingModeCode code=\"I\"/><acceptAckCode code=\"AL\"/><receiver typeCode=\"RCV\"><telecom/><device classCode=\"DEV\" determinerCode=\"INSTANCE\"><id root=\"12.16.156.10011.0.1.1\" extension=\"HIP\"/></device></receiver><sender typeCode=\"SND\"><telecom/><device classCode=\"DEV\" determinerCode=\"INSTANCE\"><id root=\"2.16.156.10011.0.1.2\" extension=\"");
  86. sb.append("BIONIME");
  87. sb.append("\"/></device></sender>");
  88. sb.append("<controlActProcess classCode=\"CACT\" moodCode=\"EVN\"><code code=\"CUSTOM_TE00000\" codeSystem=\"2.16.840.1.113883.1.6\"/><subject typeCode=\"SUBJ\">\t");
  89. sb.append("<registrationRequest classCode=\"REG\" moodCode=\"RQO\">");
  90. sb.append("<username>");
  91. sb.append("BIONIME");
  92. sb.append("</username>");
  93. sb.append("<password>");
  94. sb.append("123456");
  95. sb.append("</password>");
  96. sb.append("<orgCode>");
  97. sb.append("630000000422");
  98. sb.append("</orgCode>");
  99. sb.append("</registrationRequest></subject></controlActProcess></CUSTOM_IN00000>");
  100. sb.append("]]>");
  101. return sb.toString();
  102. }
  103. public static Document GeneratorWebserviceBySoap(String soapPrefix, String endPointURL, String namespace, String methodName, String soapActionURI, String[] inputNames, String[] inputValues, String tagName) throws Exception {
  104. log.info("Use mawei soap util");
  105. Document doc = null;
  106. SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
  107. SOAPConnection soapConnection = soapConnectionFactory.createConnection();
  108. MessageFactory messageFactory = MessageFactory.newInstance("SOAP 1.2 Protocol");
  109. SOAPMessage soapRequest = messageFactory.createMessage();
  110. soapRequest.setProperty("javax.xml.soap.write-xml-declaration", "true");
  111. MimeHeaders reqHeaders = soapRequest.getMimeHeaders();
  112. reqHeaders.addHeader("SOAPAction", soapActionURI);
  113. SOAPEnvelope reqEnvelope = soapRequest.getSOAPPart().getEnvelope();
  114. reqEnvelope.removeNamespaceDeclaration(reqEnvelope.getPrefix());
  115. reqEnvelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
  116. reqEnvelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
  117. reqEnvelope.addNamespaceDeclaration(soapPrefix, "http://www.w3.org/2003/05/soap-envelope");
  118. reqEnvelope.setPrefix(soapPrefix);
  119. SOAPHeader reqHeader = soapRequest.getSOAPHeader();
  120. reqHeader.setPrefix(soapPrefix);
  121. SOAPBody reqBody = soapRequest.getSOAPBody();
  122. reqBody.setPrefix(soapPrefix);
  123. SOAPBodyElement reqBodyAccountVerity = reqBody.addBodyElement(new QName(namespace, methodName));
  124. reqBodyAccountVerity.addChildElement(inputNames[0]).addTextNode(inputValues[0]);
  125. reqBodyAccountVerity.addChildElement(inputNames[1]).addTextNode(inputValues[1]);
  126. soapRequest.saveChanges();
  127. if (soapRequest != null) {
  128. ByteArrayOutputStream out_req = new ByteArrayOutputStream();
  129. ByteArrayOutputStream out_res = new ByteArrayOutputStream();
  130. soapRequest.writeTo(out_req);
  131. // log.info("SOAP Request:");
  132. // log.info(new String(out_req.toByteArray()));
  133. SOAPMessage soapResponse = soapConnection.call(soapRequest, endPointURL);
  134. soapResponse.writeTo(out_res);
  135. soapConnection.close();
  136. // log.info("SOAP Response:");
  137. // log.info(new String(out_res.toByteArray()));
  138. SOAPBody resBody = soapResponse.getSOAPBody();
  139. String result = resBody.getElementsByTagName(tagName).item(0).getFirstChild().getNodeValue();
  140. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  141. try {
  142. DocumentBuilder builder = factory.newDocumentBuilder();
  143. doc = builder.parse(new InputSource(new StringReader(result)));
  144. } catch (Exception var26) {
  145. var26.printStackTrace();
  146. }
  147. }
  148. return doc;
  149. }
  150. public static String GeneratorWebserviceBySoapResultUpload(String soapPrefix, String endPointURL, String namespace, String methodName, String soapActionURI, String[] inputNames, String[] inputValues, String tagName) throws Exception {
  151. log.info("Use mawei soap util");
  152. SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
  153. SOAPConnection soapConnection = soapConnectionFactory.createConnection();
  154. MessageFactory messageFactory = MessageFactory.newInstance("SOAP 1.2 Protocol");
  155. SOAPMessage soapRequest = messageFactory.createMessage();
  156. soapRequest.setProperty("javax.xml.soap.write-xml-declaration", "true");
  157. MimeHeaders reqHeaders = soapRequest.getMimeHeaders();
  158. reqHeaders.addHeader("SOAPAction", soapActionURI);
  159. SOAPEnvelope reqEnvelope = soapRequest.getSOAPPart().getEnvelope();
  160. reqEnvelope.removeNamespaceDeclaration(reqEnvelope.getPrefix());
  161. reqEnvelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
  162. reqEnvelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
  163. reqEnvelope.addNamespaceDeclaration(soapPrefix, "http://www.w3.org/2003/05/soap-envelope");
  164. reqEnvelope.setPrefix(soapPrefix);
  165. SOAPHeader reqHeader = soapRequest.getSOAPHeader();
  166. reqHeader.setPrefix(soapPrefix);
  167. SOAPBody reqBody = soapRequest.getSOAPBody();
  168. reqBody.setPrefix(soapPrefix);
  169. SOAPBodyElement reqBodyAccountVerity = reqBody.addBodyElement(new QName(namespace, methodName));
  170. reqBodyAccountVerity.addChildElement(inputNames[0]).addTextNode(inputValues[0]);
  171. reqBodyAccountVerity.addChildElement(inputNames[1]).addTextNode(inputValues[1]);
  172. soapRequest.saveChanges();
  173. String result = null;
  174. if (soapRequest != null) {
  175. ByteArrayOutputStream out_req = new ByteArrayOutputStream();
  176. ByteArrayOutputStream out_res = new ByteArrayOutputStream();
  177. soapRequest.writeTo(out_req);
  178. // log.info("SOAP Request:");
  179. // log.info(new String(out_req.toByteArray()));
  180. SOAPMessage soapResponse = soapConnection.call(soapRequest, endPointURL);
  181. soapResponse.writeTo(out_res);
  182. soapConnection.close();
  183. // log.info("SOAP Response:");
  184. // log.info(new String(out_res.toByteArray()));
  185. SOAPBody resBody = soapResponse.getSOAPBody();
  186. result = resBody.getElementsByTagName(tagName).item(0).getFirstChild().getNodeValue();
  187. }
  188. return result;
  189. }
  190. }