fix: ESB功能优化

ESB功能优化
This commit is contained in:
song-tianyang 2021-04-27 16:19:02 +08:00 committed by 刘瑞斌
parent 988c31be43
commit 30c5fe20c8
6 changed files with 78 additions and 27 deletions

View File

@ -134,7 +134,7 @@ public class EsbDataStruct {
element.addText(this.value);
} else {
for (EsbDataStruct child : children) {
child.genXmlElementByChildren(document);
child.genXmlElementByChildren(element);
}
}
}

View File

@ -5,9 +5,13 @@ import io.metersphere.api.dto.automation.EsbDataStruct;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
/**
@ -30,12 +34,37 @@ public class EsbDataParser {
if (dataStruct != null) {
dataStruct.genXmlElementByDocument(document);
xmlString = document.getRootElement().asXML();
// 设置XML文档格式
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
// 设置XML编码方式,即是用指定的编码方式保存XML文档到字符串(String),这里也可以指定为GBK或是ISO8859-1
outputFormat.setEncoding("UTF-8");
//outputFormat.setSuppressDeclaration(true); //是否生产xml头
outputFormat.setIndent(true); //设置是否缩进
outputFormat.setNewlines(true); //设置是否换行
try {
// stringWriter字符串是用来保存XML文档的
StringWriter stringWriter = new StringWriter();
// xmlWriter是用来把XML文档写入字符串的(工具)
XMLWriter xmlWriter = new XMLWriter(stringWriter, outputFormat);
// 把创建好的XML文档写入字符串
xmlWriter.write(document);
// 打印字符串,即是XML文档
xmlString = stringWriter.toString();
xmlWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (StringUtils.isEmpty(xmlString)) {
xmlString = "";
} else {
xmlString = xmlString.replaceAll(" ", "");
}
return xmlString;
}

View File

@ -8,10 +8,13 @@ import io.metersphere.api.dto.definition.parse.esb.EsbExcelDataStruct;
import io.metersphere.api.dto.definition.parse.esb.EsbSheetDataStruct;
import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor;
import io.metersphere.api.dto.definition.request.sampler.MsTCPSampler;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.request.RequestType;
import io.metersphere.api.service.EsbApiParamService;
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.base.domain.ApiModule;
import io.metersphere.base.domain.EsbApiParamsWithBLOBs;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.SessionUtils;
import io.swagger.models.Model;
import org.apache.commons.lang3.StringUtils;
@ -599,6 +602,10 @@ public class ESBParser extends EsbAbstractParser {
savedNames.add(reqName);
}
String esbSendRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n${SERVICE}";
String reqDataStructStr = generateDataStrcut(headSheetData, interfaceData, true);
String respDataStrutStr = generateDataStrcut(headSheetData, interfaceData, false);
String apiId = UUID.randomUUID().toString();
ApiDefinitionWithBLOBs apiDefinition = new ApiDefinitionWithBLOBs();
apiDefinition.setName(reqName);
@ -607,7 +614,7 @@ public class ESBParser extends EsbAbstractParser {
apiDefinition.setProjectId(this.projectId);
apiDefinition.setModuleId(importRequest.getModuleId());
apiDefinition.setModulePath(importRequest.getModulePath());
apiDefinition.setRequest(genTCPSampler());
apiDefinition.setRequest(genTCPSampler(esbSendRequest, reqDataStructStr));
if (StringUtils.equalsIgnoreCase("schedule", importRequest.getType())) {
apiDefinition.setUserId(importRequest.getUserId());
} else {
@ -620,8 +627,6 @@ public class ESBParser extends EsbAbstractParser {
EsbApiParamsWithBLOBs apiParams = new EsbApiParamsWithBLOBs();
apiParams.setId(UUID.randomUUID().toString());
apiParams.setResourceId(apiId);
String reqDataStructStr = generateDataStrcut(headSheetData, interfaceData, true);
String respDataStrutStr = generateDataStrcut(headSheetData, interfaceData, false);
apiParams.setDataStruct(reqDataStructStr);
apiParams.setResponseDataStruct(respDataStrutStr);
@ -634,12 +639,38 @@ public class ESBParser extends EsbAbstractParser {
return resultModel;
}
private String genTCPSampler() {
private String genTCPSampler(String sendRequest, String esbDataStruct) {
MsTCPSampler tcpSampler = new MsTCPSampler();
MsJSR223PreProcessor preProcessor = new MsJSR223PreProcessor();
tcpSampler.setTcpPreProcessor(preProcessor);
tcpSampler.setProtocol("ESB");
tcpSampler.setClassname("TCPClientImpl");
tcpSampler.setReUseConnection(false);
String script = "String report = ctx.getCurrentSampler().getRequestData();\n" +
" if(report!=null){\n" +
" //补足8位长度前置补0\n" +
" String reportlengthStr = String.format(\"%08d\",report.length());\n" +
" report = reportlengthStr+report;\n" +
" ctx.getCurrentSampler().setRequestData(report);\n" +
" }";
// frontScriptList.add(script);
if (tcpSampler.getTcpPreProcessor() != null) {
tcpSampler.getTcpPreProcessor().setScriptLanguage("groovy");
tcpSampler.getTcpPreProcessor().setScript(script);
}
if (StringUtils.isNotEmpty(sendRequest)) {
tcpSampler.setRequest(sendRequest);
}
if (StringUtils.isNotEmpty(esbDataStruct)) {
EsbApiParamService esbApiParamService = CommonBeanFactory.getBean(EsbApiParamService.class);
List<KeyValue> keyValueList = esbApiParamService.genKeyValueListByDataStruct(tcpSampler, esbDataStruct);
tcpSampler.setParameters(keyValueList);
}
return JSON.toJSONString(tcpSampler);
}
@ -701,7 +732,7 @@ public class ESBParser extends EsbAbstractParser {
if (!bodyList.isEmpty()) {
EsbDataStruct bodyStruct = new EsbDataStruct();
bodyStruct.initDefaultData("SYS_BODY", null, null, null);
bodyStruct.initDefaultData("BODY", null, null, null);
dataStruct.getChildren().add(bodyStruct);
Map<String, EsbDataStruct> childrenEsbDataStructMap = new HashMap<>();
//用来判断节点有没有在array节点内
@ -734,22 +765,6 @@ public class ESBParser extends EsbAbstractParser {
return JSONArray.toJSONString(list);
}
// private void parseParameters(HarRequest harRequest, MsHTTPSamplerProxy request) {
// List<HarQueryParm> queryStringList = harRequest.queryString;
// queryStringList.forEach(harQueryParm -> {
// parseQueryParameters(harQueryParm, request.getArguments());
// });
// List<HarHeader> harHeaderList = harRequest.headers;
// harHeaderList.forEach(harHeader -> {
// parseHeaderParameters(harHeader, request.getHeaders());
// });
// List<HarCookie> harCookieList = harRequest.cookies;
// harCookieList.forEach(harCookie -> {
// parseCookieParameters(harCookie, request.getHeaders());
// });
// }
private String getDefaultStringValue(String val) {
return StringUtils.isBlank(val) ? "" : val;
}

View File

@ -770,7 +770,7 @@ public class ApiAutomationService {
try {
hashTree = generateHashTree(item, reportId, planEnvMap);
} catch (Exception ex) {
MSException.throwException(ex.getMessage());
MSException.throwException("解析运行步骤失败!场景名称:" + item.getName());
}
//存储报告
batchMapper.insert(report);

View File

@ -285,7 +285,7 @@ public class EsbApiParamService {
// }
//通过esb数据结构生成keyValue集合以及发送参数
private List<KeyValue> genKeyValueListByDataStruct(MsTCPSampler tcpSampler, String esbDataStruct) {
public List<KeyValue> genKeyValueListByDataStruct(MsTCPSampler tcpSampler, String esbDataStruct) {
List<KeyValue> keyValueList = new ArrayList<>();
String sendRequest = tcpSampler.getRequest();
String paramRegexStr = "\\$\\{([^}]*)\\}";

View File

@ -353,13 +353,19 @@ public class MockConfigService {
mockExpectConfigMapper.deleteByPrimaryKey(id);
}
public JSONObject getGetParamMap(String urlParams, ApiDefinitionWithBLOBs api) {
public JSONObject getGetParamMap(String urlParams, ApiDefinitionWithBLOBs api, HttpServletRequest request) {
JSONObject paramMap = this.getSendRestParamMapByIdAndUrl(api, urlParams);
Enumeration<String> paramNameItor = request.getParameterNames();
JSONObject object = new JSONObject();
while (paramNameItor.hasMoreElements()) {
String key = paramNameItor.nextElement();
String value = request.getParameter(key);
paramMap.put(key, value);
}
return paramMap;
}
public JSONObject getPostParamMap(HttpServletRequest request) {
System.out.println(request.getContentType());
if (StringUtils.equalsIgnoreCase("application/JSON", request.getContentType())) {
JSONObject object = null;
try {
@ -572,7 +578,8 @@ public class MockConfigService {
*/
boolean isMatch = false;
for (ApiDefinitionWithBLOBs api : aualifiedApiList) {
JSONObject paramMap = this.getGetParamMap(urlSuffix, api);
JSONObject paramMap = this.getGetParamMap(urlSuffix, api, request);
MockConfigResponse mockConfigData = this.findByApiId(api.getId());
if (mockConfigData != null && mockConfigData.getMockExpectConfigList() != null) {
MockExpectConfigResponse finalExpectConfig = this.findExpectConfig(mockConfigData.getMockExpectConfigList(), paramMap);