Skip to content

Commit

Permalink
Autoclose resources
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Jul 11, 2024
1 parent e4b3901 commit 5d36835
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
28 changes: 16 additions & 12 deletions src/main/java/mpo/dayon/assistant/gui/Assistant.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,14 @@ public void actionPerformed(ActionEvent ev) {
}

private void resolvePublicIp() throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(WHATSMYIP_SERVER_URL))
.timeout(Duration.ofSeconds(5))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
HttpResponse<String> response;
try (HttpClient client = HttpClient.newHttpClient()) {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(WHATSMYIP_SERVER_URL))
.timeout(Duration.ofSeconds(5))
.build();
response = client.send(request, HttpResponse.BodyHandlers.ofString());
}
publicIp = response.body();
}
};
Expand Down Expand Up @@ -574,12 +576,14 @@ public void actionPerformed(ActionEvent ev) {
}

private void requestToken() throws IOException, InterruptedException {
HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(format(tokenServerUrl, networkConfiguration.getPort())))
.timeout(Duration.ofSeconds(5))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
HttpResponse<String> response;
try (HttpClient client = HttpClient.newBuilder().build()) {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(format(tokenServerUrl, networkConfiguration.getPort())))
.timeout(Duration.ofSeconds(5))
.build();
response = client.send(request, HttpResponse.BodyHandlers.ofString());
}
token = response.body().trim();
}
};
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/mpo/dayon/common/version/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@ boolean isLatestVersion() {

public String getLatestRelease() {
if (latestVersion == null) {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(RELEASE_LOCATION + "latest"))
.timeout(Duration.ofSeconds(5))
.build();
try {
try (HttpClient client = HttpClient.newHttpClient()) {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(RELEASE_LOCATION + "latest"))
.timeout(Duration.ofSeconds(5))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
String latestLocation = response.headers().firstValue("Location").orElse(null);
if (latestLocation != null) {
Expand Down

0 comments on commit 5d36835

Please sign in to comment.