Search in sources :

Example 1 with IdeaWideProxySelector

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();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) IdeaWideProxySelector(com.intellij.util.net.IdeaWideProxySelector) HttpConfigurable(com.intellij.util.net.HttpConfigurable) IdeaWideAuthenticator(com.intellij.util.net.IdeaWideAuthenticator) Authenticator(okhttp3.Authenticator) IdeaWideAuthenticator(com.intellij.util.net.IdeaWideAuthenticator)

Example 2 with IdeaWideProxySelector

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;
}
Also used : HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) IdeaWideAuthenticator(com.intellij.util.net.IdeaWideAuthenticator) Proxy(java.net.Proxy) IdeaWideProxySelector(com.intellij.util.net.IdeaWideProxySelector) HttpConfigurable(com.intellij.util.net.HttpConfigurable) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) PasswordAuthentication(java.net.PasswordAuthentication) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

HttpConfigurable (com.intellij.util.net.HttpConfigurable)2 IdeaWideAuthenticator (com.intellij.util.net.IdeaWideAuthenticator)2 IdeaWideProxySelector (com.intellij.util.net.IdeaWideProxySelector)2 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 PasswordAuthentication (java.net.PasswordAuthentication)1 Proxy (java.net.Proxy)1 SocketAddress (java.net.SocketAddress)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 Authenticator (okhttp3.Authenticator)1 OkHttpClient (okhttp3.OkHttpClient)1 NotNull (org.jetbrains.annotations.NotNull)1