修改请求

This commit is contained in:
lvleigang 2024-10-17 10:29:31 +08:00
parent 3ab107390c
commit 937395744e
3 changed files with 227 additions and 61 deletions

View File

@ -0,0 +1,58 @@
package com.hzya.frame.sysnew.application.entity;
/**
* 应用表(SysApplication)实体类
*
* @author makejava
* @since 2023-09-22 16:22:18
*/
public class U8CReturnEntity {
private String status;
private String data;
private String taskNumber;
private String errorcode;
private String errormsg;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String getTaskNumber() {
return taskNumber;
}
public void setTaskNumber(String taskNumber) {
this.taskNumber = taskNumber;
}
public String getErrorcode() {
return errorcode;
}
public void setErrorcode(String errorcode) {
this.errorcode = errorcode;
}
public String getErrormsg() {
return errormsg;
}
public void setErrormsg(String errormsg) {
this.errormsg = errormsg;
}
}

View File

@ -73,6 +73,7 @@ import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
@ -1688,10 +1689,18 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
@DSTransactional()
public JsonResultEntity externalCallInterface(ServletRequest servletRequest, ServletResponse servletResponse) {
long startTime1 = System.currentTimeMillis();
//例如A应用发送数据到中台中台转发到B应用
HttpServletRequest request = (HttpServletRequest) servletRequest;
logger.info("发送数据1"+(System.currentTimeMillis()-startTime1));
String oldbodys = ServletUtil.getBody(servletRequest);
logger.info("发送数据2"+(System.currentTimeMillis()-startTime1));
Map<String, String> oldheaderMap = ServletUtil.getHeaderMap(request);
logger.info("发送数据3"+(System.currentTimeMillis()-startTime1));
String oldquerys = request.getQueryString();
//应用key
String publicKey = request.getHeader("publicKey");
@ -1701,8 +1710,11 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
String appId = request.getHeader("appId");
//apiCode
String apiCode = request.getHeader("apiCode");
logger.info("发送数据4"+(System.currentTimeMillis()-startTime1));
String ip = IPHelper.getIpAddr(request);
logger.info("发送数据5"+(System.currentTimeMillis()-startTime1));
if (publicKey == null || "".equals(publicKey)) {
return BaseResult.getFailureMessageEntity("请先传递公钥");
}
@ -1718,17 +1730,20 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
logger.info("请求参数publicKey" + publicKey + "】secretKey" + secretKey + "】appId" + appId + "】apiCode" + apiCode);
//根据请求a应用的公钥密钥是否能查找到一条数据
logger.info("发送数据6"+(System.currentTimeMillis()-startTime1));
SysApplicationEntity sendApp = getAppByPublicKeySecretKey(publicKey, secretKey);
if (sendApp == null) {
//saveLog(new SysApplicationEntity(), new SysApplicationEntity(), new SysApplicationApiEntity(), oldbodys, null, oldheaderMap, null, null, oldquerys, null, null, false,"公钥、密钥错误");
return BaseResult.getFailureMessageEntity("公钥、密钥错误,请联系管理员");
}
//判断应用是否启用
if (sendApp.getAppStatus() == null || !"1".equals(sendApp.getAppStatus())) {
saveLog(sendApp, new SysApplicationEntity(), new SysApplicationApiEntity(), oldbodys, null, oldheaderMap, null, null, oldquerys, null, null, false,sendApp.getName() + "应用未启用");
return BaseResult.getFailureMessageEntity(sendApp.getName() + "应用未启用,请联系管理员");
}
logger.info("发送数据7"+(System.currentTimeMillis()-startTime1));
SysApplicationEntity receiveApp = getAppByAppId(appId);
@ -1746,12 +1761,14 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
saveLog(sendApp, receiveApp, new SysApplicationApiEntity(), oldbodys, null, oldheaderMap, null, null, oldquerys, null, null, false,receiveApp.getName() + "应用接口环境未启用" );
return BaseResult.getFailureMessageEntity(receiveApp.getName() + "应用接口环境未启用,请联系管理员");
}
logger.info("发送数据8"+(System.currentTimeMillis()-startTime1));
SysApplicationApiEntity receiveApi = getApiByAppIdApiCode(receiveApp.getId(), apiCode);
if (receiveApi == null) {
saveLog(sendApp, receiveApp, new SysApplicationApiEntity(), oldbodys, null, oldheaderMap, null, null, oldquerys, null, null, false,receiveApp.getName() + ":" + apiCode + "未启用或者未创建" );
return BaseResult.getFailureMessageEntity(receiveApp.getName() + ":" + apiCode + "未启用或者未创建");
}
logger.info("发送数据9"+(System.currentTimeMillis()-startTime1));
SysApplicationApiAuthEntity sysApplicationApiAuthEntity = getApiAuthByNameAppId(sendApp.getId(), receiveApp.getId());
if (sysApplicationApiAuthEntity == null) {
@ -1763,6 +1780,7 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
saveLog(sendApp, receiveApp, receiveApi, oldbodys, null, oldheaderMap, null, null, oldquerys, null, null, false,receiveApp.getName() + "发送应用" + receiveApp.getName() + "的ip白名单配置错误" );
return BaseResult.getFailureMessageEntity(receiveApp.getName() + "发送应用" + receiveApp.getName() + "的ip白名单配置错误");
}
logger.info("发送数据10"+(System.currentTimeMillis()-startTime1));
SysApplicationApiAuthDetailEntity sysApplicationApiAuthDetailEntity = getApiAuthDetailByAppIdApiIdTripartiteSystemId(receiveApp.getId(), receiveApi.getId(), sysApplicationApiAuthEntity.getId());
if (sysApplicationApiAuthDetailEntity == null) {
@ -1770,6 +1788,7 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
return BaseResult.getFailureMessageEntity(receiveApi.getApiName() + "未授权给" + sendApp.getName() + ",请联系管理员");
}
SysExtensionApiEntity sysExtensionApiEntity = new SysExtensionApiEntity();
logger.info("发送数据11"+(System.currentTimeMillis()-startTime1));
List<String> a = Arrays.asList(new String[]{"apicode", "appid", "secretkey", "publickey", "x-forwarded-for", "cookie", "x-forwarded-proto", "x-real-ip", "content-length", "accept-language", "host", "content-type", "connection", "cache-control", "accept-encoding", "pragma", "accept", "user-agent"});
@ -1791,6 +1810,7 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
}
}
logger.info("发送数据12"+(System.currentTimeMillis()-startTime1));
sysExtensionApiEntity.setSendApp(sendApp);
@ -1838,6 +1858,8 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
}
}
logger.info("发送数据13"+(System.currentTimeMillis()-startTime1));
headers = sysExtensionApiEntity.getHeaders();
String querys = sysExtensionApiEntity.getQuerys();
String bodys = sysExtensionApiEntity.getBodys();
@ -1860,6 +1882,8 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
if ("2".equals(receiveApi.getRequestMethod())) {
method = "GET";
}
logger.info("发送数据14"+(System.currentTimeMillis()-startTime1));
//List<String> a = Arrays.asList(new String[]{"apicode", "appid", "secretkey", "publickey", "x-forwarded-for", "cookie", "x-forwarded-proto", "x-real-ip", "content-length", "accept-language", "host", "content-type", "connection", "cache-control", "accept-encoding", "pragma", "accept", "user-agent"});
//Map<String, String> headers = new HashMap<>();
//if (receiveApi.getHeaderIn() != null && !"".equals(receiveApi.getHeaderIn())) {
@ -1881,6 +1905,8 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
if ("POST".equals(method)) {
logger.info("发送数据15"+(System.currentTimeMillis()-startTime1));
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.disableCookieManagement().build();
@ -1897,6 +1923,8 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
}
StringBuilder body = new StringBuilder();
boolean flag = true;
logger.info("发送数据16"+(System.currentTimeMillis()-startTime1));
try {
if (bodys != null && !"".equals(bodys)) {
ByteArrayEntity entity = new ByteArrayEntity(bodys.getBytes("UTF-8"));
@ -1904,13 +1932,16 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
post.setEntity(entity);
}
response = closeableHttpClient.execute(post);
logger.info("发送数据17"+(System.currentTimeMillis()-startTime1));
HttpEntity entity = response.getEntity();
synchronized (lock) {
body.append(EntityUtils.toString(entity,"UTF-8"));
}
body.append(resultToStr(entity));
//body.append(EntityUtils.toString(entity,"UTF-8"));
flag = true;
logger.info("返回结果:" + body);
logger.info("发送数据18"+(System.currentTimeMillis()-startTime1));
} catch (Exception e) {
logger.error("请求错误:" + e.getMessage());
body.append(e.getMessage());
@ -1930,8 +1961,13 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
}
}
logger.info("发送数据19 "+(System.currentTimeMillis()-startTime1));
logger.info("保存日志开始");
SysMessageManageLogEntity sysMessageManageLogEntity = saveLog(sendApp, receiveApp, receiveApi, oldbodys,bodys, oldheaderMap,headers, headers, oldquerys,querys, body.toString(),true,null);
logger.info("发送数据20 "+(System.currentTimeMillis()-startTime1));
if (methods != null && methods.length > 0) {
for (Method m : methods) {
if (null != m) {
@ -1947,16 +1983,30 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
}
}
}
logger.info("发送数据21 "+(System.currentTimeMillis()-startTime1));
logger.info("保存日志结束");
if (flag) {
if (JSONUtil.isTypeJSON(body.toString())) {
JSONObject jsonObject = JSONObject.parseObject(body.toString());
try {
if(sysMessageManageLogEntity.getStatus() != null && "3".equals(sysMessageManageLogEntity.getStatus())){
return BaseResult.getSuccessMessageEntity("转发成功", jsonObject);
if(sysMessageManageLogEntity.getReturnObject() != null ){
return BaseResult.getSuccessMessageEntity("转发成功", sysMessageManageLogEntity.getReturnObject());
}else {
return BaseResult.getSuccessMessageEntity("转发成功", sysMessageManageLogEntity.getU8CReturnEntity());
}
}else {
return BaseResult.getFailureMessageEntity("转发失败", jsonObject);
if(sysMessageManageLogEntity.getReturnObject() != null ){
return BaseResult.getFailureMessageEntity("转发失败", sysMessageManageLogEntity.getReturnObject());
}else if(sysMessageManageLogEntity.getU8CReturnEntity() != null ){
return BaseResult.getSuccessMessageEntity("转发失败", sysMessageManageLogEntity.getU8CReturnEntity());
}else {
JSONObject jsonObject = JSONObject.parseObject(body.toString());
logger.info("发送数据错误22 "+(System.currentTimeMillis()-startTime1));
return BaseResult.getFailureMessageEntity("转发失败", jsonObject);
}
}
} else {
} catch (Exception e) {
logger.info("发送数据错误22 "+(System.currentTimeMillis()-startTime1));
if(sysMessageManageLogEntity.getStatus() != null && "3".equals(sysMessageManageLogEntity.getStatus())){
return BaseResult.getSuccessMessageEntity("转发成功", body);
}else {
@ -1986,6 +2036,7 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
boolean flag = true;
try {
response = closeableHttpClient.execute(get);
HttpEntity entity = response.getEntity();
synchronized (lock) {
@ -2030,14 +2081,14 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
}
logger.info("保存日志结束");
if (flag) {
if (JSONUtil.isTypeJSON(body.toString())) {
try {
JSONObject jsonObject = JSONObject.parseObject(body.toString());
if(sysMessageManageLogEntity.getStatus() != null && "3".equals(sysMessageManageLogEntity.getStatus())){
return BaseResult.getSuccessMessageEntity("转发成功", jsonObject);
}else {
return BaseResult.getFailureMessageEntity("转发失败", jsonObject);
}
} else {
} catch (Exception e) {
if(sysMessageManageLogEntity.getStatus() != null && "3".equals(sysMessageManageLogEntity.getStatus())){
return BaseResult.getSuccessMessageEntity("转发成功", body);
}else {
@ -2050,6 +2101,28 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
}
}
/*****
* 请求结果转换str
* @content:
* @author 👻👻👻👻👻👻👻👻 gjh
* @date 2024-10-16 16:42
* @param
* @return String
**/
private String resultToStr(HttpEntity entity) throws IOException {
InputStream inputStream = entity.getContent();
StringBuilder sb = new StringBuilder();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer))!= -1) {
sb.append(new String(buffer, 0, bytesRead, "utf-8"));
}
String result = sb.toString();
return result;
}
@Override
@DSTransactional()
public JsonResultEntity externalCallInterfaceResend(SysMessageManageLogEntity resendLogEntity) {
@ -2545,55 +2618,77 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
if (receiveApi.getReturnSuccessField() != null && !"".equals(receiveApi.getReturnSuccessField())
&& receiveApi.getReturnSuccessValue() != null && !"".equals(receiveApi.getReturnSuccessValue())) {
if (JSONUtil.isTypeJSON(body)) {
JSONObject cheackdatas = JSONObject.parseObject(body);
JSONObject datas = JSONObject.parseObject(body);
String checkdata = cheackdatas.getString(receiveApi.getReturnSuccessField());
if (checkdata != null && receiveApi.getReturnSuccessValue().equals(checkdata)) {
sysMessageManageLogEntity.setStatus("3");//返回信息
if (receiveApi.getReturnMsg() != null && !"".equals(receiveApi.getReturnMsg())) {
String returnMsg = cheackdatas.getString(receiveApi.getReturnMsg());
sysMessageManageLogEntity.setRemark("接口调用成功,返回信息如下:" + returnMsg);//返回信息
} else {
sysMessageManageLogEntity.setRemark("接口调用成功");//返回信息
}
}else if(receiveApi.getReturnSuccessValue().equals("not null") && checkdata != null){
sysMessageManageLogEntity.setStatus("3");//返回信息
if (receiveApi.getReturnMsg() != null && !"".equals(receiveApi.getReturnMsg())) {
String returnMsg = cheackdatas.getString(receiveApi.getReturnMsg());
sysMessageManageLogEntity.setRemark("接口调用成功,返回信息如下:" + returnMsg);//返回信息
} else {
if(receiveApi.getApiCode() != null && "8000350006".equals(receiveApi.getApiCode())){
U8CReturnEntity u8CReturnEntity = JSONObject.parseObject(body, U8CReturnEntity.class);
sysMessageManageLogEntity.setU8CReturnEntity(u8CReturnEntity);
if(u8CReturnEntity.getStatus() != null && "success".equals(u8CReturnEntity.getStatus())){
sysMessageManageLogEntity.setStatus("3");//返回信息
sysMessageManageLogEntity.setRemark("接口调用成功");//返回信息
}else {
sysMessageManageLogEntity.setStatus("4");//返回信息
sysMessageManageLogEntity.setRemark("接口调用成功,返回信息如下:" + u8CReturnEntity.getErrormsg());//返回信息
}
}else {
String fieldname = receiveApi.getReturnSuccessField();
if(fieldname.contains(".")){
String[] fileds = fieldname.split("\\.");
boolean flags = false;
for (int i = 0; i < fileds.length; i++) {
if (JSONUtil.isTypeJSON(datas.getString(fileds[i]))) {
datas = datas.getJSONObject(fileds[i]);
if(fileds.length-2 == i ){
String a = datas.getString(fileds[i+1]);
if (a != null && receiveApi.getReturnSuccessValue().equals(a)) {
flags = true;
break;
}else if(receiveApi.getReturnSuccessValue().equals("not null") && a != null){
flags = true;
break;
}else {
break;
JSONObject cheackdatas = JSONObject.parseObject(body);
sysMessageManageLogEntity.setReturnObject(cheackdatas);
JSONObject datas = JSONObject.parseObject(body);
String checkdata = cheackdatas.getString(receiveApi.getReturnSuccessField());
if (checkdata != null && receiveApi.getReturnSuccessValue().equals(checkdata)) {
sysMessageManageLogEntity.setStatus("3");//返回信息
if (receiveApi.getReturnMsg() != null && !"".equals(receiveApi.getReturnMsg())) {
String returnMsg = cheackdatas.getString(receiveApi.getReturnMsg());
sysMessageManageLogEntity.setRemark("接口调用成功,返回信息如下:" + returnMsg);//返回信息
} else {
sysMessageManageLogEntity.setRemark("接口调用成功");//返回信息
}
}else if(receiveApi.getReturnSuccessValue().equals("not null") && checkdata != null){
sysMessageManageLogEntity.setStatus("3");//返回信息
if (receiveApi.getReturnMsg() != null && !"".equals(receiveApi.getReturnMsg())) {
String returnMsg = cheackdatas.getString(receiveApi.getReturnMsg());
sysMessageManageLogEntity.setRemark("接口调用成功,返回信息如下:" + returnMsg);//返回信息
} else {
sysMessageManageLogEntity.setRemark("接口调用成功");//返回信息
}
}else {
String fieldname = receiveApi.getReturnSuccessField();
if(fieldname.contains(".")){
String[] fileds = fieldname.split("\\.");
boolean flags = false;
for (int i = 0; i < fileds.length; i++) {
if (JSONUtil.isTypeJSON(datas.getString(fileds[i]))) {
datas = datas.getJSONObject(fileds[i]);
if(fileds.length-2 == i ){
String a = datas.getString(fileds[i+1]);
if (a != null && receiveApi.getReturnSuccessValue().equals(a)) {
flags = true;
break;
}else if(receiveApi.getReturnSuccessValue().equals("not null") && a != null){
flags = true;
break;
}else {
break;
}
}
}
}
}
if(flags){
sysMessageManageLogEntity.setStatus("3");//返回信息
if (receiveApi.getReturnMsg() != null && !"".equals(receiveApi.getReturnMsg())) {
String returnMsg = cheackdatas.getString(receiveApi.getReturnMsg());
sysMessageManageLogEntity.setRemark("接口调用成功,返回信息如下:" + returnMsg);//返回信息
} else {
sysMessageManageLogEntity.setRemark("接口调用成功");//返回信息
if(flags){
sysMessageManageLogEntity.setStatus("3");//返回信息
if (receiveApi.getReturnMsg() != null && !"".equals(receiveApi.getReturnMsg())) {
String returnMsg = cheackdatas.getString(receiveApi.getReturnMsg());
sysMessageManageLogEntity.setRemark("接口调用成功,返回信息如下:" + returnMsg);//返回信息
} else {
sysMessageManageLogEntity.setRemark("接口调用成功");//返回信息
}
}else {
sysMessageManageLogEntity.setStatus("4");//返回信息
if (receiveApi.getReturnMsg() != null && !"".equals(receiveApi.getReturnMsg())) {
String returnMsg = cheackdatas.getString(receiveApi.getReturnMsg());
sysMessageManageLogEntity.setRemark("接口调用失败,返回值错误,返回信息如下:" + returnMsg);//返回信息
} else {
sysMessageManageLogEntity.setRemark("接口调用失败,返回值错误");//返回信息
}
}
}else {
sysMessageManageLogEntity.setStatus("4");//返回信息
if (receiveApi.getReturnMsg() != null && !"".equals(receiveApi.getReturnMsg())) {
@ -2603,15 +2698,6 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
sysMessageManageLogEntity.setRemark("接口调用失败,返回值错误");//返回信息
}
}
}else {
sysMessageManageLogEntity.setStatus("4");//返回信息
if (receiveApi.getReturnMsg() != null && !"".equals(receiveApi.getReturnMsg())) {
String returnMsg = cheackdatas.getString(receiveApi.getReturnMsg());
sysMessageManageLogEntity.setRemark("接口调用失败,返回值错误,返回信息如下:" + returnMsg);//返回信息
} else {
sysMessageManageLogEntity.setRemark("接口调用失败,返回值错误");//返回信息
}
}
}
} else {

View File

@ -1,5 +1,7 @@
package com.hzya.frame.sysnew.messageManageLog.entity;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.sysnew.application.entity.U8CReturnEntity;
import com.hzya.frame.web.entity.BaseEntity;
import java.util.Date;
@ -46,6 +48,10 @@ public class SysMessageManageLogEntity extends BaseEntity {
private Date createTimeStart;
private Date createTimeEnd;
private List<String> ids;
private JSONObject returnObject;
private U8CReturnEntity u8CReturnEntity;
public String getReturnType() {
return returnType;
}
@ -196,5 +202,21 @@ public class SysMessageManageLogEntity extends BaseEntity {
public void setCreateTimeEnd(Date createTimeEnd) {
this.createTimeEnd = createTimeEnd;
}
public JSONObject getReturnObject() {
return returnObject;
}
public void setReturnObject(JSONObject returnObject) {
this.returnObject = returnObject;
}
public U8CReturnEntity getU8CReturnEntity() {
return u8CReturnEntity;
}
public void setU8CReturnEntity(U8CReturnEntity u8CReturnEntity) {
this.u8CReturnEntity = u8CReturnEntity;
}
}