zhangshenghang commented on code in PR #9968:
URL: https://github.com/apache/seatunnel/pull/9968#discussion_r2498959597
##########
seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/rest/service/BaseLogService.java:
##########
@@ -47,30 +53,87 @@ public String getLogPath() {
}
protected String sendGet(String urlString) {
+ HttpURLConnection connection = null;
try {
- HttpURLConnection connection = (HttpURLConnection) new
URL(urlString).openConnection();
+ connection = (HttpURLConnection) new
URL(urlString).openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
+ int code = connection.getResponseCode();
if (connection.getResponseCode() == 200) {
- try (InputStream is = connection.getInputStream();
- ByteArrayOutputStream baos = new
ByteArrayOutputStream()) {
- byte[] buffer = new byte[1024];
- int len;
- while ((len = is.read(buffer)) != -1) {
- baos.write(buffer, 0, len);
- }
- return baos.toString();
- }
+ return readAll(connection.getInputStream());
}
+ log.warn("GET {} -> HTTP {}", urlString, code);
} catch (IOException e) {
- log.error("Send get Fail.{}", ExceptionUtils.getMessage(e));
+ log.error("Send GET failed: url={}, err={}", urlString,
ExceptionUtils.getMessage(e));
+ } finally {
+ if (connection != null) {
+ connection.disconnect();
+ }
}
return null;
}
+ /**
+ * Send GET request with Basic Auth
+ *
+ * @param urlString url
+ * @param user name
+ * @param pass password
+ * @return log
+ */
+ protected String sendGet(String urlString, String user, String pass) {
+ HttpURLConnection connection = null;
+ try {
+ connection = (HttpURLConnection) new
URL(urlString).openConnection();
+ connection.setRequestMethod("GET");
+ connection.setConnectTimeout(5000);
+ connection.setReadTimeout(5000);
+
+ // Basic Auth
+ if (user != null && pass != null) {
+
+ Encoder encoder = Base64.getEncoder();
+ String auth = user + ":" + pass;
+ String token =
encoder.encodeToString(auth.getBytes(StandardCharsets.UTF_8));
+
+ connection.setRequestProperty(AUTHORIZATION_HEADER,
BASIC_PREFIX + token);
+ }
+
+ connection.connect();
+
+ int code = connection.getResponseCode();
+ if (connection.getResponseCode() == 200) {
+ return readAll(connection.getInputStream());
+ }
+ log.warn("GET {} -> HTTP {}", urlString, code);
+ } catch (IOException e) {
+ log.error("Send GET failed: url={}, err={}", urlString,
ExceptionUtils.getMessage(e));
+ } finally {
+ if (connection != null) {
+ connection.disconnect();
+ }
+ }
+ return null;
+ }
Review Comment:
It seems that a lot of their code is duplicated; let's merge it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]