diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/EsbDataStruct.java b/backend/src/main/java/io/metersphere/api/dto/automation/EsbDataStruct.java index c15ddce336..295cd9fdd0 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/EsbDataStruct.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/EsbDataStruct.java @@ -134,7 +134,7 @@ public class EsbDataStruct { element.addText(this.value); } else { for (EsbDataStruct child : children) { - child.genXmlElementByChildren(document); + child.genXmlElementByChildren(element); } } } diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/parse/EsbDataParser.java b/backend/src/main/java/io/metersphere/api/dto/automation/parse/EsbDataParser.java index 1944c5dd85..3d45f1f713 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/parse/EsbDataParser.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/parse/EsbDataParser.java @@ -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; } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/ESBParser.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/ESBParser.java index 5cbe4ce716..47e3561102 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/parse/ESBParser.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/ESBParser.java @@ -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 = "\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 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 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 queryStringList = harRequest.queryString; -// queryStringList.forEach(harQueryParm -> { -// parseQueryParameters(harQueryParm, request.getArguments()); -// }); -// List harHeaderList = harRequest.headers; -// harHeaderList.forEach(harHeader -> { -// parseHeaderParameters(harHeader, request.getHeaders()); -// }); -// List harCookieList = harRequest.cookies; -// harCookieList.forEach(harCookie -> { -// parseCookieParameters(harCookie, request.getHeaders()); -// }); -// } - - private String getDefaultStringValue(String val) { return StringUtils.isBlank(val) ? "" : val; } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 1d21e3fed1..0c79bfc5b2 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -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); diff --git a/backend/src/main/java/io/metersphere/api/service/EsbApiParamService.java b/backend/src/main/java/io/metersphere/api/service/EsbApiParamService.java index 60e557d4bb..f648cee5aa 100644 --- a/backend/src/main/java/io/metersphere/api/service/EsbApiParamService.java +++ b/backend/src/main/java/io/metersphere/api/service/EsbApiParamService.java @@ -285,7 +285,7 @@ public class EsbApiParamService { // } //通过esb数据结构生成keyValue集合,以及发送参数 - private List genKeyValueListByDataStruct(MsTCPSampler tcpSampler, String esbDataStruct) { + public List genKeyValueListByDataStruct(MsTCPSampler tcpSampler, String esbDataStruct) { List keyValueList = new ArrayList<>(); String sendRequest = tcpSampler.getRequest(); String paramRegexStr = "\\$\\{([^}]*)\\}"; diff --git a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java index 3cd09f10e4..590fccad2a 100644 --- a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java +++ b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java @@ -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 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);