@Override
public CredentialsProvider extendCredentialProvider(HttpClientBuilder httpClientBuilder,
CredentialsProvider credentialsProvider,
GerritAuthData authData) {
HttpConfigurable proxySettings = HttpConfigurable.getInstance();
IdeaWideProxySelector ideaWideProxySelector = new IdeaWideProxySelector(proxySettings);
// This will always return at least one proxy, which can be the "NO_PROXY" instance.
List<Proxy> proxies = ideaWideProxySelector.select(URI.create(authData.getHost()));
// Find the first real proxy with an address type we support.
for (Proxy proxy : proxies) {
SocketAddress socketAddress = proxy.address();
if (HttpConfigurable.isRealProxy(proxy) && socketAddress instanceof InetSocketAddress) {
InetSocketAddress address = (InetSocketAddress) socketAddress;
HttpHost proxyHttpHost = new HttpHost(address.getHostName(), address.getPort());
httpClientBuilder.setProxy(proxyHttpHost);
// Here we use the single username/password that we got from IDEA's settings. It feels kinda strange
// to use these credential but it's probably what the user expects.
if (proxySettings.PROXY_AUTHENTICATION) {
AuthScope authScope = new AuthScope(proxySettings.PROXY_HOST, proxySettings.PROXY_PORT);
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(proxySettings.PROXY_LOGIN, proxySettings.getPlainProxyPassword());
credentialsProvider.setCredentials(authScope, credentials);
break;
}
}
}