From 69e2baf5518079bfc16cfadc2fb29842fb3de85d Mon Sep 17 00:00:00 2001
From: wuxw <928255095@qq.com>
Date: 星期二, 14 一月 2020 21:02:48 +0800
Subject: [PATCH] 加入userName

---
 Api/src/main/java/com/java110/api/smo/impl/ApiServiceSMOImpl.java |  176 +++++++++++++++++++++++++++++++---------------------------
 1 files changed, 93 insertions(+), 83 deletions(-)

diff --git a/Api/src/main/java/com/java110/api/smo/impl/ApiServiceSMOImpl.java b/Api/src/main/java/com/java110/api/smo/impl/ApiServiceSMOImpl.java
index 4616666..c0f4604 100644
--- a/Api/src/main/java/com/java110/api/smo/impl/ApiServiceSMOImpl.java
+++ b/Api/src/main/java/com/java110/api/smo/impl/ApiServiceSMOImpl.java
@@ -3,14 +3,24 @@
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.java110.api.smo.IApiServiceSMO;
-import com.java110.common.cache.AppRouteCache;
-import com.java110.common.cache.MappingCache;
-import com.java110.common.constant.*;
-import com.java110.common.exception.*;
-import com.java110.common.kafka.KafkaFactory;
-import com.java110.common.log.LoggerEngine;
-import com.java110.common.util.DateUtil;
-import com.java110.common.util.StringUtil;
+import com.java110.utils.cache.AppRouteCache;
+import com.java110.utils.cache.MappingCache;
+import com.java110.utils.constant.CommonConstant;
+import com.java110.utils.constant.KafkaConstant;
+import com.java110.utils.constant.MappingConstant;
+import com.java110.utils.constant.ResponseConstant;
+import com.java110.utils.constant.ServiceCodeConstant;
+import com.java110.utils.exception.BusinessException;
+import com.java110.utils.exception.DecryptException;
+import com.java110.utils.exception.InitConfigDataException;
+import com.java110.utils.exception.ListenerExecuteException;
+import com.java110.utils.exception.NoAuthorityException;
+import com.java110.utils.exception.SMOException;
+import com.java110.utils.kafka.KafkaFactory;
+import com.java110.utils.log.LoggerEngine;
+import com.java110.utils.util.DateUtil;
+import com.java110.utils.util.StringUtil;
+import com.java110.core.client.RestTemplate;
 import com.java110.core.context.ApiDataFlow;
 import com.java110.core.context.DataFlow;
 import com.java110.core.factory.AuthenticationFactory;
@@ -20,13 +30,13 @@
 import com.java110.entity.center.AppService;
 import com.java110.entity.center.DataFlowLinksCost;
 import com.java110.event.service.api.ServiceDataFlowEventPublishing;
-import com.java110.service.smo.IQueryServiceSMO;
 import org.apache.commons.lang3.math.NumberUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
-import org.springframework.web.client.RestTemplate;
 
 import java.util.Date;
 import java.util.List;
@@ -41,6 +51,7 @@
 //@Transactional
 public class ApiServiceSMOImpl extends LoggerEngine implements IApiServiceSMO {
 
+    private static Logger logger = LoggerFactory.getLogger(ApiServiceSMOImpl.class);
 
     @Autowired
     private RestTemplate restTemplate;
@@ -48,18 +59,17 @@
     @Autowired
     private RestTemplate restTemplateNoLoadBalanced;
 
-    @Autowired
-    private IQueryServiceSMO queryServiceSMOImpl;
 
     /**
-     *  鏈嶅姟璋冨害
+     * 鏈嶅姟璋冨害
+     *
      * @param reqJson 璇锋眰鎶ユ枃json
      * @param headers
      * @return
      * @throws SMOException
      */
     @Override
-    public ResponseEntity<String> service(String reqJson, Map<String, String> headers) throws SMOException{
+    public ResponseEntity<String> service(String reqJson, Map<String, String> headers) throws SMOException {
 
         ApiDataFlow dataFlow = null;
 
@@ -71,12 +81,12 @@
 
         try {
             //鍦╬ost鍜� put 鏃舵墠瀛樺湪鎶ユ枃鍔犲瘑鐨勬儏鍐�
-            if("POST,PUT".contains(headers.get(CommonConstant.HTTP_METHOD))){
-                reqJson = decrypt(reqJson,headers);
+            if ("POST,PUT".contains(headers.get(CommonConstant.HTTP_METHOD))) {
+                reqJson = decrypt(reqJson, headers);
             }
 
             //1.0 鍒涘缓鏁版嵁娴�
-            dataFlow = DataFlowFactory.newInstance(ApiDataFlow.class).builder(reqJson,headers);
+            dataFlow = DataFlowFactory.newInstance(ApiDataFlow.class).builder(reqJson, headers);
 
             //2.0 鍔犺浇閰嶇疆淇℃伅
             initConfigData(dataFlow);
@@ -89,20 +99,20 @@
 
             responseEntity = dataFlow.getResponseEntity();
 
-        } catch (DecryptException e){ //瑙e瘑寮傚父
-            responseEntity = new ResponseEntity<String>("瑙e瘑寮傚父锛�"+e.getMessage(), HttpStatus.NON_AUTHORITATIVE_INFORMATION);
-        }catch (BusinessException e) {
-            responseEntity = new ResponseEntity<String>("涓氬姟澶勭悊寮傚父锛�"+e.getMessage(), HttpStatus.BAD_REQUEST);
+        } catch (DecryptException e) { //瑙e瘑寮傚父
+            responseEntity = new ResponseEntity<String>("瑙e瘑寮傚父锛�" + e.getMessage(), HttpStatus.NON_AUTHORITATIVE_INFORMATION);
+        } catch (BusinessException e) {
+            responseEntity = new ResponseEntity<String>("涓氬姟澶勭悊寮傚父锛�" + e.getMessage(), HttpStatus.BAD_REQUEST);
         } catch (NoAuthorityException e) {
-            responseEntity = new ResponseEntity<String>("閴存潈澶辫触锛�"+e.getMessage(), HttpStatus.UNAUTHORIZED);
-        } catch (InitConfigDataException e){
-            responseEntity = new ResponseEntity<String>("鍒濆鍖栧け璐ワ細"+e.getMessage(), HttpStatus.BAD_REQUEST);
-        }catch (Exception e) {
-            logger.error("鍐呴儴寮傚父锛�",e);
-            responseEntity = new ResponseEntity<String>("鍐呴儴寮傚父锛�"+e.getMessage()+e.getLocalizedMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+            responseEntity = new ResponseEntity<String>("閴存潈澶辫触锛�" + e.getMessage(), HttpStatus.UNAUTHORIZED);
+        } catch (InitConfigDataException e) {
+            responseEntity = new ResponseEntity<String>("鍒濆鍖栧け璐ワ細" + e.getMessage(), HttpStatus.BAD_REQUEST);
+        } catch (Exception e) {
+            logger.error("鍐呴儴寮傚父锛�", e);
+            responseEntity = new ResponseEntity<String>("鍐呴儴寮傚父锛�" + e.getMessage() + e.getLocalizedMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
 
         } finally {
-            if(dataFlow != null) {
+            if (dataFlow != null) {
                 //杩欓噷璁板綍鏃ュ織
                 Date endDate = DateUtil.getCurrentDate();
 
@@ -114,7 +124,7 @@
                 //澶勭悊杩斿洖鎶ユ枃閴存潈
                 //AuthenticationFactory.putSign(dataFlow);
             }
-            if (responseEntity == null){
+            if (responseEntity == null) {
                 //resJson = encrypt(responseJson.toJSONString(),headers);
                 responseEntity = new ResponseEntity<String>(resJson, HttpStatus.OK);
             }
@@ -126,32 +136,35 @@
     }
 
 
-
     /**
      * 鎶掑啓杩斿洖澶翠俊鎭�
+     *
      * @param dataFlow
      */
-    private void putResponseHeader(DataFlow dataFlow,Map<String,String> headers) {
+    private void putResponseHeader(DataFlow dataFlow, Map<String, String> headers) {
         headers.put("responseTime", DateUtil.getDefaultFormateTimeString(new Date()));
-        headers.put("transactionId",dataFlow.getTransactionId());
+        headers.put("transactionId", dataFlow.getTransactionId());
     }
 
     /**
      * 瑙e瘑
+     *
      * @param reqJson
      * @return
      */
-    private String decrypt(String reqJson,Map<String,String> headers) throws DecryptException{
+    private String decrypt(String reqJson, Map<String, String> headers) throws DecryptException {
         try {
             if (MappingConstant.VALUE_ON.equals(headers.get(CommonConstant.ENCRYPT))) {
                 logger.debug("瑙e瘑鍓嶅瓧绗︼細" + reqJson);
-                reqJson = new String(AuthenticationFactory.decrypt(reqJson.getBytes("UTF-8"), AuthenticationFactory.loadPrivateKey(MappingConstant.KEY_PRIVATE_STRING)
-                        , NumberUtils.isNumber(headers.get(CommonConstant.ENCRYPT_KEY_SIZE)) ? Integer.parseInt(headers.get(CommonConstant.ENCRYPT_KEY_SIZE)) :
-                                Integer.parseInt(MappingCache.getValue(MappingConstant.KEY_DEFAULT_DECRYPT_KEY_SIZE))),"UTF-8");
+                reqJson = new String(AuthenticationFactory.decrypt(reqJson.getBytes("UTF-8"),
+                        AuthenticationFactory.loadPrivateKey(MappingConstant.KEY_PRIVATE_STRING)
+                        , NumberUtils.isNumber(headers.get(CommonConstant.ENCRYPT_KEY_SIZE)) ?
+                                Integer.parseInt(headers.get(CommonConstant.ENCRYPT_KEY_SIZE)) :
+                                Integer.parseInt(MappingCache.getValue(MappingConstant.KEY_DEFAULT_DECRYPT_KEY_SIZE))), "UTF-8");
                 logger.debug("瑙e瘑鍚庡瓧绗︼細" + reqJson);
             }
-        }catch (Exception e){
-            throw new DecryptException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR,"瑙e瘑澶辫触");
+        } catch (Exception e) {
+            throw new DecryptException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR, "瑙e瘑澶辫触");
         }
 
         return reqJson;
@@ -159,25 +172,25 @@
 
     /**
      * 鍔犲瘑
+     *
      * @param resJson
      * @param headers
      * @return
      */
-    private String encrypt(String resJson,Map<String,String> headers){
+    private String encrypt(String resJson, Map<String, String> headers) {
         try {
             if (MappingConstant.VALUE_ON.equals(headers.get(CommonConstant.ENCRYPT))) {
                 logger.debug("鍔犲瘑鍓嶅瓧绗︼細" + resJson);
                 resJson = new String(AuthenticationFactory.encrypt(resJson.getBytes("UTF-8"), AuthenticationFactory.loadPubKey(MappingConstant.KEY_PUBLIC_STRING)
                         , NumberUtils.isNumber(headers.get(CommonConstant.ENCRYPT_KEY_SIZE)) ? Integer.parseInt(headers.get(CommonConstant.ENCRYPT_KEY_SIZE)) :
-                                Integer.parseInt(MappingCache.getValue(MappingConstant.KEY_DEFAULT_DECRYPT_KEY_SIZE))),"UTF-8");
+                                Integer.parseInt(MappingCache.getValue(MappingConstant.KEY_DEFAULT_DECRYPT_KEY_SIZE))), "UTF-8");
                 logger.debug("鍔犲瘑鍚庡瓧绗︼細" + resJson);
             }
-        }catch (Exception e){
-            logger.error("鍔犲瘑澶辫触锛�",e);
+        } catch (Exception e) {
+            logger.error("鍔犲瘑澶辫触锛�", e);
         }
         return resJson;
     }
-
 
 
     /**
@@ -193,13 +206,13 @@
         if (appRoutes == null) {
             //娣诲姞鑰楁椂
             DataFlowFactory.addCostTime(dataFlow, "initConfigData", "鍔犺浇閰嶇疆鑰楁椂", startDate);
-            throw new InitConfigDataException(ResponseConstant.RESULT_CODE_INNER_ERROR,"褰撳墠娌℃湁鑾峰彇鍒癆ppId瀵瑰簲鐨勪俊鎭紝appId = "+dataFlow.getAppId());
+            throw new InitConfigDataException(ResponseConstant.RESULT_CODE_INNER_ERROR, "褰撳墠娌℃湁鑾峰彇鍒癆ppId瀵瑰簲鐨勪俊鎭紝appId = " + dataFlow.getAppId());
         }
-        for(AppRoute appRoute: appRoutes) {
+        for (AppRoute appRoute : appRoutes) {
             dataFlow.addAppRoutes(appRoute);
         }
         //
-        if("-1".equals(dataFlow.getDataFlowId()) || StringUtil.isNullOrNone(dataFlow.getDataFlowId())){
+        if ("-1".equals(dataFlow.getDataFlowId()) || StringUtil.isNullOrNone(dataFlow.getDataFlowId())) {
             dataFlow.setDataFlowId(GenerateCodeFactory.getDataFlowId());
         }
 
@@ -216,7 +229,7 @@
     private void judgeAuthority(ApiDataFlow dataFlow) throws NoAuthorityException {
         Date startDate = DateUtil.getCurrentDate();
 
-        if (StringUtil.isNullOrNone(dataFlow.getAppId()) || dataFlow.getAppRoutes().size() == 0 ) {
+        if (StringUtil.isNullOrNone(dataFlow.getAppId()) || dataFlow.getAppRoutes().size() == 0) {
             //娣诲姞鑰楁椂
             DataFlowFactory.addCostTime(dataFlow, "judgeAuthority", "閴存潈鑰楁椂", startDate);
             throw new NoAuthorityException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR, "APP_ID 涓虹┖鎴栦笉姝g‘");
@@ -228,9 +241,9 @@
             throw new NoAuthorityException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR, "TRANSACTION_ID 涓嶈兘涓虹┖");
         }
 
-        if(!StringUtil.isNullOrNone(dataFlow.getAppRoutes().get(0).getSecurityCode())){
+        if (!StringUtil.isNullOrNone(dataFlow.getAppRoutes().get(0).getSecurityCode())) {
             String sign = AuthenticationFactory.apiDataFlowMd5(dataFlow);
-            if(!sign.equals(dataFlow.getReqSign().toLowerCase())){
+            if (!sign.equals(dataFlow.getReqSign().toLowerCase())) {
                 throw new NoAuthorityException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR, "绛惧悕澶辫触");
             }
         }
@@ -239,6 +252,10 @@
             //娣诲姞鑰楁椂
             DataFlowFactory.addCostTime(dataFlow, "judgeAuthority", "閴存潈鑰楁椂", startDate);
             throw new NoAuthorityException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR, "requestTime 鏍煎紡涓嶅锛岄伒寰獃yyyMMddHHmmss鏍煎紡");
+        }
+        //鐢ㄦ埛ID鏍¢獙
+        if (StringUtil.isNullOrNone(dataFlow.getUserId())) {
+            throw new NoAuthorityException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR, "USER_ID 涓嶈兘涓虹┖");
         }
 
 
@@ -252,6 +269,7 @@
             throw new NoAuthorityException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR, "AppId 娌℃湁鏉冮檺璁块棶 serviceCode = " + dataFlow.getRequestHeaders().get(CommonConstant.HTTP_SERVICE));
         }
 
+
         //妫�楠岀櫧鍚嶅崟
         List<String> whileListIp = dataFlow.getAppRoutes().get(0).getWhileListIp();
         if (whileListIp != null && whileListIp.size() > 0 && !whileListIp.contains(dataFlow.getIp())) {
@@ -262,7 +280,7 @@
 
         //妫�鏌ラ粦鍚嶅崟
         List<String> backListIp = dataFlow.getAppRoutes().get(0).getBackListIp();
-        if (backListIp != null && backListIp.size() > 0&& backListIp.contains(dataFlow.getIp())) {
+        if (backListIp != null && backListIp.size() > 0 && backListIp.contains(dataFlow.getIp())) {
             //娣诲姞鑰楁椂
             DataFlowFactory.addCostTime(dataFlow, "judgeAuthority", "閴存潈鑰楁椂", startDate);
             throw new NoAuthorityException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR, "褰撳墠IP琚檺鍒朵笉鑳借闂湇鍔�");
@@ -283,66 +301,65 @@
         //鎷垮埌褰撳墠鏈嶅姟
         AppService appService = DataFlowFactory.getService(dataFlow, dataFlow.getRequestHeaders().get(CommonConstant.HTTP_SERVICE));
         //杩欓噷瀵归�忎紶绫诲鐞�
-        if("NT".equals(appService.getIsInstance())){
+        if ("NT".equals(appService.getIsInstance())) {
             //濡傛灉鏄�忎紶绫� 璇锋眰鏂瑰紡蹇呴』涓庢帴鍙f彁渚涙柟璋冪敤鏂瑰紡涓�鑷�
             String httpMethod = dataFlow.getRequestCurrentHeaders().get(CommonConstant.HTTP_METHOD);
-            if(!appService.getMethod().equals(httpMethod)) {
+            if (!appService.getMethod().equals(httpMethod)) {
                 throw new ListenerExecuteException(ResponseConstant.RESULT_CODE_ERROR,
-                        "鏈嶅姟銆�" + appService.getServiceCode() + "銆戣皟鐢ㄦ柟寮忎笉瀵硅妫�鏌�,褰撳墠璇锋眰鏂瑰紡涓猴細"+httpMethod);
+                        "鏈嶅姟銆�" + appService.getServiceCode() + "銆戣皟鐢ㄦ柟寮忎笉瀵硅妫�鏌�,褰撳墠璇锋眰鏂瑰紡涓猴細" + httpMethod);
             }
             dataFlow.setApiCurrentService(ServiceCodeConstant.SERVICE_CODE_DO_SERVICE_TRANSFER);
-        }else{
+        } else {
             dataFlow.setApiCurrentService(dataFlow.getRequestHeaders().get(CommonConstant.HTTP_SERVICE));
         }
-        ServiceDataFlowEventPublishing.multicastEvent(dataFlow,appService);
+        ServiceDataFlowEventPublishing.multicastEvent(dataFlow, appService);
         DataFlowFactory.addCostTime(dataFlow, "invokeBusinessSystem", "璋冪敤涓嬫父绯荤粺鑰楁椂", startDate);
     }
 
 
-
-
-
     /**
      * 淇濆瓨鏃ュ織淇℃伅
+     *
      * @param requestJson
      */
-    private void saveLogMessage(String requestJson,String responseJson){
+    private void saveLogMessage(String requestJson, String responseJson) {
 
-        try{
-            if(MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_LOG_ON_OFF))){
+        try {
+            if (MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_LOG_ON_OFF))) {
                 JSONObject log = new JSONObject();
-                log.put("request",requestJson);
-                log.put("response",responseJson);
-                KafkaFactory.sendKafkaMessage(KafkaConstant.TOPIC_LOG_NAME,"",log.toJSONString());
+                log.put("request", requestJson);
+                log.put("response", responseJson);
+                KafkaFactory.sendKafkaMessage(KafkaConstant.TOPIC_LOG_NAME, "", log.toJSONString());
             }
-        }catch (Exception e){
-            logger.error("鎶ラ敊鏃ュ織鍑洪敊浜嗭紝",e);
+        } catch (Exception e) {
+            logger.error("鎶ラ敊鏃ュ織鍑洪敊浜嗭紝", e);
         }
     }
 
     /**
      * 淇濆瓨鑰楁椂淇℃伅
+     *
      * @param dataFlow
      */
-    private void saveCostTimeLogMessage(DataFlow dataFlow){
-        try{
-            if(MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_COST_TIME_ON_OFF))){
+    private void saveCostTimeLogMessage(DataFlow dataFlow) {
+        try {
+            if (MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_COST_TIME_ON_OFF))) {
                 List<DataFlowLinksCost> dataFlowLinksCosts = dataFlow.getLinksCostDates();
                 JSONObject costDate = new JSONObject();
                 JSONArray costDates = new JSONArray();
                 JSONObject newObj = null;
-                for(DataFlowLinksCost dataFlowLinksCost : dataFlowLinksCosts){
+                for (DataFlowLinksCost dataFlowLinksCost : dataFlowLinksCosts) {
                     newObj = JSONObject.parseObject(JSONObject.toJSONString(dataFlowLinksCost));
-                    newObj.put("dataFlowId",dataFlow.getDataFlowId());
-                    newObj.put("transactionId",dataFlow.getTransactionId());
+                    newObj.put("dataFlowId", dataFlow.getDataFlowId());
+                    newObj.put("transactionId", dataFlow.getTransactionId());
                     costDates.add(newObj);
                 }
-                costDate.put("costDates",costDates);
+                costDate.put("costDates", costDates);
 
-                KafkaFactory.sendKafkaMessage(KafkaConstant.TOPIC_COST_TIME_LOG_NAME,"",costDate.toJSONString());
+                KafkaFactory.sendKafkaMessage(KafkaConstant.TOPIC_COST_TIME_LOG_NAME, "", costDate.toJSONString());
             }
-        }catch (Exception e){
-            logger.error("鎶ラ敊鏃ュ織鍑洪敊浜嗭紝",e);
+        } catch (Exception e) {
+            logger.error("鎶ラ敊鏃ュ織鍑洪敊浜嗭紝", e);
         }
     }
 
@@ -355,13 +372,6 @@
         this.restTemplate = restTemplate;
     }
 
-    public IQueryServiceSMO getQueryServiceSMOImpl() {
-        return queryServiceSMOImpl;
-    }
-
-    public void setQueryServiceSMOImpl(IQueryServiceSMO queryServiceSMOImpl) {
-        this.queryServiceSMOImpl = queryServiceSMOImpl;
-    }
 
     public RestTemplate getRestTemplateNoLoadBalanced() {
         return restTemplateNoLoadBalanced;

--
Gitblit v1.8.0