old mode 100644
new mode 100755
| | |
| | | import com.java110.core.factory.AuthenticationFactory; |
| | | import com.java110.core.factory.DataFlowFactory; |
| | | import com.java110.core.factory.GenerateCodeFactory; |
| | | import com.java110.core.log.LoggerFactory; |
| | | import com.java110.core.smo.ISaveTransactionLogSMO; |
| | | import com.java110.core.trace.Java110TraceLog; |
| | | import com.java110.entity.center.AppRoute; |
| | | import com.java110.entity.center.AppService; |
| | | import com.java110.entity.center.DataFlowLinksCost; |
| | | import com.java110.po.transactionLog.TransactionLogPo; |
| | | import com.java110.utils.cache.AppRouteCache; |
| | | import com.java110.utils.cache.MappingCache; |
| | | import com.java110.utils.constant.*; |
| | |
| | | import com.java110.utils.log.LoggerEngine; |
| | | import com.java110.utils.util.DateUtil; |
| | | import com.java110.utils.util.StringUtil; |
| | | import com.java110.vo.ResultVo; |
| | | 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; |
| | |
| | | @Autowired |
| | | private RestTemplate outRestTemplate; |
| | | |
| | | @Autowired |
| | | private ISaveTransactionLogSMO saveTransactionLogSMOImpl; |
| | | |
| | | |
| | | /** |
| | | * 服务调度 |
| | |
| | | * @throws SMOException |
| | | */ |
| | | @Override |
| | | @Java110TraceLog |
| | | public ResponseEntity<String> service(String reqJson, Map<String, String> headers) throws SMOException { |
| | | |
| | | ApiDataFlow dataFlow = null; |
| | | |
| | | //JSONObject responseJson = null; |
| | | Date startDate = DateUtil.getCurrentDate(); |
| | | |
| | | ResponseEntity<String> responseEntity = null; |
| | | |
| | |
| | | responseEntity = dataFlow.getResponseEntity(); |
| | | |
| | | } catch (DecryptException e) { //解密异常 |
| | | responseEntity = new ResponseEntity<String>("解密异常:" + e.getMessage(), HttpStatus.NON_AUTHORITATIVE_INFORMATION); |
| | | logger.error("内部异常:", e); |
| | | responseEntity = ResultVo.error("解密异常:" + e.getMessage()); |
| | | } catch (BusinessException e) { |
| | | responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.BAD_REQUEST); |
| | | logger.error("内部异常:", e); |
| | | responseEntity = ResultVo.error(e.getMessage()); |
| | | } catch (NoAuthorityException e) { |
| | | responseEntity = new ResponseEntity<String>("鉴权失败:" + e.getMessage(), HttpStatus.UNAUTHORIZED); |
| | | logger.error("内部异常:", e); |
| | | responseEntity = ResultVo.error("鉴权失败:" + e.getMessage()); |
| | | } catch (InitConfigDataException e) { |
| | | responseEntity = new ResponseEntity<String>("初始化失败:" + e.getMessage(), HttpStatus.BAD_REQUEST); |
| | | logger.error("内部异常:", e); |
| | | responseEntity = ResultVo.error("初始化失败:" + e.getMessage()); |
| | | } catch (Exception e) { |
| | | logger.error("内部异常:", e); |
| | | responseEntity = new ResponseEntity<String>("内部异常:" + e.getMessage() + e.getLocalizedMessage(), HttpStatus.INTERNAL_SERVER_ERROR); |
| | | |
| | | responseEntity = ResultVo.error("内部异常:" + e.getMessage() + e.getLocalizedMessage()); |
| | | } finally { |
| | | Date endDate = DateUtil.getCurrentDate(); |
| | | if (dataFlow != null) { |
| | | //这里记录日志 |
| | | Date endDate = DateUtil.getCurrentDate(); |
| | | |
| | | dataFlow.setEndDate(endDate); |
| | | //添加耗时 |
| | | //DataFlowFactory.addCostTime(dataFlow, "service", "业务处理总耗时", dataFlow.getStartDate(), dataFlow.getEndDate()); |
| | | //保存耗时 |
| | | //saveCostTimeLogMessage(dataFlow); |
| | | //处理返回报文鉴权 |
| | | //AuthenticationFactory.putSign(dataFlow); |
| | | } |
| | |
| | | //resJson = encrypt(responseJson.toJSONString(),headers); |
| | | responseEntity = new ResponseEntity<String>(resJson, HttpStatus.OK); |
| | | } |
| | | //这里保存耗时,以及日志 |
| | | return responseEntity; |
| | | |
| | | //添加耗时 |
| | | saveLog(dataFlow, startDate, endDate, reqJson, responseEntity); |
| | | //这里保存耗时,以及日志 |
| | | } |
| | | return responseEntity; |
| | | } |
| | | |
| | | /** |
| | | * 日志记录 |
| | | * |
| | | * @param dataFlow |
| | | * @param startDate |
| | | * @param endDate |
| | | */ |
| | | private void saveLog(ApiDataFlow dataFlow, Date startDate, Date endDate, String reqJson, ResponseEntity<String> responseEntity) { |
| | | |
| | | if (dataFlow == null) { |
| | | return; |
| | | } |
| | | |
| | | String serviceCode = dataFlow.getRequestHeaders().get(CommonConstant.HTTP_SERVICE); |
| | | |
| | | String logServiceCode = MappingCache.getValue(MappingConstant.DOMAIN_SYSTEM_SWITCH,MappingCache.LOG_SERVICE_CODE); |
| | | |
| | | //日志查询不记录 |
| | | if ("/transactionLog/queryTransactionLog".equals(serviceCode) |
| | | || "/transactionLog/queryTransactionLogMessage".equals(serviceCode) |
| | | || "file.getFile".equals(serviceCode) |
| | | || "file.getFileByObjId".equals(serviceCode) |
| | | || "/machine/heartbeat".equals(serviceCode) // 心跳也不记录 |
| | | ) { |
| | | return; |
| | | } |
| | | |
| | | if (StringUtil.isEmpty(logServiceCode) || "OFF".equals(logServiceCode.toUpperCase())) { |
| | | return; |
| | | } |
| | | if (logServiceCode.contains("|")) { |
| | | String[] logServiceCodes = logServiceCode.split("|"); |
| | | |
| | | for (String lServiceCode : logServiceCodes) { |
| | | if (serviceCode.equals(lServiceCode.trim())) { |
| | | doSaveLog(dataFlow, startDate, endDate, serviceCode, reqJson, responseEntity); |
| | | return; |
| | | } |
| | | } |
| | | } |
| | | |
| | | if ("all".equals(logServiceCode.trim().toLowerCase())) { |
| | | doSaveLog(dataFlow, startDate, endDate, serviceCode, reqJson, responseEntity); |
| | | return; |
| | | } |
| | | |
| | | if (serviceCode.equals(logServiceCode.trim())) { |
| | | doSaveLog(dataFlow, startDate, endDate, serviceCode, reqJson, responseEntity); |
| | | } |
| | | } |
| | | |
| | | |
| | | private void doSaveLog(ApiDataFlow dataFlow, Date startDate, Date endDate, String serviceCode, String reqJson, ResponseEntity<String> responseEntity) { |
| | | |
| | | TransactionLogPo transactionLogPo = new TransactionLogPo(); |
| | | transactionLogPo.setAppId(dataFlow.getAppId()); |
| | | transactionLogPo.setCostTime((endDate.getTime() - startDate.getTime()) + ""); |
| | | transactionLogPo.setIp(""); |
| | | transactionLogPo.setServiceCode(serviceCode); |
| | | transactionLogPo.setSrcIp(dataFlow.getRequestHeaders().get(CommonConstant.HTTP_SRC_IP)); |
| | | transactionLogPo.setState(responseEntity.getStatusCode() != HttpStatus.OK ? "F" : "S"); |
| | | transactionLogPo.setTimestamp(dataFlow.getRequestTime()); |
| | | transactionLogPo.setUserId(dataFlow.getUserId()); |
| | | transactionLogPo.setTransactionId(dataFlow.getTransactionId()); |
| | | transactionLogPo.setRequestHeader(dataFlow.getRequestHeaders() != null ? dataFlow.getRequestHeaders().toString() : ""); |
| | | transactionLogPo.setResponseHeader(responseEntity.getHeaders().toSingleValueMap().toString()); |
| | | transactionLogPo.setRequestMessage(reqJson); |
| | | transactionLogPo.setResponseMessage(responseEntity.getBody()); |
| | | saveTransactionLogSMOImpl.saveLog(transactionLogPo); |
| | | } |
| | | |
| | | |
| | |
| | | if (!StringUtil.isNullOrNone(dataFlow.getAppRoutes().get(0).getSecurityCode())) { |
| | | String sign = AuthenticationFactory.apiDataFlowMd5(dataFlow); |
| | | if (!sign.equals(dataFlow.getReqSign().toLowerCase())) { |
| | | throw new NoAuthorityException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR, "签名失败" + sign); |
| | | throw new NoAuthorityException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR, "签名失败"); |
| | | } |
| | | } |
| | | |
| | |
| | | "服务【" + appService.getServiceCode() + "】调用方式不对请检查,当前请求方式为:" + httpMethod); |
| | | } |
| | | dataFlow.setApiCurrentService(ServiceCodeConstant.SERVICE_CODE_SYSTEM_TRANSFER); |
| | | } else if ("CMD".equals(appService.getIsInstance())) { |
| | | //如果是透传类 请求方式必须与接口提供方调用方式一致 |
| | | String httpMethod = dataFlow.getRequestCurrentHeaders().get(CommonConstant.HTTP_METHOD); |
| | | if (!appService.getMethod().equals(httpMethod)) { |
| | | throw new ListenerExecuteException(ResponseConstant.RESULT_CODE_ERROR, |
| | | "服务【" + appService.getServiceCode() + "】调用方式不对请检查,当前请求方式为:" + httpMethod); |
| | | } |
| | | dataFlow.setApiCurrentService(ServiceCodeConstant.SERVICE_CODE_SYSTEM_CMD); |
| | | } else { |
| | | dataFlow.setApiCurrentService(dataFlow.getRequestHeaders().get(CommonConstant.HTTP_SERVICE)); |
| | | } |
| | |
| | | private void saveLogMessage(String requestJson, String responseJson) { |
| | | |
| | | try { |
| | | if (MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_LOG_ON_OFF))) { |
| | | if (MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.DOMAIN_SYSTEM_SWITCH,MappingConstant.KEY_LOG_ON_OFF))) { |
| | | JSONObject log = new JSONObject(); |
| | | log.put("request", requestJson); |
| | | log.put("response", responseJson); |
| | |
| | | */ |
| | | private void saveCostTimeLogMessage(DataFlow dataFlow) { |
| | | try { |
| | | if (MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_COST_TIME_ON_OFF))) { |
| | | if (MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.DOMAIN_SYSTEM_SWITCH,MappingConstant.KEY_COST_TIME_ON_OFF))) { |
| | | List<DataFlowLinksCost> dataFlowLinksCosts = dataFlow.getLinksCostDates(); |
| | | JSONObject costDate = new JSONObject(); |
| | | JSONArray costDates = new JSONArray(); |