SysConfig.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. package com.yihu.ehr.config;
  2. import com.fasterxml.jackson.databind.JsonNode;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.yihu.ehr.model.PatientIdentity;
  5. import com.yihu.ehr.util.encrypt.RSA;
  6. import com.yihu.ehr.util.http.HOPClient;
  7. import com.yihu.ehr.util.http.Response;
  8. import com.yihu.ehr.util.log.LogUtil;
  9. import com.yihu.ehr.util.operator.ConfigureUtil;
  10. import com.yihu.ehr.util.operator.StringUtil;
  11. import org.dom4j.Document;
  12. import org.dom4j.DocumentException;
  13. import org.dom4j.Element;
  14. import org.dom4j.io.SAXReader;
  15. import java.io.InputStream;
  16. import java.util.HashMap;
  17. import java.util.List;
  18. public class SysConfig {
  19. public static final String HOS_RESOURCES_CONFIG = "/config/sys.config.xml";
  20. public static String publicKeyMethod;
  21. public static String tokenMethod;
  22. private static volatile SysConfig instance = null;
  23. private String platformUrl;
  24. private boolean debugMode;
  25. private String tempFile;
  26. private String publicKey;
  27. private HashMap<String, PatientIdentity> patientIdentityHashMap;
  28. private String trustStorePath;
  29. private String trustStorePwd;
  30. private String registerDataSet;
  31. private String registerIdCardNo;
  32. private String platformUserName;
  33. private String platformUserPass;
  34. private String platformAppId;
  35. private String platformAppSecret;
  36. private String accessToken;
  37. private String refreshToken;
  38. private String platformUser;
  39. private SysConfig() {
  40. patientIdentityHashMap = new HashMap<>();
  41. init();
  42. }
  43. public static SysConfig getInstance() {
  44. if (instance == null) {
  45. synchronized (SysConfig.class) {
  46. if (instance == null) {
  47. try {
  48. instance = new SysConfig();
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. }
  54. }
  55. return instance;
  56. }
  57. public void finalize() throws Throwable {
  58. }
  59. public Boolean isDebugMode() {
  60. return debugMode;
  61. }
  62. public String getTempFile() {
  63. return this.tempFile;
  64. }
  65. public void setTempFile(String tempFile) {
  66. this.tempFile = tempFile;
  67. }
  68. public void setTrustStorePath(String trustStorePath) {
  69. this.trustStorePath = trustStorePath;
  70. HOPCLientInit();
  71. }
  72. public String getPlatformUrl() {
  73. return platformUrl;
  74. }
  75. /**
  76. * @return String
  77. * @throws Exception
  78. * @modify 2015.09.15 airhead 修订日志信息不对问题
  79. */
  80. public String getPublicKey() throws Exception {
  81. if (publicKey != null) {
  82. return publicKey;
  83. }
  84. if (publicKeyMethod == null) {
  85. publicKeyMethod = ConfigureUtil.getProValue(ConfigureUtil.CRAWLER_PROPERTIES, "ha.url.security.publicKey.get");
  86. }
  87. HOPClient hopClient = new HOPClient(HOPClient.HTTPS);
  88. hopClient.setAppId(SysConfig.getInstance().getPlatformAppId());
  89. hopClient.setUrl(SysConfig.getInstance().getPlatformUrl());
  90. hopClient.setMethod(publicKeyMethod + platformUserName);
  91. Response response = hopClient.get();
  92. if (response == null) {
  93. LogUtil.fatal("获取公钥失败.");
  94. return null;
  95. }
  96. if (response.statusCode != 200) {
  97. LogUtil.fatal("获取公钥失败,错误代码:" + response.statusCode);
  98. return null;
  99. }
  100. ObjectMapper mapper = new ObjectMapper();
  101. JsonNode rootNode = mapper.readValue(response.body, JsonNode.class);
  102. String code = rootNode.path("code").asText();
  103. if ("ehr.invalid.user".equals(code)) {
  104. LogUtil.fatal("获取公钥失败:无效用户,请检查 Sys.config.xml文件中 <user>节点值是否正确");
  105. return null;
  106. }
  107. JsonNode publicNode = rootNode.path("result").path("public_key");
  108. if (publicNode.isMissingNode()) {
  109. LogUtil.fatal("获取公钥失败,返回数据格式不正确.");
  110. return null;
  111. }
  112. publicKey = publicNode.asText();
  113. return publicKey;
  114. }
  115. public PatientIdentity getPatientIdentity(String dataSetCode) {
  116. return patientIdentityHashMap.get(dataSetCode);
  117. }
  118. public HashMap<String, PatientIdentity> getPatientIdentityHashMap() {
  119. return patientIdentityHashMap;
  120. }
  121. public String getRegisterDataSet() {
  122. return registerDataSet;
  123. }
  124. public String getRegisterIdCardNo() {
  125. return registerIdCardNo;
  126. }
  127. /**
  128. * @return String
  129. * @modify 2015.09.15 airhead 修订日志信息不对问题
  130. */
  131. public String getAccessToken() {
  132. try {
  133. if (tokenMethod == null) {
  134. tokenMethod = ConfigureUtil.getProValue(ConfigureUtil.CRAWLER_PROPERTIES, "ha.url.security.token.get");
  135. }
  136. HOPClient hopClient = new HOPClient(HOPClient.HTTPS);
  137. hopClient.setAppId(SysConfig.getInstance().getPlatformAppId());
  138. hopClient.setUrl(SysConfig.getInstance().getPlatformUrl());
  139. hopClient.setMethod(tokenMethod);
  140. // hopClient.setParam("grant_type", "password");
  141. hopClient.setParam("user_name", getPlatformUserName());
  142. hopClient.setParam("app_secret", SysConfig.getInstance().getPlatformAppSecret());
  143. String publicKey = getPublicKey();
  144. String encryptPwd = RSA.encrypt(platformUserPass, RSA.genPublicKey(publicKey));
  145. hopClient.setParam("rsa_pw", encryptPwd);
  146. Response response = hopClient.get();
  147. if (response == null) {
  148. LogUtil.fatal("获取令牌失败.");
  149. return null;
  150. }
  151. if (response.statusCode != 200) {
  152. LogUtil.fatal("获取令牌失败,错误代码:" + response.statusCode);
  153. return null;
  154. }
  155. ObjectMapper mapper = new ObjectMapper();
  156. JsonNode rootNode = mapper.readValue(response.body, JsonNode.class);
  157. String code = rootNode.path("code").asText();
  158. if (code.equals("ehr.invalid.username.or.pwd")) {
  159. LogUtil.fatal("无效用户名或密码,请检查Sys.config.xml文件中 <user>节点值是否正确");
  160. return null;
  161. }
  162. if (code.equals("ehr.app.register.invalid")) {
  163. LogUtil.fatal("该app应用在系统中不存在,请检查Sys.config.xml文件中 <app>节点值是否正确");
  164. return null;
  165. }
  166. if (code.equals("ehr.security.token.expired") && !StringUtil.isEmpty(refreshToken)) {
  167. hopClient.reset();
  168. hopClient.setMethod(tokenMethod);
  169. hopClient.setAppId(SysConfig.getInstance().getPlatformAppId());
  170. hopClient.setParam("grant_type", "password");
  171. hopClient.setParam("refresh_token", refreshToken);
  172. hopClient.setParam("user_id", getPlatformUserName());
  173. response = hopClient.get();
  174. if (response == null) {
  175. LogUtil.fatal("刷新令牌失败.");
  176. return null;
  177. }
  178. if (response.statusCode != 200) {
  179. LogUtil.fatal("刷新令牌失败,错误代码:" + response.statusCode);
  180. return null;
  181. }
  182. rootNode = mapper.readValue(response.body, JsonNode.class);
  183. code = rootNode.path("code").asText();
  184. if (StringUtil.isEmpty(code) || !code.equals("0")) {
  185. LogUtil.fatal("刷新令牌失败,错误代码:" + code);
  186. return null;
  187. }
  188. }
  189. accessToken = rootNode.path("result").get("access_token").asText();
  190. refreshToken = rootNode.path("result").path("refresh_token").asText();
  191. return accessToken;
  192. } catch (Exception e) {
  193. LogUtil.fatal(e.getMessage());
  194. }
  195. return null;
  196. }
  197. public String getPlatformUser() {
  198. return platformUser;
  199. }
  200. public String getPlatformUserName() {
  201. return platformUserName;
  202. }
  203. public String getPlatformAppId() {
  204. return platformAppId;
  205. }
  206. private String getPlatformAppSecret() {
  207. return platformAppSecret;
  208. }
  209. private Document getDocument() throws DocumentException {
  210. SAXReader reader = new SAXReader();
  211. Document document = null;
  212. try {
  213. InputStream inputStream = SysConfig.class.getResourceAsStream(HOS_RESOURCES_CONFIG);
  214. document = reader.read(inputStream);
  215. return document;
  216. } catch (DocumentException de) {
  217. LogUtil.fatal("读取classpath下的xml文档路径发生异常");
  218. return null;
  219. }
  220. }
  221. private void init() {
  222. try {
  223. Document document = this.getDocument();
  224. Element rootElement = null;
  225. if (document != null) {
  226. rootElement = document.getRootElement();
  227. }
  228. if (rootElement == null) {
  229. return;
  230. }
  231. this.initPlatForm(rootElement);
  232. this.initCrawler(rootElement);
  233. this.initEventNo(rootElement);
  234. String debug = rootElement.elementTextTrim("debug");
  235. if (StringUtil.isEmpty(debug) || debug.equals("false")) {
  236. debugMode = false;
  237. }
  238. } catch (Exception e) {
  239. e.printStackTrace();
  240. LogUtil.fatal(e.getMessage());
  241. }
  242. }
  243. private void initPlatForm(Element rootElement) throws Exception {
  244. Element platform = rootElement.element("platform");
  245. String platformUrl = platform.elementTextTrim("url");
  246. if (!StringUtil.isEmpty(platformUrl)) {
  247. this.platformUrl = platformUrl;
  248. }
  249. Element trustStore = platform.element("trust_store");
  250. trustStorePwd = trustStore.attributeValue("pwd");
  251. Element platformUserElem = platform.element("user");
  252. platformUserName = platformUserElem.getTextTrim();
  253. platformUserPass = platformUserElem.attributeValue("pwd");
  254. Element platformAppElem = platform.element("app");
  255. platformAppId = platformAppElem.getTextTrim();
  256. platformAppSecret = platformAppElem.attributeValue("secret");
  257. }
  258. private void initCrawler(Element rootElement) {
  259. List queueDataSets = rootElement.element("patient_queue").elements("dataset");
  260. for (Object obj : queueDataSets) {
  261. if (obj instanceof Element) {
  262. Element element = (Element) obj;
  263. String dataSetCode = element.attributeValue("code");
  264. String eventNo = element.elementTextTrim("event_no");
  265. String refTime = element.elementTextTrim("ref_time");
  266. PatientIdentity patientIdentity = new PatientIdentity(eventNo, refTime);
  267. patientIdentityHashMap.put(dataSetCode, patientIdentity);
  268. }
  269. }
  270. Element registerDataSet = rootElement.element("register").element("dataset");
  271. this.registerDataSet = registerDataSet.attributeValue("code");
  272. this.registerIdCardNo = registerDataSet.elementTextTrim("id_card");
  273. }
  274. private void initEventNo(Element rootElement) {
  275. List eventItems = rootElement.element("event_no").elements("item");
  276. for (Object obj : eventItems) {
  277. if (obj instanceof Element) {
  278. Element element = (Element) obj;
  279. String eventNoCode = element.getTextTrim();
  280. PatientIdentity.addEventNoCode(eventNoCode);
  281. }
  282. }
  283. }
  284. private void HOPCLientInit () {
  285. try {
  286. HOPClient.initHttps(trustStorePath, trustStorePwd);
  287. } catch (Exception e) {
  288. LogUtil.error(e.getMessage());
  289. }
  290. }
  291. }//end SysConfig