-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHttpClientSQLE.java
More file actions
281 lines (232 loc) · 9.54 KB
/
HttpClientSQLE.java
File metadata and controls
281 lines (232 loc) · 9.54 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package sqle.util;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.DataOutputStream;
import java.nio.charset.StandardCharsets;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.google.gson.*;
import sqle.config.*;
public class HttpClientSQLE {
private static final String loginPath = "/v1/dms/sessions";
private static final String projectPath = "/v2/dms/projects";
private static final String driversPath = "/sqle/v1/configurations/drivers";
private static final String dataSourcePath = "/v2/dms/projects/%s/db_services";
private static final String schemaPath = "/sqle/v1/projects/%s/instances/%s/schemas";
private static final String auditPath = "/sqle/v2/audit_files";
private SQLESettings settings;
private String uriHead;
private String token;
public HttpClientSQLE() {
settings = SQLESettings.getInstance();
}
private void DetermineHaveToken() throws Exception {
if (settings.getLoginType().equals(SQLESettings.PasswordLogin)) {
if (token == null || token.isEmpty()) {
Login();
}
} else {
token = settings.getAccessToekn();
}
}
private String GetAddr() {
String uriHead;
boolean EnableHttps = settings.isEnableHttps();
if (EnableHttps) {
uriHead = "https://" + settings.getSQLEAddr();
} else {
uriHead = "http://" + settings.getSQLEAddr();
}
return uriHead;
}
public void Login() throws Exception {
Map<String, String> req = new HashMap<>();
uriHead = GetAddr();
req.put("username", settings.getUserName());
req.put("password", settings.getPassword());
Gson gson = new Gson();
String reqJson = gson.toJson(req);
String formatStr = String.format("{\"session\":%s}", reqJson);
JsonObject resp = sendPOST(uriHead + loginPath, formatStr);
if (resp.get("code").getAsInt() != 0) {
throw new Exception("login failed: " + resp.get("message").getAsString());
}
token = resp.get("data").getAsJsonObject().get("token").getAsString();
this.settings.setToken(token);
}
public Map<String, String> GetProjectList() throws Exception {
DetermineHaveToken();
String reqPath = String.format("%s?page_index=%s&page_size=%s", projectPath, "1", "999999");
JsonObject resp = sendGet(GetAddr() + reqPath);
if (resp.get("code").getAsInt() != 0) {
throw new Exception("get data source list failed: " + resp.get("message").getAsString());
}
JsonArray projectsArray = resp.getAsJsonArray("data");
Map<String, String> projectMap = new HashMap<>();
try {
for (JsonElement element : projectsArray) {
if (element.isJsonObject()) {
JsonObject projectObject = element.getAsJsonObject();
ListProject project = parseJsonToProject(projectObject);
projectMap.put(project.getName(), project.getProjectUid());
}
}
} catch (Exception e) {
e.printStackTrace();
// 处理其他异常
}
settings.setProjectUidMap(projectMap);
return projectMap;
}
private static ListProject parseJsonToProject(JsonObject json) throws Exception {
String uid = json.get("uid").getAsString();
String name = json.get("name").getAsString();
return new ListProject(uid, name);
}
public ArrayList<String> GetDBTypes() throws Exception {
DetermineHaveToken();
JsonObject resp = sendGet(GetAddr() + driversPath);
if (resp.get("code").getAsInt() != 0) {
throw new Exception("get db type failed: " + resp.get("message").getAsString());
}
JsonArray array = resp.get("data").getAsJsonObject().get("driver_name_list").getAsJsonArray();
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < array.size(); i++) {
list.add(array.get(i).getAsString());
}
return list;
}
public ArrayList<String> GetDataSourceNameList(String projectName, String dbType) throws Exception {
DetermineHaveToken();
String projectID = settings.getProjectUidMap().get(projectName);
String dataSourcePath = String.format(HttpClientSQLE.dataSourcePath, projectID);
String encodedDbType = URLEncoder.encode(dbType, "UTF-8");
String reqPath = String.format("%s?filter_by_db_type=%s&page_index=%s&page_size=%s", dataSourcePath,
encodedDbType, "1", "999999");
JsonObject resp = sendGet(GetAddr() + reqPath);
if (resp.get("code").getAsInt() != 0) {
throw new Exception("get data source list failed: " + resp.get("message").getAsString());
}
JsonArray projectsArray = resp.getAsJsonArray("data");
ArrayList<String> list = new ArrayList<>();
try {
for (JsonElement element : projectsArray) {
if (element.isJsonObject()) {
JsonObject projectObject = element.getAsJsonObject();
SQLEDataSourceNameListResult dataSource = parseJsonToDataSource(projectObject);
list.add(dataSource.getName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
private static SQLEDataSourceNameListResult parseJsonToDataSource(JsonObject json) throws Exception {
String name = json.getAsJsonPrimitive("name").getAsString();
return new SQLEDataSourceNameListResult(name);
}
public ArrayList<String> GetSchemaList(String projectName, String dataSourceName) throws Exception {
DetermineHaveToken();
String reqPath = String.format(HttpClientSQLE.schemaPath, projectName, dataSourceName);
JsonObject resp = sendGet(GetAddr() + reqPath);
if (resp.get("code").getAsInt() != 0) {
throw new Exception("get schema name list: " + resp.get("message").getAsString());
}
JsonArray array = resp.get("data").getAsJsonObject().get("schema_name_list").getAsJsonArray();
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < array.size(); i++) {
list.add(array.get(i).getAsString());
}
return list;
}
public SQLEAuditResult AuditSQL(String[] contents, SQLESettings.AuditType type) throws Exception {
DetermineHaveToken();
Map<String, Object> req = new HashMap<>();
req.put("instance_type", settings.getDBType());
req.put("file_contents", contents);
req.put("project_name", settings.getProjectName());
req.put("instance_name", settings.getDataSourceName());
req.put("schema_name", settings.getSchemaName());
switch (type) {
case SQL:
req.put("sql_type", "sql");
break;
case MyBatis:
req.put("sql_type", "mybatis");
break;
}
Gson newGson = new Gson();
String reqJson = newGson.toJson(req);
JsonObject resp = sendPOST(GetAddr() + auditPath, reqJson);
if (resp.get("code").getAsInt() != 0) {
throw new Exception("audit failed: " + resp.get("message").getAsString());
}
JsonObject data = resp.get("data").getAsJsonObject();
return newGson.fromJson(data, new SQLEAuditResult().getClass());
}
private JsonObject sendGet(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", "Bearer " + token);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
int responseCode = conn.getResponseCode();
String respStr = response.toString();
if (responseCode == 401) {
if (settings.getLoginType().equals(SQLESettings.PasswordLogin)) {
Login();
return sendGet(path);
} else {
throw new Exception("response code != 200, message: " + respStr);
}
}
if (responseCode != 200) {
throw new Exception("response code != 200, message: " + respStr);
}
return new JsonParser().parse(respStr).getAsJsonObject();
}
private JsonObject sendPOST(String path, String request) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Bearer " + token);
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.write(request.getBytes(StandardCharsets.UTF_8));
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
int responseCode = conn.getResponseCode();
String respStr = response.toString();
if (responseCode == 401) {
if (settings.getLoginType().equals(SQLESettings.PasswordLogin)) {
Login();
return sendGet(path);
} else {
throw new Exception("response code != 200, message: " + respStr);
}
}
if (responseCode != 200) {
throw new Exception("response code != 200, message: " + respStr);
}
return new JsonParser().parse(respStr).getAsJsonObject();
}
}