Commit d6402b13 authored by zhangc's avatar zhangc

优化API调用代码

parent 196e03a6
......@@ -8,6 +8,7 @@ CREATE TABLE `t_api_interface` (
`target_url` varchar(100) DEFAULT NULL COMMENT '目标url',
`api_type` varchar(50) NULL DEFAULT '' COMMENT '接入类型:字典表对应key值',
`req_type` varchar(20) NULL DEFAULT NULL COMMENT '请求方式: GET, POST',
`post_type` varchar(20) NULL DEFAULT NULL COMMENT 'post请求类型:1.x-www-form-urlencoded 2.form-data 3.json',
`api_function` varchar(200) DEFAULT NULL COMMENT '接口功能',
`status` varchar(50) NOT NULL COMMENT '状态(DRAFT-草稿 ISSUE-发布 OUTMODED-过时 4FREEZE-冻结 EXPIRY-失效)',
`output_type` varchar(50) NULL DEFAULT 'JSON' COMMENT '输出类型:flow 流形式输出, json格式输出',
......
......@@ -117,7 +117,12 @@
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-webmvc-adapter</artifactId>
<version>1.8.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
......@@ -149,6 +154,8 @@
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!-- 代码自动生成依赖 end-->
</dependencies>
......
......@@ -36,6 +36,9 @@ public enum GatewayResultCode implements ResultCode {
/** 无效请求 */
ILLEGAL_TIMETEMP("ILLEGAL_TIMETEMP", "无效时间戳"),
/** 无效授权 */
ILLEGAL_AUTHORIZATION("ILLEGAL_AUTHORIZATION", "无效授权"),
/** 请求次数受限 */
REQUEST_LIMIT_EXCEPTION("REQUEST_LIMIT_EXCEPTION", "请求次数受限"),
......@@ -53,8 +56,8 @@ public enum GatewayResultCode implements ResultCode {
/** 请求未授权 */
REQUEST_NOT_AUTH("REQUEST_NOT_AUTH", "请求未授权"),
/** 请求组织不存在 */
ORG_NOT_EXIST("ORG_NOT_EXIST", "请求组织不存在"),
/** 组织不存在 */
ORG_NOT_EXIST("ORG_NOT_EXIST", "组织不存在"),
/** API状态异常 */
API_STATUS_EXCEPTION("API_STATUS_EXCEPTION", "API状态异常"),
......@@ -65,12 +68,12 @@ public enum GatewayResultCode implements ResultCode {
/** 文件地址不存在 */
DATA_BIG_ADDR_UNEXIST("DATA_BIG_ADDR_UNEXIST", "文件地址不存在!"),
/** 请求组织状态异常 */
ORG_STATE_EXCEPTION("ORG_STATE_EXCEPTION", "请求组织状态异常"),
/** 组织状态异常 */
ORG_STATE_EXCEPTION("ORG_STATE_EXCEPTION", "组织状态异常"),
/** 请求信息不存在 */
REQUEST_INFO_UNEXIST("REQUEST_INFO_UNEXIST", "请求信息不存在"),
/** 远程请求异常 */
DISTANCE_REQUEST_EXCEPTION("DISTANCE_REQUEST_EXCEPTION", "流形式输出异常"),
DISTANCE_REQUEST_EXCEPTION("DISTANCE_REQUEST_EXCEPTION", "远程请求异常"),
/** 单次调用金额不足 */
CALL_AMOUNT_NOT_ENOUGH("CALL_AMOUNT_NOT_ENOUGH", "单次调用金额不足"),
......
package com.jz.dm.common.enums;
import com.alibaba.fastjson.JSON;
import com.jz.common.constant.ResultMsg;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.common.enums
* @PROJECT_NAME: jz-dm-parent
* @NAME: ResultFlowCode
* @DATE: 2021-2-25/14:40
* @DAY_NAME_SHORT: 周四
* @Description:
**/
public enum ResultFlowCode {
/** 限流异常 */
FLOW_EXCEPTION(410, ResultMsg.LIMIT_FLOW_EXCEPTION),
/** 降级异常 */
DEGRADE_EXCEPTION(411, ResultMsg.LIMIT_DEGRADE_EXCEPTION),
/** 热点异常 */
PARAM_EXCEPTION(412, ResultMsg.LIMIT_PARAM_EXCEPTION),
/** 系统规则异常 */
SYSTEM_EXCEPTION(413, ResultMsg.LIMIT_SYSTEM_EXCEPTION),
/** 认证异常 */
AUTHORITY_EXCEPTION(414, ResultMsg.LIMIT_AUTHORITY_EXCEPTION);
private int code;
/**
* 提示信息
*/
private ResultMsg msg;
private ResultFlowCode(int code, ResultMsg msg) {
this.code = code;
this.msg = msg;
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
public int getCode() {
return code;
}
public ResultMsg getMsg() {
return this.msg;
}
public void setMsg(ResultMsg msg) {
this.msg = msg;
}
}
package com.jz.dm.common.exception;
import com.jz.common.constant.ResultMsg;
import com.jz.dm.common.enums.ResultFlowCode;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.common.exception
* @PROJECT_NAME: jz-dm-parent
* @NAME: LimitFlowException
* @DATE: 2021-2-25/14:27
* @DAY_NAME_SHORT: 周四
* @Description: 限流异常处理器
**/
@Data
@EqualsAndHashCode(callSuper = true)
public class LimitFlowException extends RuntimeException {
private static final long serialVersionUID = -3963657380514719229L;
/**
* 错误代码
*/
protected ResultFlowCode code;
/**
* 错误信息
*/
protected String message;
private Throwable cause;
public static LimitFlowException of(ResultMsg errorMsg) {
return LimitFlowException.of(ResultFlowCode.FLOW_EXCEPTION,errorMsg.getMsg());
}
public static LimitFlowException of_error(String errorMsg) {
return LimitFlowException.of(ResultFlowCode.FLOW_EXCEPTION,errorMsg);
}
public static LimitFlowException of(ResultFlowCode code) {
LimitFlowException ufaceException = new LimitFlowException();
ufaceException.setCode(code);
ufaceException.setMessage(code.getMsg().getMsg());
return ufaceException;
}
public static LimitFlowException of(ResultFlowCode code, String message) {
LimitFlowException ufaceException = new LimitFlowException();
ufaceException.setCode(code);
ufaceException.setMessage(message);
return ufaceException;
}
public static LimitFlowException of(ResultFlowCode code, Throwable cause) {
LimitFlowException ufaceException = new LimitFlowException();
ufaceException.setCode(code);
ufaceException.setCause(cause);
ufaceException.setMessage("todo need get from db/properties");
return ufaceException;
}
}
package com.jz.dm.common.util;
import com.alibaba.fastjson.JSON;
import com.jz.dm.common.constant.LoggingConstants;
import org.apache.commons.lang3.StringUtils;
import java.io.UnsupportedEncodingException;
......@@ -29,36 +28,15 @@ public class MapUtil {
* @param signType
* @return
*/
public static String getSignValue(String apiKey, String method, String signType) {
StringBuilder builder = new StringBuilder();
builder.append("apiKey=").append(apiKey).append(LoggingConstants.AND_SPILT)
.append("method=").append(method).append(LoggingConstants.AND_SPILT)
.append("signType=").append(signType);
return builder.toString();
}
//public static String getSignValue(String apiKey, String method, String signType) {
// StringBuilder builder = new StringBuilder();
// builder.append("apiKey=").append(apiKey).append(LoggingConstants.AND_SPILT)
// .append("method=").append(method).append(LoggingConstants.AND_SPILT)
// .append("signType=").append(signType);
// return builder.toString();
//}
/**
* 给map集合中的key实现字典排序
*
* @param map
* @return
*/
public Map<String, Object> mapKeySort(Map<String, Object> map) {
ArrayList list = new ArrayList();
for (Map.Entry<String, Object> entry : map.entrySet()) {
list.add(entry.getKey());
//System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
HashMap<String, Object> newMap = new HashMap<String, Object>();
//运用Collections的sort()方法对其进行排序 sort()方法需要传 连个参数,一个是需要进行排序的Collection 另一个是一个Comparator
Collections.sort(list, new SpellComparatorUtils());
for (int i = 0; i < list.size(); i++) {
newMap.put(list.get(i).toString(), map.get(list.get(i).toString()));
}
return newMap;
}
/**
* 将对象转成TreeMap,属性名为key,属性值为value
......@@ -147,12 +125,11 @@ public class MapUtil {
}
/**
*   * 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串
*   * 把数组所有元素字典排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串
*   * @param params 需要排序并参与字符拼接的参数组
*   * @return 拼接后字符串
*   
*   * @return 拼接后字符串 
*/
public static String createLinkStringByGet(Map<String, String> params) {
public static String stringNormalSort(Map<String, String> params) {
List<String> keys = new ArrayList<String>(params.keySet());
Collections.sort(keys);
String prestr = "";
......
package com.jz.dm.config;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.jz.dm.common.enums.ResultFlowCode;
import com.jz.dm.common.exception.LimitFlowException;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.config
* @PROJECT_NAME: jz-dm-parent
* @NAME: GatewayUrlBlockHandler
* @DATE: 2021-2-25/14:16
* @DAY_NAME_SHORT: 周四
* @Description: sentinel限流异常处理类
**/
@Component
public class GatewayUrlBlockHandler implements BlockExceptionHandler {
@Override
public void handle(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, BlockException e) throws Exception {
if (e instanceof FlowException){
throw LimitFlowException.of(ResultFlowCode.FLOW_EXCEPTION);
}else if (e instanceof DegradeException){
throw LimitFlowException.of(ResultFlowCode.DEGRADE_EXCEPTION);
}else if (e instanceof ParamFlowException){
throw LimitFlowException.of(ResultFlowCode.PARAM_EXCEPTION);
}else if (e instanceof SystemBlockException){
throw LimitFlowException.of(ResultFlowCode.SYSTEM_EXCEPTION);
}else if (e instanceof AuthorityException){
throw LimitFlowException.of(ResultFlowCode.AUTHORITY_EXCEPTION);
}
/*// 设置返回json数据
httpServletResponse.setStatus(200);
httpServletResponse.setHeader("Content-Type","application/json;charset=UTF-8");
httpServletResponse.getWriter().write(JSON.toJSONString(backMap));*/
}
}
......@@ -101,7 +101,7 @@ public class AuthFilter extends AbstractFilter {
throw new GatewayException(GatewayResultCode.REQUEST_NOT_AUTH);
}
if ("1".equals(authAuth.getHandler())) {//处理状态,已经调用成功后授权码失效
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST);
throw new GatewayException(GatewayResultCode.ILLEGAL_AUTHORIZATION);
}
//查询认证组织信息
ApiOrg apiOrg = organizationManageService.getAuthOrganization(authAuth.getApiOrgId());
......@@ -231,7 +231,7 @@ public class AuthFilter extends AbstractFilter {
JSONObject result = JSONObject.parseObject(respResult);
if (null != result) {
if (200 != result.getInteger("code")) {
log.info("~~~~~~~~~~~~~~~~~~异常信息为:{}", result.getString("message"));
log.info("~~~~~~~~~~~~~~~~~~远程调用异常:{}", result.getString("message"));
throw new GatewayException(GatewayResultCode.CALL_AMOUNT_NOT_ENOUGH);
}
}
......
......@@ -37,7 +37,6 @@ public class CheckArgsFilter extends AbstractFilter {
|| StringUtil.isEmpty(request.getTimestamp()) || StringUtil.isEmpty(request.getParams())) {
throw new GatewayException(GatewayResultCode.ILLEGAL_ARGUMENT); //无效参数
}
// 设置默认值
if (StringUtil.isEmpty(request.getFormat())) {
request.setFormat(Format.JSON.name());
}
......
......@@ -17,6 +17,9 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* @Description: 验签过滤器(验证签名信息)
* @Author: Mr.zhang
......@@ -45,7 +48,7 @@ public class VerifySignFilter extends AbstractFilter {
JSONObject jsonObject = JSONObject.parseObject(request.getParams());
if (!jsonObject.getBoolean("isTest")) {//是否测试调用
//对签约参数进行字典排序
String signParams = MapUtil.getSignValue(request.getApiKey(), request.getMethod(), request.getSignType());
String signParams = MapUtil.stringNormalSort(assembleSignMap(request));
if (StringUtils.isNotBlank(signParams)) {
String authCode = jsonObject.getString("authCode");
//需要传入授权码
......@@ -54,7 +57,7 @@ public class VerifySignFilter extends AbstractFilter {
throw new GatewayException(GatewayResultCode.REQUEST_NOT_AUTH);
}
String sign = Md5.encrypt(signParams, apiAuthInfo.getSalt());
if (!request.getSign().equals(sign)) {
if (!request.getSign().equalsIgnoreCase(sign)) {
throw new GatewayException(GatewayResultCode.SIGN_ERROR);
}
} else {
......@@ -63,7 +66,7 @@ public class VerifySignFilter extends AbstractFilter {
}
chain.doFilter(request, response);
} catch (GatewayException ex) {
LogUtil.error(LOGGER, ex,"sign response error. response=" + response.getResponse());
LogUtil.error(LOGGER, ex, "sign response error. response=" + response.getResponse());
response.clearAttributes();
response.setCode(ex.getResultCode().getCode());
response.setMsg(ex.getResultCode().getMsg());
......@@ -74,4 +77,17 @@ public class VerifySignFilter extends AbstractFilter {
}
}
/**
* 组装签名参数
* @param request
* @return
*/
private Map assembleSignMap(GatewayRequest request) {
Map<String, String> paramsMap = new HashMap<>();
paramsMap.put("apiKey", request.getApiKey());
paramsMap.put("method", request.getMethod());
paramsMap.put("signType", request.getSignType());
paramsMap.put("params", request.getParams());
return paramsMap;
}
}
......@@ -2,6 +2,9 @@ package com.jz.dm.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jz.dm.models.domian.ApiFunction;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* <p>
......@@ -13,4 +16,11 @@ import com.jz.dm.models.domian.ApiFunction;
*/
public interface ApiFunctionMapper extends BaseMapper<ApiFunction> {
/**
* 查询function名称列表
* @return
*/
@Select("SELECT `name` FROM t_api_function WHERE `status`=1 AND is_deleted=0")
List<String> listFunctionNames();
}
......@@ -60,6 +60,12 @@ public class ApiInterface extends BaseObject implements Serializable {
@TableField("req_type")
private String reqType;
/**
* post请求类型:1.x-www-form-urlencoded 2.form-data 3.json
*/
@TableField("post_type")
private String postType;
/**
* '输出类型:flow 流形式输出, json格式输出',
*/
......
package com.jz.dm.models.enity;
import com.jz.dm.common.util.OpenApiResponse;
import com.jz.dm.models.domian.ApiInterface;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.models.enity
* @PROJECT_NAME: jz-dm-parent
* @NAME: DataTableSelectTO
* @DATE: 2021-2-24/10:10
* @DAY_NAME_SHORT: 周三
* @Description: 数据表查询-数据传输对象
**/
@Data
@ApiModel("数据表查询领域对象")
public class DataTableSelectTO implements Serializable {
@ApiModelProperty(value = "api对象")
private ApiInterface apiInterface;
@ApiModelProperty(value = "业务请求参数")
private String reqParams;
@ApiModelProperty(value = "业务返回体对象")
private OpenApiResponse openApiResponse;
@ApiModelProperty(value = "授权码")
private String authCode;
public DataTableSelectTO(ApiInterface apiInterface,String reqParams,OpenApiResponse openApiResponse){
this.apiInterface = apiInterface;
this.reqParams = reqParams;
this.openApiResponse = openApiResponse;
}
public DataTableSelectTO(ApiInterface apiInterface,String reqParams,OpenApiResponse openApiResponse,String authCode){
this.apiInterface = apiInterface;
this.reqParams = reqParams;
this.openApiResponse = openApiResponse;
this.authCode = authCode;
}
public DataTableSelectTO(){}
}
package com.jz.dm.service.request;
import com.jz.dm.models.enity.DataTableSelectTO;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.service.request.impl
* @PROJECT_NAME: jz-dm-parent
* @NAME: DataBagService
* @DATE: 2021-2-24/10:55
* @DAY_NAME_SHORT: 周三
* @Description:
**/
public interface DataBagService {
/**
* 数据包查询
* @param dataBagSelectTO
* @return
*/
public boolean dataBagSelect(DataTableSelectTO dataBagSelectTO);
}
package com.jz.dm.service.request;
import com.jz.dm.models.enity.DataTableSelectTO;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.service.request
* @PROJECT_NAME: jz-dm-parent
* @NAME: DataSelectService
* @DATE: 2021-2-24/10:36
* @DAY_NAME_SHORT: 周三
* @Description:
**/
public interface DataSelectService {
/**
* 数据查询处理类
* @param dataTableSelectTO
*/
public Boolean dataTableHandle(DataTableSelectTO dataTableSelectTO);
}
package com.jz.dm.service.request;
import com.jz.dm.models.enity.DataTableSelectTO;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.service.request
* @PROJECT_NAME: jz-dm-parent
* @NAME: ThirdSelectService
* @DATE: 2021-2-24/10:43
* @DAY_NAME_SHORT: 周三
* @Description:
**/
public interface ThirdSelectService {
/**
* 三方查询处理
* @param dataTableSelectTO
* @return
*/
public boolean thirdSelectHandle(DataTableSelectTO dataTableSelectTO);
}
package com.jz.dm.service.request.impl;
import com.alibaba.fastjson.JSONObject;
import com.jz.dm.common.enums.GatewayResultCode;
import com.jz.dm.common.exception.GatewayException;
import com.jz.dm.common.util.stream.HttpDownload;
import com.jz.dm.models.enity.DataTableSelectTO;
import com.jz.dm.service.ApiLogService;
import com.jz.dm.service.request.DataBagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.service.request.impl
* @PROJECT_NAME: jz-dm-parent
* @NAME: DataBagHandle
* @DATE: 2021-2-24/10:56
* @DAY_NAME_SHORT: 周三
* @Description:
**/
@Slf4j
@Component
public class DataBagHandle implements DataBagService {
@Autowired
private ApiLogService reqLogService;
/**
* 数据包下载 $$ 数据银行+DMP
*
* @param dataBagSelectTO
* @return
*/
@Override
public boolean dataBagSelect(DataTableSelectTO dataBagSelectTO) {
JSONObject param = JSONObject.parseObject(dataBagSelectTO.getReqParams());
String fileLocation = param.getString("file_location");
String datasourceId = param.getString("datasourceId");
if (null == fileLocation || null == datasourceId) {
throw new GatewayException(GatewayResultCode.DATA_BIG_ADDR_UNEXIST);
}
JSONObject requestParams = new JSONObject();
requestParams.put("file_location", fileLocation);
requestParams.put("datasourceId", datasourceId);
HttpDownload.postDownload(dataBagSelectTO.getApiInterface().getTargetUrl(), requestParams);
reqLogService.updateLog(dataBagSelectTO.getApiInterface().getLogId(),null);
dataBagSelectTO.getOpenApiResponse().setCode(GatewayResultCode.SUCCESS.getCode());
dataBagSelectTO.getOpenApiResponse().setMsg(GatewayResultCode.SUCCESS.getMsg());
return true;
}
}
package com.jz.dm.service.request.impl;
import com.alibaba.fastjson.JSONObject;
import com.jz.common.utils.HttpsUtils;
import com.jz.dm.common.constant.LoggingConstants;
import com.jz.dm.common.enums.GatewayResultCode;
import com.jz.dm.common.enums.apiInterface.ApiInfoOutTypeEnum;
import com.jz.dm.common.exception.GatewayException;
import com.jz.dm.common.util.OpenApiResponse;
import com.jz.dm.common.util.stream.HttpDownload;
import com.jz.dm.models.domian.ApiInterface;
import com.jz.dm.models.domian.ApiInterfaceCustom;
import com.jz.dm.models.enity.DataTableSelectTO;
import com.jz.dm.service.ApiInterfaceService;
import com.jz.dm.service.ApiLogService;
import com.jz.dm.service.request.DataSelectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.service.request
* @PROJECT_NAME: jz-dm-parent
* @NAME: DataTableSelectHandle
* @DATE: 2021-2-24/10:06
* @DAY_NAME_SHORT: 周三
* @Description: 数据表查询处理类
**/
@Slf4j
@Component
public class DataTableSelectHandle implements DataSelectService {
@Autowired
private ApiInterfaceService apiInterfaceService;
@Autowired
private ApiLogService reqLogService;
@Autowired
private HttpsUtils httpsUtils;
/**
* 数据表查询处理
* @param dataTableSelectTO
*/
@Override
public Boolean dataTableHandle(DataTableSelectTO dataTableSelectTO) {
JSONObject jsonParams = JSONObject.parseObject(dataTableSelectTO.getReqParams());
Map param = JSONObject.parseObject(jsonParams.getString("request_fileds"), Map.class);
//校验参数合法性
ApiInterfaceCustom apiCustomInfo = checkParamLegal(param, dataTableSelectTO.getApiInterface());
if (ApiInfoOutTypeEnum.FLOW.name().equals(dataTableSelectTO.getApiInterface().getOutputType())) {//文件流形式请求
return flowRequestMethod(dataTableSelectTO.getApiInterface().getTargetUrl(), param, dataTableSelectTO.getOpenApiResponse(), apiCustomInfo, dataTableSelectTO.getApiInterface());
} else if (ApiInfoOutTypeEnum.JSON.name().equals(dataTableSelectTO.getApiInterface().getOutputType())) { //json格式请求
return jsonRequestMethod(dataTableSelectTO.getApiInterface().getTargetUrl(), param, jsonParams,
dataTableSelectTO.getOpenApiResponse(), apiCustomInfo,dataTableSelectTO.getApiInterface());
} 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");
String field = param.get(name);
if (null == field) {
tag = true;
}
}
}
if (tag) {
throw new GatewayException(GatewayResultCode.REQUEST_PARAM_EMPTY);
}
}
}
return apiCustomInfo;
}
/**
* 数据查询---flow流请求方式
*
* @param targetUrl
* @param param
* @param response
* @return
*/
private boolean flowRequestMethod(String targetUrl, Map<String, String> param,
OpenApiResponse response, ApiInterfaceCustom apiCustomInfo,
ApiInterface apiInterface) {
String dataSize = param.get("dataSize");
Integer valueOf = -1;
if (StringUtils.isNotBlank(dataSize)) {
valueOf = Integer.valueOf(dataSize);
}
net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param);
JSONObject requestParams = new JSONObject();
requestParams.put("datasourceId", apiCustomInfo.getEsDataSource());
requestParams.put("query_database", apiCustomInfo.getEsDataBase());
requestParams.put("query_table", apiCustomInfo.getEsTable());
requestParams.put("request_fileds", reqParams);
requestParams.put("response_fields", assembleResponseParams(apiCustomInfo.getResponseParam()));
requestParams.put("data_size", valueOf);
HttpDownload.postDownload(targetUrl, requestParams);
reqLogService.updateLog(apiInterface.getLogId(), null);
response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg());
return true;
}
/**
* 组装响应参数
*
* @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);
}
/**
* 数据查询--json请求方式
*
* @param targetUrl
* @param param
* @param response
* @param apiCustomInfo
* @return
*/
private boolean jsonRequestMethod(String targetUrl, Map<String, String> param, JSONObject jsonParams,
OpenApiResponse response, ApiInterfaceCustom apiCustomInfo,ApiInterface apiInterface) {
JSONObject params = new JSONObject();
try {
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", jsonParams.getJSONObject("request_fileds"));//请求参数
params.put("response_fields", assembleResponseParams(apiCustomInfo.getResponseParam()));//响应参数
params.put("is_test", jsonParams.getBoolean("is_test"));//是否是测试
Integer pageNum = jsonParams.getInteger("page_num");
Integer pageSize = jsonParams.getInteger("page_size");
params.put("page_size", apiCustomInfo.getPageRow());
params.put("page_num", 1);
if (null != pageNum) {
params.put("page_num", pageNum);
}
if (null != pageSize) {
params.put("page_size", pageSize);
}
} catch (Exception ex) {
log.error("数据转换异常:{}", ex.getMessage());
ex.printStackTrace();
}
String respResult = httpsUtils.submitPost(targetUrl, params.toString());
return callMethodResponse(respResult, response, apiInterface);
}
/**
* 调用方法处理结果
*
* @param result
* @param response
*/
private boolean callMethodResponse(String result, OpenApiResponse response,ApiInterface apiInterface) {
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.setMsg(GatewayResultCode.SUCCESS.getMsg());
response.setAttribute("responseResult",resp);
if (null != apiInterface.getLogId()){
reqLogService.updateLog(apiInterface.getLogId(),result);
}
return true;
} else {
log.error("~~~~~~~~~~~~~~~~远程请求异常~~~~~~~~~~~~~~~~~");
reqLogService.updateLog(apiInterface.getLogId(),result);
throw new GatewayException(GatewayResultCode.DISTANCE_REQUEST_EXCEPTION);
}
}
}
package com.jz.dm.service.request.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jz.common.utils.HttpsUtils;
import com.jz.dm.common.enums.GatewayResultCode;
import com.jz.dm.common.exception.GatewayException;
import com.jz.dm.common.util.OpenApiResponse;
import com.jz.dm.mapper.ApiFunctionMapper;
import com.jz.dm.models.domian.ApiFunction;
import com.jz.dm.models.domian.ApiInterface;
import com.jz.dm.models.domian.ApiInterfaceCustom;
import com.jz.dm.models.enity.DataTableSelectTO;
import com.jz.dm.service.ApiInterfaceService;
import com.jz.dm.service.ApiLogService;
import com.jz.dm.service.request.ThirdSelectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.service.request
* @PROJECT_NAME: jz-dm-parent
* @NAME: ThirdSelectHandle
* @DATE: 2021-2-24/10:42
* @DAY_NAME_SHORT: 周三
* @Description:
**/
@Slf4j
@Component
public class ThirdSelectHandle implements ThirdSelectService {
public static final String JSON_CONTENT_FORM = "application/json; charset=utf-8";
public static final String CONTENT_FORM = "application/x-www-form-urlencoded;charset=UTF-8";
public static final String FROM_DATA_CONTENT_FORM = "application/from-data;charset=UTF-8";
@Autowired
private ApiLogService reqLogService;
@Autowired
private HttpsUtils httpsUtils;
@Autowired
private ApiFunctionMapper apiFunctionMapper;
@Autowired
private ApiInterfaceService apiInterfaceService;
/**
* 三方查询处理
* @param dataTableSelectTO
* @return
*/
@Override
public boolean thirdSelectHandle(DataTableSelectTO dataTableSelectTO) {
if ("POST".equalsIgnoreCase(dataTableSelectTO.getApiInterface().getReqType())) {
Map map = JSONObject.parseObject(dataTableSelectTO.getReqParams(), Map.class);
Map reqParam = assembleReqParam(map, dataTableSelectTO.getApiInterface(), dataTableSelectTO.getAuthCode());
net.sf.json.JSONObject fromObject = net.sf.json.JSONObject.fromObject(reqParam);
HashMap<String, String> headers = new HashMap<>();
if ("1".equals(dataTableSelectTO.getApiInterface().getPostType())) {
headers.put("Content-type", CONTENT_FORM);
} else if ("2".equals(dataTableSelectTO.getApiInterface().getPostType())) {
headers.put("Content-type", FROM_DATA_CONTENT_FORM);
} else {
headers.put("Content-type", JSON_CONTENT_FORM);
}
String result = httpsUtils.submitPost(dataTableSelectTO.getApiInterface().getTargetUrl(), fromObject.toString(),headers);
return callMethodResponse(result,dataTableSelectTO.getOpenApiResponse(), dataTableSelectTO.getApiInterface());
} else {
Map map = JSONObject.parseObject(dataTableSelectTO.getReqParams(), Map.class);
Map reqParam = assembleReqParam(map, dataTableSelectTO.getApiInterface(), dataTableSelectTO.getAuthCode());
String result = httpsUtils.doGet(dataTableSelectTO.getApiInterface().getTargetUrl(), reqParam);
return callMethodResponse(result, dataTableSelectTO.getOpenApiResponse(), dataTableSelectTO.getApiInterface());
}
}
/**
* 组装请求参数
*
* @param param
* @param apiInterface
* @return
*/
private Map assembleReqParam(Map param, ApiInterface apiInterface, String authCode) {
List<String> listFuncNames = apiFunctionMapper.listFunctionNames();
ApiInterfaceCustom apiCustomInfo = apiInterfaceService.getApiCustomInfo(apiInterface.getId());
if (null != apiCustomInfo) {
List<JSONObject> jsonList = JSONObject.parseArray(apiCustomInfo.getInboxParam(), JSONObject.class);
if (CollectionUtils.isNotEmpty(jsonList)) {
for (JSONObject paObject : jsonList) {
String functionName = paObject.getString(("type"));
if (listFuncNames.contains(functionName)) {
String name = paObject.getString("name");
if (functionName.equals("function03")) {
String functionVal = getFunctionReqData(functionName, authCode, null);
if (StringUtils.isNotBlank(functionVal)) {
param.put(name, functionVal);
}
} else {
String functionVal = getFunctionReqData(functionName, null, param);
if (StringUtils.isNotBlank(functionVal)) {
param.put(name, functionVal);
}
}
}
}
}
}
return param;
}
private String getFunctionReqData(String name, String authCode, Map<String, String> signMap) {
String resultStr = "";
QueryWrapper<ApiFunction> queryWra = new QueryWrapper<>();
queryWra.eq("name", name);
queryWra.eq("status", true);
queryWra.eq("is_deleted", 0);
ApiFunction apiFunction = apiFunctionMapper.selectOne(queryWra);
if (null != apiFunction) {
JSONObject reqParameter = new JSONObject();
if ("name".equals("function03")) {//根据不同模板组装不同请求参数
reqParameter.put("authCode", authCode);
} else {
reqParameter.put("signParams", signMap);
}
String result = httpsUtils.submitPost(apiFunction.getTemplateUrl(), reqParameter.toString());
if (null != result) {
JSONObject jsonObject = JSONObject.parseObject(result);
if (200 == jsonObject.getInteger("code")) {
if ("name".equals("function03")) {
resultStr = jsonObject.getString("data");
} else {
JSONObject data = jsonObject.getJSONObject("data");
resultStr = data.getString("signStr");
}
}
}
}
return resultStr;
}
/**
* 调用方法处理结果
*
* @param result
* @param response
*/
private boolean callMethodResponse(String result, OpenApiResponse response, ApiInterface apiInterface) {
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.setMsg(GatewayResultCode.SUCCESS.getMsg());
response.setAttribute("responseResult", resp);
if (null != apiInterface.getLogId()) {
reqLogService.updateLog(apiInterface.getLogId(), result);
}
return true;
} else {
log.error("~~~~~~~~~~~~~~~~远程请求异常~~~~~~~~~~~~~~~~~");
reqLogService.updateLog(apiInterface.getLogId(), result);
throw new GatewayException(GatewayResultCode.DISTANCE_REQUEST_EXCEPTION);
}
}
}
......@@ -8,6 +8,9 @@ import com.jz.dm.gateway.SpringTestCase;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap;
import java.util.Map;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.gateway.api
......@@ -23,12 +26,16 @@ public class ApiReqTest extends SpringTestCase {
@Autowired
private HttpsUtils httpsUtils;
/**
* 生成数据查询json请求签名
*/
@Test
public void TestGatewayReq() {
public void TestGatewayJsonReq() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("apiKey", "Y18q7v2Rq332H3ox");
jsonObject.put("method", "query");
jsonObject.put("method", "request");
jsonObject.put("signType", "MD5");
long time = System.currentTimeMillis();
String date = String.valueOf(time);
......@@ -36,21 +43,73 @@ public class ApiReqTest extends SpringTestCase {
JSONObject params = new JSONObject();
params.put("authCode", "20210000021281934021n275Hn63qaxP");
params.put("reqParams", new JSONObject());
params.put("isTest",false);
JSONObject reqParams = new JSONObject();
reqParams.put("datasourceId","1");
reqParams.put("query_database","product");
reqParams.put("query_table","table1");
JSONObject fields = new JSONObject();
fields.put("flelds1","xxxxxx");
fields.put("flelds2","xxxxxx");
reqParams.put("request_fileds",fields);
reqParams.put("response_fields","field1,field2,field3,field4");
reqParams.put("page_num",1);
reqParams.put("page_size",100);
params.put("reqParams",reqParams);
jsonObject.put("params",params);
try {
String apiKey = jsonObject.getString("apiKey");
String method = jsonObject.getString("method");
String signType = jsonObject.getString("signType");
String signature = MapUtil.getSignValue(apiKey, method, signType);
String sign = Md5.encrypt(signature, "RPf8HL36");
jsonObject.put("sign", sign);
/* String response = httpsUtils.submitPost(url, jsonObject.toString());*/
Map<String, String> paramsMap = new HashMap<>();
paramsMap.put("apiKey", jsonObject.getString("apiKey"));
paramsMap.put("method", jsonObject.getString("method"));
paramsMap.put("signType", jsonObject.getString("signType"));
paramsMap.put("params", params.toString());
String signParams = MapUtil.stringNormalSort(paramsMap);
String sign = Md5.encrypt(signParams, "RPf8HL36");
System.out.println("时间戳为:"+date+"--签名为:=="+sign);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成数据查询flow请求签名
*/
@Test
public void TestGatewayFlowReq() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("apiKey", "fM59D6210E3K436m");
jsonObject.put("method", "request");
jsonObject.put("signType", "MD5");
long time = System.currentTimeMillis();
String date = String.valueOf(time);
jsonObject.put("timestamp", date);
JSONObject params = new JSONObject();
params.put("authCode", "20210000000111181048520T38nzc5x7");
params.put("isTest",false);
JSONObject reqParams = new JSONObject();
reqParams.put("datasourceId","1");
reqParams.put("query_database","product");
reqParams.put("query_table","table1");
JSONObject fields = new JSONObject();
fields.put("flelds1","xxxxxx");
fields.put("flelds2","xxxxxx");
reqParams.put("request_fileds",fields);
reqParams.put("response_fields","field1,field2,field3,field4");
reqParams.put("data_size",500);
params.put("reqParams",reqParams);
jsonObject.put("params",params);
try {
Map<String, String> paramsMap = new HashMap<>();
paramsMap.put("apiKey", jsonObject.getString("apiKey"));
paramsMap.put("method", jsonObject.getString("method"));
paramsMap.put("signType", jsonObject.getString("signType"));
paramsMap.put("params", params.toString());
String signParams = MapUtil.stringNormalSort(paramsMap);
String sign = Md5.encrypt(signParams, "IE36FItU");
System.out.println("时间戳为:"+date+"--签名为:=="+sign);
} catch (Exception e) {
e.printStackTrace();
}
}
}
......@@ -38,7 +38,7 @@ public class TestHttpReq extends SpringTestCase {
headers.put("connection", "keep-alive");
headers.put("Charsert", "UTF-8");
headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
String response = httpsUtils.submitPost(baseUrlPost, jsonObject,headers);
String response = httpsUtils.submitPost(baseUrlPost, jsonObject.toString(),headers);
System.out.println(response);
}
......
......@@ -38,7 +38,7 @@ public class TestMakeApi extends SpringTestCase {
params.put("apiProtocl","HTTPS");
params.put("type","1");
try {
String result = httpsUtils.submitPost(url, params,headers);
String result = httpsUtils.submitPost(url, params.toString(),headers);
System.out.println("接受到的结果为:"+result);
} catch (Exception e) {
e.printStackTrace();
......
......@@ -17,14 +17,14 @@ public enum ResultCode {
/** 执行成功 */
SUCCESS(200, ResultMsg.SUCCESS),
/** 执行失败 */
FAILURE(300, ResultMsg.DATA_NOT_FOUND),
/** 未授权的访问 */
FAILURE(300, ResultMsg.DATA_NOT_FOUND);
/*
*//** 未授权的访问 *//*
UNAUTHORIZED(401, ResultMsg.UNAUTHORIZED),
/** 授权过期 */
*//** 授权过期 *//*
INVALID_TOKEN(402, ResultMsg.INVALID_TOKEN),
/** 禁止访问 */
FORBIDOM(403, ResultMsg.FORBIDOM);
*//** 禁止访问 *//*
FORBIDOM(403, ResultMsg.FORBIDOM);*/
private int code;
......
package com.jz.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
......@@ -45,20 +44,20 @@ public class HttpsUtils {
* @param headers 请求头
* @return
*/
public String submitPost(String url, JSONObject params, Map<String, String> headers) {
public String submitPost(String url, String params, Map<String, String> headers) {
CloseableHttpClient httpClient = getHttpClient();
String body = null;
CloseableHttpResponse response = null;
try {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-type",JSON_CONTENT_FORM );
/* httpPost.setHeader("Content-type",JSON_CONTENT_FORM );*/
if (null != headers && headers.size() > 0) {
for (Map.Entry<String, String> e : headers.entrySet()) {
httpPost.addHeader(e.getKey(), e.getValue());
}
}
if (StringUtils.isNotBlank(params.toString())) {
httpPost.setEntity(new StringEntity(params.toString(), Consts.UTF_8));
if (StringUtils.isNotBlank(params)) {
httpPost.setEntity(new StringEntity(params, Consts.UTF_8));
}
response = httpClient.execute(httpPost);
body = getBody(response.getEntity());
......
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