All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bizmda.bizsip.integrator.controller.IntegratorController Maven / Gradle / Ivy

There is a newer version: 1.0.0.Beta9
Show newest version
package com.bizmda.bizsip.integrator.controller;

import cn.hutool.core.text.StrFormatter;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.bizmda.bizsip.common.*;
import com.bizmda.bizsip.integrator.checkrule.*;
import com.bizmda.bizsip.integrator.config.IntegratorServiceMapping;
import com.bizmda.bizsip.integrator.executor.AbstractIntegratorExecutor;
import com.bizmda.bizsip.integrator.service.SipServiceLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author 史正烨
 */
@Slf4j
@RestController
//@RequestMapping("/")
public class IntegratorController {
    @Autowired
    private IntegratorServiceMapping integratorServiceMapping;
    @Autowired
    private CheckRuleConfigMapping checkRuleConfigMapping;
    @Autowired
    private SipServiceLogService sipServiceLogService;

    @PostMapping(value="/api",consumes = "application/json", produces = "application/json")
    public BizMessage doApiService(HttpServletRequest request, HttpServletResponse response,
                                               @RequestBody JSONObject inJsonObject,
                                               @PathVariable(required = false) Map pathVariables,
                                               @RequestParam(required = false) Map parameters) throws BizException {
        String serviceId = request.getHeader("Biz-Service-Id");
        BizUtils.debug("integrator开始处理服务",serviceId);
        BizUtils.trace("请求消息",inJsonObject);
        BizMessage inMessage = BizMessage.createNewTransaction();
        inMessage.setData(inJsonObject);
        BizUtils.bizMessageThreadLocal.set(inMessage);

        log.debug("校验域级规则");
        JSONArray jsonArray = this.checkFieldRule(serviceId,inJsonObject);
        if (!jsonArray.isEmpty()) {
            log.warn("域级校验出错:{}",jsonArray);
            throw new BizException(BizResultEnum.CHECKRULE_FIELD_CHECK_ERROR,jsonArray.toString());
        }

        log.debug("校验服务级规划");
        jsonArray = this.checkServiceRule(serviceId,inJsonObject);
        if (!jsonArray.isEmpty()) {
            log.warn("服务级检验出错{}",jsonArray);
            throw new BizException(BizResultEnum.CHECKRULE_SERVICE_CHECK_ERROR,jsonArray.toString());
        }

        AbstractIntegratorExecutor integratorService = this.integratorServiceMapping.getIntegratorService(serviceId);
        if (integratorService == null) {
            throw new BizException(BizResultEnum.INTEGRATOR_SERVICE_NOT_FOUND,
                    StrFormatter.format("聚合服务不存在:{}",serviceId));
        }

        BizUtils.tmContextThreadLocal.set(new TmContext());
        BizUtils.serviceIdThreadLocal.set(serviceId);
        BizMessage outMessage = null;
        log.debug("integrator执行聚合服务");
//        log.trace("请求消息:\n{}",BizUtils.buildBizMessageLog(inMessage));
        try {
            outMessage = integratorService.doBizService(inMessage);
        }
        catch (Exception e) {
            log.error("integrator执行出错",e);
            outMessage = BizMessage.buildFailMessage(inMessage,e);
        }
        finally {
            BizUtils.tmContextThreadLocal.remove();
            BizUtils.bizMessageThreadLocal.remove();
            BizUtils.serviceIdThreadLocal.remove();
        }
//        log.trace("响应消息:\n{}",BizUtils.buildBizMessageLog(outMessage));
        if (outMessage.getCode() == 0) {
            log.debug("保存成功日志");
            this.sipServiceLogService.saveSuccessServiceLog(inMessage,outMessage);
        }
        else {
            log.debug("保存失败日志");
            this.sipServiceLogService.saveErrorServiceLog(inMessage,outMessage);
        }
        return outMessage;
    }

    private JSONArray checkFieldRule(String serviceId, JSONObject message) throws BizException {
        BizUtils.debug("入参",serviceId,message);
        JSONArray jsonArray = new JSONArray();
        CheckRuleConfig checkRuleConfig = this.checkRuleConfigMapping.getCheckRuleConfig(serviceId);
        if (checkRuleConfig == null) {
            return jsonArray;
        }
        List fieldCheckRuleList = checkRuleConfig.getFieldCheckRuleList();
        if (fieldCheckRuleList == null) {
            return jsonArray;
        }
        List fieldChcekRuleResultList = FieldCheckRuleHelper.checkFieldRule(message, fieldCheckRuleList,checkRuleConfig.getFieldCheckMode());

        for(FieldChcekRuleResult fieldChcekRuleResult:fieldChcekRuleResultList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.set("field", fieldChcekRuleResult.getField());
            jsonObject.set("message", fieldChcekRuleResult.getMessage());
            jsonArray.add(jsonObject);
        }
        BizUtils.debug("返回",jsonArray);
        return jsonArray;
    }

    private JSONArray checkServiceRule(String serviceId, JSONObject message) throws BizException {
        BizUtils.debug("入参",serviceId,message);
        JSONArray jsonArray = new JSONArray();
        CheckRuleConfig checkRuleConfig = this.checkRuleConfigMapping.getCheckRuleConfig(serviceId);
        if (checkRuleConfig == null) {
            return jsonArray;
        }
        List serviceCheckRuleList = checkRuleConfig.getServiceCheckRuleList();
        if (serviceCheckRuleList == null) {
            return jsonArray;
        }
        List serviceChcekRuleResultList = ServiceCheckRuleHelper.checkServiceRule(message, serviceCheckRuleList,checkRuleConfig.getFieldCheckMode());

        for(ServiceChcekRuleResult serviceChcekRuleResult:serviceChcekRuleResultList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.set("message", serviceChcekRuleResult.getResult());
            jsonArray.add(jsonObject);
        }
        BizUtils.debug("返回",jsonArray);
        return jsonArray;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy