use of com.intellij.util.net.IdeaWideProxySelector in project intellij-common by redhat-developer.
the class NetworkUtils method getClient.
public static OkHttpClient getClient() {
final HttpConfigurable httpConfigurable = HttpConfigurable.getInstance();
final IdeaWideProxySelector ideaWideProxySelector = new IdeaWideProxySelector(httpConfigurable);
final IdeaWideAuthenticator ideaWideAuthenticator = new IdeaWideAuthenticator(httpConfigurable);
final Authenticator proxyAuthenticator = getProxyAuthenticator(ideaWideAuthenticator);
final OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.proxySelector(ideaWideProxySelector).proxyAuthenticator(proxyAuthenticator);
return builder.build();
}
use of com.intellij.util.net.IdeaWideProxySelector in project intellij-common by redhat-developer.
the class NetworkUtils method buildEnvironmentVariables.
@NotNull
public static Map<String, String> buildEnvironmentVariables(String url) throws URISyntaxException {
final int FIRST = 0;
final String HTTP_PROXY = "HTTP_PROXY";
final String HTTPS_PROXY = "HTTPS_PROXY";
final String ALL_PROXY = "ALL_PROXY";
final Set<String> proxyEnvironmentVariables = Sets.newHashSet(HTTP_PROXY, HTTPS_PROXY, ALL_PROXY);
final Map<String, String> environmentVariables = new HashMap<>(6);
final HttpConfigurable httpConfigurable = HttpConfigurable.getInstance();
final IdeaWideProxySelector ideaWideProxySelector = new IdeaWideProxySelector(httpConfigurable);
final URI uri = new URI(url);
final List<Proxy> proxies = ideaWideProxySelector.select(uri);
if (!proxies.isEmpty()) {
final Proxy proxy = proxies.get(FIRST);
final Proxy.Type type = proxy.type();
switch(type) {
case HTTP:
case SOCKS:
final SocketAddress address = proxy.address();
if (address instanceof InetSocketAddress) {
final InetSocketAddress socketAddress = (InetSocketAddress) address;
final InetAddress inetAddress = socketAddress.getAddress();
final int port = socketAddress.getPort();
final IdeaWideAuthenticator ideaWideAuthenticator = new IdeaWideAuthenticator(httpConfigurable);
final Optional<PasswordAuthentication> optionalPasswordAuthentication = Optional.ofNullable(ideaWideAuthenticator.getPasswordAuthentication());
String userName = null;
String password = null;
if (optionalPasswordAuthentication.isPresent()) {
final PasswordAuthentication passwordAuthentication = optionalPasswordAuthentication.get();
userName = passwordAuthentication.getUserName();
password = Arrays.toString(passwordAuthentication.getPassword());
}
String finalUserName = userName;
String finalPassword = password;
proxyEnvironmentVariables.forEach(envVarName -> {
final String envVarValue = buildHttpProxy(type, finalUserName, finalPassword, inetAddress, port);
environmentVariables.put(envVarName, envVarValue);
environmentVariables.put(envVarName.toLowerCase(), envVarValue);
});
}
break;
}
}
return environmentVariables;
}
Aggregations