Commit 33c87819 authored by fa_pms01's avatar fa_pms01

支持WebService请求

parent 43cde115
...@@ -9,6 +9,8 @@ import utils.HandleFileUtils; ...@@ -9,6 +9,8 @@ import utils.HandleFileUtils;
import utils.ThirdApiCaller; import utils.ThirdApiCaller;
import javax.swing.*; import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.*; import java.util.*;
...@@ -28,6 +30,7 @@ public class WindowStart extends JFrame { ...@@ -28,6 +30,7 @@ public class WindowStart extends JFrame {
private JTextArea outTextArea; private JTextArea outTextArea;
private JComboBox<Model> modelBox; private JComboBox<Model> modelBox;
private JComboBox<Model> coreNumBox; private JComboBox<Model> coreNumBox;
private JComboBox<Model> typeBox;
private List<Param> paramList = new ArrayList<>(); private List<Param> paramList = new ArrayList<>();
public static final String SPACE = "\n\r"; public static final String SPACE = "\n\r";
...@@ -51,7 +54,13 @@ public class WindowStart extends JFrame { ...@@ -51,7 +54,13 @@ public class WindowStart extends JFrame {
//循环 根据名称读取对应的文件 //循环 根据名称读取对应的文件
if (CollectionUtils.isNotEmpty(fileFullNames)) { if (CollectionUtils.isNotEmpty(fileFullNames)) {
StringBuilder sb = new StringBuilder("开始解析文本请求内容:").append(SPACE); StringBuilder sb = new StringBuilder("开始解析文本请求内容:").append(SPACE);
List<Param> paramListTemp = buildTxtData(fileFullNames, sb); Model typeModel = (Model)typeBox.getSelectedItem();
List<Param> paramListTemp = new ArrayList<>();
if (typeModel.getModelCode() == 0){// Restfull
paramListTemp= buildTxtData4RestFull(fileFullNames, sb);
} else if (typeModel.getModelCode() == 1) {//WebService
paramListTemp = buildTxtData4WebService(fileFullNames, sb);
}
if (CollectionUtils.isNotEmpty(paramListTemp)){ if (CollectionUtils.isNotEmpty(paramListTemp)){
paramList = paramListTemp; paramList = paramListTemp;
inTextArea.setText(inTextArea.getText() + SPACE + sb.toString()); inTextArea.setText(inTextArea.getText() + SPACE + sb.toString());
...@@ -59,17 +68,10 @@ public class WindowStart extends JFrame { ...@@ -59,17 +68,10 @@ public class WindowStart extends JFrame {
} }
}); });
fileTextFile = new JTextField("C:\\Users\\GetWind\\Desktop\\TestCaseFileRootPath", 50); fileTextFile = new JTextField("C:\\Users\\GetWind\\Desktop\\TestCaseFileRootPath", 50);
// fileTextFile.addActionListener(new InputListener()); urlTextFile = new JTextField("http://localhost:9080/gateway/Service/JobManagementWebService?wsdl", 50);
frame.add(fileTextFile);
frame.add(fileInput);
urlTextFile = new JTextField("http://localhost:9080/gateway/httpServer", 50);
// urlTextFile.addActionListener(new InputListener());
frame.add(urlTextFile);
urlTextFile = new JTextField("http://localhost:9080/gateway/httpServer", 50);
inTextArea = new JTextArea(10, 30); // 设置行数和列数 inTextArea = new JTextArea(10, 30); // 设置行数和列数
inTextArea.setLineWrap(true); // 设置自动换行 inTextArea.setLineWrap(true); // 设置自动换行
inTextArea.setWrapStyleWord(true); // 设置按单词换行 inTextArea.setWrapStyleWord(true); // 设置按单词换行
// inTextArea.set
JScrollPane inScrollPane = new JScrollPane(inTextArea); // 添加滚动条 JScrollPane inScrollPane = new JScrollPane(inTextArea); // 添加滚动条
outTextArea = new JTextArea(10, 30); // 设置行数和列数 outTextArea = new JTextArea(10, 30); // 设置行数和列数
outTextArea.setLineWrap(true); // 设置自动换行 outTextArea.setLineWrap(true); // 设置自动换行
...@@ -81,7 +83,8 @@ public class WindowStart extends JFrame { ...@@ -81,7 +83,8 @@ public class WindowStart extends JFrame {
Model model = (Model)modelBox.getSelectedItem(); Model model = (Model)modelBox.getSelectedItem();
Model coreNum = (Model)coreNumBox.getSelectedItem(); Model coreNum = (Model)coreNumBox.getSelectedItem();
outTextArea.setText("开始执行: 运行模式[ " + model.getModel() + " ],线程数[ " + coreNum.getModel() + " ]" + SPACE); outTextArea.setText("开始执行: 运行模式[ " + model.getModel() + " ],线程数[ " + coreNum.getModel() + " ]" + SPACE);
callTest(paramList, url, model.getModelCode(), coreNum); Model typeModel = (Model)typeBox.getSelectedItem();
callTest(paramList, url, model.getModelCode(), coreNum, typeModel.getModelCode());
}); });
modelBox = new JComboBox<>(); modelBox = new JComboBox<>();
modelBox.addItem(new Model("并行", 1)); modelBox.addItem(new Model("并行", 1));
...@@ -94,6 +97,9 @@ public class WindowStart extends JFrame { ...@@ -94,6 +97,9 @@ public class WindowStart extends JFrame {
frame.add(coreNumBox); frame.add(coreNumBox);
} }
}); });
typeBox = new JComboBox<>();
typeBox.addItem(new Model("WebService", 1));
typeBox.addItem(new Model("RestFull", 0));
coreNumBox = new JComboBox<>(); coreNumBox = new JComboBox<>();
coreNumBox.addItem(new Model("10", 10)); coreNumBox.addItem(new Model("10", 10));
coreNumBox.addItem(new Model("9", 9)); coreNumBox.addItem(new Model("9", 9));
...@@ -109,6 +115,7 @@ public class WindowStart extends JFrame { ...@@ -109,6 +115,7 @@ public class WindowStart extends JFrame {
fileTextFile.setBounds(50,50,450,30); fileTextFile.setBounds(50,50,450,30);
fileInput.setBounds(860,225,100,30);//设置组件大小和位置 fileInput.setBounds(860,225,100,30);//设置组件大小和位置
urlTextFile.setBounds(50,100,450,30);//设置组件大小和位置 urlTextFile.setBounds(50,100,450,30);//设置组件大小和位置
typeBox.setBounds(860, 100, 100, 30);
inScrollPane.setBounds(50, 150, 800, 200); inScrollPane.setBounds(50, 150, 800, 200);
outScrollPane.setBounds(50, 350, 800, 300); outScrollPane.setBounds(50, 350, 800, 300);
startButton.setBounds(860, 475, 100, 30); startButton.setBounds(860, 475, 100, 30);
...@@ -123,14 +130,21 @@ public class WindowStart extends JFrame { ...@@ -123,14 +130,21 @@ public class WindowStart extends JFrame {
frame.add(startButton); frame.add(startButton);
frame.add(modelBox); frame.add(modelBox);
frame.add(coreNumBox); frame.add(coreNumBox);
frame.add(typeBox);
frame.setVisible(true);//显示 frame.setVisible(true);//显示
frame.setResizable(false);//固定大小 frame.setResizable(false);//固定大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//可以关闭窗口 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//可以关闭窗口
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent we) {
System.exit(0);
}
});
} }
private void callTest(List<Param> paramList, String url, int model, Model coreNum) { private void callTest(List<Param> paramList, String url, int model, Model coreNum, int type) {
rebuildList(paramList, url); rebuildList(paramList, url);
StringBuffer str = new StringBuffer(); StringBuffer str = new StringBuffer();
if (model == 1){ if (model == 1){
...@@ -140,12 +154,12 @@ public class WindowStart extends JFrame { ...@@ -140,12 +154,12 @@ public class WindowStart extends JFrame {
} }
ForkJoinPool pool = new ForkJoinPool(coreCount); ForkJoinPool pool = new ForkJoinPool(coreCount);
ForkJoinTask task = pool.submit(() -> { ForkJoinTask task = pool.submit(() -> {
paramList.parallelStream().forEach(s -> str.append(call(s.getHead(), s.getBody(), s.getUrl()))); paramList.parallelStream().forEach(s -> str.append(call(s.getHead(), s.getBody(), s.getUrl(), type)));
}); });
task.join(); task.join();
pool.shutdown(); pool.shutdown();
} else { } else {
paramList.forEach(s -> str.append(call(s.getHead(), s.getBody(), s.getUrl()))); paramList.forEach(s -> str.append(call(s.getHead(), s.getBody(), s.getUrl(), type)));
} }
outTextArea.setText(outTextArea.getText() + str.toString()); outTextArea.setText(outTextArea.getText() + str.toString());
} }
...@@ -154,7 +168,7 @@ public class WindowStart extends JFrame { ...@@ -154,7 +168,7 @@ public class WindowStart extends JFrame {
paramList.forEach(p -> p.setUrl(url)); paramList.forEach(p -> p.setUrl(url));
} }
private static String call(Map header, Map bodyObject, String url){ private static String call(Map header, Map bodyObject, String url, int type){
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
result.append("time=[").append(System.currentTimeMillis()).append("] ").append(SPACE); result.append("time=[").append(System.currentTimeMillis()).append("] ").append(SPACE);
try { try {
...@@ -162,7 +176,11 @@ public class WindowStart extends JFrame { ...@@ -162,7 +176,11 @@ public class WindowStart extends JFrame {
argMap.put("head", header); argMap.put("head", header);
argMap.put("body", bodyObject); argMap.put("body", bodyObject);
result.append("Request:").append(bodyObject).append(SPACE).append("Response:"); result.append("Request:").append(bodyObject).append(SPACE).append("Response:");
if (type == 0){// Restfull
result.append(ThirdApiCaller.postJson(url, argMap, header, String.class)); result.append(ThirdApiCaller.postJson(url, argMap, header, String.class));
} else if (type == 1) {//WebService
result.append(ThirdApiCaller.postForWS(url, argMap));
}
} catch (Exception e) { } catch (Exception e) {
result.append("Response Error:[").append(e.getMessage()).append("]"); result.append("Response Error:[").append(e.getMessage()).append("]");
} }
...@@ -170,7 +188,31 @@ public class WindowStart extends JFrame { ...@@ -170,7 +188,31 @@ public class WindowStart extends JFrame {
} }
private List<Param> buildTxtData(List<String> fileFullNames, StringBuilder sb) { private List<Param> buildTxtData4RestFull(List<String> fileFullNames, StringBuilder sb) {
List<String> failFullNames = new ArrayList<>();
List<Param> dtoList = new ArrayList<>();
for (String fileFullName : fileFullNames) {
try {
String txtDataStr = HandleFileUtils.readTxtFile(fileFullName);
Map txtMap = JsonUtils.toMap(txtDataStr);
sb.append(txtMap.toString()).append(SPACE);
Map head = MapUtils.getMap(txtMap, "head");
Map body = MapUtils.getMap(txtMap, "body");
dtoList.add(new Param(head, body, ""));
} catch (Exception e) {
sb.append("文件解析失败:").append(e.getMessage()).append(SPACE);
failFullNames.add(fileFullName);
}
sb.append(SPACE);
}
if (CollectionUtils.isNotEmpty(failFullNames)) {
failFullNames.forEach(f -> sb.append("解析失败的文件名:").append(f).append(SPACE));
}
return dtoList;
}
private List<Param> buildTxtData4WebService(List<String> fileFullNames, StringBuilder sb) {
List<String> failFullNames = new ArrayList<>(); List<String> failFullNames = new ArrayList<>();
List<Param> dtoList = new ArrayList<>(); List<Param> dtoList = new ArrayList<>();
for (String fileFullName : fileFullNames) { for (String fileFullName : fileFullNames) {
......
package utils; package utils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.msg.JsonUtils;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
...@@ -12,16 +15,16 @@ import java.util.HashMap; ...@@ -12,16 +15,16 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
/**
* @author JP
* @createTime 2021/9/23
*/
public class ThirdApiCaller { public class ThirdApiCaller {
private static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded"; private static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded";
private static final String JSON_CONTENT_TYPE = "application/json;charset=utf8"; private static final String JSON_CONTENT_TYPE = "application/json;charset=utf8";
private static final String XML_CONTENT_TYPE = "application/xml;charset=utf8"; private static final String XML_CONTENT_TYPE = "application/xml;charset=utf8";
private static final String XML_TEXT_CONTENT_TYPE = "text/xml;charset=UTF-8";
private static final String NAME_SPACE = "http://xml.fa-software.com/mycim/wsdd/";
private static final String SERVICE = "execute";
private static final String SUBSTR_START = "<Response>";
private static final String SUBSTR_END = "</Response>";
private static RestTemplate REST_TEMPLATE; private static RestTemplate REST_TEMPLATE;
...@@ -77,11 +80,24 @@ public class ThirdApiCaller { ...@@ -77,11 +80,24 @@ public class ThirdApiCaller {
} }
} }
public static <T> T postXml(String url, Object body, Map<String, String> header, Class<T> clazz) { public static String postForWS(String url, Map<String, Object> body) {
try { try {
HttpHeaders httpHeaders = createXmlHttpHeaders(header); // 创建对象用于xml的拼接
body = body == null ? new HashMap<String, String>() : body; HttpHeaders headers = new HttpHeaders();
return REST_TEMPLATE.postForObject(url, new HttpEntity(body, httpHeaders), clazz); MediaType mediaType = MediaType.parseMediaType(XML_TEXT_CONTENT_TYPE);
headers.setContentType(mediaType);
String soapRequestData = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:wsdd=\"" + NAME_SPACE + "\">" +
"<soapenv:Header/>" +
"<soapenv:Body>" +
"<wsdd:" + SERVICE + ">" +
"<Request>" +
JsonUtils.toString(body) +
"</Request>" +
"</wsdd:" + SERVICE + ">" +
"</soapenv:Body>" +
"</soapenv:Envelope>";
HttpEntity<String> httpEntity = new HttpEntity<>(soapRequestData, headers);
return substrResponse(REST_TEMPLATE.postForObject(url, httpEntity, String.class));
} catch (Exception e) { } catch (Exception e) {
throw e; throw e;
} }
...@@ -120,4 +136,11 @@ public class ThirdApiCaller { ...@@ -120,4 +136,11 @@ public class ThirdApiCaller {
return headers; return headers;
} }
private static String substrResponse(String result){
if (StringUtils.isNotBlank(result) && result.contains(SUBSTR_START) && result.contains(SUBSTR_END)){
result = StringUtils.substringBetween(result, SUBSTR_START, SUBSTR_END);
}
return result;
}
} }
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