123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- package com.yihu.hos.crawler.service;
- import com.fasterxml.jackson.databind.JsonNode;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.databind.node.ObjectNode;
- import com.yihu.hos.core.datatype.StringUtil;
- import com.yihu.hos.core.encrypt.MD5;
- import com.yihu.hos.core.http.HTTPResponse;
- import com.yihu.hos.core.http.HttpClientKit;
- import com.yihu.hos.core.log.Logger;
- import com.yihu.hos.core.log.LoggerFactory;
- import com.yihu.hos.crawler.model.adapter.AdapterDataSet;
- import com.yihu.hos.crawler.model.config.SysConfig;
- import com.yihu.hos.crawler.model.patient.Patient;
- import com.yihu.hos.crawler.model.transform.EhrCondition;
- import com.yihu.hos.web.framework.constant.SqlConstants;
- import org.json.JSONObject;
- import org.springframework.core.io.ClassPathResource;
- import org.springframework.core.io.Resource;
- import org.springframework.core.io.support.EncodedResource;
- import org.springframework.core.io.support.PropertiesLoaderUtils;
- import sun.misc.BASE64Encoder;
- import java.io.File;
- import java.util.*;
- /**
- * Created by hzp on 2016/3/10.
- */
- public class EsbHttp {
- public static String defaultHttpUrl;
- public static String clientId;
- public static String clientKey;
- public static String httpGateway;
- public static String defaultHttpUser;
- public static String defaultHttpPassword;
- public static String sslKeyStore;
- public static String sslPassword;
- private static Logger logger = LoggerFactory.getLogger(EsbHttp.class);
- static {
- //默认配置
- try {
- Resource resource = new ClassPathResource("config/http.properties");
- EncodedResource encRes = new EncodedResource(resource, "UTF-8");
- Properties props = PropertiesLoaderUtils.loadProperties(encRes);
- defaultHttpUrl = props.getProperty("httpUrl");
- defaultHttpUser = props.getProperty("httpUser");
- defaultHttpPassword = props.getProperty("httpPassword");
- clientId = props.getProperty("clientId");
- clientKey = props.getProperty("clientKey");
- sslKeyStore = props.getProperty("sslKeystore");
- sslPassword = props.getProperty("sslPassword");
- } catch (Exception e) {
- System.out.print(e.getMessage());
- }
- }
- /***************************** 用户接口 *********************************************/
- /**
- * 用户登录验证
- */
- public static HTTPResponse loginAction(String user, String password) throws Exception {
- String loginAction = defaultHttpUrl + "/authorizations/users/" + user;
- Map<String, String> header = new HashMap<>();
- String auth = new BASE64Encoder().encode((user + ":" + password).getBytes());
- header.put("Authorization", "Basic " + auth);
- return HttpClientKit.put(loginAction, null, header);
- }
- /*
- * 获取用户信息
- * */
- public static HTTPResponse getUserInfo(String user, String token) {
- String url = defaultHttpUrl + "/users/" + user;
- Map<String, String> params = new HashMap<>();
- params.put("token", token);
- params.put("user", user);
- return HttpClientKit.get(url, params);
- }
- /***************************** 应用接口 *********************************************/
- /**
- * 获取本机指纹
- *
- * @return
- */
- private static String GetFingerprint() {
- try {
- return UUID.randomUUID().toString();
- } catch (Exception e) {
- System.out.print(e.getMessage());
- return "";
- }
- }
- /**
- * 应用登录验证
- */
- public static String getToken() {
- try {
- String loginAction = defaultHttpUrl + "/authorizations/clients/" + clientId;
- Map<String, String> header = new HashMap<>();
- header.put("Authorization", "Basic " + clientKey);
- //本地指纹
- Map<String, String> params = new HashMap<>();
- params.put("info", "{\"fingerprint\": \"" + GetFingerprint() + "\"}");
- HTTPResponse response = HttpClientKit.put(loginAction, params, header);
- if (response != null && response.getStatusCode() == 200) {
- JSONObject obj = new JSONObject(response.getBody());
- //判断是否成功
- if (obj.has("token")) {
- return obj.getString("token");
- } else {
- logger.info("返回未包含token。");
- return null;
- }
- } else {
- String msg = "获取Token失败。";
- if (response != null) {
- msg += "(错误代码:" + response.getStatusCode() + ",错误信息:" + response.getBody() + ")";
- }
- logger.info(msg);
- return null;
- }
- } catch (Exception ex) {
- logger.info("获取Token失败," + ex.getMessage());
- return null;
- }
- }
- /**
- * 获取病人列表
- */
- public static String getPatientList(AdapterDataSet adapterDataSet, List<EhrCondition> queryParams) {
- try {
- ObjectMapper mapper = new ObjectMapper();
- ObjectNode paramsNode = mapper.createObjectNode();
- paramsNode.put("tableCode", adapterDataSet.getAdapterDataSetT().getStdDatasetCode());
- paramsNode.put("condition", mapper.writeValueAsString(queryParams));
- Map<String, String> formParams = new HashMap<>();
- formParams.put("api", "collectionData");
- String params = mapper.writeValueAsString(paramsNode);
- formParams.put("param", params);
- HTTPResponse response = HttpClientKit.post(httpGateway, formParams);
- if (response == null || response.getStatusCode() != 200) {
- logger.error("获取病人列表错误,请求HTTP错误,请检查配置或HTTP是否可用.");
- return "";
- }
- JsonNode responseNode = mapper.readValue(response.getBody(), JsonNode.class);
- String code = responseNode.path("responseCode").asText();
- if (StringUtil.isEmpty(code) || !code.equals("10000")) {
- logger.error("获取病人列表错误,请求HTTP错误,请检查集成平台网关是否可用.");
- return "";
- }
- String rootStr = responseNode.path("responseResult").asText();
- if ("".equals(rootStr)) {
- logger.error("获取病人列表错误,集成平台获取病人列表失败.");
- return "";
- }
- return rootStr;
- } catch (Exception e) {
- logger.error("获取病人列表失败!", e);
- return "";
- }
- }
- public static String getFecthData(Map<String, String> formParams) {
- try {
- HTTPResponse response = HttpClientKit.post(httpGateway, formParams);
- if (response == null || response.getStatusCode() != 200) {
- logger.info("获取病人数据错误,请求HTTP错误,请检查配置或HTTP是否可用.");
- return SqlConstants.EMPTY;
- }
- ObjectMapper mapper = new ObjectMapper();
- JsonNode responseNode = mapper.readValue(response.getBody(), JsonNode.class);
- String code = responseNode.path("responseCode").asText();
- if (StringUtil.isEmpty(code) || !code.equals("10000")) {
- logger.info("获取病人数据错误,请求HTTP错误,请检查集成平台网关是否可用.");
- return SqlConstants.EMPTY;
- }
- String rootStr = responseNode.path("responseResult").asText();
- if (SqlConstants.EMPTY.equals(rootStr)) {
- logger.info("获取病人数据错误,集成平台获取病人数据失败.");
- return SqlConstants.EMPTY;
- }
- return rootStr;
- } catch (Exception e) {
- logger.error("获取病人数据失败.", e);
- return SqlConstants.EMPTY;
- }
- }
- /**
- * 获取公钥
- */
- public static String getPublicKey(String orgCode) {
- try {
- String token = getToken();
- if (!StringUtil.isEmpty(SysConfig.getInstance().getPublicKeyMap().get(orgCode))) {
- return SysConfig.getInstance().getPublicKeyMap().get(orgCode);
- }
- Map<String, String> header = new HashMap<>();
- header.put("Authorization", "Basic " + clientKey);
- Map<String, String> paramMap = new HashMap<>();
- paramMap.put("org_code", orgCode);
- paramMap.put("token", token);
- String publicKeyMethod = defaultHttpUrl + "/organizations/" + orgCode + "/key";
- HTTPResponse response = HttpClientKit.get(publicKeyMethod, paramMap, header);
- if (response != null && response.getStatusCode() == 200) {
- JSONObject json = new JSONObject(response.getBody());
- if (json.has("publicKey")) {
- String publicKey = json.getString("publicKey");
- SysConfig.getInstance().getPublicKeyMap().put(orgCode, publicKey);
- return publicKey;
- } else {
- logger.info("获取公钥失败,返回未包含publicKey。");
- return null;
- }
- } else {
- String msg = "获取公钥失败。";
- if (response != null) {
- msg += "(错误代码:" + response.getStatusCode() + ",错误信息:" + response.getBody() + ")";
- }
- logger.info(msg);
- return null;
- }
- } catch (Exception e) {
- logger.info(e.getMessage());
- return null;
- }
- }
- /**
- * 获取健康云平台标准版本号
- */
- public static String getRemoteVersion(String orgCode) {
- try {
- String token = getToken();
- String versionMethod = defaultHttpUrl + "/adaptions/org_plan/version";
- Map<String, String> header = new HashMap<>();
- header.put("Authorization", "Basic " + clientKey);
- Map<String, String> params = new HashMap<>();
- params.put("org_code", orgCode);
- params.put("token", token);
- HTTPResponse response = HttpClientKit.get(versionMethod, params, header);
- if (response != null && response.getStatusCode() == 200) {
- return response.getBody();
- } else {
- String msg = "获取健康云平台标准版本号失败";
- if (response != null) {
- msg += "(错误代码:" + response.getStatusCode() + ",错误信息:" + response.getBody() + ")";
- }
- logger.info(msg);
- return null;
- }
- } catch (Exception e) {
- logger.info("获取远程版本号异常");
- logger.error(e.getCause().toString());
- return null;
- }
- }
- /**
- * 注册病人
- */
- public static Boolean register(Patient patient, String data, String token) {
- try {
- JSONObject json = new JSONObject(data);
- String colName = SysConfig.registerIdCardNo;
- Map<String, String> header = new HashMap<>();
- header.put("Authorization", "Basic " + clientKey);
- header.put("User-Agent", "client " + clientId);
- if (json != null && json.has("data")) {
- JSONObject p = (JSONObject) json.getJSONArray("data").get(0);
- if (!p.has(colName) || StringUtil.isEmpty(p.get(colName))) {
- logger.info("注册病人信息请求失败:身份证号码为空,patient_id=" + patient.getPatientId() + ", event_no=" + patient.getEventNo());
- return false;
- } else {
- String idCord = p.getString(colName);
- String registerMethod = defaultHttpUrl + "/patients/" + idCord;
- if (StringUtil.isEmpty(data)) {
- logger.info("注册病人信息请求失败:无具体病人信息,patient_id=" + patient.getPatientId() + ", event_no=" + patient.getEventNo());
- return false;
- }
- Map<String, String> paramMap = new HashMap<>();
- paramMap.put("demographic_id", idCord);
- paramMap.put("json", data);
- paramMap.put("token", token);
- HTTPResponse response = HttpClientKit.post(registerMethod, paramMap, header);
- if (response != null && response.getStatusCode() == 200) {
- logger.info("注册病人信息成功。patient_id:" + patient.getPatientId() + ", event_no:" + patient.getEventNo());
- return true;
- } else {
- String msg = "注册病人信息请求失败。patient_id:" + patient.getPatientId() + ", event_no:" + patient.getEventNo();
- if (response != null) {
- msg += "(错误代码:" + response.getStatusCode() + ",错误信息:" + response.getBody() + ")";
- }
- logger.info(msg);
- return false;
- }
- }
- } else {
- logger.info("注册病人信息请求失败:传入数据无效,patient_id=" + patient.getPatientId() + ", event_no=" + patient.getEventNo());
- return false;
- }
- } catch (Exception e) {
- logger.info("注册病人信息请求失败." + e.getMessage());
- return false;
- }
- }
- /**
- * 上传病人档案
- */
- public static boolean upload(Patient patient, File file, String encryptPwd, String token) {
- try {
- String uploadMethod = defaultHttpUrl + "/packages";
- String fileMd5 = MD5.getMd5ByFile(file);
- Map<String, String> formParams = new HashMap<>();
- formParams.put("md5", fileMd5);
- formParams.put("package_crypto", encryptPwd);
- formParams.put("org_code", patient.getOrgCode());
- formParams.put("token", token);
- Map<String, String> header = new HashMap<>();
- header.put("Authorization", "Basic " + clientKey);
- header.put("User-Agent", "client " + clientId);
- HTTPResponse response = HttpClientKit.postFile(uploadMethod, file.getAbsolutePath(), formParams, header);
- if (response != null && response.getStatusCode() == 200) {
- logger.info("上传病人档案成功,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
- return true;
- } else {
- String msg = "上传病人档案请求失败,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo();
- if (response != null) {
- msg += "(错误代码:" + response.getStatusCode() + ",错误信息:" + response.getBody() + ")";
- }
- logger.info(msg);
- return false;
- }
- } catch (Exception e) {
- logger.info("上传病人档案异常,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
- logger.error(e.getCause().toString());
- return false;
- }
- }
- /**
- * 下载标准包
- */
- public static HTTPResponse download(String remoteVersion, String orgCode) {
- try {
- String token = getToken();
- String downLoadMethod = defaultHttpUrl + "/adaptions/" + orgCode + "/source";
- Map<String, String> params = new HashMap<>();
- params.put("version_code", remoteVersion);
- params.put("org_code", orgCode);
- params.put("token", token);
- Map<String, String> header = new HashMap<>();
- header.put("Authorization", "Basic " + clientKey);
- HTTPResponse response = HttpClientKit.get(downLoadMethod, params, header);
- return response;
- } catch (Exception e) {
- logger.info("下载标准包异常:");
- logger.error(e.getCause().toString());
- return null;
- }
- }
- }
|