wuxw7
2018-05-09 1254c7d457b742e16ffc8c7820a445e1985fa55c
java110-bean/src/main/java/com/java110/entity/center/DataFlow.java
@@ -3,7 +3,6 @@
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
/**
@@ -11,6 +10,9 @@
 * Created by wuxw on 2018/4/13.
 */
public class DataFlow {
    private String dataFlowId;
    private String oId;
@@ -20,6 +22,8 @@
    private String appId;
    private String userId;
    private String ip;
    private String orderTypeCd;
@@ -51,17 +55,26 @@
    //请求完成时间
    private Date endDate;
    private String reqJson;
    private JSONObject reqJson;
    private String resJson;
    private JSONObject resJson;
    private List<Business> businesses;
    private String requestURL;
    private List<DataFlowLinksCost> linksCostDatas = new ArrayList<DataFlowLinksCost>();
    private List<DataFlowLinksCost> linksCostDates = new ArrayList<DataFlowLinksCost>();
    private Map<String,String> headers = new HashMap<String,String>();
    /*private AppRoute appRoute;*/
    private List<AppRoute> appRoutes = new ArrayList<AppRoute>();
    //请求业务系统报文
    private JSONObject requestBusinessJson;
    //业务系统返回报文
    private JSONObject responseBusinessJson;
    public String getoId() {
        return oId;
@@ -75,7 +88,13 @@
        return businesses;
    }
    public String getDataFlowId() {
        return dataFlowId;
    }
    public void setDataFlowId(String dataFlowId) {
        this.dataFlowId = dataFlowId;
    }
    public void setBusinesses(List<Business> businesses) {
        this.businesses = businesses;
@@ -227,21 +246,7 @@
        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;
@@ -255,23 +260,86 @@
        return headers;
    }
    /*public AppRoute getAppRoute() {
        return appRoute;
    }
    public void setAppRoute(AppRoute appRoute) {
        this.appRoute = appRoute;
    }*/
    public List<AppRoute> getAppRoutes() {
        return appRoutes;
    }
    public void addAppRoutes(AppRoute appRoute) {
        this.appRoutes.add(appRoute);
    }
    public String getIp() {
        return ip;
    }
    public void setIp(String ip) {
        this.ip = ip;
    }
    public JSONObject getRequestBusinessJson() {
        return requestBusinessJson;
    }
    public void setRequestBusinessJson(JSONObject requestBusinessJson) {
        this.requestBusinessJson = requestBusinessJson;
    }
    public JSONObject getResponseBusinessJson() {
        return responseBusinessJson;
    }
    public void setResponseBusinessJson(JSONObject responseBusinessJson) {
        this.responseBusinessJson = responseBusinessJson;
    }
    public JSONObject getReqJson() {
        return reqJson;
    }
    public void setReqJson(JSONObject reqJson) {
        this.reqJson = reqJson;
    }
    public JSONObject getResJson() {
        return resJson;
    }
    public void setResJson(JSONObject resJson) {
        this.resJson = resJson;
    }
    public List<DataFlowLinksCost> getLinksCostDates() {
        return linksCostDates;
    }
    /**
     * 添加各个环节的耗时
     * @param dataFlowLinksCost
     */
    public void addLinksCostDatas(DataFlowLinksCost dataFlowLinksCost){
        this.linksCostDatas.add(dataFlowLinksCost);
    public void addLinksCostDates(DataFlowLinksCost dataFlowLinksCost){
        this.linksCostDates.add(dataFlowLinksCost);
    }
    public DataFlow builder(String reqInfo, HttpServletRequest request) throws Exception{
    public DataFlow builder(String reqInfo, Map<String,String> headerAll) throws Exception{
        try{
            Business business = null;
            JSONObject reqInfoObj = JSONObject.parseObject(reqInfo);
            JSONObject orderObj = reqInfoObj.getJSONObject("orders");
            JSONArray businessArray = reqInfoObj.getJSONArray("business");
            this.setReqJson(reqInfoObj);
            this.setDataFlowId(orderObj.containsKey("dataFlowId")?orderObj.getString("dataFlowId"):"-1");
            this.setAppId(orderObj.getString("appId"));
            this.setAppId(orderObj.getString("transactionId"));
            this.setTransactionId(orderObj.getString("transactionId"));
            this.setUserId(orderObj.getString("userId"));
            this.setOrderTypeCd(orderObj.getString("orderTypeCd"));
            this.setRemark(orderObj.getString("remark"));
@@ -287,101 +355,20 @@
                }
            }
            //将url参数写到header map中
            initUrlParam(request);
            if (headerAll != null){
                this.headers.putAll(headerAll);
                this.setRequestURL(headers.get("REQUEST_URL"));
                this.setIp(headers.get("IP"));
            }
            //将 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;
    }
}