| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package com.uas.eis.task.httpclient;
- import com.alibaba.fastjson.JSON;
- import com.uas.eis.beans.x5domain.*;
- import com.uas.eis.dao.BaseDao;
- import com.uas.eis.utils.Configuration;
- import com.uas.eis.utils.X5StringUtils;
- import org.apache.commons.codec.digest.DigestUtils;
- import org.apache.http.HttpResponse;
- import org.apache.http.StatusLine;
- import org.apache.http.client.HttpResponseException;
- import org.apache.http.client.fluent.Form;
- import org.apache.http.client.fluent.Request;
- import org.apache.http.client.fluent.Response;
- import org.apache.http.util.EntityUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import javax.xml.bind.DatatypeConverter;
- import java.io.UnsupportedEncodingException;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- public class X5JsonHttpClient extends X5HttpClient {
- private final Logger logger = LoggerFactory.getLogger(this.getClass());
- /**
- * 构造器
- */
- public X5JsonHttpClient(String appId, String appKey, String url) {
- this.appid = appId;
- this.appkey = appKey;
- this.url = url;
- }
- public <T> X5Response<T> post(Object body, Configuration configuration, BaseDao baseDao, String type) {
- String kind="";
- if("ProductStock".equals(type)){
- kind="物料库存同步";
- }else if("ProductWIP".equals(type)){
- kind="物料WIP同步";
- }
- X5Response<T> x5Response;
- if (X5StringUtils.isEmpty(appid) || X5StringUtils.isEmpty(appkey) || X5StringUtils.isEmpty(url)) {
- throw new X5Exception("1016", "The request at least contains appid appkey and url.");
- }
- String bodyString = null;
- if (body instanceof String) {
- bodyString = body.toString();
- } else {
- bodyString = JSON.toJSONString(body);
- }
- String sign = upperCase(DigestUtils.md5Hex(appid + bodyString + appkey));
- //拼装header信息
- X5RequestHeader header = new X5RequestHeader();
- header.setAppid(appid);
- header.setSign(sign);
- header.setUrl(url);
- //拼装X5请求信息
- X5Request x5Request = new X5Request();
- x5Request.setHeader(header);
- x5Request.setBody(bodyString);
- //构造请求参数
- String data = X5StringUtils.encodeBase64(JSON.toJSONString(x5Request));
- String encoding = null;
- String account=configuration.getX5username()+":"+configuration.getX5password();
- try {
- encoding = DatatypeConverter.printBase64Binary(account.getBytes("UTF-8")); //username password 自行修改 中间":"不可少
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- try {
- 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();
- HttpResponse httpResponse = executeResult.returnResponse();
- StatusLine statusLine = httpResponse.getStatusLine();
- int statusCode = statusLine.getStatusCode();
- String responseContent = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
- if (statusCode == 200) {
- x5Response = JSON.parseObject(responseContent, X5Response.class);
- String HeaderCode = x5Response.getHeader().getCode();
- if("200".equals(HeaderCode)){
- baseDao.execute("insert into XIAOMIDOCKINGLOG(ML_ID,ML_DATE,ML_CONTENT,ML_RESULT,ML_TYPE)" +
- "values(XIAOMIDOCKINGLOG_seq.nextval,sysdate,'上传成功!','','"+kind+"')");
- logger.info("时间:"+new Date()+"状态码:"+statusCode);
- Map<String,Object> datas = (Map<String,Object>) body;
- List<Map<String,Object>> list = (List<Map<String,Object>>) datas.get("data");
- if(list.size()>0){
- baseDao.execute("update CUSTOMTABLE set CT_SENDSTATUS='已上传',ct_senddate=sysdate where ct_id="+list.get(0).get("ct_id"));
- }
- }else{
- baseDao.execute("insert into XIAOMIDOCKINGLOG(ML_ID,ML_DATE,ML_CONTENT,ML_RESULT,ML_TYPE)" +
- "values(XIAOMIDOCKINGLOG_seq.nextval,sysdate,'上传失败!','状态码:"+x5Response.getHeader().getCode() + " 错误描述:" + x5Response.getHeader().getDesc()+"','"+kind+"')");
- Map<String,Object> datas = (Map<String,Object>) body;
- List<Map<String,Object>> list = (List<Map<String,Object>>) datas.get("data");
- if(list.size()>0){
- 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"));
- }
- }
- } else {
- baseDao.execute("insert into XIAOMIDOCKINGLOG(ML_ID,ML_DATE,ML_CONTENT,ML_RESULT,ML_TYPE)" +
- "values(XIAOMIDOCKINGLOG_seq.nextval,sysdate,'上传失败!','"+statusLine.getStatusCode() + " " + statusLine.getReasonPhrase()+"','"+kind+"')");
- Map<String,Object> datas = (Map<String,Object>) body;
- List<Map<String,Object>> list = (List<Map<String,Object>>) datas.get("data");
- if(list.size()>0){
- baseDao.execute("update CUSTOMTABLE set CT_ERROR='状态码:"+statusLine.getStatusCode()+"错误:"+statusLine.getReasonPhrase()+"',ct_senddate=sysdate where ct_id="+list.get(0).get("ct_id"));
- }
- logger.info("时间:"+new Date()+"状态码:"+statusLine.getStatusCode()+"错误:"+statusLine.getReasonPhrase());
- throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
- }
- } catch (Exception ex) {
- x5Response = new X5Response(new X5ResponseHeader("2000", ex.getMessage()));
- }
- return x5Response;
- }
- /**
- * 将String转为大写
- */
- private static String upperCase(String str) {
- return str == null ? null : str.toUpperCase();
- }
- }
|