Commit 78fd3f92 authored by zhangc's avatar zhangc

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

parent 77e8de6b
package com.jz.dm.common.util.stream; package com.jz.dm.common.util.stream;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.*; import org.apache.http.*;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; 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 org.apache.http.impl.client.CloseableHttpClient;
import java.io.File; import java.io.File;
...@@ -14,6 +19,7 @@ import java.text.MessageFormat; ...@@ -14,6 +19,7 @@ import java.text.MessageFormat;
import static com.jz.common.utils.HttpClientPool.getHttpClient; import static com.jz.common.utils.HttpClientPool.getHttpClient;
/** /**
* @author ZC * @author ZC
* @PACKAGE_NAME: com.sentinel.project.util * @PACKAGE_NAME: com.sentinel.project.util
...@@ -48,8 +54,90 @@ public class HttpDownload { ...@@ -48,8 +54,90 @@ public class HttpDownload {
* @param url * @param url
* @return * @return
*/ */
public static void download(String url) { public static void getDownload(String url) {
download(url, null); 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 { ...@@ -81,8 +169,8 @@ public class HttpDownload {
} }
if (filepath != null) { if (filepath != null) {
fileName = filepath + splash + fileName; fileName = filepath + splash + fileName;
}else { } else {
fileName= splash + fileName; fileName = splash + fileName;
} }
File file = new File(fileName); File file = new File(fileName);
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
...@@ -185,8 +273,22 @@ public class HttpDownload { ...@@ -185,8 +273,22 @@ public class HttpDownload {
String docx = "http://192.168.1.140:8090/api/download/docx"; String docx = "http://192.168.1.140:8090/api/download/docx";
String pdf = "http://192.168.1.140:8090/api/download/pdf"; String pdf = "http://192.168.1.140:8090/api/download/pdf";
String xlsx = "http://192.168.1.140:8090/api/download/xlsx"; 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"; postDownloadFolder(postDownload,jsonObject,null);
HttpDownload.download(xlsx, filepath);
} }
} }
...@@ -12,6 +12,7 @@ import io.swagger.annotations.Api; ...@@ -12,6 +12,7 @@ import io.swagger.annotations.Api;
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.util.CollectionUtils; 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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -32,7 +33,8 @@ public class GatewayController { ...@@ -32,7 +33,8 @@ public class GatewayController {
@Autowired @Autowired
private GatewayService gatewayService; 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, public String gateway(@RequestBody String json, HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) { HttpServletResponse httpServletResponse) {
GatewayRequest gatewayRequest = JSON.parseObject(json, GatewayRequest.class); GatewayRequest gatewayRequest = JSON.parseObject(json, GatewayRequest.class);
......
...@@ -126,7 +126,6 @@ public class AuthFilter extends AbstractFilter { ...@@ -126,7 +126,6 @@ public class AuthFilter extends AbstractFilter {
response.clearAttributes(); response.clearAttributes();
response.setCode(GatewayResultCode.UNKNOWN_EXCEPTION.getCode()); response.setCode(GatewayResultCode.UNKNOWN_EXCEPTION.getCode());
response.setMsg(GatewayResultCode.UNKNOWN_EXCEPTION.getMsg()); response.setMsg(GatewayResultCode.UNKNOWN_EXCEPTION.getMsg());
} }
} }
......
package com.jz.dm.filter; package com.jz.dm.filter;
import com.jz.dm.common.enums.GatewayResultCode;
import com.jz.dm.common.exception.OpenApiException; import com.jz.dm.common.exception.OpenApiException;
import com.jz.dm.common.util.LogUtil; import com.jz.dm.common.util.LogUtil;
import com.jz.dm.common.util.ResultCode; import com.jz.dm.common.util.ResultCode;
import com.jz.dm.models.enity.GatewayRequest; import com.jz.dm.models.enity.GatewayRequest;
import com.jz.dm.models.enity.GatewayResponse; import com.jz.dm.models.enity.GatewayResponse;
import com.jz.dm.common.enums.GatewayResultCode;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -19,13 +19,14 @@ import java.util.List; ...@@ -19,13 +19,14 @@ import java.util.List;
*/ */
public class FilterChainImpl implements FilterChain { 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>(); private final List<Filter> filters = new ArrayList<Filter>();
public FilterChainImpl() { } public FilterChainImpl() {
}
public FilterChainImpl(List<Filter> filters) { public FilterChainImpl(List<Filter> filters) {
addFilters(filters); addFilters(filters);
...@@ -42,7 +43,7 @@ public class FilterChainImpl implements FilterChain { ...@@ -42,7 +43,7 @@ public class FilterChainImpl implements FilterChain {
} catch (OpenApiException ex) { } catch (OpenApiException ex) {
ResultCode resultCode = ex.getResultCode(); ResultCode resultCode = ex.getResultCode();
LogUtil.error(LOGGER, ex, "doFilter occur exception,code=" + resultCode.getCode() 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.setCode(ex.getResultCode().getCode());
response.setMsg(ex.getResultCode().getMsg()); response.setMsg(ex.getResultCode().getMsg());
} catch (Throwable ex) { } catch (Throwable ex) {
......
...@@ -19,15 +19,16 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -19,15 +19,16 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
* @Description: 验签过滤器(验证签名信息) * @Description: 验签过滤器(验证签名信息)
* @Author: Mr.zhang * @Author: Mr.zhang
* @Date: 2021-1-6 * @Date: 2021-1-6
*/ */
@Component @Component
public class VerifySignFilter extends AbstractFilter { public class VerifySignFilter extends AbstractFilter {
private static final Logger LOGGER = LoggerFactory.getLogger(VerifySignFilter.class); private static final Logger LOGGER = LoggerFactory.getLogger(VerifySignFilter.class);
@Autowired @Autowired
private ApiInterfaceService apiInterfaceService; private ApiInterfaceService apiInterfaceService;
@Override @Override
public int getOrder() { public int getOrder() {
return Constants.FILTER_ORDER_3; return Constants.FILTER_ORDER_3;
...@@ -37,27 +38,27 @@ public class VerifySignFilter extends AbstractFilter { ...@@ -37,27 +38,27 @@ public class VerifySignFilter extends AbstractFilter {
public String getFilterName() { public String getFilterName() {
return "VerifySignFilter"; return "VerifySignFilter";
} }
@Override @Override
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()); JSONObject jsonObject = JSONObject.parseObject(request.getParams());
if (!jsonObject.getBoolean("isTest")){//是否测试调用 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)) {
String authCode = jsonObject.getString("authCode"); String authCode = jsonObject.getString("authCode");
//需要传入授权码 //需要传入授权码
ApiAuth apiAuthInfo = apiInterfaceService.getApiAuthInfo(request.getApiKey(),authCode ); ApiAuth apiAuthInfo = apiInterfaceService.getApiAuthInfo(request.getApiKey(), authCode);
if (null == apiAuthInfo){ if (null == apiAuthInfo) {
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST); throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST);
} }
String sign = Md5.encrypt(signParams, apiAuthInfo.getSalt()); String sign = Md5.encrypt(signParams, apiAuthInfo.getSalt());
if (!request.getSign().equals(sign)){ if (!request.getSign().equals(sign)) {
throw new GatewayException(GatewayResultCode.SIGN_ERROR); throw new GatewayException(GatewayResultCode.SIGN_ERROR);
} }
}else { } else {
throw new GatewayException(GatewayResultCode.SIGN_ERROR); throw new GatewayException(GatewayResultCode.SIGN_ERROR);
} }
} }
...@@ -69,8 +70,8 @@ public class VerifySignFilter extends AbstractFilter { ...@@ -69,8 +70,8 @@ public class VerifySignFilter extends AbstractFilter {
response.setCode(ex.getResultCode().getCode()); response.setCode(ex.getResultCode().getCode());
response.setMsg(ex.getResultCode().getMsg()); response.setMsg(ex.getResultCode().getMsg());
} catch (Throwable ex) { } catch (Throwable ex) {
if(ex instanceof GatewayException){ if (ex instanceof GatewayException) {
throw(GatewayException) ex; throw (GatewayException) ex;
} }
LogUtil.error(LOGGER, ex, LogUtil.error(LOGGER, ex,
"signatureFilter doFilter error. response=" + response.getResponse()); "signatureFilter doFilter error. response=" + response.getResponse());
......
package com.jz.dm.gateway; package com.jz.dm.gateway;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.jz.dm.common.exception.GatewayException;
import com.jz.dm.common.exception.OpenApiException; import com.jz.dm.common.exception.OpenApiException;
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;
...@@ -65,18 +66,17 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher { ...@@ -65,18 +66,17 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher {
response.setMsg(OpenApiResultCode.ILLEGAL_INTERFACE.getMsg()); response.setMsg(OpenApiResultCode.ILLEGAL_INTERFACE.getMsg());
return JSON.toJSONString(response.getAttributes()); return JSON.toJSONString(response.getAttributes());
} }
try { try {
OpenApiRequest request = new OpenApiRequest(context.getOpenApiParams()); OpenApiRequest request = new OpenApiRequest(context.getOpenApiParams());
request.setApiKey(context.getApiKey()); request.setApiKey(context.getApiKey());
request.setExtAttributes(context.getExtAttributes()); request.setExtAttributes(context.getExtAttributes());
openApiService.doService(request, response); openApiService.doService(request, response);
} catch (Throwable ex) { }catch (Throwable ex) {
if (ex instanceof OpenApiException) { if (ex instanceof OpenApiException) {
OpenApiException oae = (OpenApiException) ex; throw(OpenApiException) ex;
response.setCode(oae.getResultCode().getCode()); } else if (ex instanceof GatewayException){
response.setMsg(oae.getResultCode().getMsg()); throw(GatewayException) ex;
} else { }else{
LOGGER.error("OpenApiService doService error,DispatchContext=" + context, ex); LOGGER.error("OpenApiService doService error,DispatchContext=" + context, ex);
response.setCode(OpenApiResultCode.UNKNOWN_EXCEPTION.getCode()); response.setCode(OpenApiResultCode.UNKNOWN_EXCEPTION.getCode());
response.setMsg(OpenApiResultCode.UNKNOWN_EXCEPTION.getMsg()); response.setMsg(OpenApiResultCode.UNKNOWN_EXCEPTION.getMsg());
...@@ -84,8 +84,8 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher { ...@@ -84,8 +84,8 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher {
return JSON.toJSONString(response.getAttributes()); return JSON.toJSONString(response.getAttributes());
} }
if (StringUtil.isEmpty(response.getCode())) { if (StringUtil.isEmpty(response.getCode())) {
response.setCode(OpenApiResultCode.SUCCESS.getCode()); response.setCode(OpenApiResultCode.UNKNOWN_EXCEPTION.getCode());
response.setMsg(OpenApiResultCode.SUCCESS.getMsg()); response.setMsg(OpenApiResultCode.UNKNOWN_EXCEPTION.getMsg());
} }
return JSON.toJSONString(response.getAttributes()); return JSON.toJSONString(response.getAttributes());
} }
......
...@@ -49,6 +49,10 @@ public class GatewayRequest implements Serializable { ...@@ -49,6 +49,10 @@ public class GatewayRequest implements Serializable {
@ApiModelProperty(value="请求参数,JSON格式") @ApiModelProperty(value="请求参数,JSON格式")
private String params; private String params;
@ApiModelProperty(value="是否测试请求")
private Boolean isTest;
@ApiModelProperty(value="扩展属性") @ApiModelProperty(value="扩展属性")
private final Map<String, Attribute> extAttributes = new HashMap<String, Attribute>(); private final Map<String, Attribute> extAttributes = new HashMap<String, Attribute>();
......
...@@ -29,6 +29,8 @@ import org.springframework.stereotype.Service; ...@@ -29,6 +29,8 @@ 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.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -70,12 +72,6 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -70,12 +72,6 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
@Value("${data.bank.balanceUrl}") @Value("${data.bank.balanceUrl}")
private String balanceUrl; private String balanceUrl;
/**
* 数据包下载路径
*/
@Value("${dataPackage.downloadUrl}")
private String downloadUrl;
/** /**
* API请求逻辑处理 * API请求逻辑处理
...@@ -93,48 +89,42 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -93,48 +89,42 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
JSONObject parameter = JSONObject.parseObject(request.getOpenApiParams()); JSONObject parameter = JSONObject.parseObject(request.getOpenApiParams());
ApiInterface apiInterface = apiInterfaceService.getApiInfo(request.getApiKey()); ApiInterface apiInterface = apiInterfaceService.getApiInfo(request.getApiKey());
ApiAuth apiAuth = null; ApiAuth apiAuth = null;
JSONObject jsonParams =null; JSONObject jsonParams = null;
try { try {
Boolean isTest = parameter.getBoolean("isTest");
String reqParams = parameter.getString("reqParams"); String reqParams = parameter.getString("reqParams");
parameter.put("is_test", isTest);
Map paramMap = null; Map paramMap = null;
if (StringUtils.isNotBlank(reqParams)) { if (StringUtils.isNotBlank(reqParams)) {
jsonParams = JSONObject.parseObject(reqParams); jsonParams = JSONObject.parseObject(reqParams);
paramMap = JSONObject.parseObject(jsonParams.getString("request_fileds"), Map.class); paramMap = JSONObject.parseObject(jsonParams.getString("request_fileds"), Map.class);
jsonParams.put("is_test",false);
} }
if (!isTest) {//如果不是测试 String authCode = parameter.getString("authCode");
String authCode = parameter.getString("authCode"); if (StringUtils.isBlank(authCode)) {
if (StringUtils.isBlank(authCode)) { throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST);
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST); }
} verifyApiInterface(apiInterface);
verifyApiInterface(apiInterface); apiAuth = authService.getAuthInfo(authCode);
apiAuth = authService.getAuthInfo(authCode); verifyAuth(apiAuth);
verifyAuth(apiAuth); //取出缓存数据
//取出缓存数据 /* String redisReqParam = redisUtils.get(request.getApiKey());*/
/* String redisReqParam = redisUtils.get(request.getApiKey());*/ String redisReqParam = null;
String redisReqParam = null; 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"); String joinType = jsonObject.getString("joinType");
String joinType = jsonObject.getString("joinType"); bResult = rangRequestTarget(outputType, targetUrl, paramMap,
bResult = rangRequestTarget(outputType, targetUrl, false, paramMap, jsonParams, joinType, apiInterface, response);
jsonParams, joinType, apiInterface, response); } else {//不存在查询数据库
} else {//不存在查询数据库 bResult = rangRequestTarget(apiInterface.getOutputType(),
bResult = rangRequestTarget(apiInterface.getOutputType(), apiInterface.getTargetUrl(), paramMap, jsonParams, apiInterface.getJoinType(), apiInterface, response);
apiInterface.getTargetUrl(), false, paramMap,jsonParams, apiInterface.getJoinType(), apiInterface, response); }
} //调用成功请求数据银行扣款
//调用成功请求数据银行扣款 if (AuthModeEnum.POWER_CALL_MODE.name().equals(apiAuth.getAuthMode())) {
if (AuthModeEnum.POWER_CALL_MODE.name().equals(apiAuth.getAuthMode())) { notifierMinusMoney(parameter, bResult);
notifierMinusMoney(parameter, bResult); //按次调用时处理(处理为已调用)
//按次调用时处理(处理为已调用) authService.updateApiAuthStatus(apiAuth);
authService.updateApiAuthStatus(apiAuth);
}
} else {
bResult = rangRequestTarget(ApiInfoOutTypeEnum.JSON.name(), apiInterface.getTargetUrl(),
true, paramMap,jsonParams,apiInterface.getJoinType(), apiInterface, response);
} }
} catch (Exception ex) { } catch (Exception ex) {
if (ex instanceof GatewayException) { if (ex instanceof GatewayException) {
...@@ -158,12 +148,11 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -158,12 +148,11 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
* @param outputType * @param outputType
* @param targetUrl * @param targetUrl
* @param param * @param param
* @param isTest 是否是测试
* @param joinType * @param joinType
* @param response * @param response
*/ */
private boolean rangRequestTarget(String outputType, String targetUrl, Boolean isTest, private boolean rangRequestTarget(String outputType, String targetUrl,
Map<String, String> param, JSONObject jsonParams,String joinType, Map<String, String> param, JSONObject jsonParams, String joinType,
ApiInterface apiInterface, OpenApiResponse response) { ApiInterface apiInterface, OpenApiResponse response) {
if (StringUtils.isBlank(outputType)) { if (StringUtils.isBlank(outputType)) {
outputType = ApiInfoOutTypeEnum.JSON.name(); outputType = ApiInfoOutTypeEnum.JSON.name();
...@@ -174,18 +163,14 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -174,18 +163,14 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
if (StringUtils.isBlank(joinType)) { if (StringUtils.isBlank(joinType)) {
throw new GatewayException(GatewayResultCode.API_TYPE_ERROR); throw new GatewayException(GatewayResultCode.API_TYPE_ERROR);
} }
if (!isTest) { if ("10002".equals(joinType) || "10009".equals(joinType)) {//数据表查询
if ("10002".equals(joinType) || "10009".equals(joinType)) {//数据表查询 return dataTableSelect(outputType, targetUrl, param, jsonParams, apiInterface, response);
return dataTableSelect(outputType, targetUrl, param,jsonParams, apiInterface, response); } else if ("10004".equals(joinType) || "10008".equals(joinType)) {//三方查询
} else if ("10004".equals(joinType) || "10008".equals(joinType)) {//三方查询 return thirdSelect(targetUrl, apiInterface, response);
return thirdSelect(targetUrl, apiInterface, response); } else if ("10005".equals(joinType) || "10007".equals(joinType)) {//数据包查询
} else if ("10005".equals(joinType) || "10007".equals(joinType)) {//数据包查询 return dataBagDownload(targetUrl,jsonParams,response);
return dataBagDownload(param);
} else {
throw new GatewayException(GatewayResultCode.API_TYPE_ERROR);
}
} else { } 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 { ...@@ -207,22 +192,22 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
/** /**
* 数据包下载 $$ 数据银行+DMP * 数据包下载 $$ 数据银行+DMP
* * @param targetUrl
* @param param * @param param
* @return * @return
*/ */
private boolean dataBagDownload(Map<String, String> param) { private boolean dataBagDownload(String targetUrl,JSONObject param,OpenApiResponse response) {
String fileLocation = param.get("fileLocation"); String fileLocation = param.getString("file_location");
String datasourceId = param.get("datasourceId"); String datasourceId = param.getString("datasourceId");
if (null == fileLocation || null == datasourceId) { if (null == fileLocation || null == datasourceId) {
throw new GatewayException(GatewayResultCode.DATA_BIG_ADDR_UNEXIST); throw new GatewayException(GatewayResultCode.DATA_BIG_ADDR_UNEXIST);
} }
StringBuilder builder = new StringBuilder(); JSONObject requestParams = new JSONObject();
builder.append("?").append("file_location=") requestParams.put("file_location",fileLocation);
.append(fileLocation).append("&") requestParams.put("datasourceId",datasourceId);
.append("datasourceId=") HttpDownload.postDownload(targetUrl,requestParams);
.append(datasourceId); response.setCode(GatewayResultCode.SUCCESS.getCode());
HttpDownload.download(downloadUrl + builder.toString()); response.setMsg(GatewayResultCode.SUCCESS.getMsg());
return true; return true;
} }
...@@ -236,12 +221,13 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -236,12 +221,13 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
* @param response * @param response
* @return * @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); ApiInterfaceCustom apiCustomInfo = checkParamLegal(param, apiInterface);
if (ApiInfoOutTypeEnum.FLOW.name().equals(outputType)) {//文件流形式请求 if (ApiInfoOutTypeEnum.FLOW.name().equals(outputType)) {//文件流形式请求
return flowRequestMethod(targetUrl, param, response, apiCustomInfo); return flowRequestMethod(targetUrl, param, response, apiCustomInfo);
} else if (ApiInfoOutTypeEnum.JSON.name().equals(outputType)) { //json格式请求 } else if (ApiInfoOutTypeEnum.JSON.name().equals(outputType)) { //json格式请求
return jsonRequestMethod(targetUrl, param,jsonParams, response, apiCustomInfo); return jsonRequestMethod(targetUrl, param, jsonParams, response, apiCustomInfo);
} else { } else {
throw new GatewayException(GatewayResultCode.OUTPUT_TYPE_EXCEPTION); throw new GatewayException(GatewayResultCode.OUTPUT_TYPE_EXCEPTION);
} }
...@@ -278,7 +264,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -278,7 +264,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
} }
/** /**
* 数据查询---流请求方式 * 数据查询---flow流请求方式
* *
* @param targetUrl * @param targetUrl
* @param param * @param param
...@@ -292,26 +278,25 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -292,26 +278,25 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
if (StringUtils.isNotBlank(dataSize)) { if (StringUtils.isNotBlank(dataSize)) {
valueOf = Integer.valueOf(dataSize); valueOf = Integer.valueOf(dataSize);
} }
StringBuilder builder = new StringBuilder(); String request$Filed =null;
net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param); net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param);
builder.append(targetUrl).append("?") try {
.append("datasourceId=").append(apiCustomInfo.getEsDataSource()) request$Filed = URLEncoder.encode(reqParams.toString(), "UTF-8");
.append("query_database=").append(apiCustomInfo.getEsDataBase()) } catch (UnsupportedEncodingException e) {
.append("request_fileds=").append(reqParams).append("response_fields=") log.error("流下载入参编码异常-------",e.getMessage());
.append(assembleResponseParams(apiCustomInfo.getResponseParam())) }
.append(valueOf); JSONObject requestParams = new JSONObject();
/* for (Map.Entry<String, String> entry : param.entrySet()) { requestParams.put("datasourceId",apiCustomInfo.getEsDataSource());
String key = entry.getKey(); requestParams.put("query_database",apiCustomInfo.getEsDataBase());
String value = entry.getValue(); requestParams.put("query_table",apiCustomInfo.getEsTable());
builder.append(key).append(LoggingConstants.AND_EQUAL) requestParams.put("request_fileds",request$Filed);
.append(value).append(LoggingConstants.AND_SPILT); requestParams.put("response_fields",assembleResponseParams(apiCustomInfo.getResponseParam()));
}*/ requestParams.put("data_size",valueOf);
HttpDownload.download(builder.toString()); HttpDownload.postDownload(targetUrl,requestParams);
response.setCode(GatewayResultCode.SUCCESS.getCode()); response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg()); response.setMsg(GatewayResultCode.SUCCESS.getMsg());
return true; return true;
} }
/** /**
* 数据查询--json请求方式 * 数据查询--json请求方式
* *
...@@ -321,7 +306,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -321,7 +306,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
* @param apiCustomInfo * @param apiCustomInfo
* @return * @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) { OpenApiResponse response, ApiInterfaceCustom apiCustomInfo) {
JSONObject params = new JSONObject(); JSONObject params = new JSONObject();
try { try {
...@@ -331,6 +316,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -331,6 +316,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param); net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param);
params.put("request_fileds", reqParams);//请求参数 params.put("request_fileds", reqParams);//请求参数
params.put("response_fields", assembleResponseParams(apiCustomInfo.getResponseParam()));//响应参数 params.put("response_fields", assembleResponseParams(apiCustomInfo.getResponseParam()));//响应参数
params.put("is_test",jsonParams.get("is_test"));//是否是测试
Integer pageNum = jsonParams.getInteger("page_num"); Integer pageNum = jsonParams.getInteger("page_num");
Integer pageSize = jsonParams.getInteger("page_size"); Integer pageSize = jsonParams.getInteger("page_size");
params.put("page_size", apiCustomInfo.getPageSize()); params.put("page_size", apiCustomInfo.getPageSize());
...@@ -342,7 +328,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -342,7 +328,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
params.put("page_size", pageSize); params.put("page_size", pageSize);
} }
} catch (Exception ex) { } catch (Exception ex) {
log.error("数据转换异常:{}",ex.getMessage()); log.error("数据转换异常:{}", ex.getMessage());
ex.printStackTrace(); ex.printStackTrace();
} }
String respResult = httpsUtils.submitPost(targetUrl, params.toString()); 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: ...@@ -107,8 +107,6 @@ logging:
zhl: zhl:
springbootlogback: off springbootlogback: off
#数据包访问链接
dataPackage:
downloadUrl: http://xxx.com
...@@ -27,15 +27,14 @@ public class ApiReqTest extends SpringTestCase { ...@@ -27,15 +27,14 @@ public class ApiReqTest extends SpringTestCase {
public void TestGatewayReq() { public void TestGatewayReq() {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put("apiKey", "sE862E97j7Yzo049"); jsonObject.put("apiKey", "8trDpp4WRl92850o");
jsonObject.put("method", "query"); jsonObject.put("method", "query");
jsonObject.put("signType", "MD5"); jsonObject.put("signType", "MD5");
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
jsonObject.put("timestamp", String.valueOf(time)); jsonObject.put("timestamp", String.valueOf(time));
JSONObject params = new JSONObject(); JSONObject params = new JSONObject();
params.put("authCode", "202100000001118191258T718d78591J"); params.put("authCode", "2021000000011118104856J1QR4u9Afm");
params.put("selectType", "10006");
params.put("reqParams", new JSONObject()); params.put("reqParams", new JSONObject());
jsonObject.put("params",params); jsonObject.put("params",params);
try { try {
...@@ -44,7 +43,7 @@ public class ApiReqTest extends SpringTestCase { ...@@ -44,7 +43,7 @@ public class ApiReqTest extends SpringTestCase {
String signType = jsonObject.getString("signType"); String signType = jsonObject.getString("signType");
String signature = MapUtil.getSignValue(apiKey, method, signType); String signature = MapUtil.getSignValue(apiKey, method, signType);
String salt = Md5.encrypt(signature, "33tgT3g2"); String salt = Md5.encrypt(signature, "7330lQl2");
jsonObject.put("sign", salt); jsonObject.put("sign", salt);
String response = httpsUtils.submitPost(url, jsonObject.toString()); String response = httpsUtils.submitPost(url, jsonObject.toString());
System.out.println(response); System.out.println(response);
......
package com.jz.dm.gateway.orther; package com.jz.dm.gateway.orther;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
...@@ -12,6 +15,20 @@ import java.util.stream.Collectors; ...@@ -12,6 +15,20 @@ import java.util.stream.Collectors;
*/ */
public class TestStr { public class TestStr {
public static void main(String[] args) { 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>(); List<String> list1 = new ArrayList<String>();
list1.add("1"); list1.add("1");
list1.add("2"); list1.add("2");
...@@ -31,23 +48,23 @@ public class TestStr { ...@@ -31,23 +48,23 @@ public class TestStr {
// 交集 // 交集
List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList()); List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList());
System.out.println("---交集 intersection---"); System.out.println("---交集 intersection---");
intersection.parallelStream().forEach(System.out :: println); intersection.parallelStream().forEach(System.out::println);
// 差集 (list1 - list2) // 差集 (list1 - list2)
List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(Collectors.toList()); List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(Collectors.toList());
System.out.println("---差集 reduce1 (list1 - list2)---"); System.out.println("---差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out :: println); reduce1.parallelStream().forEach(System.out::println);
// 差集 (list2 - list1) // 差集 (list2 - list1)
List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(Collectors.toList()); List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(Collectors.toList());
System.out.println("---差集 reduce2 (list2 - list1)---"); System.out.println("---差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out :: println); reduce2.parallelStream().forEach(System.out::println);
//合拼差 //合拼差
reduce1.addAll(reduce2); reduce1.addAll(reduce2);
System.out.println("---合拼差 reduce1 (list2 -> list1)---"); 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 { ...@@ -55,19 +72,18 @@ public class TestStr {
List<String> listAll2 = list2.parallelStream().collect(Collectors.toList()); List<String> listAll2 = list2.parallelStream().collect(Collectors.toList());
listAll.addAll(listAll2); listAll.addAll(listAll2);
System.out.println("---并集 listAll---"); 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()); List<String> listAllDistinct = listAll.stream().distinct().collect(Collectors.toList());
System.out.println("---得到去重并集 listAllDistinct---"); System.out.println("---得到去重并集 listAllDistinct---");
listAllDistinct.parallelStream().forEachOrdered(System.out :: println); listAllDistinct.parallelStream().forEachOrdered(System.out::println);
System.out.println("---原来的List1---"); System.out.println("---原来的List1---");
list1.parallelStream().forEachOrdered(System.out :: println); list1.parallelStream().forEachOrdered(System.out::println);
System.out.println("---原来的List2---"); 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; ...@@ -3,7 +3,7 @@ package com.jz.dm.gateway.orther;
import com.jz.dm.gateway.SpringTestCase; import com.jz.dm.gateway.SpringTestCase;
import lombok.extern.slf4j.Slf4j; 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 * @author ZC
...@@ -21,6 +21,6 @@ public class TestStreamReq extends SpringTestCase { ...@@ -21,6 +21,6 @@ public class TestStreamReq extends SpringTestCase {
//@Test //@Test
public void testStreamReq() { 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