[FEATURE] Add newHttpClientBuilder()

This commit is contained in:
Gravita 2024-03-08 14:37:15 +07:00
parent e28c9773fc
commit d686d9a388

View file

@ -6,6 +6,7 @@
import pro.gravit.utils.helper.LogHelper;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.net.URI;
@ -55,6 +56,24 @@ public static ThreadFactory getDaemonThreadFactory(String name) {
};
}
public static HttpClient.Builder newHttpClientBuilder() {
try {
if(isCertificatePinning) {
return HttpClient.newBuilder()
.sslContext(makeSSLContext())
.version(isNoHttp2 ? HttpClient.Version.HTTP_1_1 : HttpClient.Version.HTTP_2)
.followRedirects(HttpClient.Redirect.NORMAL);
} else {
return HttpClient.newBuilder()
.version(isNoHttp2 ? HttpClient.Version.HTTP_1_1 : HttpClient.Version.HTTP_2)
.followRedirects(HttpClient.Redirect.NORMAL);
}
} catch (NoSuchAlgorithmException | CertificateException | KeyStoreException | IOException |
KeyManagementException e) {
throw new RuntimeException(e);
}
}
public static SSLSocketFactory makeSSLSocketFactory() throws NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, KeyManagementException {
if (sslSocketFactory != null) return sslSocketFactory;
SSLContext sslContext = makeSSLContext();
@ -110,17 +129,8 @@ public static Downloader newDownloader(ExecutorService executor) {
if (executor == null) {
throw new NullPointerException();
}
HttpClient.Builder builder = HttpClient.newBuilder()
.version(isNoHttp2 ? HttpClient.Version.HTTP_1_1 : HttpClient.Version.HTTP_2)
.followRedirects(HttpClient.Redirect.NORMAL)
HttpClient.Builder builder = newHttpClientBuilder()
.executor(executor);
if (isCertificatePinning) {
try {
builder.sslContext(makeSSLContext());
} catch (Exception e) {
throw new SecurityException(e);
}
}
HttpClient client = builder.build();
return new Downloader(client, executor);
}