123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- package com.yihu.ehr.util.httpclient;
- import com.fasterxml.jackson.databind.JsonNode;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.yihu.ehr.common.config.SysConfig;
- import com.yihu.ehr.common.constants.Constants;
- import com.yihu.ehr.model.Patient;
- import com.yihu.ehr.service.intf.ISystemManager;
- import com.yihu.ehr.util.encrypt.MD5;
- import com.yihu.ehr.util.log.LogUtil;
- import com.yihu.ehr.util.operator.ConfigureUtil;
- import com.yihu.ehr.util.operator.StringUtil;
- import org.apache.http.HttpStatus;
- import org.apache.http.NameValuePair;
- import org.apache.http.message.BasicNameValuePair;
- import org.json.JSONObject;
- import org.springframework.beans.factory.annotation.Autowired;
- import sun.misc.BASE64Encoder;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * Created by hzp on 2016/3/10.
- */
- public class EsbHttp {
- @Autowired
- private static ISystemManager system;
- /***************************** 用户接口 *********************************************/
- /**
- * 用户登录验证
- */
- public static Response loginAction(String user,String password) throws Exception{
- String loginAction = HttpHelper.defaultHttpUrl+"/authorizations/users/" + user;
- Map<String,Object> header = new HashMap<>();
- String auth = new BASE64Encoder().encode((user+":"+password).getBytes());
- header.put("Authorization","Basic "+auth);
- return HttpHelper.put(loginAction, null, header);
- }
- /*
- * 获取用户信息
- * */
- public static Response getUserInfo(String user,String token)
- {
- String url = HttpHelper.defaultHttpUrl+"/users/" + user;
- Map<String,Object> params = new HashMap<>();
- params.put("token",token);
- params.put("user",user);
- return HttpHelper.get(url, params);
- }
- /***************************** 应用接口 *********************************************/
- /**
- * 获取本机指纹
- * @return
- */
- private static String GetFingerprint(){
- try {
- return system.getSystemParam("FINGER_PRINT");
- }
- catch (Exception e)
- {
- System.out.print(e.getMessage());
- return "";
- }
- }
- /**
- * 应用登录验证
- */
- public static String getToken(){
- try {
- String loginAction = HttpHelper.defaultHttpUrl + "/authorizations/clients/" + HttpHelper.clientId;
- Map<String, Object> header = new HashMap<>();
- header.put("Authorization", "Basic " + HttpHelper.clientKey);
- //本地指纹
- Map<String, Object> params = new HashMap<String, Object>();
- params.put("info", "{\"fingerprint\": \"" + GetFingerprint() + "\"}");
- Response response = HttpHelper.put(loginAction, params, header);
- if (response != null && response.getStatusCode() == HttpStatus.SC_OK) {
- JSONObject obj = new JSONObject(response.getBody());
- //判断是否成功
- if (obj.has("token")) {
- return obj.getString("token");
- } else {
- LogUtil.info("返回未包含token。");
- return null;
- }
- } else {
- String msg = "获取Token失败。";
- if (response != null) {
- msg += "(错误代码:" + response.getStatusCode() + ",错误信息:" + response.getBody() + ")";
- }
- LogUtil.info(msg);
- return null;
- }
- }
- catch (Exception ex)
- {
- LogUtil.info("获取Token失败," + ex.getMessage());
- return null;
- }
- }
- /**
- * 修改远程补传状态
- */
- public static void changeFillMiningStatus(String remoteId,String message, String status){
- try {
- String token = getToken();
- Map<String, Object> paramMap = new HashMap<>();
- paramMap.put("id", remoteId);
- paramMap.put("status", status);
- paramMap.put("message", message);
- paramMap.put("token", token);
- String fillMiningMethod = HttpHelper.defaultHttpUrl + "/simplified-esb/changeFillMiningStatus";
- Response response = HttpHelper.post(fillMiningMethod, paramMap);
- if (response != null && response.getStatusCode() == HttpStatus.SC_OK) {
- LogUtil.info("修改远程补传状态成功。");
- }
- else{
- String msg = "修改远程补传状态失败。";
- if (response != null)
- {
- msg +="(错误代码:"+ response.getStatusCode() + ",错误信息:"+response.getBody()+")";
- }
- LogUtil.info(msg);
- }
- }
- catch (Exception ex)
- {
- LogUtil.info("修改远程补传状态失败." + ex.getMessage());
- }
- }
- /**
- * 获取公钥
- */
- public static String getPublicKey(){
- try {
- String token = getToken();
- if (SysConfig.getInstance().getPublicKey() != null) {
- return SysConfig.getInstance().getPublicKey();
- }
- String orgCode = SysConfig.getInstance().getOrgCode();
- Map<String, Object> paramMap = new HashMap<>();
- paramMap.put("org_code", orgCode);
- paramMap.put("token", token);
- String publicKeyMethod = HttpHelper.defaultHttpUrl + "/organizations/"+orgCode+"/key";
- Response response = HttpHelper.get(publicKeyMethod, paramMap);
- if (response != null && response.getStatusCode() == HttpStatus.SC_OK) {
- JSONObject json = new JSONObject(response.getBody());
- if(json.has("publicKey"))
- {
- String key = json.getString("publicKey");
- SysConfig.getInstance().setPublicKey(key);
- return key;
- }
- else{
- LogUtil.info("获取公钥失败,返回未包含publicKey。");
- return null;
- }
- }
- else{
- String msg = "获取公钥失败。";
- if (response != null)
- {
- msg +="(错误代码:"+ response.getStatusCode() + ",错误信息:"+response.getBody()+")";
- }
- LogUtil.info(msg);
- return null;
- }
- } catch (Exception e) {
- LogUtil.info(e.getMessage());
- return null;
- }
- }
- /**
- * 获取健康云平台标准版本号
- */
- public static String getRemoteVersion(String orgCode) {
- try {
- String token = getToken();
- String versionMethod = HttpHelper.defaultHttpUrl + "/adaptions/org_plan/version";
- Map<String, Object> params = new HashMap<>();
- params.put("org_code", orgCode);
- params.put("token", token);
- Response response = HttpHelper.get(versionMethod, params);
- if (response != null && response.getStatusCode() == HttpStatus.SC_OK) {
- return response.getBody();
- }
- else{
- String msg = "获取健康云平台标准版本号失败";
- if (response != null)
- {
- msg +="(错误代码:"+ response.getStatusCode() + ",错误信息:"+response.getBody()+")";
- }
- LogUtil.info(msg);
- return null;
- }
- } catch (Exception e) {
- LogUtil.fatal("获取远程版本号异常");
- LogUtil.error(e);
- return null;
- }
- }
- /**
- * 注册病人
- */
- public static Boolean register(Patient patient, String data, String token) {
- try {
- JSONObject json = new JSONObject(data);
- String colName = SysConfig.registerIdCardNo;
- if(json!=null && json.has("data"))
- {
- JSONObject p = (JSONObject)json.getJSONArray("data").get(0);
- if(!p.has(colName) || p.get(colName).equals(null) || p.getString(colName).length()==0)
- {
- LogUtil.info("注册病人信息请求失败:身份证号码为空,patient_id=" + patient.getPatientId() + ", event_no=" + patient.getEventNo());
- return false;
- }
- else{
- String idCord = p.getString(colName);
- String registerMethod = HttpHelper.defaultHttpUrl + "/patients/"+idCord;
- if (StringUtil.isEmpty(data)) {
- LogUtil.info("注册病人信息请求失败:无具体病人信息,patient_id=" + patient.getPatientId() + ", event_no=" + patient.getEventNo());
- return false;
- }
- Map<String, Object> paramMap = new HashMap<>();
- paramMap.put("demographic_id", idCord);
- paramMap.put("json", data);
- paramMap.put("token", token);
- Response response = HttpHelper.post(registerMethod, paramMap);
- if (response != null && response.getStatusCode() == HttpStatus.SC_OK) {
- LogUtil.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()+")";
- }
- LogUtil.info(msg);
- return false;
- }
- }
- }
- else{
- LogUtil.info("注册病人信息请求失败:传入数据无效,patient_id=" + patient.getPatientId() + ", event_no=" + patient.getEventNo());
- return false;
- }
- }
- catch (Exception e)
- {
- LogUtil.info("注册病人信息请求失败."+e.getMessage());
- return false;
- }
- }
- /**
- * 上传病人档案
- */
- public static boolean upload(Patient patient, File file, String encryptPwd, String token) {
- try {
- String uploadMethod = HttpHelper.defaultHttpUrl + "/packages";
- String fileMd5= MD5.getMd5ByFile(file);
- Map<String, Object> paramMap = new HashMap<>();
- List<NameValuePair> formParams = new ArrayList<>();
- formParams.add(new BasicNameValuePair("md5", fileMd5));
- formParams.add(new BasicNameValuePair("package_crypto", encryptPwd));
- formParams.add(new BasicNameValuePair("org_code", SysConfig.getInstance().getOrgCode()));
- formParams.add(new BasicNameValuePair("token", token));
- Response response = HttpHelper.postFile(uploadMethod, formParams, file.getAbsolutePath());
- if (response != null && response.getStatusCode() == HttpStatus.SC_OK) {
- LogUtil.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()+")";
- }
- LogUtil.info(msg);
- return false;
- }
- }
- catch (Exception e) {
- LogUtil.fatal("上传病人档案异常,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
- LogUtil.error(e);
- return false;
- }
- }
- /**
- * 下载标准包
- */
- public static Response download(String remoteVersion, String orgCode) {
- try {
- String token = getToken();
- String downLoadMethod = HttpHelper.defaultHttpUrl + "/adaptions/"+orgCode+"/source";
- Map<String, Object> params = new HashMap<>();
- params.put("version_code", remoteVersion);
- params.put("org_code", orgCode);
- params.put("token", token);
- Response response = HttpHelper.get(downLoadMethod, params);
- return response;
- } catch (Exception e) {
- LogUtil.fatal("下载标准包异常:");
- LogUtil.error(e);
- return null;
- }
- }
- }
|