Copilot commented on code in PR #9573:
URL: https://github.com/apache/seatunnel/pull/9573#discussion_r2218134517
##########
seatunnel-connectors-v2/connector-http/connector-http-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/http/client/HttpClientProvider.java:
##########
@@ -528,4 +532,24 @@ public void close() throws IOException {
httpClient.close();
}
}
+
+ /**
+ * Check if the url is available by sending a simple HEAD request.
+ *
+ * @param httpParameter the http parameter
+ */
+ @SneakyThrows
+ public void checkUrlAccessibility(HttpParameter httpParameter) {
+ HttpHead request = new HttpHead(httpParameter.getUrl());
+ Map<String, String> headers = httpParameter.getHeaders();
+ if (Objects.nonNull(headers)) {
+ headers.forEach(request::addHeader);
+ }
+ CloseableHttpResponse response = httpClient.execute(request);
+ int statusCode = response.getStatusLine().getStatusCode();
+ if (!(statusCode >= HttpStatus.SC_OK && statusCode <
HttpStatus.SC_MULTIPLE_CHOICES)) {
+ log.warn("Failed to connect to host: {}", httpParameter.getUrl());
+ throw new ConnectException("URL is not accessible. status code: "
+ statusCode);
Review Comment:
Using @SneakyThrows hides exception handling and makes it unclear what
exceptions this method can throw. Consider explicit exception declaration in
the method signature or proper try-catch handling.
```suggestion
public void checkUrlAccessibility(HttpParameter httpParameter) throws
IOException, ConnectException {
HttpHead request = new HttpHead(httpParameter.getUrl());
Map<String, String> headers = httpParameter.getHeaders();
if (Objects.nonNull(headers)) {
headers.forEach(request::addHeader);
}
try (CloseableHttpResponse response = httpClient.execute(request)) {
int statusCode = response.getStatusLine().getStatusCode();
if (!(statusCode >= HttpStatus.SC_OK && statusCode <
HttpStatus.SC_MULTIPLE_CHOICES)) {
log.warn("Failed to connect to host: {}",
httpParameter.getUrl());
throw new ConnectException("URL is not accessible. status
code: " + statusCode);
}
} catch (IOException e) {
log.error("An error occurred while checking URL accessibility:
{}", ExceptionUtils.getStackTrace(e));
throw e;
```
##########
seatunnel-connectors-v2/connector-http/connector-http-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/http/client/HttpClientProvider.java:
##########
@@ -528,4 +532,24 @@ public void close() throws IOException {
httpClient.close();
}
}
+
+ /**
+ * Check if the url is available by sending a simple HEAD request.
+ *
+ * @param httpParameter the http parameter
+ */
+ @SneakyThrows
+ public void checkUrlAccessibility(HttpParameter httpParameter) {
+ HttpHead request = new HttpHead(httpParameter.getUrl());
+ Map<String, String> headers = httpParameter.getHeaders();
+ if (Objects.nonNull(headers)) {
+ headers.forEach(request::addHeader);
+ }
+ CloseableHttpResponse response = httpClient.execute(request);
+ int statusCode = response.getStatusLine().getStatusCode();
+ if (!(statusCode >= HttpStatus.SC_OK && statusCode <
HttpStatus.SC_MULTIPLE_CHOICES)) {
+ log.warn("Failed to connect to host: {}", httpParameter.getUrl());
+ throw new ConnectException("URL is not accessible. status code: "
+ statusCode);
Review Comment:
The HTTP response is not being closed, which will cause resource leaks. Use
try-with-resources or explicitly close the response in a finally block.
```suggestion
try (CloseableHttpResponse response = httpClient.execute(request)) {
int statusCode = response.getStatusLine().getStatusCode();
if (!(statusCode >= HttpStatus.SC_OK && statusCode <
HttpStatus.SC_MULTIPLE_CHOICES)) {
log.warn("Failed to connect to host: {}",
httpParameter.getUrl());
throw new ConnectException("URL is not accessible. status
code: " + statusCode);
}
```
--
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]