package com.yihu.ehr.config; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.yihu.ehr.model.PatientIdentity; import com.yihu.ehr.util.encrypt.RSA; import com.yihu.ehr.util.http.HOPClient; import com.yihu.ehr.util.http.Response; import com.yihu.ehr.util.log.LogUtil; import com.yihu.ehr.util.operator.ConfigureUtil; import com.yihu.ehr.util.operator.StringUtil; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.io.InputStream; import java.util.HashMap; import java.util.List; public class SysConfig { public static final String HOS_RESOURCES_CONFIG = "/config/sys.config.xml"; public static String publicKeyMethod; public static String tokenMethod; private static volatile SysConfig instance = null; private String platformUrl; private boolean debugMode; private String tempFile; private String publicKey; private HashMap patientIdentityHashMap; private String trustStorePath; private String trustStorePwd; private String registerDataSet; private String registerIdCardNo; private String platformUserName; private String platformUserPass; private String platformAppId; private String platformAppSecret; private String accessToken; private String refreshToken; private String platformUser; private SysConfig() { patientIdentityHashMap = new HashMap<>(); init(); } public static SysConfig getInstance() { if (instance == null) { synchronized (SysConfig.class) { if (instance == null) { try { instance = new SysConfig(); } catch (Exception e) { e.printStackTrace(); } } } } return instance; } public void finalize() throws Throwable { } public Boolean isDebugMode() { return debugMode; } public String getTempFile() { return this.tempFile; } public void setTempFile(String tempFile) { this.tempFile = tempFile; } public void setTrustStorePath(String trustStorePath) { this.trustStorePath = trustStorePath; HOPCLientInit(); } public String getPlatformUrl() { return platformUrl; } /** * @return String * @throws Exception * @modify 2015.09.15 airhead 修订日志信息不对问题 */ public String getPublicKey() throws Exception { if (publicKey != null) { return publicKey; } if (publicKeyMethod == null) { publicKeyMethod = ConfigureUtil.getProValue(ConfigureUtil.CRAWLER_PROPERTIES, "ha.url.security.publicKey.get"); } HOPClient hopClient = new HOPClient(HOPClient.HTTPS); hopClient.setAppId(SysConfig.getInstance().getPlatformAppId()); hopClient.setUrl(SysConfig.getInstance().getPlatformUrl()); hopClient.setMethod(publicKeyMethod + platformUserName); Response response = hopClient.get(); if (response == null) { LogUtil.fatal("获取公钥失败."); return null; } if (response.statusCode != 200) { LogUtil.fatal("获取公钥失败,错误代码:" + response.statusCode); return null; } ObjectMapper mapper = new ObjectMapper(); JsonNode rootNode = mapper.readValue(response.body, JsonNode.class); String code = rootNode.path("code").asText(); if ("ehr.invalid.user".equals(code)) { LogUtil.fatal("获取公钥失败:无效用户,请检查 Sys.config.xml文件中 节点值是否正确"); return null; } JsonNode publicNode = rootNode.path("result").path("public_key"); if (publicNode.isMissingNode()) { LogUtil.fatal("获取公钥失败,返回数据格式不正确."); return null; } publicKey = publicNode.asText(); return publicKey; } public PatientIdentity getPatientIdentity(String dataSetCode) { return patientIdentityHashMap.get(dataSetCode); } public HashMap getPatientIdentityHashMap() { return patientIdentityHashMap; } public String getRegisterDataSet() { return registerDataSet; } public String getRegisterIdCardNo() { return registerIdCardNo; } /** * @return String * @modify 2015.09.15 airhead 修订日志信息不对问题 */ public String getAccessToken() { try { if (tokenMethod == null) { tokenMethod = ConfigureUtil.getProValue(ConfigureUtil.CRAWLER_PROPERTIES, "ha.url.security.token.get"); } HOPClient hopClient = new HOPClient(HOPClient.HTTPS); hopClient.setAppId(SysConfig.getInstance().getPlatformAppId()); hopClient.setUrl(SysConfig.getInstance().getPlatformUrl()); hopClient.setMethod(tokenMethod); // hopClient.setParam("grant_type", "password"); hopClient.setParam("user_name", getPlatformUserName()); hopClient.setParam("app_secret", SysConfig.getInstance().getPlatformAppSecret()); String publicKey = getPublicKey(); String encryptPwd = RSA.encrypt(platformUserPass, RSA.genPublicKey(publicKey)); hopClient.setParam("rsa_pw", encryptPwd); Response response = hopClient.get(); if (response == null) { LogUtil.fatal("获取令牌失败."); return null; } if (response.statusCode != 200) { LogUtil.fatal("获取令牌失败,错误代码:" + response.statusCode); return null; } ObjectMapper mapper = new ObjectMapper(); JsonNode rootNode = mapper.readValue(response.body, JsonNode.class); String code = rootNode.path("code").asText(); if (code.equals("ehr.invalid.username.or.pwd")) { LogUtil.fatal("无效用户名或密码,请检查Sys.config.xml文件中 节点值是否正确"); return null; } if (code.equals("ehr.app.register.invalid")) { LogUtil.fatal("该app应用在系统中不存在,请检查Sys.config.xml文件中 节点值是否正确"); return null; } if (code.equals("ehr.security.token.expired") && !StringUtil.isEmpty(refreshToken)) { hopClient.reset(); hopClient.setMethod(tokenMethod); hopClient.setAppId(SysConfig.getInstance().getPlatformAppId()); hopClient.setParam("grant_type", "password"); hopClient.setParam("refresh_token", refreshToken); hopClient.setParam("user_id", getPlatformUserName()); response = hopClient.get(); if (response == null) { LogUtil.fatal("刷新令牌失败."); return null; } if (response.statusCode != 200) { LogUtil.fatal("刷新令牌失败,错误代码:" + response.statusCode); return null; } rootNode = mapper.readValue(response.body, JsonNode.class); code = rootNode.path("code").asText(); if (StringUtil.isEmpty(code) || !code.equals("0")) { LogUtil.fatal("刷新令牌失败,错误代码:" + code); return null; } } accessToken = rootNode.path("result").get("access_token").asText(); refreshToken = rootNode.path("result").path("refresh_token").asText(); return accessToken; } catch (Exception e) { LogUtil.fatal(e.getMessage()); } return null; } public String getPlatformUser() { return platformUser; } public String getPlatformUserName() { return platformUserName; } public String getPlatformAppId() { return platformAppId; } private String getPlatformAppSecret() { return platformAppSecret; } private Document getDocument() throws DocumentException { SAXReader reader = new SAXReader(); Document document = null; try { InputStream inputStream = SysConfig.class.getResourceAsStream(HOS_RESOURCES_CONFIG); document = reader.read(inputStream); return document; } catch (DocumentException de) { LogUtil.fatal("读取classpath下的xml文档路径发生异常"); return null; } } private void init() { try { Document document = this.getDocument(); Element rootElement = null; if (document != null) { rootElement = document.getRootElement(); } if (rootElement == null) { return; } this.initPlatForm(rootElement); this.initCrawler(rootElement); this.initEventNo(rootElement); String debug = rootElement.elementTextTrim("debug"); if (StringUtil.isEmpty(debug) || debug.equals("false")) { debugMode = false; } } catch (Exception e) { e.printStackTrace(); LogUtil.fatal(e.getMessage()); } } private void initPlatForm(Element rootElement) throws Exception { Element platform = rootElement.element("platform"); String platformUrl = platform.elementTextTrim("url"); if (!StringUtil.isEmpty(platformUrl)) { this.platformUrl = platformUrl; } Element trustStore = platform.element("trust_store"); trustStorePwd = trustStore.attributeValue("pwd"); Element platformUserElem = platform.element("user"); platformUserName = platformUserElem.getTextTrim(); platformUserPass = platformUserElem.attributeValue("pwd"); Element platformAppElem = platform.element("app"); platformAppId = platformAppElem.getTextTrim(); platformAppSecret = platformAppElem.attributeValue("secret"); } private void initCrawler(Element rootElement) { List queueDataSets = rootElement.element("patient_queue").elements("dataset"); for (Object obj : queueDataSets) { if (obj instanceof Element) { Element element = (Element) obj; String dataSetCode = element.attributeValue("code"); String eventNo = element.elementTextTrim("event_no"); String refTime = element.elementTextTrim("ref_time"); PatientIdentity patientIdentity = new PatientIdentity(eventNo, refTime); patientIdentityHashMap.put(dataSetCode, patientIdentity); } } Element registerDataSet = rootElement.element("register").element("dataset"); this.registerDataSet = registerDataSet.attributeValue("code"); this.registerIdCardNo = registerDataSet.elementTextTrim("id_card"); } private void initEventNo(Element rootElement) { List eventItems = rootElement.element("event_no").elements("item"); for (Object obj : eventItems) { if (obj instanceof Element) { Element element = (Element) obj; String eventNoCode = element.getTextTrim(); PatientIdentity.addEventNoCode(eventNoCode); } } } private void HOPCLientInit () { try { HOPClient.initHttps(trustStorePath, trustStorePwd); } catch (Exception e) { LogUtil.error(e.getMessage()); } } }//end SysConfig