Commit 02d5428f authored by zhangc's avatar zhangc

commit 添加api请求相关类

parent 7ecbcfde
...@@ -66,8 +66,12 @@ public enum GatewayResultCode implements ResultCode { ...@@ -66,8 +66,12 @@ public enum GatewayResultCode implements ResultCode {
API_STATUS_EXCEPTION("API_STATUS_EXCEPTION", "API状态异常"), API_STATUS_EXCEPTION("API_STATUS_EXCEPTION", "API状态异常"),
/** 请求组织状态异常 */ /** 请求组织状态异常 */
ORG_STATE_EXCEPTION("ORG_STATE_EXCEPTION", "请求组织状态异常"); ORG_STATE_EXCEPTION("ORG_STATE_EXCEPTION", "请求组织状态异常"),
/** 请求信息不存在 */
REQUEST_INFO_UNEXIST("REQUEST_INFO_UNEXIST", "请求信息不存在"),
/** 输出类型错误 */
OUTPUT_TYPE_EXCEPTION("OUTPUT_TYPE_EXCEPTION", "输出类型错误!");
/** /**
* 初始化保存到map里方便根据code获取 * 初始化保存到map里方便根据code获取
*/ */
......
...@@ -16,7 +16,7 @@ import java.util.Map; ...@@ -16,7 +16,7 @@ import java.util.Map;
*/ */
public class OpenApiRequest { public class OpenApiRequest {
private String appKey; //apiKey private String apiKey; //apiKey
private String openApiParams; private String openApiParams;
...@@ -41,11 +41,11 @@ public class OpenApiRequest { ...@@ -41,11 +41,11 @@ public class OpenApiRequest {
} }
public String getAppKey() { public String getAppKey() {
return appKey; return apiKey;
} }
public void setAppKey(String appKey) { public void setAppKey(String appKey) {
this.appKey = appKey; this.apiKey = appKey;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
......
...@@ -75,7 +75,7 @@ public class AuthFilter extends AbstractFilter { ...@@ -75,7 +75,7 @@ public class AuthFilter extends AbstractFilter {
JSONObject parameter = JSONObject.parseObject(request.getParams()); JSONObject parameter = JSONObject.parseObject(request.getParams());
if (null != parameter) { if (null != parameter) {
String authCode = parameter.getString("authCode"); String authCode = parameter.getString("authCode");
ApiAuth authAuth = authService.getAuthUser(authCode); ApiAuth authAuth = authService.getAuthUser(authCode,apiInterface.getId());
if (null == authAuth) { if (null == authAuth) {
throw new GatewayException(GatewayResultCode.REQUEST_NOT_AUTH); throw new GatewayException(GatewayResultCode.REQUEST_NOT_AUTH);
} }
......
...@@ -13,4 +13,6 @@ import com.jz.dm.models.domian.ApiInterface; ...@@ -13,4 +13,6 @@ import com.jz.dm.models.domian.ApiInterface;
* @Description: * @Description:
**/ **/
public interface ApiInterfaceMapper extends BaseMapper<ApiInterface> { public interface ApiInterfaceMapper extends BaseMapper<ApiInterface> {
} }
...@@ -18,9 +18,9 @@ import java.util.Map; ...@@ -18,9 +18,9 @@ import java.util.Map;
@Data @Data
public class GatewayRequest implements Serializable { public class GatewayRequest implements Serializable {
@ParamName("app_key") @ParamName("api_key")
@ApiModelProperty(value = "appKey API唯一标识") @ApiModelProperty(value = "appKey API唯一标识")
private String appKey; private String apiKey;
@ApiModelProperty(value = "接口名称") @ApiModelProperty(value = "接口名称")
private String method; private String method;
...@@ -88,7 +88,7 @@ public class GatewayRequest implements Serializable { ...@@ -88,7 +88,7 @@ public class GatewayRequest implements Serializable {
*/ */
@Override @Override
public String toString() { public String toString() {
return "GatewayRequest [appKey=" + appKey + ", method=" + method + ", format=" + format return "GatewayRequest [appKey=" + apiKey + ", method=" + method + ", format=" + format
+ ", signType=" + signType + ", sign=" + sign + ", signType=" + signType + ", sign=" + sign
+ ", timestamp=" + timestamp + ", version=" + version + ", timestamp=" + timestamp + ", version=" + version
+ ", params=" + params + ", extAttributes=" + ", params=" + params + ", extAttributes="
......
...@@ -24,8 +24,15 @@ public interface ApiInterfaceService { ...@@ -24,8 +24,15 @@ public interface ApiInterfaceService {
/** /**
* 获取api信息 * 获取api信息
* @param appKey * @param apiKey
* @return
*/
ApiInterface getApiInfo(String apiKey);
/**
* 获取apikey信息
* @param apiKey
* @return * @return
*/ */
ApiInterface getApiInfo(String appKey); ApiInterface getReqTargetInfo(String apiKey);
} }
...@@ -59,7 +59,8 @@ public interface AuthService { ...@@ -59,7 +59,8 @@ public interface AuthService {
/** /**
* 获取API授权信息 * 获取API授权信息
* @param authCode * @param authCode
* @param apiId
* @return * @return
*/ */
ApiAuth getAuthUser( String authCode); ApiAuth getAuthUser( String authCode,Long apiId);
} }
...@@ -54,14 +54,22 @@ public class ApiInterfaceServiceImpl implements ApiInterfaceService { ...@@ -54,14 +54,22 @@ public class ApiInterfaceServiceImpl implements ApiInterfaceService {
/** /**
* 获取API信息 * 获取API信息
* @param appKey * @param apiKey
* @return * @return
*/ */
@Override @Override
public ApiInterface getApiInfo(String appKey) { public ApiInterface getApiInfo(String apiKey) {
QueryWrapper<ApiInterface> query = new QueryWrapper<>();
query.eq("is_deleted",0);
query.eq("api_key",apiKey);
return apiInterfaceMapper.selectOne(query);
}
@Override
public ApiInterface getReqTargetInfo(String apiKey) {
QueryWrapper<ApiInterface> query = new QueryWrapper<>(); QueryWrapper<ApiInterface> query = new QueryWrapper<>();
query.eq("api_key",apiKey);
query.eq("is_deleted",0); query.eq("is_deleted",0);
query.eq("api_key",appKey);
return apiInterfaceMapper.selectOne(query); return apiInterfaceMapper.selectOne(query);
} }
} }
...@@ -267,12 +267,14 @@ public class AuthServiceImpl implements AuthService { ...@@ -267,12 +267,14 @@ public class AuthServiceImpl implements AuthService {
/** /**
* 获取API授权信息 * 获取API授权信息
* @param authCode * @param authCode
* @param apiId
* @return * @return
*/ */
@Override @Override
public ApiAuth getAuthUser(String authCode) { public ApiAuth getAuthUser(String authCode,Long apiId) {
QueryWrapper<ApiAuth> query = new QueryWrapper<>(); QueryWrapper<ApiAuth> query = new QueryWrapper<>();
query.eq("auth_code",authCode); query.eq("auth_code",authCode);
query.eq("api_interface_id",apiId);
query.eq("is_deleted",0); query.eq("is_deleted",0);
return apiAuthMapper.selectOne(query); return apiAuthMapper.selectOne(query);
} }
......
...@@ -4,9 +4,11 @@ import com.alibaba.fastjson.JSONObject; ...@@ -4,9 +4,11 @@ import com.alibaba.fastjson.JSONObject;
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.enums.GatewayResultCode; import com.jz.dm.common.enums.GatewayResultCode;
import com.jz.dm.common.exception.GatewayException;
import com.jz.dm.common.util.OpenApiRequest; import com.jz.dm.common.util.OpenApiRequest;
import com.jz.dm.common.util.OpenApiResponse; import com.jz.dm.common.util.OpenApiResponse;
import com.jz.dm.gateway.OpenApiService; import com.jz.dm.gateway.OpenApiService;
import com.jz.dm.models.domian.ApiInterface;
import com.jz.dm.service.ApiInterfaceService; import com.jz.dm.service.ApiInterfaceService;
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;
...@@ -47,53 +49,80 @@ public class ApiQueryService implements OpenApiService { ...@@ -47,53 +49,80 @@ public class ApiQueryService implements OpenApiService {
private RedisUtils redisUtils; private RedisUtils redisUtils;
@Autowired @Autowired
private HttpsUtils httpsUtils; private HttpsUtils httpsUtils;
@Override @Override
@ApiLogAspect(description = "API请求日志") @ApiLogAspect(description = "API请求日志")
@AccessLimit(limit = 10000,sec = 1) @AccessLimit(limit = 10000, sec = 1)
@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) {
JSONObject parameter = JSONObject.parseObject(request.getOpenApiParams()); JSONObject parameter = JSONObject.parseObject(request.getOpenApiParams());
String apiType = parameter.getString("apiType"); String selectType = parameter.getString("selectType");
if (StringUtils.isNotBlank(apiType)){ String reqParams = parameter.getString("reqParams");
Map paramMap =null;
if (StringUtils.isNotBlank(reqParams)) {
paramMap = (Map) JSONObject.parseObject(reqParams);
}
String apiInterface = redisUtils.get(request.getAppKey());
if (StringUtils.isNotBlank(selectType)) {
try { try {
switch (apiType){ switch (selectType) {
case "QUERY_TAG": //标签查询 case "QUERY_TAG": //标签查询
//String reqParam = parameter.getString("request_param");
//apiInterfaceService.getTagReqData();
break; break;
case "BIG_DATA_QUERY"://大数据查询 case "BIG_DATA_QUERY"://大数据查询
break; break;
case "CUSTOM_QUERY": //自定义查询(三方查询) case "CUSTOM_QUERY": //自定义查询(三方查询)
if (StringUtils.isNotBlank(apiInterface)){//redis中存在
JSONObject jsonCustom = JSONObject.parseObject(apiInterface);
String targetUrl = jsonCustom.getString("targetUrl");
rangRequestTarget("json",targetUrl,null,response);
}else {//redis中不存在,查询数据库
ApiInterface apiReqDto =
apiInterfaceService.getReqTargetInfo(request.getAppKey());
if (null == apiReqDto) {
throw new GatewayException(GatewayResultCode.REQUEST_INFO_UNEXIST);
}
rangRequestTarget("json",apiReqDto.getTargetUrl(),null,response);
}
break; break;
case "DATA_BANK_QUERY": //数据银行查询 case "DATA_BANK_QUERY": //数据银行查询
String reqParams = parameter.getString("reqParams"); if (null != apiInterface) {//redis中存在
if (StringUtils.isNotBlank(reqParams)){
String apiInterface = redisUtils.get(request.getAppKey());
if (null != apiInterface){//存在
JSONObject jsonObject = JSONObject.parseObject(apiInterface); JSONObject jsonObject = JSONObject.parseObject(apiInterface);
String requestParam = jsonObject.getString("requestParam"); String targetUrl = jsonObject.getString("targetUrl");
String targetUrl =jsonObject.getString("targetUrl"); String outputType = jsonObject.getString("outputType");
Map param = (Map)JSONObject.parseObject(requestParam); rangRequestTarget(outputType, targetUrl, paramMap, response);
String respResult = httpsUtils.doGet(targetUrl, param); } else {//不存在查询数据库
response.setAttribute(respResult); ApiInterface apiReqDto =
}else {//不存在查询数据库 apiInterfaceService.getReqTargetInfo(request.getAppKey());
//apiInterfaceService.getReqTargetInfo(request.getAppKey()); if (null == apiReqDto) {
throw new GatewayException(GatewayResultCode.REQUEST_INFO_UNEXIST);
} }
rangRequestTarget(apiReqDto.getOutputType(), apiReqDto.getTargetUrl(),paramMap,response);
} }
break; break;
case "DATA_BAG_QUERY": //数据包查询 case "DATA_BAG_QUERY": //数据银行数据包查询
break; break;
default : default:
response.setCode(GatewayResultCode.ILLEGAL_REQUEST.getCode()); response.setCode(GatewayResultCode.ILLEGAL_REQUEST.getCode());
response.setMsg(GatewayResultCode.ILLEGAL_REQUEST.getMsg()); response.setMsg(GatewayResultCode.ILLEGAL_REQUEST.getMsg());
break; break;
} }
} catch(Exception e) { } catch (Exception e) {
log.error("~~~~~~~~~~~~~~~请求api信息异常~~~~~~~~~~~~~"); log.error("~~~~~~~~~~~~~~~请求api信息异常~~~~~~~~~~~~~");
log.error("异常信息:{}",e.getMessage()); log.error("异常信息:{}", e.getMessage());
response.setCode(GatewayResultCode.ILLEGAL_REQUEST.getCode()); response.setCode(GatewayResultCode.ILLEGAL_REQUEST.getCode());
response.setMsg(GatewayResultCode.ILLEGAL_REQUEST.getMsg()); response.setMsg(GatewayResultCode.ILLEGAL_REQUEST.getMsg());
} }
} }
} }
private void rangRequestTarget(String outputType, String targetUrl, Map<String, String> param, OpenApiResponse response) {
if ("flow".equals(outputType)) {//文件流形式请求
// httpsUtils.doGetImg()TODO 流形式未实现
} else if ("json".equals(outputType)) {
String respResult = httpsUtils.doGet(targetUrl, param);
response.setAttribute(respResult);
} else {
throw new GatewayException(GatewayResultCode.OUTPUT_TYPE_EXCEPTION);
}
}
} }
...@@ -9,4 +9,5 @@ ...@@ -9,4 +9,5 @@
update_user, is_deleted update_user, is_deleted
</sql> </sql>
</mapper> </mapper>
\ No newline at end of file
package com.jz.common.utils; package com.jz.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
...@@ -45,7 +44,7 @@ public class HttpsUtils { ...@@ -45,7 +44,7 @@ public class HttpsUtils {
* @param headers 请求头 * @param headers 请求头
* @return * @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(); CloseableHttpClient httpClient = getHttpClient();
String body = null; String body = null;
CloseableHttpResponse response = null; CloseableHttpResponse response = null;
...@@ -57,8 +56,8 @@ public class HttpsUtils { ...@@ -57,8 +56,8 @@ public class HttpsUtils {
httpPost.addHeader(e.getKey(), e.getValue()); httpPost.addHeader(e.getKey(), e.getValue());
} }
} }
if (StringUtils.isNotBlank(params.toString())) { if (StringUtils.isNotBlank(params)) {
httpPost.setEntity(new StringEntity(params.toString(), Consts.UTF_8)); httpPost.setEntity(new StringEntity(params, Consts.UTF_8));
} }
response = httpClient.execute(httpPost); response = httpClient.execute(httpPost);
body = getBody(response.getEntity()); 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