-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
ApiImportAbstractParser.java
95 lines (84 loc) · 3.69 KB
/
ApiImportAbstractParser.java
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
package io.metersphere.api.parser.api;
import io.metersphere.api.dto.converter.ApiDefinitionImportDetail;
import io.metersphere.api.dto.request.ImportRequest;
import io.metersphere.api.dto.request.http.MsHTTPConfig;
import io.metersphere.api.dto.request.http.MsHTTPElement;
import io.metersphere.api.dto.request.http.body.*;
import io.metersphere.api.parser.ImportParser;
import io.metersphere.project.dto.environment.auth.NoAuth;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.LogUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
public abstract class ApiImportAbstractParser<T> implements ImportParser<T> {
protected String projectId;
protected String getApiTestStr(InputStream source) {
StringBuilder testStr = null;
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(source, StandardCharsets.UTF_8))) {
testStr = new StringBuilder();
String inputStr;
while ((inputStr = bufferedReader.readLine()) != null) {
testStr.append(inputStr);
}
source.close();
} catch (Exception e) {
LogUtils.error(e.getMessage(), e);
throw new MSException(e.getMessage());
}
return StringUtils.isNotBlank(testStr) ? testStr.toString() : StringUtils.EMPTY;
}
protected ApiDefinitionImportDetail buildApiDefinition(String name, String path, String method,String modulePath, ImportRequest importRequest) {
ApiDefinitionImportDetail apiDefinition = new ApiDefinitionImportDetail();
apiDefinition.setName(name);
apiDefinition.setPath(formatPath(path));
apiDefinition.setProtocol(importRequest.getProtocol());
apiDefinition.setMethod(method);
apiDefinition.setProjectId(this.projectId);
apiDefinition.setCreateUser(importRequest.getUserId());
apiDefinition.setModulePath(modulePath);
apiDefinition.setResponse(new ArrayList<>());
return apiDefinition;
}
protected MsHTTPElement buildHttpRequest(String name, String path, String method) {
MsHTTPElement request = new MsHTTPElement();
request.setName(name);
// 路径去掉域名/IP 地址,保留方法名称及参数
request.setPath(formatPath(path));
request.setMethod(method);
request.setHeaders(new ArrayList<>());
request.setQuery(new ArrayList<>());
request.setRest(new ArrayList<>());
request.setBody(new Body());
MsHTTPConfig httpConfig = new MsHTTPConfig();
httpConfig.setConnectTimeout(60000L);
httpConfig.setResponseTimeout(60000L);
request.setOtherConfig(httpConfig);
request.setAuthConfig(new NoAuth());
Body body = new Body();
body.setBinaryBody(new BinaryBody());
body.setFormDataBody(new FormDataBody());
body.setXmlBody(new XmlBody());
body.setRawBody(new RawBody());
body.setNoneBody(new NoneBody());
body.setJsonBody(new JsonBody());
body.setWwwFormBody(new WWWFormBody());
body.setNoneBody(new NoneBody());
body.setBodyType(Body.BodyType.NONE.name());
request.setBody(body);
return request;
}
protected String formatPath(String url) {
try {
URI urlObject = new URI(url);
return StringUtils.isBlank(urlObject.getPath()) ? url : urlObject.getPath();
} catch (Exception ex) {
//只需要返回?前的路径
return url.split("\\?")[0];
}
}
}