Commit 93162abe authored by zhangc's avatar zhangc

添加api测试接口

parent edbf048a
...@@ -9,6 +9,7 @@ CREATE TABLE `t_api_interface` ( ...@@ -9,6 +9,7 @@ CREATE TABLE `t_api_interface` (
`api_path` varchar(100) DEFAULT NULL COMMENT '请求地址', `api_path` varchar(100) DEFAULT NULL COMMENT '请求地址',
`target_url` varchar(100) DEFAULT NULL COMMENT '目标url', `target_url` varchar(100) DEFAULT NULL COMMENT '目标url',
`api_type` varchar(50) NULL DEFAULT '' COMMENT 'api类型:1 数据银行制作大数据表 2 数据银行制作数据包,3,数据银行制作自定义API 4 API实时接入 6 标签查询 9自定义', `api_type` varchar(50) NULL DEFAULT '' COMMENT 'api类型:1 数据银行制作大数据表 2 数据银行制作数据包,3,数据银行制作自定义API 4 API实时接入 6 标签查询 9自定义',
`req_method` varchar(20) NULL DEFAULT NULL COMMENT '请求方式: GET, POST',
`api_function` varchar(200) DEFAULT NULL COMMENT '接口功能', `api_function` varchar(200) DEFAULT NULL COMMENT '接口功能',
`join_type` varchar(50) NULL DEFAULT NULL COMMENT '接入类型:字典表对应key值', `join_type` varchar(50) NULL DEFAULT NULL COMMENT '接入类型:字典表对应key值',
`test_example` varchar(255) NULL DEFAULT NULL COMMENT '测试实例', `test_example` varchar(255) NULL DEFAULT NULL COMMENT '测试实例',
...@@ -49,6 +50,8 @@ CREATE TABLE `t_api_interface_custom` ( ...@@ -49,6 +50,8 @@ CREATE TABLE `t_api_interface_custom` (
`resp_code` varchar(300) DEFAULT NULL COMMENT '响应状态码', `resp_code` varchar(300) DEFAULT NULL COMMENT '响应状态码',
`api_example` varchar(300) DEFAULT NULL COMMENT 'api返回样例', `api_example` varchar(300) DEFAULT NULL COMMENT 'api返回样例',
`inbox_param` varchar(300) NULL DEFAULT NULL COMMENT '固定参数', `inbox_param` varchar(300) NULL DEFAULT NULL COMMENT '固定参数',
`page_num` int(10) NULL DEFAULT '1' COMMENT '当前页',
`page_size` int(10) NULL DEFAULT '100' COMMENT '当前页显示数'
`remark` varchar(500) DEFAULT NULL COMMENT '备注', `remark` varchar(500) DEFAULT NULL COMMENT '备注',
`create_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `create_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`create_user` varchar(100) DEFAULT NULL COMMENT '创建人', `create_user` varchar(100) DEFAULT NULL COMMENT '创建人',
......
...@@ -37,7 +37,6 @@ public enum GatewayResultCode implements ResultCode { ...@@ -37,7 +37,6 @@ public enum GatewayResultCode implements ResultCode {
ILLEGAL_TIMETEMP("ILLEGAL_TIMETEMP", "无效时间戳"), ILLEGAL_TIMETEMP("ILLEGAL_TIMETEMP", "无效时间戳"),
/** 请求次数受限 */ /** 请求次数受限 */
REQUEST_LIMIT_EXCEPTION("REQUEST_LIMIT_EXCEPTION", "请求次数受限"), REQUEST_LIMIT_EXCEPTION("REQUEST_LIMIT_EXCEPTION", "请求次数受限"),
...@@ -60,6 +59,12 @@ public enum GatewayResultCode implements ResultCode { ...@@ -60,6 +59,12 @@ public enum GatewayResultCode implements ResultCode {
/** API状态异常 */ /** API状态异常 */
API_STATUS_EXCEPTION("API_STATUS_EXCEPTION", "API状态异常"), API_STATUS_EXCEPTION("API_STATUS_EXCEPTION", "API状态异常"),
/** API类型错误 */
API_TYPE_ERROR("API_TYPE_ERROR", "API类型错误"),
/** 文件地址不存在 */
DATA_BIG_ADDR_UNEXIST("DATA_BIG_ADDR_UNEXIST", "文件地址不存在!"),
/** 请求组织状态异常 */ /** 请求组织状态异常 */
ORG_STATE_EXCEPTION("ORG_STATE_EXCEPTION", "请求组织状态异常"), ORG_STATE_EXCEPTION("ORG_STATE_EXCEPTION", "请求组织状态异常"),
/** 请求信息不存在 */ /** 请求信息不存在 */
......
...@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; ...@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jz.common.utils.HttpsUtils; import com.jz.common.utils.HttpsUtils;
import com.jz.common.utils.RedisUtils; import com.jz.common.utils.RedisUtils;
import com.jz.dm.common.constant.Constants; import com.jz.dm.common.constant.Constants;
import com.jz.dm.common.constant.TagConstants;
import com.jz.dm.common.enums.GatewayResultCode; import com.jz.dm.common.enums.GatewayResultCode;
import com.jz.dm.common.enums.apiInterface.ApiStatusEnum; import com.jz.dm.common.enums.apiInterface.ApiStatusEnum;
import com.jz.dm.common.enums.auth.AuthReqTypeEnum; import com.jz.dm.common.enums.auth.AuthReqTypeEnum;
...@@ -89,7 +90,11 @@ public class AuthFilter extends AbstractFilter { ...@@ -89,7 +90,11 @@ public class AuthFilter extends AbstractFilter {
} }
} }
JSONObject parameter = JSONObject.parseObject(request.getParams()); JSONObject parameter = JSONObject.parseObject(request.getParams());
if (null != parameter) { if (null == parameter) {
throw new GatewayException(GatewayResultCode.REQUEST_PARAM_EMPTY);
}
Boolean isTest = parameter.getBoolean("isTest");
if (!isTest) {//是否测试数据
String authCode = parameter.getString("authCode"); String authCode = parameter.getString("authCode");
ApiAuth authAuth = authService.getAuthUser(authCode, apiInterface.getId()); ApiAuth authAuth = authService.getAuthUser(authCode, apiInterface.getId());
if (null == authAuth) { if (null == authAuth) {
...@@ -107,7 +112,7 @@ public class AuthFilter extends AbstractFilter { ...@@ -107,7 +112,7 @@ public class AuthFilter extends AbstractFilter {
if (!apiOrg.getStatus()) { if (!apiOrg.getStatus()) {
throw new GatewayException(GatewayResultCode.ORG_STATE_EXCEPTION); throw new GatewayException(GatewayResultCode.ORG_STATE_EXCEPTION);
} }
checkBill(request, authCode, authAuth, apiOrg); checkBill(request, authCode, authAuth);
} }
chain.doFilter(request, response); chain.doFilter(request, response);
} catch (Exception e) { } catch (Exception e) {
...@@ -132,19 +137,17 @@ public class AuthFilter extends AbstractFilter { ...@@ -132,19 +137,17 @@ public class AuthFilter extends AbstractFilter {
* @param request * @param request
* @param authCode * @param authCode
* @param authAuth * @param authAuth
* @param apiOrg
* @throws ParseException * @throws ParseException
*/ */
private void checkBill(GatewayRequest request, String authCode, private void checkBill(GatewayRequest request, String authCode,
ApiAuth authAuth, ApiOrg apiOrg) throws ParseException { ApiAuth authAuth) throws ParseException {
switch (authAuth.getAuthMode()) { switch (authAuth.getAuthMode()) {
case "POWER_CALL_MODE": //按次调用 case "POWER_CALL_MODE": //按次调用
//查询数据银行银行余额是否充足 //查询数据银行银行余额是否充足
getDataAmountResult(request); getDataAmountResult(request);
try { //记录请求次数(每天限制请求次数) try { //记录请求次数(每天限制请求次数)
// String limitKey = TagConstants.OPEN_API_REDIS_LIMIT_KEY + authCode; String limitKey = TagConstants.OPEN_API_REDIS_LIMIT_KEY + authCode;
String limitKey = authCode; Integer reqValue = (Integer) redisUtils.getObj(limitKey);
String reqValue = (String)redisUtils.getObj(limitKey);
long timeOut = 0; long timeOut = 0;
if (AuthReqTypeEnum.DAY.name().equals(authAuth.getReqType())) { //按天 if (AuthReqTypeEnum.DAY.name().equals(authAuth.getReqType())) { //按天
timeOut = DateUtil.calculateNowResidueTime(); timeOut = DateUtil.calculateNowResidueTime();
...@@ -154,11 +157,11 @@ public class AuthFilter extends AbstractFilter { ...@@ -154,11 +157,11 @@ public class AuthFilter extends AbstractFilter {
//暂时不支持年 //暂时不支持年
} }
if (null != reqValue) { if (null != reqValue) {
Integer value = Integer.valueOf(reqValue); /* Integer value = Integer.valueOf(reqValue);*/
if (value > authAuth.getReqFrequency()) {//超出最大请求次数 if (reqValue > authAuth.getReqFrequency()) {//超出最大请求次数
throw new GatewayException(GatewayResultCode.REQUEST_LIMIT_EXCEPTION); throw new GatewayException(GatewayResultCode.REQUEST_LIMIT_EXCEPTION);
} else if (value <= authAuth.getReqFrequency()) { } else if (reqValue <= authAuth.getReqFrequency()) {
redisUtils.delAndAdd(limitKey, limitKey, value + 1, timeOut); redisUtils.delAndAdd(limitKey, limitKey, reqValue + 1, timeOut);
} }
} else { } else {
redisUtils.set(limitKey, 1, timeOut); redisUtils.set(limitKey, 1, timeOut);
...@@ -217,7 +220,7 @@ public class AuthFilter extends AbstractFilter { ...@@ -217,7 +220,7 @@ public class AuthFilter extends AbstractFilter {
pObject.put("assetsId", assetsId); pObject.put("assetsId", assetsId);
pObject.put("userId", userId); pObject.put("userId", userId);
pObject.put("dataPrice", dataPrice); pObject.put("dataPrice", dataPrice);
String respResult = httpUtils.submitPost(balanceUrl+"/mall/financeCustomerAssets/findAssets", pObject.toString()); String respResult = httpUtils.submitPost(balanceUrl + "/mall/financeCustomerAssets/findAssets", pObject.toString());
JSONObject result = JSONObject.parseObject(respResult); JSONObject result = JSONObject.parseObject(respResult);
if (null != result) { if (null != result) {
if (200 != result.getInteger("code")) { if (200 != result.getInteger("code")) {
......
...@@ -41,10 +41,12 @@ public class VerifySignFilter extends AbstractFilter { ...@@ -41,10 +41,12 @@ public class VerifySignFilter extends AbstractFilter {
protected void internalDoFilter(GatewayRequest request, GatewayResponse response, protected void internalDoFilter(GatewayRequest request, GatewayResponse response,
FilterChain chain) { FilterChain chain) {
try { try {
JSONObject jsonObject = JSONObject.parseObject(request.getParams());
if (!jsonObject.getBoolean("isTest")){//是否测试调用
//对签约参数进行字典排序 //对签约参数进行字典排序
String signParams = MapUtil.getSignValue(request.getApiKey(),request.getMethod(),request.getSignType()); String signParams = MapUtil.getSignValue(request.getApiKey(),request.getMethod(),request.getSignType());
if (StringUtils.isNotBlank(signParams)){ if (StringUtils.isNotBlank(signParams)){
JSONObject jsonObject = JSONObject.parseObject(request.getParams());
String authCode = jsonObject.getString("authCode"); String authCode = jsonObject.getString("authCode");
//需要传入授权码 //需要传入授权码
ApiAuth apiAuthInfo = apiInterfaceService.getApiAuthInfo(request.getApiKey(),authCode ); ApiAuth apiAuthInfo = apiInterfaceService.getApiAuthInfo(request.getApiKey(),authCode );
...@@ -58,6 +60,7 @@ public class VerifySignFilter extends AbstractFilter { ...@@ -58,6 +60,7 @@ public class VerifySignFilter extends AbstractFilter {
}else { }else {
throw new GatewayException(GatewayResultCode.SIGN_ERROR); throw new GatewayException(GatewayResultCode.SIGN_ERROR);
} }
}
chain.doFilter(request, response); chain.doFilter(request, response);
} catch (SignatureException ex) { } catch (SignatureException ex) {
LogUtil.error(LOGGER, ex, LogUtil.error(LOGGER, ex,
......
...@@ -66,6 +66,11 @@ public class ApiInterface extends BaseObject implements Serializable { ...@@ -66,6 +66,11 @@ public class ApiInterface extends BaseObject implements Serializable {
*/ */
@TableField("target_url") @TableField("target_url")
private String targetUrl; private String targetUrl;
/**
* 请求方式:GET POST
*/
@TableField("req_method")
private String reqMethod;
/** /**
* 接入类型:对应字典表key * 接入类型:对应字典表key
*/ */
......
...@@ -93,5 +93,15 @@ public class ApiInterfaceCustom extends BaseObject implements Serializable { ...@@ -93,5 +93,15 @@ public class ApiInterfaceCustom extends BaseObject implements Serializable {
*/ */
@TableField("inbox_param") @TableField("inbox_param")
private String inboxParam; private String inboxParam;
/**
* 当前页
*/
@TableField("page_num")
private Integer pageNum;
/**
* 当前页显示数据
*/
@TableField("page_size")
private Integer pageSize;
} }
...@@ -40,6 +40,9 @@ public class ApiInterfaceReq implements Serializable { ...@@ -40,6 +40,9 @@ public class ApiInterfaceReq implements Serializable {
@NotNull(message="目标地址不能为空") @NotNull(message="目标地址不能为空")
public String targetUrl; public String targetUrl;
@ApiModelProperty(value = "请求方式:GET POST",required = false)
public String reqMethod;
@ApiModelProperty(value = "超时时间",required = true) @ApiModelProperty(value = "超时时间",required = true)
@NotNull(message="超时时间不能为空") @NotNull(message="超时时间不能为空")
public String timeout; public String timeout;
...@@ -54,7 +57,7 @@ public class ApiInterfaceReq implements Serializable { ...@@ -54,7 +57,7 @@ public class ApiInterfaceReq implements Serializable {
@ApiModelProperty(value = "api描述",required = false) @ApiModelProperty(value = "api描述",required = false)
public String apiFunction; public String apiFunction;
@ApiModelProperty(value = "父类文件id,一级文件夹传入0",required = false) /* @ApiModelProperty(value = "父类文件id,一级文件夹传入0",required = false)
public Long parentId; public Long parentId;
@ApiModelProperty(value = "文件id",required = false) @ApiModelProperty(value = "文件id",required = false)
...@@ -62,7 +65,7 @@ public class ApiInterfaceReq implements Serializable { ...@@ -62,7 +65,7 @@ public class ApiInterfaceReq implements Serializable {
@ApiModelProperty(value = "项目id",required = true) @ApiModelProperty(value = "项目id",required = true)
@NotNull(message="项目id不能为空") @NotNull(message="项目id不能为空")
public Long projectId; public Long projectId;*/
@ApiModelProperty(value = "状态",required = false) @ApiModelProperty(value = "状态",required = false)
......
...@@ -40,9 +40,13 @@ public class MakeDataBankApiReq implements Serializable { ...@@ -40,9 +40,13 @@ public class MakeDataBankApiReq implements Serializable {
@NotNull(message = "输出类型不能为空") @NotNull(message = "输出类型不能为空")
public String outputType; public String outputType;
@ApiModelProperty(value = "是否分页:",required = true) @ApiModelProperty(value = "请求方式:GET POST",required = true)
@NotNull(message = "请求方式不能为空")
public String reqMethod;
/*@ApiModelProperty(value = "是否分页:",required = true)
@NotNull(message = "是否分页不能为空") @NotNull(message = "是否分页不能为空")
public Boolean page; public Boolean page;*/
@ApiModelProperty(value = "限流类型:DAY 按天,MONTH 按月",required = true) @ApiModelProperty(value = "限流类型:DAY 按天,MONTH 按月",required = true)
@NotNull(message = "限流类型不能为空") @NotNull(message = "限流类型不能为空")
...@@ -52,9 +56,15 @@ public class MakeDataBankApiReq implements Serializable { ...@@ -52,9 +56,15 @@ public class MakeDataBankApiReq implements Serializable {
@NotNull(message = "目标地址不能为空") @NotNull(message = "目标地址不能为空")
public String targetUrl; public String targetUrl;
@ApiModelProperty(value = "文件id",required = true) @ApiModelProperty(value = "当前页",required = true)
@NotNull(message = "文件id不能为空") public Integer pageNum;
public Long fileId;
@ApiModelProperty(value = "当前页显示条数",required = true)
public Integer pageSize;
//@ApiModelProperty(value = "文件id",required = true)
//@NotNull(message = "文件id不能为空")
//public Long fileId;
@ApiModelProperty(value = "签名",required = true) @ApiModelProperty(value = "签名",required = true)
@NotNull(message = "签名不能为空") @NotNull(message = "签名不能为空")
...@@ -69,7 +79,8 @@ public class MakeDataBankApiReq implements Serializable { ...@@ -69,7 +79,8 @@ public class MakeDataBankApiReq implements Serializable {
@ApiModelProperty(value = "接口描述",required = false) @ApiModelProperty(value = "接口描述",required = false)
public String apiDesc; public String apiDesc;
@ApiModelProperty(value = "数据包下载路径",required = false)
public String fileLocation;
@ApiModelProperty(value = "超时时间",required = false) @ApiModelProperty(value = "超时时间",required = false)
public String timeout; public String timeout;
......
...@@ -146,13 +146,13 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -146,13 +146,13 @@ public class ProducerServiceImpl implements ProducerService {
@Override @Override
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW) @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public Result addDataBankApiInfo(MakeDataBankApiReq req) { public Result addDataBankApiInfo(MakeDataBankApiReq req) {
QueryWrapper<ApiInterfaceFile> query = new QueryWrapper<>(); /* QueryWrapper<ApiInterfaceFile> query = new QueryWrapper<>();
query.eq("file_source", "2"); query.eq("file_source", "2");
query.eq("is_deleted", 0); query.eq("is_deleted", 0);
List<ApiInterfaceFile> fileSource = apiInterfaceFileMapper.selectList(query); List<ApiInterfaceFile> fileSource = apiInterfaceFileMapper.selectList(query);
if (fileSource.size() == 0 || fileSource.size() >= 2) { if (fileSource.size() == 0 || fileSource.size() >= 2) {
return Result.of_error("文件夹信息异常!"); return Result.of_error("文件夹信息异常!");
} }*/
if (StringUtils.isNotBlank(req.getOutputType())) { if (StringUtils.isNotBlank(req.getOutputType())) {
if (!req.getOutputType().contains(ApiInfoOutTypeEnum.JSON.name()) || if (!req.getOutputType().contains(ApiInfoOutTypeEnum.JSON.name()) ||
!req.getOutputType().contains(ApiInfoOutTypeEnum.FLOW.name())) { !req.getOutputType().contains(ApiInfoOutTypeEnum.FLOW.name())) {
...@@ -175,6 +175,7 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -175,6 +175,7 @@ public class ProducerServiceImpl implements ProducerService {
saveDataBanker(req, apiKey, "3"); saveDataBanker(req, apiKey, "3");
break; break;
case "10007"://数据银行数据包上传 case "10007"://数据银行数据包上传
if(StringUtils.isNotBlank(req.getFileLocation()))return Result.of_error("文件地址不存在!");
saveDataBanker(req, apiKey, "2"); saveDataBanker(req, apiKey, "2");
break; break;
case "10008"://数据银行制作大数据表 case "10008"://数据银行制作大数据表
...@@ -199,6 +200,7 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -199,6 +200,7 @@ public class ProducerServiceImpl implements ProducerService {
JSONObject object = new JSONObject(); JSONObject object = new JSONObject();
object.put("targetUrl", req.getTargetUrl()); object.put("targetUrl", req.getTargetUrl());
object.put("outputType", req.getOutputType()); object.put("outputType", req.getOutputType());
object.put("reqMethod",req.getReqMethod());
saveObjOnRedis(apiInterface.getApiKey(), object); saveObjOnRedis(apiInterface.getApiKey(), object);
} }
...@@ -340,10 +342,10 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -340,10 +342,10 @@ public class ProducerServiceImpl implements ProducerService {
//生成ApiKey //生成ApiKey
String apiKey = getApiKey(); String apiKey = getApiKey();
ApiInterface apiInterface = new ApiInterface(); ApiInterface apiInterface = new ApiInterface();
if (null == req.getFileId()) {//创建下级文件夹 /* if (null == req.getFileId()) {//创建下级文件夹
Long fileId = checkFileExist(req.getProjectId(), req.getParentId(), req.getCreateUser()); Long fileId = checkFileExist(req.getProjectId(), req.getParentId(), req.getCreateUser());
req.setFileId(fileId); req.setFileId(fileId);
} }*/
BeanUtils.copyProperties(req, apiInterface); BeanUtils.copyProperties(req, apiInterface);
apiInterface.setApiKey(apiKey); apiInterface.setApiKey(apiKey);
apiInterface.setApiProtocl(req.getTransMode());//传输方式 https http apiInterface.setApiProtocl(req.getTransMode());//传输方式 https http
......
...@@ -15,11 +15,13 @@ import com.jz.dm.common.util.stream.HttpDownload; ...@@ -15,11 +15,13 @@ import com.jz.dm.common.util.stream.HttpDownload;
import com.jz.dm.gateway.OpenApiService; import com.jz.dm.gateway.OpenApiService;
import com.jz.dm.models.domian.ApiAuth; import com.jz.dm.models.domian.ApiAuth;
import com.jz.dm.models.domian.ApiInterface; import com.jz.dm.models.domian.ApiInterface;
import com.jz.dm.models.domian.ApiInterfaceCustom;
import com.jz.dm.service.ApiInterfaceService; import com.jz.dm.service.ApiInterfaceService;
import com.jz.dm.service.AuthService; import com.jz.dm.service.AuthService;
import com.jz.dm.web.annotation.AccessLimit; import com.jz.dm.web.annotation.AccessLimit;
import com.jz.dm.web.annotation.ApiLogAspect; import com.jz.dm.web.annotation.ApiLogAspect;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -27,6 +29,7 @@ import org.springframework.stereotype.Service; ...@@ -27,6 +29,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
...@@ -61,9 +64,19 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -61,9 +64,19 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
@Autowired @Autowired
private HttpsUtils httpsUtils; private HttpsUtils httpsUtils;
/**
* 数据银行扣款链接
*/
@Value("${data.bank.balanceUrl}") @Value("${data.bank.balanceUrl}")
private String balanceUrl; private String balanceUrl;
/**
* 数据包下载路径
*/
@Value("${dataPackage.downloadUrl}")
private String downloadUrl;
/** /**
* API请求逻辑处理 * API请求逻辑处理
* *
...@@ -76,38 +89,38 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -76,38 +89,38 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
@SentinelResource(value = "api.gateway", fallback = "fallbackGateway") @SentinelResource(value = "api.gateway", fallback = "fallbackGateway")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW) @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void doService(OpenApiRequest request, OpenApiResponse response) { public void doService(OpenApiRequest request, OpenApiResponse response) {
boolean bResult = false;
JSONObject parameter = JSONObject.parseObject(request.getOpenApiParams()); JSONObject parameter = JSONObject.parseObject(request.getOpenApiParams());
String authCode = parameter.getString("authCode"); ApiInterface apiInterface = apiInterfaceService.getApiInfo(request.getApiKey());
if (StringUtils.isBlank(authCode)) { ApiAuth apiAuth = null;
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST); try {
} Boolean isTest = parameter.getBoolean("isTest");
verifyApiInterface(apiInterfaceService.getApiInfo(request.getApiKey()));
ApiAuth apiAuth = authService.getAuthInfo(authCode);
verifyAuth(apiAuth);
String reqParams = parameter.getString("reqParams"); String reqParams = parameter.getString("reqParams");
Map paramMap = null; Map paramMap = null;
if (StringUtils.isNotBlank(reqParams)) { if (StringUtils.isNotBlank(reqParams)) {
paramMap = (Map) JSONObject.parseObject(reqParams); paramMap = (Map) JSONObject.parseObject(reqParams);
paramMap.put("is_test",isTest);
} }
if (!isTest) {//如果不是测试
String authCode = parameter.getString("authCode");
if (StringUtils.isBlank(authCode)) {
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST);
}
verifyApiInterface(apiInterface);
apiAuth = authService.getAuthInfo(authCode);
verifyAuth(apiAuth);
//取出缓存数据 //取出缓存数据
String redisReqParam = redisUtils.get(request.getApiKey()); String redisReqParam = redisUtils.get(request.getApiKey());
boolean bResult = false;
try {
if (StringUtils.isNotBlank(redisReqParam)) {//redis中存在 if (StringUtils.isNotBlank(redisReqParam)) {//redis中存在
//解析出API制作成功时的参数配置 //解析出API制作成功时的参数配置
JSONObject jsonObject = JSONObject.parseObject(redisReqParam); JSONObject jsonObject = JSONObject.parseObject(redisReqParam);
String targetUrl = jsonObject.getString("targetUrl"); String targetUrl = jsonObject.getString("targetUrl");
String outputType = jsonObject.getString("outputType"); String outputType = jsonObject.getString("outputType");
bResult = rangRequestTarget(outputType, targetUrl, paramMap, response); String joinType = jsonObject.getString("joinType");
bResult = rangRequestTarget(outputType, targetUrl,false, paramMap, joinType, apiInterface, response);
} else {//不存在查询数据库 } else {//不存在查询数据库
ApiInterface apiInterface =
apiInterfaceService.getReqTargetInfo(request.getApiKey());
if (null == apiInterface) {
throw new GatewayException(GatewayResultCode.REQUEST_INFO_UNEXIST);
}
bResult = rangRequestTarget(apiInterface.getOutputType(), bResult = rangRequestTarget(apiInterface.getOutputType(),
apiInterface.getTargetUrl(), paramMap, response); apiInterface.getTargetUrl(),false, paramMap, apiInterface.getJoinType(), apiInterface, response);
} }
//调用成功请求数据银行扣款 //调用成功请求数据银行扣款
if (AuthModeEnum.POWER_CALL_MODE.name().equals(apiAuth.getAuthMode())) { if (AuthModeEnum.POWER_CALL_MODE.name().equals(apiAuth.getAuthMode())) {
...@@ -115,6 +128,10 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -115,6 +128,10 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
//按次调用时处理(处理为已调用) //按次调用时处理(处理为已调用)
authService.updateApiAuthStatus(apiAuth); authService.updateApiAuthStatus(apiAuth);
} }
} else {
bResult = rangRequestTarget(ApiInfoOutTypeEnum.JSON.name(), apiInterface.getTargetUrl(),
true,paramMap, apiInterface.getJoinType(), apiInterface, response);
}
} catch (Exception ex) { } catch (Exception ex) {
if (ex instanceof GatewayException) { if (ex instanceof GatewayException) {
//调用失败回调数据银行解冻 //调用失败回调数据银行解冻
...@@ -137,52 +154,231 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -137,52 +154,231 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
* @param outputType * @param outputType
* @param targetUrl * @param targetUrl
* @param param * @param param
* @param isTest 是否是测试
* @param joinType
* @param response * @param response
*/ */
private boolean rangRequestTarget(String outputType, String targetUrl, private boolean rangRequestTarget(String outputType, String targetUrl,Boolean isTest,
Map<String, String> param, OpenApiResponse response) { Map<String, String> param, String joinType,
ApiInterface apiInterface, OpenApiResponse response) {
if (StringUtils.isBlank(outputType)) { if (StringUtils.isBlank(outputType)) {
outputType = ApiInfoOutTypeEnum.JSON.name(); outputType = ApiInfoOutTypeEnum.JSON.name();
} }
if (StringUtils.isBlank(targetUrl)) { if (StringUtils.isBlank(targetUrl)) {
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST); throw new GatewayException(GatewayResultCode.REQUEST_PARAM_EMPTY);
}
if (StringUtils.isBlank(joinType)) {
throw new GatewayException(GatewayResultCode.API_TYPE_ERROR);
}
if (!isTest){
if ("10002".equals(joinType) || "10009".equals(joinType)) {//数据表查询
return dataTableSelect(outputType, targetUrl, param, apiInterface, response);
} else if ("10004".equals(joinType) || "10008".equals(joinType)) {//三方查询
return thirdSelect(targetUrl, apiInterface, response);
} else if ("10005".equals(joinType) || "10007".equals(joinType)) {//数据包查询
return dataBagDownload(param);
} else {
throw new GatewayException(GatewayResultCode.API_TYPE_ERROR);
}
}else {
return dataTableSelect(outputType, targetUrl, param, apiInterface, response);
} }
}
/**
* 三方查询 $$ 数据银行+DMP
*
* @param targetUrl
* @param apiInterface
* @param response
* @return
*/
private boolean thirdSelect(String targetUrl, ApiInterface apiInterface, OpenApiResponse response) {
if ("POST".equalsIgnoreCase(apiInterface.getReqType())) {
return callMethodResponse(httpsUtils.submitPost(targetUrl, null), response);
} else {
return callMethodResponse(httpsUtils.doGet(targetUrl, null), response);
}
}
/**
* 数据包下载 $$ 数据银行+DMP
*
* @param param
* @return
*/
private boolean dataBagDownload(Map<String, String> param) {
String fileLocation = param.get("fileLocation");
String datasourceId = param.get("datasourceId");
if (null == fileLocation || null == datasourceId) {
throw new GatewayException(GatewayResultCode.DATA_BIG_ADDR_UNEXIST);
}
StringBuilder builder = new StringBuilder();
builder.append("?").append("file_location=")
.append(fileLocation).append("&")
.append("datasourceId=")
.append(datasourceId);
HttpDownload.download(downloadUrl + builder.toString());
return true;
}
/**
* 数据表查询
*
* @param outputType
* @param targetUrl
* @param param
* @param apiInterface
* @param response
* @return
*/
private boolean dataTableSelect(String outputType, String targetUrl, Map<String, String> param, ApiInterface apiInterface, OpenApiResponse response) {
ApiInterfaceCustom apiCustomInfo = checkParamLegal(param, apiInterface);
if (ApiInfoOutTypeEnum.FLOW.name().equals(outputType)) {//文件流形式请求 if (ApiInfoOutTypeEnum.FLOW.name().equals(outputType)) {//文件流形式请求
return flowRequestMethod(targetUrl, param, response, apiCustomInfo);
} else if (ApiInfoOutTypeEnum.JSON.name().equals(outputType)) { //json格式请求
return jsonRequestMethod(targetUrl, param, response, apiCustomInfo);
} else {
throw new GatewayException(GatewayResultCode.OUTPUT_TYPE_EXCEPTION);
}
}
/**
* 校验参数合法性
*
* @param param
* @param apiInterface
* @return
*/
private ApiInterfaceCustom checkParamLegal(Map<String, String> param, ApiInterface apiInterface) {
ApiInterfaceCustom apiCustomInfo = apiInterfaceService.getApiCustomInfo(apiInterface.getId());
boolean tag = false;
if (null != apiCustomInfo) {
List<Map> mapList = JSONObject.parseArray(apiCustomInfo.getRequestParam(), Map.class);
if (CollectionUtils.isNotEmpty(mapList)) {
for (Map map : mapList) {
if ((Boolean) map.get("required")) {
String name = (String) map.get("name");
if (null == param.get(name)) {
tag = true;
}
}
}
if (tag) {
throw new GatewayException(GatewayResultCode.REQUEST_PARAM_EMPTY);
}
}
}
return apiCustomInfo;
}
/**
* 数据查询---流请求方式
*
* @param targetUrl
* @param param
* @param response
* @return
*/
private boolean flowRequestMethod(String targetUrl, Map<String, String> param,
OpenApiResponse response, ApiInterfaceCustom apiCustomInfo) {
String dataSize = param.get("dataSize");
Integer valueOf = -1;
if (StringUtils.isNotBlank(dataSize)) {
valueOf = Integer.valueOf(dataSize);
}
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.append(targetUrl).append("?"); net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param);
for (Map.Entry<String, String> entry : param.entrySet()) { builder.append(targetUrl).append("?")
.append("datasourceId=").append(apiCustomInfo.getEsDataSource())
.append("query_database=").append(apiCustomInfo.getEsDataBase())
.append("request_fileds=").append(reqParams).append("response_fields=")
.append(assembleResponseParams(apiCustomInfo.getResponseParam()))
.append(valueOf);
/* for (Map.Entry<String, String> entry : param.entrySet()) {
String key = entry.getKey(); String key = entry.getKey();
String value = entry.getValue(); String value = entry.getValue();
builder.append(key).append(LoggingConstants.AND_EQUAL) builder.append(key).append(LoggingConstants.AND_EQUAL)
.append(value).append(LoggingConstants.AND_SPILT); .append(value).append(LoggingConstants.AND_SPILT);
} }*/
String baseUrl = builder.substring(0, builder.length() - 1); HttpDownload.download(builder.toString());
HttpDownload.download(baseUrl);
response.setCode(GatewayResultCode.SUCCESS.getCode()); response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg()); response.setMsg(GatewayResultCode.SUCCESS.getMsg());
return true; return true;
} else if (ApiInfoOutTypeEnum.JSON.name().equals(outputType)) { //json格式请求 }
response.setAttribute("调用成功!");
response.setMsg(GatewayResultCode.SUCCESS.getMsg()); /**
response.setCode(GatewayResultCode.SUCCESS.getCode()); * 数据查询--json请求方式
/*String respResult = httpsUtils.doGet(targetUrl, param); *
JSONObject resp = JSONObject.parseObject(respResult); * @param targetUrl
if (null != resp && 200 == resp.getInteger("code")) { * @param param
* @param response
* @param apiCustomInfo
* @return
*/
private boolean jsonRequestMethod(String targetUrl, Map<String, String> param,
OpenApiResponse response, ApiInterfaceCustom apiCustomInfo) {
JSONObject params = new JSONObject();
params.put("datasourceId", apiCustomInfo.getEsDataSource());//数据源id
params.put("query_database", apiCustomInfo.getEsDataBase());//数据源库名称
params.put("query_table", apiCustomInfo.getEsTable());//数据源库表
net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param);
params.put("request_fileds", reqParams);//请求参数
params.put("response_fields", assembleResponseParams(apiCustomInfo.getResponseParam()));//响应参数
Integer pageNum = Integer.valueOf(param.get("pageNum"));
Integer pageSize = Integer.valueOf(param.get("pageSize"));
params.put("page_size", apiCustomInfo.getPageSize());
params.put("page_num", apiCustomInfo.getPageNum());
if (null != pageNum) {
params.put("page_num", pageNum);
}
if (null != pageSize) {
params.put("page_size", pageSize);
}
String respResult = httpsUtils.submitPost(targetUrl, params.toString());
return callMethodResponse(respResult, response);
}
/**
* 组装响应参数
*
* @param respParams
* @return
*/
private String assembleResponseParams(String respParams) {
StringBuilder builder = new StringBuilder();
List<Map> mapList = JSONObject.parseArray(respParams, Map.class);
if (CollectionUtils.isNotEmpty(mapList)) {
for (Map map : mapList) {
String name = (String) map.get("name");
builder.append(name).append(LoggingConstants.SEP);
}
}
return builder.substring(0, builder.length() - 1);
}
/**
* 调用方法处理结果
*
* @param result
* @param response
*/
private boolean callMethodResponse(String result, OpenApiResponse response) {
if (null == result) {
throw new GatewayException(GatewayResultCode.DISTANCE_REQUEST_EXCEPTION);
}
JSONObject resp = JSONObject.parseObject(result);
if (null != resp && "ESC00000".equals(resp.getString("return_code"))) {
response.setCode(GatewayResultCode.SUCCESS.getCode()); response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg()); response.setMsg(GatewayResultCode.SUCCESS.getMsg());
response.setAttribute(resp.getString("data")); response.setAttribute(resp);
return true; return true;
} else { } else {
log.error("~~~~~~~~~~~~~~~~远程请求异常~~~~~~~~~~~~~~~~~"); log.error("~~~~~~~~~~~~~~~~远程请求异常~~~~~~~~~~~~~~~~~");
throw new GatewayException(GatewayResultCode.DISTANCE_REQUEST_EXCEPTION); throw new GatewayException(GatewayResultCode.DISTANCE_REQUEST_EXCEPTION);
}*/
} else {
throw new GatewayException(GatewayResultCode.OUTPUT_TYPE_EXCEPTION);
} }
return false;
} }
/** /**
* 通知扣款 * 通知扣款
* *
...@@ -198,7 +394,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -198,7 +394,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
jsonReq.put("userId", userId); jsonReq.put("userId", userId);
jsonReq.put("dataPrice", dataPrice); jsonReq.put("dataPrice", dataPrice);
jsonReq.put("callStatus", bResult);//true 调用成功 扣款 false 调用失败,解冻金额 jsonReq.put("callStatus", bResult);//true 调用成功 扣款 false 调用失败,解冻金额
String responseResult = httpsUtils.submitPost(balanceUrl+"/mall/financeCustomerAssets/unfreezeMoney", jsonReq.toString()); String responseResult = httpsUtils.submitPost(balanceUrl + "/mall/financeCustomerAssets/unfreezeMoney", jsonReq.toString());
JSONObject paramsResult = JSONObject.parseObject(responseResult); JSONObject paramsResult = JSONObject.parseObject(responseResult);
if (null != paramsResult) { if (null != paramsResult) {
if (200 != paramsResult.getInteger("code")) { if (200 != paramsResult.getInteger("code")) {
......
...@@ -107,5 +107,8 @@ logging: ...@@ -107,5 +107,8 @@ logging:
zhl: zhl:
springbootlogback: off springbootlogback: off
#数据包访问链接
dataPackage:
downloadUrl: http://xxx.com
...@@ -43,10 +43,10 @@ public class TestRedisUserSave extends SpringTestCase { ...@@ -43,10 +43,10 @@ public class TestRedisUserSave extends SpringTestCase {
bankApiReq.setSignType("MD5"); bankApiReq.setSignType("MD5");
bankApiReq.setJoinType("10004"); bankApiReq.setJoinType("10004");
bankApiReq.setOutputType("JSON"); bankApiReq.setOutputType("JSON");
bankApiReq.setPage(false); //bankApiReq.setPage(false);
bankApiReq.setReqType("DAY"); bankApiReq.setReqType("DAY");
bankApiReq.setTargetUrl("www.baidu.com"); bankApiReq.setTargetUrl("www.baidu.com");
bankApiReq.setFileId(1L); //bankApiReq.setFileId(1L);
bankApiReq.setSign("F2A8E3CFE528D6AE5C5B075046653F3E"); bankApiReq.setSign("F2A8E3CFE528D6AE5C5B075046653F3E");
bankApiReq.setReqFrequency(0L); bankApiReq.setReqFrequency(0L);
bankApiReq.setApiDesc("4444"); bankApiReq.setApiDesc("4444");
......
package com.jz.dm.gateway.orther; package com.jz.dm.gateway.orther;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/** /**
* @ClassName: * @ClassName:
* @Author: Carl * @Author: Carl
...@@ -8,10 +12,62 @@ package com.jz.dm.gateway.orther; ...@@ -8,10 +12,62 @@ package com.jz.dm.gateway.orther;
*/ */
public class TestStr { public class TestStr {
public static void main(String[] args) { public static void main(String[] args) {
String str = "/picture/logo/T6gxlv137nFA555z.jpg"; List<String> list1 = new ArrayList<String>();
// String[] split = str.split("\\/"); list1.add("1");
System.out.println(str.substring(0, str.lastIndexOf("/") + 1)); list1.add("2");
String substring = str.substring(str.lastIndexOf(".")); list1.add("3");
System.out.println(substring); list1.add("4");
list1.add("5");
List<String> list2 = new ArrayList<String>();
list2.add("1");
list2.add("2");
list2.add("3");
list2.add("4");
list2.add("5");
list2.add("7");
list2.add("9");
// 交集
List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList());
System.out.println("---交集 intersection---");
intersection.parallelStream().forEach(System.out :: println);
// 差集 (list1 - list2)
List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(Collectors.toList());
System.out.println("---差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out :: println);
// 差集 (list2 - list1)
List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(Collectors.toList());
System.out.println("---差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out :: println);
//合拼差
reduce1.addAll(reduce2);
System.out.println("---合拼差 reduce1 (list2 -> list1)---");
reduce1.parallelStream().forEach(System.out :: println);
// 并集
List<String> listAll = list1.parallelStream().collect(Collectors.toList());
List<String> listAll2 = list2.parallelStream().collect(Collectors.toList());
listAll.addAll(listAll2);
System.out.println("---并集 listAll---");
listAll.parallelStream().forEachOrdered(System.out :: println);
// 去重并集
List<String> listAllDistinct = listAll.stream().distinct().collect(Collectors.toList());
System.out.println("---得到去重并集 listAllDistinct---");
listAllDistinct.parallelStream().forEachOrdered(System.out :: println);
System.out.println("---原来的List1---");
list1.parallelStream().forEachOrdered(System.out :: println);
System.out.println("---原来的List2---");
list2.parallelStream().forEachOrdered(System.out :: println);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment