Skip to content

Commit

Permalink
refactor: use try with resource to build http client
Browse files Browse the repository at this point in the history
  • Loading branch information
damingerdai committed Feb 13, 2024
1 parent 83c1078 commit a5faee0
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/main/java/org/daming/jobs/service/impl/RepoServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.time.Duration;

@Service
Expand All @@ -24,31 +25,32 @@ public String listCommits() throws IOException, InterruptedException, NoSuchAlgo
var url = "https://api.github.com/repos/damingerdai/jobs/commits";
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, getTrustAllCerts(), new SecureRandom());
var client = HttpClient
try (var client = HttpClient
.newBuilder()
.sslContext(sslContext)
.build();;
var request = HttpRequest.newBuilder()
.uri(URI.create(url))
.timeout(Duration.ofMinutes(1))
.GET()
.timeout(Duration.ofMinutes(1))
.build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.body();
.build()){
var request = HttpRequest.newBuilder()
.uri(URI.create(url))
.timeout(Duration.ofMinutes(1))
.GET()
.timeout(Duration.ofMinutes(1))
.build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.body();
}
}

private TrustManager[] getTrustAllCerts() {
return new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
X509Certificate[] certs, String authType) {
}
}
};
Expand Down

0 comments on commit a5faee0

Please sign in to comment.