X5JsonHttpClient.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package com.uas.eis.task.httpclient;
  2. import com.alibaba.fastjson.JSON;
  3. import com.uas.eis.beans.x5domain.*;
  4. import com.uas.eis.dao.BaseDao;
  5. import com.uas.eis.utils.Configuration;
  6. import com.uas.eis.utils.X5StringUtils;
  7. import org.apache.commons.codec.digest.DigestUtils;
  8. import org.apache.http.HttpResponse;
  9. import org.apache.http.StatusLine;
  10. import org.apache.http.client.HttpResponseException;
  11. import org.apache.http.client.fluent.Form;
  12. import org.apache.http.client.fluent.Request;
  13. import org.apache.http.client.fluent.Response;
  14. import org.apache.http.util.EntityUtils;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import javax.xml.bind.DatatypeConverter;
  18. import java.io.UnsupportedEncodingException;
  19. import java.util.Date;
  20. import java.util.List;
  21. import java.util.Map;
  22. public class X5JsonHttpClient extends X5HttpClient {
  23. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  24. /**
  25. * 构造器
  26. */
  27. public X5JsonHttpClient(String appId, String appKey, String url) {
  28. this.appid = appId;
  29. this.appkey = appKey;
  30. this.url = url;
  31. }
  32. public <T> X5Response<T> post(Object body, Configuration configuration, BaseDao baseDao, String type) {
  33. String kind="";
  34. if("ProductStock".equals(type)){
  35. kind="物料库存同步";
  36. }else if("ProductWIP".equals(type)){
  37. kind="物料WIP同步";
  38. }
  39. X5Response<T> x5Response;
  40. if (X5StringUtils.isEmpty(appid) || X5StringUtils.isEmpty(appkey) || X5StringUtils.isEmpty(url)) {
  41. throw new X5Exception("1016", "The request at least contains appid appkey and url.");
  42. }
  43. String bodyString = null;
  44. if (body instanceof String) {
  45. bodyString = body.toString();
  46. } else {
  47. bodyString = JSON.toJSONString(body);
  48. }
  49. String sign = upperCase(DigestUtils.md5Hex(appid + bodyString + appkey));
  50. //拼装header信息
  51. X5RequestHeader header = new X5RequestHeader();
  52. header.setAppid(appid);
  53. header.setSign(sign);
  54. header.setUrl(url);
  55. //拼装X5请求信息
  56. X5Request x5Request = new X5Request();
  57. x5Request.setHeader(header);
  58. x5Request.setBody(bodyString);
  59. //构造请求参数
  60. String data = X5StringUtils.encodeBase64(JSON.toJSONString(x5Request));
  61. String encoding = null;
  62. String account=configuration.getX5username()+":"+configuration.getX5password();
  63. try {
  64. encoding = DatatypeConverter.printBase64Binary(account.getBytes("UTF-8")); //username password 自行修改 中间":"不可少
  65. } catch (UnsupportedEncodingException e) {
  66. e.printStackTrace();
  67. }
  68. try {
  69. Response executeResult = Request.Post(url).addHeader("Content-Type", "application/x-www-form-urlencoded").setHeader("Authorization","Basic " + encoding).connectTimeout(DEFAULT_COMMECT_TIMEOUT).socketTimeout(DEFAULT_SOCKET_TIMEOUT).bodyForm(Form.form().add("data", data).build()).execute();
  70. HttpResponse httpResponse = executeResult.returnResponse();
  71. StatusLine statusLine = httpResponse.getStatusLine();
  72. int statusCode = statusLine.getStatusCode();
  73. String responseContent = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
  74. if (statusCode == 200) {
  75. x5Response = JSON.parseObject(responseContent, X5Response.class);
  76. String HeaderCode = x5Response.getHeader().getCode();
  77. if("200".equals(HeaderCode)){
  78. baseDao.execute("insert into XIAOMIDOCKINGLOG(ML_ID,ML_DATE,ML_CONTENT,ML_RESULT,ML_TYPE)" +
  79. "values(XIAOMIDOCKINGLOG_seq.nextval,sysdate,'上传成功!','','"+kind+"')");
  80. logger.info("时间:"+new Date()+"状态码:"+statusCode);
  81. Map<String,Object> datas = (Map<String,Object>) body;
  82. List<Map<String,Object>> list = (List<Map<String,Object>>) datas.get("data");
  83. if(list.size()>0){
  84. baseDao.execute("update CUSTOMTABLE set CT_SENDSTATUS='已上传',ct_senddate=sysdate where ct_id="+list.get(0).get("ct_id"));
  85. }
  86. }else{
  87. baseDao.execute("insert into XIAOMIDOCKINGLOG(ML_ID,ML_DATE,ML_CONTENT,ML_RESULT,ML_TYPE)" +
  88. "values(XIAOMIDOCKINGLOG_seq.nextval,sysdate,'上传失败!','状态码:"+x5Response.getHeader().getCode() + " 错误描述:" + x5Response.getHeader().getDesc()+"','"+kind+"')");
  89. Map<String,Object> datas = (Map<String,Object>) body;
  90. List<Map<String,Object>> list = (List<Map<String,Object>>) datas.get("data");
  91. if(list.size()>0){
  92. baseDao.execute("update CUSTOMTABLE set CT_ERROR='状态码:"+x5Response.getHeader().getCode()+"错误:"+x5Response.getHeader().getDesc()+"',ct_senddate=sysdate where ct_id="+list.get(0).get("ct_id"));
  93. }
  94. }
  95. } else {
  96. baseDao.execute("insert into XIAOMIDOCKINGLOG(ML_ID,ML_DATE,ML_CONTENT,ML_RESULT,ML_TYPE)" +
  97. "values(XIAOMIDOCKINGLOG_seq.nextval,sysdate,'上传失败!','"+statusLine.getStatusCode() + " " + statusLine.getReasonPhrase()+"','"+kind+"')");
  98. Map<String,Object> datas = (Map<String,Object>) body;
  99. List<Map<String,Object>> list = (List<Map<String,Object>>) datas.get("data");
  100. if(list.size()>0){
  101. baseDao.execute("update CUSTOMTABLE set CT_ERROR='状态码:"+statusLine.getStatusCode()+"错误:"+statusLine.getReasonPhrase()+"',ct_senddate=sysdate where ct_id="+list.get(0).get("ct_id"));
  102. }
  103. logger.info("时间:"+new Date()+"状态码:"+statusLine.getStatusCode()+"错误:"+statusLine.getReasonPhrase());
  104. throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
  105. }
  106. } catch (Exception ex) {
  107. x5Response = new X5Response(new X5ResponseHeader("2000", ex.getMessage()));
  108. }
  109. return x5Response;
  110. }
  111. /**
  112. * 将String转为大写
  113. */
  114. private static String upperCase(String str) {
  115. return str == null ? null : str.toUpperCase();
  116. }
  117. }