wuxw7
2018-04-13 f4b8bf9cd3e0c49e92c196e6f4cabf108e6dcf10
开发HttpApi service 接口未完成,目前完成数据封装到DataFlow对象
2个文件已删除
6个文件已修改
6个文件已添加
743 ■■■■■ 已修改文件
CenterService/doc/request_protocol.json 30 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
CenterService/doc/response_protocol.json 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
CenterService/doc/~$nterService.docx 补丁 | 查看 | 原始文档 | blame | 历史
CenterService/src/main/java/com/java110/App.java 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
CenterService/src/main/java/com/java110/center/rest/HttpApi.java 26 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
CenterService/src/main/java/com/java110/center/smo/ICenterServiceSMO.java 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
CenterService/src/main/java/com/java110/center/smo/impl/CenterServiceSMOImpl.java 54 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
CodingLog.txt 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
java110-bean/src/main/java/com/java110/entity/center/Business.java 112 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
java110-bean/src/main/java/com/java110/entity/center/DataFlow.java 387 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
java110-bean/src/main/java/com/java110/entity/center/DataFlowLinksCost.java 44 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
java110-common/src/main/java/com/java110/common/factory/DataFlowFactory.java 34 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
java110-common/src/main/java/com/java110/common/util/DateUtil.java 10 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
java110-common/src/main/java/com/java110/common/util/ResponseTemplateUtil.java 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
CenterService/doc/request_protocol.json
@@ -1,27 +1,27 @@
{
  "orders": {
    "AppId": "外系统ID,分配得到",
    "TransactionId": "100000000020180409224736000001",
    "UserId": "用户ID",
    "OrderTypeCd": "订单类型,查询,受理",
    "RequestTime": "20180409224736",
    "Remark":"备注",
    "appId": "外系统ID,分配得到",
    "transactionId": "100000000020180409224736000001",
    "userId": "用户ID",
    "orderTypeCd": "订单类型,查询,受理",
    "requestTime": "20180409224736",
    "remark":"备注",
    "sign": "这个服务是否要求MD5签名",
    "Attrs": [{
      "SpecCd": "配置的字段ID",
      "Value": "具体值"
    "attrs": [{
      "specCd": "配置的字段ID",
      "value": "具体值"
    }]
  },
  "business": [{
    "ServiceCode": "queryCustInfo",
    "ServiceName": "查询客户",
    "Remark": "备注",
    "serviceCode": "queryCustInfo",
    "serviceName": "查询客户",
    "remark": "备注",
    "datas": [{
      //这里是具体业务
    }],
    "Attrs": [{
      "SpecCd": "配置的字段ID",
      "Value": "具体值"
    "attrs": [{
      "specCd": "配置的字段ID",
      "value": "具体值"
    }]
  }]
}
CenterService/doc/response_protocol.json
@@ -2,7 +2,6 @@
  "orders": {
    "TransactionId": "100000000020180409224736000001",
    "ResponseTime": "20180409224736",
    "Remark":"备注",
    "sign": "这个服务是否要求MD5签名",
    "response": {//这个是centerOrder 返回的状态结果
      "code": "1999",
CenterService/doc/~$nterService.docx
Binary files differ
CenterService/src/main/java/com/java110/App.java
File was deleted
CenterService/src/main/java/com/java110/center/rest/HttpApi.java
@@ -1,12 +1,13 @@
package com.java110.center.rest;
import com.java110.center.smo.ICenterServiceSMO;
import com.java110.common.constant.ResponseConstant;
import com.java110.common.util.ResponseTemplateUtil;
import com.java110.core.base.controller.BaseController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
/**
 * 中心http服务 统一服务类
@@ -16,14 +17,25 @@
@RestController
public class HttpApi extends BaseController {
    @Autowired
    private ICenterServiceSMO centerServiceSMOImpl;
    @RequestMapping(path = "/httpApi/service",method= RequestMethod.GET)
    public String serviceGet(@RequestParam("orderInfo") String orderInfo) {
    public String serviceGet(HttpServletRequest request) {
        return ResponseTemplateUtil.createOrderResponseJson(ResponseConstant.NO_TRANSACTION_ID,
                ResponseConstant.NO_NEED_SIGN,ResponseConstant.RESULT_CODE_ERROR,"不支持Get方法请求");
    }
    @RequestMapping(path = "/httpApi/service",method= RequestMethod.POST)
    public String servicePost(@RequestParam("orderInfo") String orderInfo) {
        return "";
    public String servicePost(@RequestBody String orderInfo, HttpServletRequest request) {
        return centerServiceSMOImpl.service(orderInfo, request);
    }
    public ICenterServiceSMO getCenterServiceSMOImpl() {
        return centerServiceSMOImpl;
    }
    public void setCenterServiceSMOImpl(ICenterServiceSMO centerServiceSMOImpl) {
        this.centerServiceSMOImpl = centerServiceSMOImpl;
    }
}
CenterService/src/main/java/com/java110/center/smo/ICenterServiceSMO.java
New file
@@ -0,0 +1,17 @@
package com.java110.center.smo;
import javax.servlet.http.HttpServletRequest;
/**
 * 中心服务 SMO 业务逻辑接口
 * Created by wuxw on 2018/4/13.
 */
public interface ICenterServiceSMO {
    /**
     * 业务统一处理服务方法
     * @param reqJson 请求报文json
     * @return
     */
    public String service(String reqJson, HttpServletRequest request) ;
}
CenterService/src/main/java/com/java110/center/smo/impl/CenterServiceSMOImpl.java
New file
@@ -0,0 +1,54 @@
package com.java110.center.smo.impl;
import com.java110.center.smo.ICenterServiceSMO;
import com.java110.common.constant.ResponseConstant;
import com.java110.common.factory.DataFlowFactory;
import com.java110.common.util.DateUtil;
import com.java110.common.util.ResponseTemplateUtil;
import com.java110.entity.center.DataFlow;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
/**
 * 中心服务处理类
 * Created by wuxw on 2018/4/13.
 */
@Service("centerServiceSMOImpl")
@Transactional
public class CenterServiceSMOImpl implements ICenterServiceSMO {
    @Override
    public String service(String reqJson, HttpServletRequest request) {
        DataFlow dataFlow = null;
        try{
            //1.0 创建消息流
            dataFlow = DataFlowFactory.newInstance().builder(reqJson, request);
        }catch (Exception e){
            return ResponseTemplateUtil.createOrderResponseJson(dataFlow == null
                            ?ResponseConstant.NO_TRANSACTION_ID
                            :dataFlow.getTransactionId(),
                    ResponseConstant.NO_NEED_SIGN,ResponseConstant.RESULT_CODE_INNER_ERROR,"内部异常了:"+e.getMessage()+e.getLocalizedMessage());
        }finally {
            //这里记录日志
            Date endDate = DateUtil.getCurrentDate();
            if(dataFlow == null){ //说明异常了
                return null;
            }
            dataFlow.setEndDate(endDate);
            //添加耗时
            DataFlowFactory.addCostTime(dataFlow,"service","业务处理总耗时",dataFlow.getStartDate(),dataFlow.getEndDate());
            //这里保存耗时,以及日志
            return ResponseTemplateUtil.createCommonResponseJson(dataFlow);
        }
    }
}
CodingLog.txt
@@ -2,4 +2,5 @@
1、重新调整结构,每个服务必须要有doc(文档),docker和src目录,启用v0.2版
2、分支管理说明,test为最新代码未测试代码(主要为了防止本地代码遗失),master 为最新测试过代码(待产品化),product 为产品化代码
3、网友反映OrderService提供的协议过于复杂,则加入CenterService服务简化协议处理服务(文档设计实际已经完成,请查看CenterService/doc/centerService.docx,待开发)
4、开发HttpApi service 接口未完成,目前完成数据封装到DataFlow对象。
java110-bean/src/main/java/com/java110/entity/center/Business.java
New file
@@ -0,0 +1,112 @@
package com.java110.entity.center;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
 * 业务数据
 * Created by wuxw on 2018/4/13.
 */
public class Business {
    private String bId;
    //业务编码
    private String serviceCode;
    private String serviceName;
    private String remark;
    private JSONArray datas;
    private JSONArray attrs;
    //返回 编码
    private String code;
    private String message;
    public String getbId() {
        return bId;
    }
    public void setbId(String bId) {
        this.bId = bId;
    }
    public String getServiceCode() {
        return serviceCode;
    }
    public void setServiceCode(String serviceCode) {
        this.serviceCode = serviceCode;
    }
    public String getServiceName() {
        return serviceName;
    }
    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }
    public String getRemark() {
        return remark;
    }
    public void setRemark(String remark) {
        this.remark = remark;
    }
    public JSONArray getDatas() {
        return datas;
    }
    public void setDatas(JSONArray datas) {
        this.datas = datas;
    }
    public JSONArray getAttrs() {
        return attrs;
    }
    public void setAttrs(JSONArray attrs) {
        this.attrs = attrs;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    /**
     * 构建成对象
     * @return
     * @throws Exception
     */
    public Business builder(JSONObject businessObj) throws Exception{
        try{
            this.setServiceCode(businessObj.getString("serviceCode"));
            this.setServiceName(businessObj.getString("serviceName"));
            this.setRemark(businessObj.getString("remark"));
            this.setDatas(businessObj.getJSONArray("datas"));
            this.setAttrs(businessObj.getJSONArray("attrs"));
        }catch (Exception e){
            throw e;
        }
        return this;
    }
}
java110-bean/src/main/java/com/java110/entity/center/DataFlow.java
New file
@@ -0,0 +1,387 @@
package com.java110.entity.center;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
/**
 * 主要用于离散成对象 httpApi service 请求过程消息记录和返回记录
 * Created by wuxw on 2018/4/13.
 */
public class DataFlow {
    private String oId;
    //交易流水
    private String transactionId;
    private String appId;
    private String userId;
    private String orderTypeCd;
    private String requestTime;
    private String remark;
    private String reqSign;
    private JSONObject reqOrders;
    private JSONArray reqBusiness;
    private String responseTime;
    private String resSign;
    private String code;
    private String message;
    private JSONObject resOrders;
    private JSONArray resBusiness;
    //请求开始时间
    private Date startDate;
    //请求完成时间
    private Date endDate;
    private String reqJson;
    private String resJson;
    private List<Business> businesses;
    private String requestURL;
    private List<DataFlowLinksCost> linksCostDatas = new ArrayList<DataFlowLinksCost>();
    private Map<String,String> headers = new HashMap<String,String>();
    public String getoId() {
        return oId;
    }
    public void setoId(String oId) {
        this.oId = oId;
    }
    public List<Business> getBusinesses() {
        return businesses;
    }
    public void setBusinesses(List<Business> businesses) {
        this.businesses = businesses;
    }
    //public DataFlow(){}
    public DataFlow(Date startDate,String code){
        this.setStartDate(startDate);
        this.setCode(code);
    }
    public void setTransactionId(String transactionId) {
        this.transactionId = transactionId;
    }
    public void setAppId(String appId) {
        this.appId = appId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public void setOrderTypeCd(String orderTypeCd) {
        this.orderTypeCd = orderTypeCd;
    }
    public void setRequestTime(String requestTime) {
        this.requestTime = requestTime;
    }
    public void setRemark(String remark) {
        this.remark = remark;
    }
    public void setReqSign(String reqSign) {
        this.reqSign = reqSign;
    }
    public void setReqOrders(JSONObject reqOrders) {
        this.reqOrders = reqOrders;
    }
    public void setReqBusiness(JSONArray reqBusiness) {
        this.reqBusiness = reqBusiness;
    }
    public void setResponseTime(String responseTime) {
        this.responseTime = responseTime;
    }
    public void setResSign(String resSign) {
        this.resSign = resSign;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public void setResOrders(JSONObject resOrders) {
        this.resOrders = resOrders;
    }
    public void setResBusiness(JSONArray resBusiness) {
        this.resBusiness = resBusiness;
    }
    public String getTransactionId() {
        return transactionId;
    }
    public String getAppId() {
        return appId;
    }
    public String getUserId() {
        return userId;
    }
    public String getOrderTypeCd() {
        return orderTypeCd;
    }
    public String getRequestTime() {
        return requestTime;
    }
    public String getRemark() {
        return remark;
    }
    public String getReqSign() {
        return reqSign;
    }
    public JSONObject getReqOrders() {
        return reqOrders;
    }
    public JSONArray getReqBusiness() {
        return reqBusiness;
    }
    public String getResponseTime() {
        return responseTime;
    }
    public String getResSign() {
        return resSign;
    }
    public String getCode() {
        return code;
    }
    public String getMessage() {
        return message;
    }
    public JSONObject getResOrders() {
        return resOrders;
    }
    public JSONArray getResBusiness() {
        return resBusiness;
    }
    public Date getStartDate() {
        return startDate;
    }
    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }
    public Date getEndDate() {
        return endDate;
    }
    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }
    public String getReqJson() {
        return reqJson;
    }
    public void setReqJson(String reqJson) {
        this.reqJson = reqJson;
    }
    public String getResJson() {
        return resJson;
    }
    public void setResJson(String resJson) {
        this.resJson = resJson;
    }
    public String getRequestURL() {
        return requestURL;
    }
    public void setRequestURL(String requestURL) {
        this.requestURL = requestURL;
    }
    public Map<String, String> getHeaders() {
        return headers;
    }
    /**
     * 添加各个环节的耗时
     * @param dataFlowLinksCost
     */
    public void addLinksCostDatas(DataFlowLinksCost dataFlowLinksCost){
        this.linksCostDatas.add(dataFlowLinksCost);
    }
    public DataFlow builder(String reqInfo, HttpServletRequest request) throws Exception{
        try{
            Business business = null;
            JSONObject reqInfoObj = JSONObject.parseObject(reqInfo);
            JSONObject orderObj = reqInfoObj.getJSONObject("orders");
            JSONArray businessArray = reqInfoObj.getJSONArray("business");
            this.setAppId(orderObj.getString("appId"));
            this.setAppId(orderObj.getString("transactionId"));
            this.setUserId(orderObj.getString("userId"));
            this.setOrderTypeCd(orderObj.getString("orderTypeCd"));
            this.setRemark(orderObj.getString("remark"));
            this.setReqSign(orderObj.getString("sign"));
            this.setRequestTime(orderObj.getString("requestTime"));
            this.setReqOrders(orderObj);
            this.setReqBusiness(businessArray);
            this.businesses = new ArrayList<Business>();
            if(businessArray != null && businessArray.size() > 0){
                for(int businessIndex = 0;businessIndex < businessArray.size();businessIndex++) {
                    business = new Business().builder(businessArray.getJSONObject(businessIndex));
                    businesses.add(business);
                }
            }
            //将url参数写到header map中
            initUrlParam(request);
            //将 header 写到header map中
            initHeadParam(request);
        }catch (Exception e){
            throw e;
        }
        return this;
    }
    /**
     * 将url参数写到header map中
     * @param request
     */
    private void initUrlParam(HttpServletRequest request) {
        /*put real ip address*/
        Map readOnlyMap = request.getParameterMap();
        StringBuffer queryString = new StringBuffer(request.getRequestURL()!=null?request.getRequestURL():"");
        if (readOnlyMap != null && !readOnlyMap.isEmpty()) {
            queryString.append("?");
            Set<String> keys = readOnlyMap.keySet();
            for (Iterator it = keys.iterator(); it.hasNext();) {
                String key = (String) it.next();
                String[] value = (String[]) readOnlyMap.get(key);
//                String[] value = (String[]) readOnlyMap.get(key);
                if(value.length>1) {
                    headers.put(key, value[0]);
                    for(int j =0 ;j<value.length;j++){
                        queryString.append(key);
                        queryString.append("=");
                        queryString.append(value[j]);
                        queryString.append("&");
                    }
                } else {
                    headers.put(key, value[0]);
                    queryString.append(key);
                    queryString.append("=");
                    queryString.append(value[0]);
                    queryString.append("&");
                }
            }
        }
        /*put requst url*/
        if (readOnlyMap != null && !readOnlyMap.isEmpty()){
            this.setRequestURL(queryString.toString().substring(0, queryString.toString().length() - 1));
        }else{
            this.setRequestURL(queryString.toString());
        }
    }
    private void initHeadParam(HttpServletRequest request) {
        headers.put("IP",getIpAddr(request));
        Enumeration reqHeaderEnum = request.getHeaderNames();
        while( reqHeaderEnum.hasMoreElements() ) {
            String headerName = (String)reqHeaderEnum.nextElement();
            headers.put(headerName, request.getHeader(headerName));
        }
    }
    /**
     * 获取IP地址
     * @param request
     * @return
     */
    private String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
java110-bean/src/main/java/com/java110/entity/center/DataFlowLinksCost.java
New file
@@ -0,0 +1,44 @@
package com.java110.entity.center;
import java.util.Date;
/**
 * Created by wuxw on 2018/4/13.
 */
public class DataFlowLinksCost {
    //环节编码
    private String LinksCode;
    private String LinksName;
    private Date startDate;
    private Date endDate;
    public Date getStartDate() {
        return startDate;
    }
    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }
    public Date getEndDate() {
        return endDate;
    }
    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }
    public DataFlowLinksCost builder(String LinksCode, String LinksName, Date startDate, Date endDate){
        this.LinksCode = LinksCode;
        this.LinksName = LinksName;
        this.startDate = startDate;
        this.endDate = endDate;
        return this;
    }
}
java110-common/src/main/java/com/java110/common/factory/DataFlowFactory.java
New file
@@ -0,0 +1,34 @@
package com.java110.common.factory;
import com.java110.common.constant.ResponseConstant;
import com.java110.common.util.DateUtil;
import com.java110.entity.center.DataFlow;
import com.java110.entity.center.DataFlowLinksCost;
import java.util.Date;
/**
 * 数据流工厂类
 * Created by wuxw on 2018/4/13.
 */
public class DataFlowFactory {
    public static DataFlow newInstance(){
        return new DataFlow(DateUtil.getCurrentDate(), ResponseConstant.RESULT_CODE_SUCCESS);
    }
    /**
     * 添加耗时
     * @param dataFlow 数据流
     * @param linksCode 环节编码
     * @param linksName 环节名称
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return
     */
    public static DataFlow addCostTime(DataFlow dataFlow, String linksCode, String linksName, Date startDate, Date endDate){
        DataFlowLinksCost dataFlowLinksCost = new DataFlowLinksCost().builder(linksCode, linksName, startDate, endDate);
        dataFlow.addLinksCostDatas(dataFlowLinksCost);
        return dataFlow;
    }
}
java110-common/src/main/java/com/java110/common/util/DateUtil.java
@@ -55,8 +55,14 @@
    }
    /**
     * 获取当前时间
     * @return
     */
    public static Date getCurrentDate(){
        Calendar calendar = Calendar.getInstance();
        return calendar.getTime();
    }
    public static String getFormatTimeString(Date date, String pattern)
    {
java110-common/src/main/java/com/java110/common/util/ResponseTemplateUtil.java
@@ -3,6 +3,7 @@
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.common.constant.ResponseConstant;
import com.java110.entity.center.DataFlow;
import java.util.Date;
@@ -71,4 +72,17 @@
    public static String createOrderResponseJson(String transactionId, String sign,String code,String message){
        return createCommonResponseJson(transactionId,sign,code,message,null);
    }
    /**
     * 组装返回报文
     * @param dataFlow 数据流
     * @return
     */
    public static String createCommonResponseJson(DataFlow dataFlow){
        return createCommonResponseJson(dataFlow.getTransactionId(),
                dataFlow.getResSign(),
                dataFlow.getCode(),
                dataFlow.getMessage(),
                dataFlow.getResBusiness());
    }
}