123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- package com.hgwebservice.util;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.soap.*;
- import java.io.StringReader;
- import java.io.ByteArrayOutputStream;
- import java.io.StringReader;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import javax.xml.namespace.QName;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.soap.MessageFactory;
- import javax.xml.soap.MimeHeaders;
- import javax.xml.soap.SOAPBody;
- import javax.xml.soap.SOAPBodyElement;
- import javax.xml.soap.SOAPConnection;
- import javax.xml.soap.SOAPConnectionFactory;
- import javax.xml.soap.SOAPEnvelope;
- import javax.xml.soap.SOAPHeader;
- import javax.xml.soap.SOAPMessage;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.NodeList;
- import org.xml.sax.InputSource;
- public class SoapUtil {
- private static final Logger log = LoggerFactory.getLogger(SoapUtil.class);
- private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyyMMddHHmmss");
- public SoapUtil() {
- }
- public static Document generatorWebserviceBySoap(String soapPrefix, String endPointURL, String action, String cdata) throws Exception {
- log.info("Soap util");
- Document doc = null;
- SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
- SOAPConnection soapConnection = soapConnectionFactory.createConnection();
- MessageFactory messageFactory = MessageFactory.newInstance();
- SOAPMessage soapRequest = messageFactory.createMessage();
- soapRequest.setProperty("javax.xml.soap.write-xml-declaration", "true");
- SOAPEnvelope reqEnvelope = soapRequest.getSOAPPart().getEnvelope();
- reqEnvelope.removeNamespaceDeclaration(reqEnvelope.getPrefix());
- reqEnvelope.addNamespaceDeclaration("soap", "http://www.w3.org/2003/05/soap-envelope");
- reqEnvelope.addNamespaceDeclaration("urn", "urn:hl7-org:v3");
- reqEnvelope.setPrefix(soapPrefix);
- SOAPHeader reqHeader = soapRequest.getSOAPHeader();
- reqHeader.setPrefix(soapPrefix);
- SOAPBody reqBody = soapRequest.getSOAPBody();
- reqBody.setPrefix(soapPrefix);
- SOAPBodyElement reqBodyAccountVerity = reqBody.addBodyElement(new QName("HIPMessageServer"));
- reqBodyAccountVerity.setPrefix("urn");
- reqBodyAccountVerity.addChildElement("action").addTextNode(action);
- reqBodyAccountVerity.addChildElement("message").addTextNode(cdata);
- soapRequest.saveChanges();
- ByteArrayOutputStream outReq = new ByteArrayOutputStream();
- ByteArrayOutputStream outRes = new ByteArrayOutputStream();
- soapRequest.writeTo(outReq);
- // log.info("SOAP Request:");
- // log.info(outReq.toString());
- SOAPMessage soapResponse = soapConnection.call(soapRequest, endPointURL);
- soapResponse.writeTo(outRes);
- soapConnection.close();
- // log.info("SOAP Response:");
- // log.info(outRes.toString());
- return ConstantUtil.convertStringToDocument(outRes.toString());
- }
- public static String getToken() throws Exception {
- Document document = generatorWebserviceBySoap("soap", "http://192.168.5.247:9280/cdxtHIP-service/webservices/IHIPMessageService", "XT00004", buildRequestXml());
- NodeList nodeList = document.getElementsByTagName("return");
- Element item = (Element)nodeList.item(0);
- String temValue = item.getFirstChild().getNodeValue();
- Document parse = ConstantUtil.convertStringToDocument(temValue);
- return parse != null ? parse.getElementsByTagName("accessToken").item(0).getFirstChild().getNodeValue() : null;
- }
- public static String buildRequestXml() {
- StringBuilder sb = new StringBuilder();
- sb.append("<![CDATA[");
- 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\"> ");
- sb.append("<id root=\"2.16.156.10011.0\" extension=\"040CD76AED0E400B9FD360387BCDE0EB\"/><creationTime value=\"");
- sb.append(SDF.format(new Date()));
- sb.append("\"/> ");
- sb.append("<interactionId root=\"2.16.840.1.113883.1.6\" extension=\"");
- sb.append("XT00004");
- sb.append("\"/>");
- 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=\"");
- sb.append("BIONIME");
- sb.append("\"/></device></sender>");
- sb.append("<controlActProcess classCode=\"CACT\" moodCode=\"EVN\"><code code=\"CUSTOM_TE00000\" codeSystem=\"2.16.840.1.113883.1.6\"/><subject typeCode=\"SUBJ\">\t");
- sb.append("<registrationRequest classCode=\"REG\" moodCode=\"RQO\">");
- sb.append("<username>");
- sb.append("BIONIME");
- sb.append("</username>");
- sb.append("<password>");
- sb.append("123456");
- sb.append("</password>");
- sb.append("<orgCode>");
- sb.append("630000000422");
- sb.append("</orgCode>");
- sb.append("</registrationRequest></subject></controlActProcess></CUSTOM_IN00000>");
- sb.append("]]>");
- return sb.toString();
- }
- public static Document GeneratorWebserviceBySoap(String soapPrefix, String endPointURL, String namespace, String methodName, String soapActionURI, String[] inputNames, String[] inputValues, String tagName) throws Exception {
- log.info("Use mawei soap util");
- Document doc = null;
- SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
- SOAPConnection soapConnection = soapConnectionFactory.createConnection();
- MessageFactory messageFactory = MessageFactory.newInstance("SOAP 1.2 Protocol");
- SOAPMessage soapRequest = messageFactory.createMessage();
- soapRequest.setProperty("javax.xml.soap.write-xml-declaration", "true");
- MimeHeaders reqHeaders = soapRequest.getMimeHeaders();
- reqHeaders.addHeader("SOAPAction", soapActionURI);
- SOAPEnvelope reqEnvelope = soapRequest.getSOAPPart().getEnvelope();
- reqEnvelope.removeNamespaceDeclaration(reqEnvelope.getPrefix());
- reqEnvelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
- reqEnvelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
- reqEnvelope.addNamespaceDeclaration(soapPrefix, "http://www.w3.org/2003/05/soap-envelope");
- reqEnvelope.setPrefix(soapPrefix);
- SOAPHeader reqHeader = soapRequest.getSOAPHeader();
- reqHeader.setPrefix(soapPrefix);
- SOAPBody reqBody = soapRequest.getSOAPBody();
- reqBody.setPrefix(soapPrefix);
- SOAPBodyElement reqBodyAccountVerity = reqBody.addBodyElement(new QName(namespace, methodName));
- reqBodyAccountVerity.addChildElement(inputNames[0]).addTextNode(inputValues[0]);
- reqBodyAccountVerity.addChildElement(inputNames[1]).addTextNode(inputValues[1]);
- soapRequest.saveChanges();
- if (soapRequest != null) {
- ByteArrayOutputStream out_req = new ByteArrayOutputStream();
- ByteArrayOutputStream out_res = new ByteArrayOutputStream();
- soapRequest.writeTo(out_req);
- // log.info("SOAP Request:");
- // log.info(new String(out_req.toByteArray()));
- SOAPMessage soapResponse = soapConnection.call(soapRequest, endPointURL);
- soapResponse.writeTo(out_res);
- soapConnection.close();
- // log.info("SOAP Response:");
- // log.info(new String(out_res.toByteArray()));
- SOAPBody resBody = soapResponse.getSOAPBody();
- String result = resBody.getElementsByTagName(tagName).item(0).getFirstChild().getNodeValue();
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- try {
- DocumentBuilder builder = factory.newDocumentBuilder();
- doc = builder.parse(new InputSource(new StringReader(result)));
- } catch (Exception var26) {
- var26.printStackTrace();
- }
- }
- return doc;
- }
- public static String GeneratorWebserviceBySoapResultUpload(String soapPrefix, String endPointURL, String namespace, String methodName, String soapActionURI, String[] inputNames, String[] inputValues, String tagName) throws Exception {
- log.info("Use mawei soap util");
- SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
- SOAPConnection soapConnection = soapConnectionFactory.createConnection();
- MessageFactory messageFactory = MessageFactory.newInstance("SOAP 1.2 Protocol");
- SOAPMessage soapRequest = messageFactory.createMessage();
- soapRequest.setProperty("javax.xml.soap.write-xml-declaration", "true");
- MimeHeaders reqHeaders = soapRequest.getMimeHeaders();
- reqHeaders.addHeader("SOAPAction", soapActionURI);
- SOAPEnvelope reqEnvelope = soapRequest.getSOAPPart().getEnvelope();
- reqEnvelope.removeNamespaceDeclaration(reqEnvelope.getPrefix());
- reqEnvelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
- reqEnvelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
- reqEnvelope.addNamespaceDeclaration(soapPrefix, "http://www.w3.org/2003/05/soap-envelope");
- reqEnvelope.setPrefix(soapPrefix);
- SOAPHeader reqHeader = soapRequest.getSOAPHeader();
- reqHeader.setPrefix(soapPrefix);
- SOAPBody reqBody = soapRequest.getSOAPBody();
- reqBody.setPrefix(soapPrefix);
- SOAPBodyElement reqBodyAccountVerity = reqBody.addBodyElement(new QName(namespace, methodName));
- reqBodyAccountVerity.addChildElement(inputNames[0]).addTextNode(inputValues[0]);
- reqBodyAccountVerity.addChildElement(inputNames[1]).addTextNode(inputValues[1]);
- soapRequest.saveChanges();
- String result = null;
- if (soapRequest != null) {
- ByteArrayOutputStream out_req = new ByteArrayOutputStream();
- ByteArrayOutputStream out_res = new ByteArrayOutputStream();
- soapRequest.writeTo(out_req);
- // log.info("SOAP Request:");
- // log.info(new String(out_req.toByteArray()));
- SOAPMessage soapResponse = soapConnection.call(soapRequest, endPointURL);
- soapResponse.writeTo(out_res);
- soapConnection.close();
- // log.info("SOAP Response:");
- // log.info(new String(out_res.toByteArray()));
- SOAPBody resBody = soapResponse.getSOAPBody();
- result = resBody.getElementsByTagName(tagName).item(0).getFirstChild().getNodeValue();
- }
- return result;
- }
- }
|