diff --git a/src/main/java/org/codinjutsu/tools/jenkins/security/DefaultSecurityClient.java b/src/main/java/org/codinjutsu/tools/jenkins/security/DefaultSecurityClient.java index 531ebeae..646148b6 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/security/DefaultSecurityClient.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/security/DefaultSecurityClient.java @@ -17,6 +17,7 @@ package org.codinjutsu.tools.jenkins.security; import com.intellij.notification.NotificationType; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.util.net.IdeHttpClientHelpers; import com.intellij.util.net.ssl.CertificateManager; @@ -102,8 +103,9 @@ class DefaultSecurityClient implements SecurityClient { if (useProxySettings) { this.configCreator = url -> { final var configForUrl = RequestConfig.copy(defaultRequestConfig); - final var useSocks = JenkinsConnectionSocketFactory.INSTANCE.prepareContext(url, sslContext, - getHttpClientContext(), dnsResolver); + final var useSocks = ApplicationManager.getApplication() + .getService(JenkinsConnectionSocketFactory.class) + .prepareContext(url, sslContext, getHttpClientContext(), dnsResolver); if (!useSocks) { IdeHttpClientHelpers.ApacheHttpClient4.setProxyForUrlIfEnabled(configForUrl, url); IdeHttpClientHelpers.ApacheHttpClient4.setProxyCredentialsForUrlIfEnabled(credentialsProvider, url); diff --git a/src/main/java/org/codinjutsu/tools/jenkins/security/JenkinsConnectionSocketFactory.java b/src/main/java/org/codinjutsu/tools/jenkins/security/JenkinsConnectionSocketFactory.java index f50e05ed..7be5df59 100644 --- a/src/main/java/org/codinjutsu/tools/jenkins/security/JenkinsConnectionSocketFactory.java +++ b/src/main/java/org/codinjutsu/tools/jenkins/security/JenkinsConnectionSocketFactory.java @@ -1,5 +1,6 @@ package org.codinjutsu.tools.jenkins.security; +import com.intellij.openapi.components.Service; import com.intellij.openapi.vfs.VfsUtil; import com.intellij.util.net.HttpConfigurable; import com.intellij.util.net.IdeaWideProxySelector; @@ -26,14 +27,13 @@ import java.util.Collections; import java.util.List; -public class JenkinsConnectionSocketFactory { - @NotNull - public static final JenkinsConnectionSocketFactory INSTANCE = new JenkinsConnectionSocketFactory(); +@Service +public final class JenkinsConnectionSocketFactory { - public @Nullable Registry getRegistry(@NotNull String url, @NotNull SSLContext sslContext) { + public @NotNull Registry getRegistry(@NotNull String url, @NotNull SSLContext sslContext) { final Proxy proxy = getProxy(url); if(proxy.type() == Proxy.Type.SOCKS) { - return createSocksRegistry(toUri(url), proxy, sslContext); + return createSocksRegistry(proxy, sslContext); } return createDefaultRegistry(sslContext); } @@ -71,12 +71,11 @@ public boolean prepareContext(@NotNull String url, return false; } - private Registry createSocksRegistry(@Nullable URI uri, - @NotNull Proxy proxy, - @NotNull SSLContext sslContext) { + private Registry createSocksRegistry(@NotNull Proxy proxy, + @NotNull SSLContext sslContext) { return RegistryBuilder.create() .register("http", new SocksPlainConnectionSocketFactory(proxy)) - .register("https", new SocksSSLConnectionSocketFactory(uri != null ? uri.getHost() : null, sslContext, proxy)) + .register("https", new SocksSSLConnectionSocketFactory(sslContext, proxy)) .build(); } @@ -110,16 +109,13 @@ public SocksPlainConnectionSocketFactory(@NotNull Proxy proxy) { this.proxy = proxy; } - @NotNull - public Proxy getProxy() { - return this.proxy; - } - + @Override @NotNull public Socket createSocket(@Nullable HttpContext context) { return new Socket(this.proxy); } + @Override @NotNull public Socket connectSocket(int connectTimeout, @Nullable Socket socket, @@ -133,27 +129,21 @@ public Socket connectSocket(int connectTimeout, } private static final class SocksSSLConnectionSocketFactory extends SSLConnectionSocketFactory { - @Nullable - private final String host; @NotNull private final Proxy proxy; - public SocksSSLConnectionSocketFactory(@Nullable String host, @NotNull SSLContext sslContext, @NotNull Proxy proxy) { + public SocksSSLConnectionSocketFactory(@NotNull SSLContext sslContext, @NotNull Proxy proxy) { super(sslContext); - this.host = host; this.proxy = proxy; } - @NotNull - public Proxy getProxy() { - return this.proxy; - } - + @Override @NotNull public Socket createSocket(@NotNull HttpContext context) { return new Socket(this.proxy); } + @Override @NotNull public Socket connectSocket(int connectTimeout, @Nullable Socket socket,