Commit 78fd3f92 authored by zhangc's avatar zhangc

添加post文件流下载方法,添加测试api方法

parent 77e8de6b
package com.jz.dm.common.util.stream;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.*;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.File;
......@@ -14,6 +19,7 @@ import java.text.MessageFormat;
import static com.jz.common.utils.HttpClientPool.getHttpClient;
/**
* @author ZC
* @PACKAGE_NAME: com.sentinel.project.util
......@@ -48,8 +54,90 @@ public class HttpDownload {
* @param url
* @return
*/
public static void download(String url) {
download(url, null);
public static void getDownload(String url) {
download(url, null);
}
/**
* 根据url下载文件,文件名从response header头中获取
*
* @param url
* @param params
*/
public static void postDownload(String url, JSONObject params) {
postDownloadFolder(url, params,null);
}
/**
* 根据url下载文件,保存到filepath中
*
* @param url
* @param params
* @param filepath
*/
private static void postDownloadFolder(String url, JSONObject params,String filepath) {
CloseableHttpClient httpClient = getHttpClient();
InputStream is = null;
FileOutputStream fileOut = null;
CloseableHttpResponse response = null;
try {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type","application/json" );
if (StringUtils.isNotBlank(params.toString())) {
httpPost.setEntity(new StringEntity(params.toString(), Consts.UTF_8));
}
response = httpClient.execute(httpPost);
Header[] allHeaders = response.getAllHeaders();
for (Header header : allHeaders) {
System.out.println(MessageFormat.format("header:{0}={1}", header.getName(), header.getValue()));
}
String fileName = response.getHeaders("Content-Disposition")[0].getValue().split("filename=")[1];
System.out.println("文件名为" + fileName);
HttpEntity entity = response.getEntity();
is = entity.getContent();
if (fileName == null) {
fileName = getFilePath(response);
}
if (filepath != null) {
fileName = filepath + splash + fileName;
} else {
fileName = splash + fileName;
}
File file = new File(fileName);
file.getParentFile().mkdirs();
fileOut = new FileOutputStream(file);
/**
* 根据实际运行效果 设置缓冲区大小
*/
byte[] buffer = new byte[cache];
int ch = 0;
while ((ch = is.read(buffer)) != -1) {
fileOut.write(buffer, 0, ch);
}
fileOut.flush();
log.info("文件下载成功!");
} catch (Exception e) {
log.info("数据下载异常:{}", e.getMessage());
e.printStackTrace();
}finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != fileOut) {
try {
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
......@@ -81,8 +169,8 @@ public class HttpDownload {
}
if (filepath != null) {
fileName = filepath + splash + fileName;
}else {
fileName= splash + fileName;
} else {
fileName = splash + fileName;
}
File file = new File(fileName);
file.getParentFile().mkdirs();
......@@ -185,8 +273,22 @@ public class HttpDownload {
String docx = "http://192.168.1.140:8090/api/download/docx";
String pdf = "http://192.168.1.140:8090/api/download/pdf";
String xlsx = "http://192.168.1.140:8090/api/download/xlsx";
String getDownload ="http://192.168.1.140:8082/api/data/query/streaming?datasourceId=2&query_database=product&query_table=table1&request_fileds=%7b%22flelds1%22:%20%22xxxx%22,%20%22field2%22:%22xxxx%22%7d&response_fields=field1,field2,field3,field4&data_size=100";
String postDownload="http://192.168.1.140:8082/api/data/query/streaming1";
//String filepath = "C:\\Users\\key\\Desktop\\ideaIU-2019.3.3";
//HttpDownload.download(xlsx, null);
//getDownload(getDownload);
JSONObject jsonObject = new JSONObject();
jsonObject.put("datasourceId",1);
jsonObject.put("query_database","product");
jsonObject.put("query_table","table1");
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("flelds1","xxxxx");
jsonObject1.put("field2","xxxxx");
jsonObject.put("request_fileds",jsonObject1);
jsonObject.put("response_fields","field1,field2,field3,field4");
jsonObject.put("data_size",100);
String filepath = "C:\\Users\\key\\Desktop\\ideaIU-2019.3.3";
HttpDownload.download(xlsx, filepath);
postDownloadFolder(postDownload,jsonObject,null);
}
}
......@@ -12,6 +12,7 @@ import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
......@@ -32,7 +33,8 @@ public class GatewayController {
@Autowired
private GatewayService gatewayService;
@RequestMapping(value = "/gateway", consumes = "application/json")
//@RequestMapping(value = "/gateway", consumes = "application/json")
@PostMapping(value="/gateway",consumes = "application/json")
public String gateway(@RequestBody String json, HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
GatewayRequest gatewayRequest = JSON.parseObject(json, GatewayRequest.class);
......
......@@ -126,7 +126,6 @@ public class AuthFilter extends AbstractFilter {
response.clearAttributes();
response.setCode(GatewayResultCode.UNKNOWN_EXCEPTION.getCode());
response.setMsg(GatewayResultCode.UNKNOWN_EXCEPTION.getMsg());
}
}
......
package com.jz.dm.filter;
import com.jz.dm.common.enums.GatewayResultCode;
import com.jz.dm.common.exception.OpenApiException;
import com.jz.dm.common.util.LogUtil;
import com.jz.dm.common.util.ResultCode;
import com.jz.dm.models.enity.GatewayRequest;
import com.jz.dm.models.enity.GatewayResponse;
import com.jz.dm.common.enums.GatewayResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -19,13 +19,14 @@ import java.util.List;
*/
public class FilterChainImpl implements FilterChain {
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
private int pos = 0;
private int pos = 0;
private final List<Filter> filters = new ArrayList<Filter>();
public FilterChainImpl() { }
public FilterChainImpl() {
}
public FilterChainImpl(List<Filter> filters) {
addFilters(filters);
......@@ -42,7 +43,7 @@ public class FilterChainImpl implements FilterChain {
} catch (OpenApiException ex) {
ResultCode resultCode = ex.getResultCode();
LogUtil.error(LOGGER, ex, "doFilter occur exception,code=" + resultCode.getCode()
+ ",msg=" + resultCode.getMsg() + ",request=" + request);
+ ",msg=" + resultCode.getMsg() + ",request=" + request);
response.setCode(ex.getResultCode().getCode());
response.setMsg(ex.getResultCode().getMsg());
} catch (Throwable ex) {
......
......@@ -19,15 +19,16 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @Description: 验签过滤器(验证签名信息)
* @Author: Mr.zhang
* @Date: 2021-1-6
*/
* @Description: 验签过滤器(验证签名信息)
* @Author: Mr.zhang
* @Date: 2021-1-6
*/
@Component
public class VerifySignFilter extends AbstractFilter {
private static final Logger LOGGER = LoggerFactory.getLogger(VerifySignFilter.class);
@Autowired
private ApiInterfaceService apiInterfaceService;
@Override
public int getOrder() {
return Constants.FILTER_ORDER_3;
......@@ -37,27 +38,27 @@ public class VerifySignFilter extends AbstractFilter {
public String getFilterName() {
return "VerifySignFilter";
}
@Override
protected void internalDoFilter(GatewayRequest request, GatewayResponse response,
FilterChain chain) {
try {
JSONObject jsonObject = JSONObject.parseObject(request.getParams());
if (!jsonObject.getBoolean("isTest")){//是否测试调用
if (!jsonObject.getBoolean("isTest")) {//是否测试调用
//对签约参数进行字典排序
String signParams = MapUtil.getSignValue(request.getApiKey(),request.getMethod(),request.getSignType());
if (StringUtils.isNotBlank(signParams)){
String signParams = MapUtil.getSignValue(request.getApiKey(), request.getMethod(), request.getSignType());
if (StringUtils.isNotBlank(signParams)) {
String authCode = jsonObject.getString("authCode");
//需要传入授权码
ApiAuth apiAuthInfo = apiInterfaceService.getApiAuthInfo(request.getApiKey(),authCode );
if (null == apiAuthInfo){
ApiAuth apiAuthInfo = apiInterfaceService.getApiAuthInfo(request.getApiKey(), authCode);
if (null == apiAuthInfo) {
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST);
}
String sign = Md5.encrypt(signParams, apiAuthInfo.getSalt());
if (!request.getSign().equals(sign)){
if (!request.getSign().equals(sign)) {
throw new GatewayException(GatewayResultCode.SIGN_ERROR);
}
}else {
} else {
throw new GatewayException(GatewayResultCode.SIGN_ERROR);
}
}
......@@ -69,8 +70,8 @@ public class VerifySignFilter extends AbstractFilter {
response.setCode(ex.getResultCode().getCode());
response.setMsg(ex.getResultCode().getMsg());
} catch (Throwable ex) {
if(ex instanceof GatewayException){
throw(GatewayException) ex;
if (ex instanceof GatewayException) {
throw (GatewayException) ex;
}
LogUtil.error(LOGGER, ex,
"signatureFilter doFilter error. response=" + response.getResponse());
......
package com.jz.dm.gateway;
import com.alibaba.fastjson.JSON;
import com.jz.dm.common.exception.GatewayException;
import com.jz.dm.common.exception.OpenApiException;
import com.jz.dm.common.util.OpenApiRequest;
import com.jz.dm.common.util.OpenApiResponse;
......@@ -65,18 +66,17 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher {
response.setMsg(OpenApiResultCode.ILLEGAL_INTERFACE.getMsg());
return JSON.toJSONString(response.getAttributes());
}
try {
OpenApiRequest request = new OpenApiRequest(context.getOpenApiParams());
request.setApiKey(context.getApiKey());
request.setExtAttributes(context.getExtAttributes());
openApiService.doService(request, response);
} catch (Throwable ex) {
}catch (Throwable ex) {
if (ex instanceof OpenApiException) {
OpenApiException oae = (OpenApiException) ex;
response.setCode(oae.getResultCode().getCode());
response.setMsg(oae.getResultCode().getMsg());
} else {
throw(OpenApiException) ex;
} else if (ex instanceof GatewayException){
throw(GatewayException) ex;
}else{
LOGGER.error("OpenApiService doService error,DispatchContext=" + context, ex);
response.setCode(OpenApiResultCode.UNKNOWN_EXCEPTION.getCode());
response.setMsg(OpenApiResultCode.UNKNOWN_EXCEPTION.getMsg());
......@@ -84,8 +84,8 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher {
return JSON.toJSONString(response.getAttributes());
}
if (StringUtil.isEmpty(response.getCode())) {
response.setCode(OpenApiResultCode.SUCCESS.getCode());
response.setMsg(OpenApiResultCode.SUCCESS.getMsg());
response.setCode(OpenApiResultCode.UNKNOWN_EXCEPTION.getCode());
response.setMsg(OpenApiResultCode.UNKNOWN_EXCEPTION.getMsg());
}
return JSON.toJSONString(response.getAttributes());
}
......
......@@ -49,6 +49,10 @@ public class GatewayRequest implements Serializable {
@ApiModelProperty(value="请求参数,JSON格式")
private String params;
@ApiModelProperty(value="是否测试请求")
private Boolean isTest;
@ApiModelProperty(value="扩展属性")
private final Map<String, Attribute> extAttributes = new HashMap<String, Attribute>();
......
......@@ -29,6 +29,8 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
......@@ -70,12 +72,6 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
@Value("${data.bank.balanceUrl}")
private String balanceUrl;
/**
* 数据包下载路径
*/
@Value("${dataPackage.downloadUrl}")
private String downloadUrl;
/**
* API请求逻辑处理
......@@ -93,48 +89,42 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
JSONObject parameter = JSONObject.parseObject(request.getOpenApiParams());
ApiInterface apiInterface = apiInterfaceService.getApiInfo(request.getApiKey());
ApiAuth apiAuth = null;
JSONObject jsonParams =null;
JSONObject jsonParams = null;
try {
Boolean isTest = parameter.getBoolean("isTest");
String reqParams = parameter.getString("reqParams");
parameter.put("is_test", isTest);
Map paramMap = null;
if (StringUtils.isNotBlank(reqParams)) {
jsonParams = JSONObject.parseObject(reqParams);
paramMap = JSONObject.parseObject(jsonParams.getString("request_fileds"), Map.class);
jsonParams.put("is_test",false);
}
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 = null;
if (StringUtils.isNotBlank(redisReqParam)) {//redis中存在
//解析出API制作成功时的参数配置
JSONObject jsonObject = JSONObject.parseObject(redisReqParam);
String targetUrl = jsonObject.getString("targetUrl");
String outputType = jsonObject.getString("outputType");
String joinType = jsonObject.getString("joinType");
bResult = rangRequestTarget(outputType, targetUrl, false, paramMap,
jsonParams, joinType, apiInterface, response);
} else {//不存在查询数据库
bResult = rangRequestTarget(apiInterface.getOutputType(),
apiInterface.getTargetUrl(), false, paramMap,jsonParams, apiInterface.getJoinType(), apiInterface, response);
}
//调用成功请求数据银行扣款
if (AuthModeEnum.POWER_CALL_MODE.name().equals(apiAuth.getAuthMode())) {
notifierMinusMoney(parameter, bResult);
//按次调用时处理(处理为已调用)
authService.updateApiAuthStatus(apiAuth);
}
} else {
bResult = rangRequestTarget(ApiInfoOutTypeEnum.JSON.name(), apiInterface.getTargetUrl(),
true, paramMap,jsonParams,apiInterface.getJoinType(), apiInterface, response);
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 = null;
if (StringUtils.isNotBlank(redisReqParam)) {//redis中存在
//解析出API制作成功时的参数配置
JSONObject jsonObject = JSONObject.parseObject(redisReqParam);
String targetUrl = jsonObject.getString("targetUrl");
String outputType = jsonObject.getString("outputType");
String joinType = jsonObject.getString("joinType");
bResult = rangRequestTarget(outputType, targetUrl, paramMap,
jsonParams, joinType, apiInterface, response);
} else {//不存在查询数据库
bResult = rangRequestTarget(apiInterface.getOutputType(),
apiInterface.getTargetUrl(), paramMap, jsonParams, apiInterface.getJoinType(), apiInterface, response);
}
//调用成功请求数据银行扣款
if (AuthModeEnum.POWER_CALL_MODE.name().equals(apiAuth.getAuthMode())) {
notifierMinusMoney(parameter, bResult);
//按次调用时处理(处理为已调用)
authService.updateApiAuthStatus(apiAuth);
}
} catch (Exception ex) {
if (ex instanceof GatewayException) {
......@@ -158,12 +148,11 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
* @param outputType
* @param targetUrl
* @param param
* @param isTest 是否是测试
* @param joinType
* @param response
*/
private boolean rangRequestTarget(String outputType, String targetUrl, Boolean isTest,
Map<String, String> param, JSONObject jsonParams,String joinType,
private boolean rangRequestTarget(String outputType, String targetUrl,
Map<String, String> param, JSONObject jsonParams, String joinType,
ApiInterface apiInterface, OpenApiResponse response) {
if (StringUtils.isBlank(outputType)) {
outputType = ApiInfoOutTypeEnum.JSON.name();
......@@ -174,18 +163,14 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
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,jsonParams, 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);
}
if ("10002".equals(joinType) || "10009".equals(joinType)) {//数据表查询
return dataTableSelect(outputType, targetUrl, param, jsonParams, 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(targetUrl,jsonParams,response);
} else {
return dataTableSelect(outputType, targetUrl, param,jsonParams, apiInterface, response);
throw new GatewayException(GatewayResultCode.API_TYPE_ERROR);
}
}
......@@ -207,22 +192,22 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
/**
* 数据包下载 $$ 数据银行+DMP
*
* @param targetUrl
* @param param
* @return
*/
private boolean dataBagDownload(Map<String, String> param) {
String fileLocation = param.get("fileLocation");
String datasourceId = param.get("datasourceId");
private boolean dataBagDownload(String targetUrl,JSONObject param,OpenApiResponse response) {
String fileLocation = param.getString("file_location");
String datasourceId = param.getString("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());
JSONObject requestParams = new JSONObject();
requestParams.put("file_location",fileLocation);
requestParams.put("datasourceId",datasourceId);
HttpDownload.postDownload(targetUrl,requestParams);
response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg());
return true;
}
......@@ -236,12 +221,13 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
* @param response
* @return
*/
private boolean dataTableSelect(String outputType, String targetUrl, Map<String, String> param,JSONObject jsonParams, ApiInterface apiInterface, OpenApiResponse response) {
protected boolean dataTableSelect(String outputType, String targetUrl, Map<String, String> param,
JSONObject jsonParams, ApiInterface apiInterface, OpenApiResponse response) {
ApiInterfaceCustom apiCustomInfo = checkParamLegal(param, apiInterface);
if (ApiInfoOutTypeEnum.FLOW.name().equals(outputType)) {//文件流形式请求
return flowRequestMethod(targetUrl, param, response, apiCustomInfo);
} else if (ApiInfoOutTypeEnum.JSON.name().equals(outputType)) { //json格式请求
return jsonRequestMethod(targetUrl, param,jsonParams, response, apiCustomInfo);
return jsonRequestMethod(targetUrl, param, jsonParams, response, apiCustomInfo);
} else {
throw new GatewayException(GatewayResultCode.OUTPUT_TYPE_EXCEPTION);
}
......@@ -278,7 +264,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
}
/**
* 数据查询---流请求方式
* 数据查询---flow流请求方式
*
* @param targetUrl
* @param param
......@@ -292,26 +278,25 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
if (StringUtils.isNotBlank(dataSize)) {
valueOf = Integer.valueOf(dataSize);
}
StringBuilder builder = new StringBuilder();
String request$Filed =null;
net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param);
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 value = entry.getValue();
builder.append(key).append(LoggingConstants.AND_EQUAL)
.append(value).append(LoggingConstants.AND_SPILT);
}*/
HttpDownload.download(builder.toString());
try {
request$Filed = URLEncoder.encode(reqParams.toString(), "UTF-8");
} catch (UnsupportedEncodingException e) {
log.error("流下载入参编码异常-------",e.getMessage());
}
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",request$Filed);
requestParams.put("response_fields",assembleResponseParams(apiCustomInfo.getResponseParam()));
requestParams.put("data_size",valueOf);
HttpDownload.postDownload(targetUrl,requestParams);
response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg());
return true;
}
/**
* 数据查询--json请求方式
*
......@@ -321,7 +306,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
* @param apiCustomInfo
* @return
*/
private boolean jsonRequestMethod(String targetUrl, Map<String, String> param,JSONObject jsonParams,
private boolean jsonRequestMethod(String targetUrl, Map<String, String> param, JSONObject jsonParams,
OpenApiResponse response, ApiInterfaceCustom apiCustomInfo) {
JSONObject params = new JSONObject();
try {
......@@ -331,6 +316,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param);
params.put("request_fileds", reqParams);//请求参数
params.put("response_fields", assembleResponseParams(apiCustomInfo.getResponseParam()));//响应参数
params.put("is_test",jsonParams.get("is_test"));//是否是测试
Integer pageNum = jsonParams.getInteger("page_num");
Integer pageSize = jsonParams.getInteger("page_size");
params.put("page_size", apiCustomInfo.getPageSize());
......@@ -342,7 +328,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
params.put("page_size", pageSize);
}
} catch (Exception ex) {
log.error("数据转换异常:{}",ex.getMessage());
log.error("数据转换异常:{}", ex.getMessage());
ex.printStackTrace();
}
String respResult = httpsUtils.submitPost(targetUrl, params.toString());
......
package com.jz.dm.service.request;
import com.alibaba.fastjson.JSONObject;
import com.jz.common.utils.RedisUtils;
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.OpenApiRequest;
import com.jz.dm.common.util.OpenApiResponse;
import com.jz.dm.gateway.OpenApiService;
import com.jz.dm.models.domian.ApiInterface;
import com.jz.dm.service.ApiInterfaceService;
import com.jz.dm.web.annotation.AccessLimit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Map;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.service.request
* @PROJECT_NAME: jz-dm-parent
* @NAME: ApiQueryTestService
* @DATE: 2021-1-25/10:13
* @DAY_NAME_SHORT: 周一
* @Description:
**/
@Service("apiQueryTestService")
@Slf4j
public class ApiQueryTestService extends ApiParamVerify implements OpenApiService {
@Override
public String getOpenApiMethod() {
return "test";
}
@Override
public String getOpenApiVersion() {
return "v1.0.0";
}
@Autowired
private ApiQueryService apiQueryService;
@Resource
private ApiInterfaceService apiInterfaceService;
@Autowired
private RedisUtils redisUtils;
/**
* API测试实现
* @param request
* @param response
*/
@Override
@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
@AccessLimit(sec = 1,limit = 1000)
public void doService(OpenApiRequest request, OpenApiResponse response) {
JSONObject parameter = JSONObject.parseObject(request.getOpenApiParams());
ApiInterface apiInterface = apiInterfaceService.getApiInfo(request.getApiKey());
verifyApiInterface(apiInterface);
String reqParams = parameter.getString("reqParams");
JSONObject jsonParams = null;
Map paramMap = null;
if (StringUtils.isNotBlank(reqParams)) {
jsonParams = JSONObject.parseObject(reqParams);
paramMap = JSONObject.parseObject(jsonParams.getString("request_fileds"), Map.class);
jsonParams.put("is_test",true);
}
//取出缓存数据
// String redisReqParam = redisUtils.get(request.getApiKey());
String redisReqParam = null;
if (StringUtils.isNotBlank(redisReqParam)) {//redis中存在
//解析出API制作成功时的参数配置
JSONObject jsonObject = JSONObject.parseObject(redisReqParam);
String targetUrl = jsonObject.getString("targetUrl");
String outputType = jsonObject.getString("outputType");
if (StringUtils.isBlank(outputType)) {
outputType = ApiInfoOutTypeEnum.JSON.name();
}
if (StringUtils.isBlank(targetUrl)) {
throw new GatewayException(GatewayResultCode.REQUEST_PARAM_EMPTY);
}
apiQueryService.dataTableSelect(outputType, targetUrl, paramMap,
jsonParams, apiInterface, response);
} else {//不存在查询数据库
apiQueryService.dataTableSelect(apiInterface.getOutputType(),
apiInterface.getTargetUrl(), paramMap, jsonParams, apiInterface, response);
}
}
}
......@@ -107,8 +107,6 @@ logging:
zhl:
springbootlogback: off
#数据包访问链接
dataPackage:
downloadUrl: http://xxx.com
......@@ -27,15 +27,14 @@ public class ApiReqTest extends SpringTestCase {
public void TestGatewayReq() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("apiKey", "sE862E97j7Yzo049");
jsonObject.put("apiKey", "8trDpp4WRl92850o");
jsonObject.put("method", "query");
jsonObject.put("signType", "MD5");
long time = System.currentTimeMillis();
jsonObject.put("timestamp", String.valueOf(time));
JSONObject params = new JSONObject();
params.put("authCode", "202100000001118191258T718d78591J");
params.put("selectType", "10006");
params.put("authCode", "2021000000011118104856J1QR4u9Afm");
params.put("reqParams", new JSONObject());
jsonObject.put("params",params);
try {
......@@ -44,7 +43,7 @@ public class ApiReqTest extends SpringTestCase {
String signType = jsonObject.getString("signType");
String signature = MapUtil.getSignValue(apiKey, method, signType);
String salt = Md5.encrypt(signature, "33tgT3g2");
String salt = Md5.encrypt(signature, "7330lQl2");
jsonObject.put("sign", salt);
String response = httpsUtils.submitPost(url, jsonObject.toString());
System.out.println(response);
......
package com.jz.dm.gateway.orther;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
......@@ -12,6 +15,20 @@ import java.util.stream.Collectors;
*/
public class TestStr {
public static void main(String[] args) {
String str ="[{\"name\":\"flelds1\",\"bindName\":\"flelds1\",\"type\":\"BIGINT\",\"sampleValue\":\"1\",\"defaultValue\":\"1\",\"required\":true,\"desc\":\"\"},{\"name\":\"flelds2\",\"bindName\":\"flelds2\",\"type\":\"BIGINT\",\"sampleValue\":\"1\",\"defaultValue\":\"1\",\"required\":true,\"desc\":\"\"}]";
String str2="[{\"name\":\"field1\",\"bindName\":\"field1\",\"type\":\"STRING\",\"sampleValue\":\"1\",\"desc\":\"\"},{\"name\":\"field2\",\"bindName\":\"field2\",\"type\":\"BIGINT\",\"sampleValue\":\"1\",\"desc\":\"\"},{\"name\":\"field3\",\"bindName\":\"field3\",\"type\":\"BIGINT\",\"sampleValue\":\"1\",\"desc\":\"\"},{\"name\":\"field4\",\"bindName\":\"field4\",\"type\":\"STRING\",\"sampleValue\":\"1\",\"desc\":\"\"}]";
List<Map> maps = JSONObject.parseArray(str2, Map.class);
for (Map map : maps) {
System.out.println("map:"+map);
}
}
/**
* 获取集合信息
*/
public static void getList() {
List<String> list1 = new ArrayList<String>();
list1.add("1");
list1.add("2");
......@@ -31,23 +48,23 @@ public class TestStr {
// 交集
List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList());
System.out.println("---交集 intersection---");
intersection.parallelStream().forEach(System.out :: println);
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);
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);
reduce2.parallelStream().forEach(System.out::println);
//合拼差
reduce1.addAll(reduce2);
System.out.println("---合拼差 reduce1 (list2 -> list1)---");
reduce1.parallelStream().forEach(System.out :: println);
reduce1.parallelStream().forEach(System.out::println);
// 并集
......@@ -55,19 +72,18 @@ public class TestStr {
List<String> listAll2 = list2.parallelStream().collect(Collectors.toList());
listAll.addAll(listAll2);
System.out.println("---并集 listAll---");
listAll.parallelStream().forEachOrdered(System.out :: println);
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);
listAllDistinct.parallelStream().forEachOrdered(System.out::println);
System.out.println("---原来的List1---");
list1.parallelStream().forEachOrdered(System.out :: println);
list1.parallelStream().forEachOrdered(System.out::println);
System.out.println("---原来的List2---");
list2.parallelStream().forEachOrdered(System.out :: println);
}
list2.parallelStream().forEachOrdered(System.out::println);
}
}
......@@ -3,7 +3,7 @@ package com.jz.dm.gateway.orther;
import com.jz.dm.gateway.SpringTestCase;
import lombok.extern.slf4j.Slf4j;
import static com.jz.dm.common.util.stream.HttpDownload.download;
import static com.jz.dm.common.util.stream.HttpDownload.getDownload;
/**
* @author ZC
......@@ -21,6 +21,6 @@ public class TestStreamReq extends SpringTestCase {
//@Test
public void testStreamReq() {
download(baseUrl);
getDownload(baseUrl);
}
}
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