EsbHttp.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package com.yihu.hos.crawler.service;
  2. import com.fasterxml.jackson.databind.JsonNode;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.fasterxml.jackson.databind.node.ObjectNode;
  5. import com.yihu.hos.core.datatype.StringUtil;
  6. import com.yihu.hos.core.encrypt.MD5;
  7. import com.yihu.hos.core.http.HTTPResponse;
  8. import com.yihu.hos.core.http.HttpClientKit;
  9. import com.yihu.hos.core.log.Logger;
  10. import com.yihu.hos.core.log.LoggerFactory;
  11. import com.yihu.hos.crawler.model.adapter.AdapterDataSet;
  12. import com.yihu.hos.crawler.model.config.SysConfig;
  13. import com.yihu.hos.crawler.model.patient.Patient;
  14. import com.yihu.hos.crawler.model.transform.EhrCondition;
  15. import com.yihu.hos.web.framework.constant.SqlConstants;
  16. import org.json.JSONObject;
  17. import org.springframework.core.io.ClassPathResource;
  18. import org.springframework.core.io.Resource;
  19. import org.springframework.core.io.support.EncodedResource;
  20. import org.springframework.core.io.support.PropertiesLoaderUtils;
  21. import sun.misc.BASE64Encoder;
  22. import java.io.File;
  23. import java.util.*;
  24. /**
  25. * Created by hzp on 2016/3/10.
  26. */
  27. public class EsbHttp {
  28. public static String defaultHttpUrl;
  29. public static String clientId;
  30. public static String clientKey;
  31. public static String httpGateway;
  32. public static String defaultHttpUser;
  33. public static String defaultHttpPassword;
  34. public static String sslKeyStore;
  35. public static String sslPassword;
  36. private static Logger logger = LoggerFactory.getLogger(EsbHttp.class);
  37. static {
  38. //默认配置
  39. try {
  40. Resource resource = new ClassPathResource("config/http.properties");
  41. EncodedResource encRes = new EncodedResource(resource, "UTF-8");
  42. Properties props = PropertiesLoaderUtils.loadProperties(encRes);
  43. defaultHttpUrl = props.getProperty("httpUrl");
  44. defaultHttpUser = props.getProperty("httpUser");
  45. defaultHttpPassword = props.getProperty("httpPassword");
  46. clientId = props.getProperty("clientId");
  47. clientKey = props.getProperty("clientKey");
  48. sslKeyStore = props.getProperty("sslKeystore");
  49. sslPassword = props.getProperty("sslPassword");
  50. } catch (Exception e) {
  51. System.out.print(e.getMessage());
  52. }
  53. }
  54. /***************************** 用户接口 *********************************************/
  55. /**
  56. * 用户登录验证
  57. */
  58. public static HTTPResponse loginAction(String user, String password) throws Exception {
  59. String loginAction = defaultHttpUrl + "/authorizations/users/" + user;
  60. Map<String, String> header = new HashMap<>();
  61. String auth = new BASE64Encoder().encode((user + ":" + password).getBytes());
  62. header.put("Authorization", "Basic " + auth);
  63. return HttpClientKit.put(loginAction, null, header);
  64. }
  65. /*
  66. * 获取用户信息
  67. * */
  68. public static HTTPResponse getUserInfo(String user, String token) {
  69. String url = defaultHttpUrl + "/users/" + user;
  70. Map<String, String> params = new HashMap<>();
  71. params.put("token", token);
  72. params.put("user", user);
  73. return HttpClientKit.get(url, params);
  74. }
  75. /***************************** 应用接口 *********************************************/
  76. /**
  77. * 获取本机指纹
  78. *
  79. * @return
  80. */
  81. private static String GetFingerprint() {
  82. try {
  83. return UUID.randomUUID().toString();
  84. } catch (Exception e) {
  85. System.out.print(e.getMessage());
  86. return "";
  87. }
  88. }
  89. /**
  90. * 应用登录验证
  91. */
  92. public static String getToken() {
  93. try {
  94. String loginAction = defaultHttpUrl + "/authorizations/clients/" + clientId;
  95. Map<String, String> header = new HashMap<>();
  96. header.put("Authorization", "Basic " + clientKey);
  97. //本地指纹
  98. Map<String, String> params = new HashMap<>();
  99. params.put("info", "{\"fingerprint\": \"" + GetFingerprint() + "\"}");
  100. HTTPResponse response = HttpClientKit.put(loginAction, params, header);
  101. if (response != null && response.getStatusCode() == 200) {
  102. JSONObject obj = new JSONObject(response.getBody());
  103. //判断是否成功
  104. if (obj.has("token")) {
  105. return obj.getString("token");
  106. } else {
  107. logger.info("返回未包含token。");
  108. return null;
  109. }
  110. } else {
  111. String msg = "获取Token失败。";
  112. if (response != null) {
  113. msg += "(错误代码:" + response.getStatusCode() + ",错误信息:" + response.getBody() + ")";
  114. }
  115. logger.info(msg);
  116. return null;
  117. }
  118. } catch (Exception ex) {
  119. logger.info("获取Token失败," + ex.getMessage());
  120. return null;
  121. }
  122. }
  123. /**
  124. * 获取病人列表
  125. */
  126. public static String getPatientList(AdapterDataSet adapterDataSet, List<EhrCondition> queryParams) {
  127. try {
  128. ObjectMapper mapper = new ObjectMapper();
  129. ObjectNode paramsNode = mapper.createObjectNode();
  130. paramsNode.put("tableCode", adapterDataSet.getAdapterDataSetT().getStdDatasetCode());
  131. paramsNode.put("condition", mapper.writeValueAsString(queryParams));
  132. Map<String, String> formParams = new HashMap<>();
  133. formParams.put("api", "collectionData");
  134. String params = mapper.writeValueAsString(paramsNode);
  135. formParams.put("param", params);
  136. HTTPResponse response = HttpClientKit.post(httpGateway, formParams);
  137. if (response == null || response.getStatusCode() != 200) {
  138. logger.error("获取病人列表错误,请求HTTP错误,请检查配置或HTTP是否可用.");
  139. return "";
  140. }
  141. JsonNode responseNode = mapper.readValue(response.getBody(), JsonNode.class);
  142. String code = responseNode.path("responseCode").asText();
  143. if (StringUtil.isEmpty(code) || !code.equals("10000")) {
  144. logger.error("获取病人列表错误,请求HTTP错误,请检查集成平台网关是否可用.");
  145. return "";
  146. }
  147. String rootStr = responseNode.path("responseResult").asText();
  148. if ("".equals(rootStr)) {
  149. logger.error("获取病人列表错误,集成平台获取病人列表失败.");
  150. return "";
  151. }
  152. return rootStr;
  153. } catch (Exception e) {
  154. logger.error("获取病人列表失败!", e);
  155. return "";
  156. }
  157. }
  158. public static String getFecthData(Map<String, String> formParams) {
  159. try {
  160. HTTPResponse response = HttpClientKit.post(httpGateway, formParams);
  161. if (response == null || response.getStatusCode() != 200) {
  162. logger.info("获取病人数据错误,请求HTTP错误,请检查配置或HTTP是否可用.");
  163. return SqlConstants.EMPTY;
  164. }
  165. ObjectMapper mapper = new ObjectMapper();
  166. JsonNode responseNode = mapper.readValue(response.getBody(), JsonNode.class);
  167. String code = responseNode.path("responseCode").asText();
  168. if (StringUtil.isEmpty(code) || !code.equals("10000")) {
  169. logger.info("获取病人数据错误,请求HTTP错误,请检查集成平台网关是否可用.");
  170. return SqlConstants.EMPTY;
  171. }
  172. String rootStr = responseNode.path("responseResult").asText();
  173. if (SqlConstants.EMPTY.equals(rootStr)) {
  174. logger.info("获取病人数据错误,集成平台获取病人数据失败.");
  175. return SqlConstants.EMPTY;
  176. }
  177. return rootStr;
  178. } catch (Exception e) {
  179. logger.error("获取病人数据失败.", e);
  180. return SqlConstants.EMPTY;
  181. }
  182. }
  183. /**
  184. * 获取公钥
  185. */
  186. public static String getPublicKey(String orgCode) {
  187. try {
  188. String token = getToken();
  189. if (!StringUtil.isEmpty(SysConfig.getInstance().getPublicKeyMap().get(orgCode))) {
  190. return SysConfig.getInstance().getPublicKeyMap().get(orgCode);
  191. }
  192. Map<String, String> header = new HashMap<>();
  193. header.put("Authorization", "Basic " + clientKey);
  194. Map<String, String> paramMap = new HashMap<>();
  195. paramMap.put("org_code", orgCode);
  196. paramMap.put("token", token);
  197. String publicKeyMethod = defaultHttpUrl + "/organizations/" + orgCode + "/key";
  198. HTTPResponse response = HttpClientKit.get(publicKeyMethod, paramMap, header);
  199. if (response != null && response.getStatusCode() == 200) {
  200. JSONObject json = new JSONObject(response.getBody());
  201. if (json.has("publicKey")) {
  202. String publicKey = json.getString("publicKey");
  203. SysConfig.getInstance().getPublicKeyMap().put(orgCode, publicKey);
  204. return publicKey;
  205. } else {
  206. logger.info("获取公钥失败,返回未包含publicKey。");
  207. return null;
  208. }
  209. } else {
  210. String msg = "获取公钥失败。";
  211. if (response != null) {
  212. msg += "(错误代码:" + response.getStatusCode() + ",错误信息:" + response.getBody() + ")";
  213. }
  214. logger.info(msg);
  215. return null;
  216. }
  217. } catch (Exception e) {
  218. logger.info(e.getMessage());
  219. return null;
  220. }
  221. }
  222. /**
  223. * 获取健康云平台标准版本号
  224. */
  225. public static String getRemoteVersion(String orgCode) {
  226. try {
  227. String token = getToken();
  228. String versionMethod = defaultHttpUrl + "/adaptions/org_plan/version";
  229. Map<String, String> header = new HashMap<>();
  230. header.put("Authorization", "Basic " + clientKey);
  231. Map<String, String> params = new HashMap<>();
  232. params.put("org_code", orgCode);
  233. params.put("token", token);
  234. HTTPResponse response = HttpClientKit.get(versionMethod, params, header);
  235. if (response != null && response.getStatusCode() == 200) {
  236. return response.getBody();
  237. } else {
  238. String msg = "获取健康云平台标准版本号失败";
  239. if (response != null) {
  240. msg += "(错误代码:" + response.getStatusCode() + ",错误信息:" + response.getBody() + ")";
  241. }
  242. logger.info(msg);
  243. return null;
  244. }
  245. } catch (Exception e) {
  246. logger.info("获取远程版本号异常");
  247. logger.error(e.getCause().toString());
  248. return null;
  249. }
  250. }
  251. /**
  252. * 注册病人
  253. */
  254. public static Boolean register(Patient patient, String data, String token) {
  255. try {
  256. JSONObject json = new JSONObject(data);
  257. String colName = SysConfig.registerIdCardNo;
  258. Map<String, String> header = new HashMap<>();
  259. header.put("Authorization", "Basic " + clientKey);
  260. header.put("User-Agent", "client " + clientId);
  261. if (json != null && json.has("data")) {
  262. JSONObject p = (JSONObject) json.getJSONArray("data").get(0);
  263. if (!p.has(colName) || StringUtil.isEmpty(p.get(colName))) {
  264. logger.info("注册病人信息请求失败:身份证号码为空,patient_id=" + patient.getPatientId() + ", event_no=" + patient.getEventNo());
  265. return false;
  266. } else {
  267. String idCord = p.getString(colName);
  268. String registerMethod = defaultHttpUrl + "/patients/" + idCord;
  269. if (StringUtil.isEmpty(data)) {
  270. logger.info("注册病人信息请求失败:无具体病人信息,patient_id=" + patient.getPatientId() + ", event_no=" + patient.getEventNo());
  271. return false;
  272. }
  273. Map<String, String> paramMap = new HashMap<>();
  274. paramMap.put("demographic_id", idCord);
  275. paramMap.put("json", data);
  276. paramMap.put("token", token);
  277. HTTPResponse response = HttpClientKit.post(registerMethod, paramMap, header);
  278. if (response != null && response.getStatusCode() == 200) {
  279. logger.info("注册病人信息成功。patient_id:" + patient.getPatientId() + ", event_no:" + patient.getEventNo());
  280. return true;
  281. } else {
  282. String msg = "注册病人信息请求失败。patient_id:" + patient.getPatientId() + ", event_no:" + patient.getEventNo();
  283. if (response != null) {
  284. msg += "(错误代码:" + response.getStatusCode() + ",错误信息:" + response.getBody() + ")";
  285. }
  286. logger.info(msg);
  287. return false;
  288. }
  289. }
  290. } else {
  291. logger.info("注册病人信息请求失败:传入数据无效,patient_id=" + patient.getPatientId() + ", event_no=" + patient.getEventNo());
  292. return false;
  293. }
  294. } catch (Exception e) {
  295. logger.info("注册病人信息请求失败." + e.getMessage());
  296. return false;
  297. }
  298. }
  299. /**
  300. * 上传病人档案
  301. */
  302. public static boolean upload(Patient patient, File file, String encryptPwd, String token) {
  303. try {
  304. String uploadMethod = defaultHttpUrl + "/packages";
  305. String fileMd5 = MD5.getMd5ByFile(file);
  306. Map<String, String> formParams = new HashMap<>();
  307. formParams.put("md5", fileMd5);
  308. formParams.put("package_crypto", encryptPwd);
  309. formParams.put("org_code", patient.getOrgCode());
  310. formParams.put("token", token);
  311. Map<String, String> header = new HashMap<>();
  312. header.put("Authorization", "Basic " + clientKey);
  313. header.put("User-Agent", "client " + clientId);
  314. HTTPResponse response = HttpClientKit.postFile(uploadMethod, file.getAbsolutePath(), formParams, header);
  315. if (response != null && response.getStatusCode() == 200) {
  316. logger.info("上传病人档案成功,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  317. return true;
  318. } else {
  319. String msg = "上传病人档案请求失败,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo();
  320. if (response != null) {
  321. msg += "(错误代码:" + response.getStatusCode() + ",错误信息:" + response.getBody() + ")";
  322. }
  323. logger.info(msg);
  324. return false;
  325. }
  326. } catch (Exception e) {
  327. logger.info("上传病人档案异常,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  328. logger.error(e.getCause().toString());
  329. return false;
  330. }
  331. }
  332. /**
  333. * 下载标准包
  334. */
  335. public static HTTPResponse download(String remoteVersion, String orgCode) {
  336. try {
  337. String token = getToken();
  338. String downLoadMethod = defaultHttpUrl + "/adaptions/" + orgCode + "/source";
  339. Map<String, String> params = new HashMap<>();
  340. params.put("version_code", remoteVersion);
  341. params.put("org_code", orgCode);
  342. params.put("token", token);
  343. Map<String, String> header = new HashMap<>();
  344. header.put("Authorization", "Basic " + clientKey);
  345. HTTPResponse response = HttpClientKit.get(downLoadMethod, params, header);
  346. return response;
  347. } catch (Exception e) {
  348. logger.info("下载标准包异常:");
  349. logger.error(e.getCause().toString());
  350. return null;
  351. }
  352. }
  353. }