EsbHttp.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package com.yihu.ehr.util.httpclient;
  2. import com.fasterxml.jackson.databind.JsonNode;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.yihu.ehr.common.config.SysConfig;
  5. import com.yihu.ehr.common.constants.Constants;
  6. import com.yihu.ehr.model.Patient;
  7. import com.yihu.ehr.service.intf.ISystemManager;
  8. import com.yihu.ehr.util.encrypt.MD5;
  9. import com.yihu.ehr.util.log.LogUtil;
  10. import com.yihu.ehr.util.operator.ConfigureUtil;
  11. import com.yihu.ehr.util.operator.StringUtil;
  12. import org.apache.http.HttpStatus;
  13. import org.apache.http.NameValuePair;
  14. import org.apache.http.message.BasicNameValuePair;
  15. import org.json.JSONObject;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import sun.misc.BASE64Encoder;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. /**
  25. * Created by hzp on 2016/3/10.
  26. */
  27. public class EsbHttp {
  28. @Autowired
  29. private static ISystemManager system;
  30. /***************************** 用户接口 *********************************************/
  31. /**
  32. * 用户登录验证
  33. */
  34. public static Response loginAction(String user,String password) throws Exception{
  35. String loginAction = HttpHelper.defaultHttpUrl+"/authorizations/users/" + user;
  36. Map<String,Object> header = new HashMap<>();
  37. String auth = new BASE64Encoder().encode((user+":"+password).getBytes());
  38. header.put("Authorization","Basic "+auth);
  39. return HttpHelper.put(loginAction, null, header);
  40. }
  41. /*
  42. * 获取用户信息
  43. * */
  44. public static Response getUserInfo(String user,String token)
  45. {
  46. String url = HttpHelper.defaultHttpUrl+"/users/" + user;
  47. Map<String,Object> params = new HashMap<>();
  48. params.put("token",token);
  49. params.put("user",user);
  50. return HttpHelper.get(url, params);
  51. }
  52. /***************************** 应用接口 *********************************************/
  53. /**
  54. * 获取本机指纹
  55. * @return
  56. */
  57. private static String GetFingerprint(){
  58. try {
  59. return system.getSystemParam("FINGER_PRINT");
  60. }
  61. catch (Exception e)
  62. {
  63. System.out.print(e.getMessage());
  64. return "";
  65. }
  66. }
  67. /**
  68. * 应用登录验证
  69. */
  70. public static String getToken(){
  71. try {
  72. String loginAction = HttpHelper.defaultHttpUrl + "/authorizations/clients/" + HttpHelper.clientId;
  73. Map<String, Object> header = new HashMap<>();
  74. header.put("Authorization", "Basic " + HttpHelper.clientKey);
  75. //本地指纹
  76. Map<String, Object> params = new HashMap<String, Object>();
  77. params.put("info", "{\"fingerprint\": \"" + GetFingerprint() + "\"}");
  78. Response response = HttpHelper.put(loginAction, params, header);
  79. if (response != null && response.getStatusCode() == HttpStatus.SC_OK) {
  80. JSONObject obj = new JSONObject(response.getBody());
  81. //判断是否成功
  82. if (obj.has("token")) {
  83. return obj.getString("token");
  84. } else {
  85. LogUtil.info("返回未包含token。");
  86. return null;
  87. }
  88. } else {
  89. String msg = "获取Token失败。";
  90. if (response != null) {
  91. msg += "(错误代码:" + response.getStatusCode() + ",错误信息:" + response.getBody() + ")";
  92. }
  93. LogUtil.info(msg);
  94. return null;
  95. }
  96. }
  97. catch (Exception ex)
  98. {
  99. LogUtil.info("获取Token失败," + ex.getMessage());
  100. return null;
  101. }
  102. }
  103. /**
  104. * 修改远程补传状态
  105. */
  106. public static void changeFillMiningStatus(String remoteId,String message, String status){
  107. try {
  108. String token = getToken();
  109. Map<String, Object> paramMap = new HashMap<>();
  110. paramMap.put("id", remoteId);
  111. paramMap.put("status", status);
  112. paramMap.put("message", message);
  113. paramMap.put("token", token);
  114. String fillMiningMethod = HttpHelper.defaultHttpUrl + "/simplified-esb/changeFillMiningStatus";
  115. Response response = HttpHelper.post(fillMiningMethod, paramMap);
  116. if (response != null && response.getStatusCode() == HttpStatus.SC_OK) {
  117. LogUtil.info("修改远程补传状态成功。");
  118. }
  119. else{
  120. String msg = "修改远程补传状态失败。";
  121. if (response != null)
  122. {
  123. msg +="(错误代码:"+ response.getStatusCode() + ",错误信息:"+response.getBody()+")";
  124. }
  125. LogUtil.info(msg);
  126. }
  127. }
  128. catch (Exception ex)
  129. {
  130. LogUtil.info("修改远程补传状态失败." + ex.getMessage());
  131. }
  132. }
  133. /**
  134. * 获取公钥
  135. */
  136. public static String getPublicKey(){
  137. try {
  138. String token = getToken();
  139. if (SysConfig.getInstance().getPublicKey() != null) {
  140. return SysConfig.getInstance().getPublicKey();
  141. }
  142. String orgCode = SysConfig.getInstance().getOrgCode();
  143. Map<String, Object> paramMap = new HashMap<>();
  144. paramMap.put("org_code", orgCode);
  145. paramMap.put("token", token);
  146. String publicKeyMethod = HttpHelper.defaultHttpUrl + "/organizations/"+orgCode+"/key";
  147. Response response = HttpHelper.get(publicKeyMethod, paramMap);
  148. if (response != null && response.getStatusCode() == HttpStatus.SC_OK) {
  149. JSONObject json = new JSONObject(response.getBody());
  150. if(json.has("publicKey"))
  151. {
  152. String key = json.getString("publicKey");
  153. SysConfig.getInstance().setPublicKey(key);
  154. return key;
  155. }
  156. else{
  157. LogUtil.info("获取公钥失败,返回未包含publicKey。");
  158. return null;
  159. }
  160. }
  161. else{
  162. String msg = "获取公钥失败。";
  163. if (response != null)
  164. {
  165. msg +="(错误代码:"+ response.getStatusCode() + ",错误信息:"+response.getBody()+")";
  166. }
  167. LogUtil.info(msg);
  168. return null;
  169. }
  170. } catch (Exception e) {
  171. LogUtil.info(e.getMessage());
  172. return null;
  173. }
  174. }
  175. /**
  176. * 获取健康云平台标准版本号
  177. */
  178. public static String getRemoteVersion(String orgCode) {
  179. try {
  180. String token = getToken();
  181. String versionMethod = HttpHelper.defaultHttpUrl + "/adaptions/org_plan/version";
  182. Map<String, Object> params = new HashMap<>();
  183. params.put("org_code", orgCode);
  184. params.put("token", token);
  185. Response response = HttpHelper.get(versionMethod, params);
  186. if (response != null && response.getStatusCode() == HttpStatus.SC_OK) {
  187. return response.getBody();
  188. }
  189. else{
  190. String msg = "获取健康云平台标准版本号失败";
  191. if (response != null)
  192. {
  193. msg +="(错误代码:"+ response.getStatusCode() + ",错误信息:"+response.getBody()+")";
  194. }
  195. LogUtil.info(msg);
  196. return null;
  197. }
  198. } catch (Exception e) {
  199. LogUtil.fatal("获取远程版本号异常");
  200. LogUtil.error(e);
  201. return null;
  202. }
  203. }
  204. /**
  205. * 注册病人
  206. */
  207. public static Boolean register(Patient patient, String data, String token) {
  208. try {
  209. JSONObject json = new JSONObject(data);
  210. String colName = SysConfig.registerIdCardNo;
  211. if(json!=null && json.has("data"))
  212. {
  213. JSONObject p = (JSONObject)json.getJSONArray("data").get(0);
  214. if(!p.has(colName) || p.get(colName).equals(null) || p.getString(colName).length()==0)
  215. {
  216. LogUtil.info("注册病人信息请求失败:身份证号码为空,patient_id=" + patient.getPatientId() + ", event_no=" + patient.getEventNo());
  217. return false;
  218. }
  219. else{
  220. String idCord = p.getString(colName);
  221. String registerMethod = HttpHelper.defaultHttpUrl + "/patients/"+idCord;
  222. if (StringUtil.isEmpty(data)) {
  223. LogUtil.info("注册病人信息请求失败:无具体病人信息,patient_id=" + patient.getPatientId() + ", event_no=" + patient.getEventNo());
  224. return false;
  225. }
  226. Map<String, Object> paramMap = new HashMap<>();
  227. paramMap.put("demographic_id", idCord);
  228. paramMap.put("json", data);
  229. paramMap.put("token", token);
  230. Response response = HttpHelper.post(registerMethod, paramMap);
  231. if (response != null && response.getStatusCode() == HttpStatus.SC_OK) {
  232. LogUtil.info("注册病人信息成功。patient_id:" + patient.getPatientId() + ", event_no:" + patient.getEventNo());
  233. return true;
  234. }
  235. else{
  236. String msg = "注册病人信息请求失败。patient_id:" + patient.getPatientId() + ", event_no:" + patient.getEventNo();
  237. if(response != null)
  238. {
  239. msg +="(错误代码:"+ response.getStatusCode() + ",错误信息:"+response.getBody()+")";
  240. }
  241. LogUtil.info(msg);
  242. return false;
  243. }
  244. }
  245. }
  246. else{
  247. LogUtil.info("注册病人信息请求失败:传入数据无效,patient_id=" + patient.getPatientId() + ", event_no=" + patient.getEventNo());
  248. return false;
  249. }
  250. }
  251. catch (Exception e)
  252. {
  253. LogUtil.info("注册病人信息请求失败."+e.getMessage());
  254. return false;
  255. }
  256. }
  257. /**
  258. * 上传病人档案
  259. */
  260. public static boolean upload(Patient patient, File file, String encryptPwd, String token) {
  261. try {
  262. String uploadMethod = HttpHelper.defaultHttpUrl + "/packages";
  263. String fileMd5= MD5.getMd5ByFile(file);
  264. Map<String, Object> paramMap = new HashMap<>();
  265. List<NameValuePair> formParams = new ArrayList<>();
  266. formParams.add(new BasicNameValuePair("md5", fileMd5));
  267. formParams.add(new BasicNameValuePair("package_crypto", encryptPwd));
  268. formParams.add(new BasicNameValuePair("org_code", SysConfig.getInstance().getOrgCode()));
  269. formParams.add(new BasicNameValuePair("token", token));
  270. Response response = HttpHelper.postFile(uploadMethod, formParams, file.getAbsolutePath());
  271. if (response != null && response.getStatusCode() == HttpStatus.SC_OK) {
  272. LogUtil.info("上传病人档案成功,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  273. return true;
  274. }
  275. else {
  276. String msg = "上传病人档案请求失败,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo();
  277. if (response != null)
  278. {
  279. msg +="(错误代码:"+ response.getStatusCode() + ",错误信息:"+response.getBody()+")";
  280. }
  281. LogUtil.info(msg);
  282. return false;
  283. }
  284. }
  285. catch (Exception e) {
  286. LogUtil.fatal("上传病人档案异常,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  287. LogUtil.error(e);
  288. return false;
  289. }
  290. }
  291. /**
  292. * 下载标准包
  293. */
  294. public static Response download(String remoteVersion, String orgCode) {
  295. try {
  296. String token = getToken();
  297. String downLoadMethod = HttpHelper.defaultHttpUrl + "/adaptions/"+orgCode+"/source";
  298. Map<String, Object> params = new HashMap<>();
  299. params.put("version_code", remoteVersion);
  300. params.put("org_code", orgCode);
  301. params.put("token", token);
  302. Response response = HttpHelper.get(downLoadMethod, params);
  303. return response;
  304. } catch (Exception e) {
  305. LogUtil.fatal("下载标准包异常:");
  306. LogUtil.error(e);
  307. return null;
  308. }
  309. }
  310. }