PostManResolver.java
9.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package com.ecar.apm.util;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.commons.io.IOUtils;
import org.springframework.util.ResourceUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONReader;
import com.ecar.apm.model.HttpRequest;
import com.ecar.apm.model.HttpSequence;
import com.ecar.apm.model.HttpRequest.HttpMethod;
import com.github.pagehelper.StringUtil;
public class PostManResolver {
public static void main(String[] args) throws Exception{
String jsonText = readJsonFile("classpath:testfile/v2.1postman_collection.json");
ArrayList<HttpRequest> list = PostManResolver.readV2FromJsonText(jsonText);
for(HttpRequest request : list){
System.out.println(request.getUrl());
}
}
public static void readToBuffer(StringBuffer buffer, String filePath) throws IOException {
//InputStream is = new FileInputStream(filePath);
InputStream is = new FileInputStream(ResourceUtils.getFile(filePath));
String line; // 用来保存每行读取的内容
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
line = reader.readLine(); // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
buffer.append("\n"); // 添加换行符
line = reader.readLine(); // 读取下一行
}
reader.close();
is.close();
}
public static String readJsonFile(String filepath) throws IOException{
InputStream is = new FileInputStream(ResourceUtils.getFile(filepath));
String jsonText = IOUtils.toString(is,"utf8");
is.close();
return jsonText;
}
public static ArrayList<HttpRequest> readFromJsonFile(String filepath){
try{
String jsonText = readJsonFile(filepath);
return readV1FromJsonText(jsonText);
}catch(Exception e){
return null;
}
}
public static ArrayList<HttpRequest> readV1FromJsonText(String jsonText){
ArrayList<String> httpOrderList = new ArrayList<String>();
ArrayList<HttpRequest> httpRequestList = new ArrayList<HttpRequest>();
//----------------------开始解析postman脚本------------------------
JSONObject root = JSONObject.parseObject(jsonText);
//order
JSONArray order = root.getJSONArray("order");
for(Object obj : order){
httpOrderList.add(obj.toString());
}
//requests
JSONArray requests = root.getJSONArray("requests");
for(Object obj : requests){
JSONObject request = (JSONObject) obj;
HttpRequest httpRequest = new HttpRequest();
httpRequest.setRemark(request.getString("id"));
//httpRequest.setName(request.getString("name"));
httpRequest.setUrl(request.getString("url"));
httpRequest.setHttpMethod(HttpMethod.valueOf(request.getString("method").toUpperCase()));
//header
String headers = request.getString("headers");
if(!StringUtil.isEmpty(headers)){
HashMap<String, String> headerMap = new HashMap<String, String>();
String[] headerArray = headers.split("\n");
for(String str : headerArray){
String[] header = str.split(": ");
headerMap.put(header[0], header[1]);
}
httpRequest.setHeadersMap(headerMap);
}
//data
String data = request.getString("data");
if(!StringUtil.isEmpty(data)){
JSONArray dataJa = JSONArray.parseArray(data);
HashMap<String, String> paramMap = new HashMap<String, String>();
for (Object o : dataJa) {
JSONObject dataJo = (JSONObject) o;
paramMap.put(dataJo.getString("key"), dataJo.getString("value"));
}
httpRequest.setParametersMap(paramMap);
}
httpRequestList.add(httpRequest);
}
//------------------------------结束解析-----------------------------
ArrayList<HttpRequest> resultList = new ArrayList<HttpRequest>();
int sort = 1;
for(String id : httpOrderList){
for(HttpRequest httpRequest : httpRequestList){
if(id.equals(httpRequest.getRemark())){
httpRequest.setSort(sort);
resultList.add(httpRequest);
sort++;
}
}
}
return resultList;
}
public static ArrayList<HttpRequest> readV2FromJsonText(String jsonText){
ArrayList<HttpRequest> httpRequestList = new ArrayList<HttpRequest>();
//----------------------开始解析postman脚本------------------------
JSONObject root = JSONObject.parseObject(jsonText);
//requests
JSONArray items = root.getJSONArray("item");
int sort = 1;
for(Object obj : items){
JSONObject item = (JSONObject) obj;
JSONObject request = item.getJSONObject("request");
HttpRequest httpRequest = new HttpRequest();
String url = request.getString("url");
if(url.contains("\"raw\":")){
httpRequest.setUrl(request.getJSONObject("url").getString("raw"));
}else{
httpRequest.setUrl(request.getString("url"));
}
httpRequest.setHttpMethod(HttpMethod.valueOf(request.getString("method").toUpperCase()));
//header
JSONArray headers = request.getJSONArray("header");
if(!headers.isEmpty()){
HashMap<String, String> headerMap = new HashMap<String, String>();
for(Object obj1 : headers){
JSONObject header = (JSONObject) obj1;
headerMap.put(header.getString("key"), header.getString("value"));
}
httpRequest.setHeadersMap(headerMap);
}
//data
String raw = request.getJSONObject("body").getString("raw");
if(!StringUtil.isEmpty(raw)){
HashMap<String, String> paramMap = new HashMap<String, String>();
String[] rawArray = raw.split("&");
for(String str : rawArray){
String[] param = str.split("=");
paramMap.put(param[0], param[1]);
}
httpRequest.setParametersMap(paramMap);
}
httpRequest.setSort(sort);
httpRequestList.add(httpRequest);
sort++;
}
//------------------------------结束解析-----------------------------
return httpRequestList;
}
public static void readBigFile() throws FileNotFoundException{
HttpSequence httpSequence = new HttpSequence();
ArrayList<String> ids = new ArrayList<String>();
ArrayList<HttpRequest> httpList = new ArrayList<HttpRequest>();
JSONReader jsonReader = new JSONReader(new FileReader(ResourceUtils.getFile("classpath:testCase.postman_collection1.json")));
jsonReader.startObject();//将整个json文件当作 Map<String,Object> 对象来解析 {,}
while(jsonReader.hasNext()) {
String key = jsonReader.readString();
if(key.equals("order")){
jsonReader.startArray();//数组
while(jsonReader.hasNext()) {
String id = jsonReader.readString();
ids.add(id);
}
jsonReader.endArray();
}else if(key.equals("requests")){
jsonReader.startArray();//---> [ 开启读List对象
while(jsonReader.hasNext()) {
HttpRequest httpRequest = new HttpRequest();
jsonReader.startObject();
while(jsonReader.hasNext()) {
String objKey = jsonReader.readString();
if(objKey.equals("id")){
httpRequest.setRemark(jsonReader.readString());
}else if(objKey.equals("url")){
httpRequest.setUrl(jsonReader.readString());
}else if(objKey.equals("method")){
httpRequest.setHttpMethod(HttpMethod.valueOf(jsonReader.readString().toUpperCase()));
}else if(objKey.equals("headers")){
HashMap<String, String> map = new HashMap<String, String>();
String[] headers = jsonReader.readString().split("\n");
for(String str : headers){
String[] header = str.split(": ");
map.put(header[0], header[1]);
}
httpRequest.setHeadersMap(map);
}else if(objKey.equals("name")){
//httpRequest.setName(jsonReader.readString());
}else if(objKey.equals("data")){
String data = jsonReader.readString();
if(data!=null){
JSONArray jsonArray = JSONArray.parseArray(data);
HashMap<String, String> map = new HashMap<String, String>();
for (Object obj : jsonArray) {
JSONObject jsonObject = (JSONObject) obj;
map.put(jsonObject.getString("key"), jsonObject.getString("value"));
}
httpRequest.setParametersMap(map);
}
}else{
jsonReader.readObject();
}
}
jsonReader.endObject();
httpList.add(httpRequest);
}
jsonReader.endArray();//---> ]
}else{
jsonReader.readObject();
}
}
jsonReader.endObject();
jsonReader.close();
ArrayList<HttpRequest> requests = new ArrayList<HttpRequest>();
int sort = 1;
for(String id : ids){
for(HttpRequest httpRequest : httpList){
if(id.equals(httpRequest.getRemark())){
httpRequest.setSort(sort);
requests.add(httpRequest);
sort++;
}
}
}
httpSequence.setHttpRequest(requests);
}
}