Search in sources :

Example 81 with Authenticator

use of java.net.Authenticator in project eclipse.jdt.ls by eclipse.

the class JavaLanguageServerPlugin method configureProxy.

private void configureProxy() {
    if (Boolean.getBoolean("jdt.ls.disableProxies")) {
        ProxySelector.setActiveProvider(DIRECT);
        return;
    }
    // It seems there is no way to set a proxy provider type (manual, native or
    // direct) without the Eclipse UI.
    // The org.eclipse.core.net plugin removes the http., https. system properties
    // when setting its preferences and a proxy provider isn't manual.
    // We save these parameters and set them after starting the
    // org.eclipse.core.net plugin.
    String httpHost = System.getProperty(HTTP_PROXY_HOST);
    String httpPort = System.getProperty(HTTP_PROXY_PORT);
    String httpUser = System.getProperty(HTTP_PROXY_USER);
    String httpPassword = System.getProperty(HTTP_PROXY_PASSWORD);
    String httpsHost = System.getProperty(HTTPS_PROXY_HOST);
    String httpsPort = System.getProperty(HTTPS_PROXY_PORT);
    String httpsUser = System.getProperty(HTTPS_PROXY_USER);
    String httpsPassword = System.getProperty(HTTPS_PROXY_PASSWORD);
    String httpsNonProxyHosts = System.getProperty(HTTPS_NON_PROXY_HOSTS);
    String httpNonProxyHosts = System.getProperty(HTTP_NON_PROXY_HOSTS);
    if (StringUtils.isNotBlank(httpUser) || StringUtils.isNotBlank(httpsUser)) {
        try {
            Platform.getBundle("org.eclipse.core.net").start(Bundle.START_TRANSIENT);
        } catch (BundleException e) {
            logException(e.getMessage(), e);
        }
        if (StringUtils.isNotBlank(httpUser) && StringUtils.isNotBlank(httpPassword)) {
            Authenticator.setDefault(new Authenticator() {

                @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(httpUser, httpPassword.toCharArray());
                }
            });
        }
        IProxyService proxyService = getProxyService();
        if (proxyService != null) {
            ProxySelector.setActiveProvider(MANUAL);
            IProxyData[] proxies = proxyService.getProxyData();
            for (IProxyData proxy : proxies) {
                if ("HTTP".equals(proxy.getType())) {
                    proxy.setHost(httpHost);
                    proxy.setPort(httpPort == null ? -1 : Integer.valueOf(httpPort));
                    proxy.setPassword(httpPassword);
                    proxy.setUserid(httpUser);
                }
                if ("HTTPS".equals(proxy.getType())) {
                    proxy.setHost(httpsHost);
                    proxy.setPort(httpsPort == null ? -1 : Integer.valueOf(httpsPort));
                    proxy.setPassword(httpsPassword);
                    proxy.setUserid(httpsUser);
                }
            }
            try {
                proxyService.setProxyData(proxies);
                if (httpHost != null) {
                    System.setProperty(HTTP_PROXY_HOST, httpHost);
                }
                if (httpPort != null) {
                    System.setProperty(HTTP_PROXY_PORT, httpPort);
                }
                if (httpUser != null) {
                    System.setProperty(HTTP_PROXY_USER, httpUser);
                }
                if (httpPassword != null) {
                    System.setProperty(HTTP_PROXY_PASSWORD, httpPassword);
                }
                if (httpsHost != null) {
                    System.setProperty(HTTPS_PROXY_HOST, httpsHost);
                }
                if (httpsPort != null) {
                    System.setProperty(HTTPS_PROXY_PORT, httpsPort);
                }
                if (httpsUser != null) {
                    System.setProperty(HTTPS_PROXY_USER, httpsUser);
                }
                if (httpsPassword != null) {
                    System.setProperty(HTTPS_PROXY_PASSWORD, httpsPassword);
                }
                if (httpsNonProxyHosts != null) {
                    System.setProperty(HTTPS_NON_PROXY_HOSTS, httpsNonProxyHosts);
                }
                if (httpNonProxyHosts != null) {
                    System.setProperty(HTTP_NON_PROXY_HOSTS, httpNonProxyHosts);
                }
            } catch (CoreException e) {
                logException(e.getMessage(), e);
            }
        }
    } else {
        ProxySelector.setActiveProvider(NATIVE);
        return;
    }
}
Also used : IProxyData(org.eclipse.core.net.proxy.IProxyData) CoreException(org.eclipse.core.runtime.CoreException) BundleException(org.osgi.framework.BundleException) Authenticator(java.net.Authenticator) IProxyService(org.eclipse.core.net.proxy.IProxyService) PasswordAuthentication(java.net.PasswordAuthentication)

Example 82 with Authenticator

use of java.net.Authenticator in project java by wavefrontHQ.

the class APIContainer method configureHttpProxy.

private void configureHttpProxy() {
    if (proxyConfig.getProxyHost() != null) {
        System.setProperty("http.proxyHost", proxyConfig.getProxyHost());
        System.setProperty("https.proxyHost", proxyConfig.getProxyHost());
        System.setProperty("http.proxyPort", String.valueOf(proxyConfig.getProxyPort()));
        System.setProperty("https.proxyPort", String.valueOf(proxyConfig.getProxyPort()));
    }
    if (proxyConfig.getProxyUser() != null && proxyConfig.getProxyPassword() != null) {
        Authenticator.setDefault(new Authenticator() {

            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                if (getRequestorType() == RequestorType.PROXY) {
                    return new PasswordAuthentication(proxyConfig.getProxyUser(), proxyConfig.getProxyPassword().toCharArray());
                } else {
                    return null;
                }
            }
        });
    }
}
Also used : Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 83 with Authenticator

use of java.net.Authenticator in project incubator-gobblin by apache.

the class AbstractJobLauncher method setDefaultAuthenticator.

/**
 * Set default {@link Authenticator} to the one provided in {@link ConfigurationKeys#DEFAULT_AUTHENTICATOR_CLASS},
 * calling the constructor using the provided {@link Properties}
 */
public static void setDefaultAuthenticator(Properties properties) {
    String authenticatorClass = properties.getProperty(ConfigurationKeys.DEFAULT_AUTHENTICATOR_CLASS);
    if (!Strings.isNullOrEmpty(authenticatorClass)) {
        Authenticator authenticator = GobblinConstructorUtils.invokeConstructor(Authenticator.class, authenticatorClass, properties);
        Authenticator.setDefault(authenticator);
    }
}
Also used : Authenticator(java.net.Authenticator)

Aggregations

Authenticator (java.net.Authenticator)83 PasswordAuthentication (java.net.PasswordAuthentication)52 URL (java.net.URL)18 Proxy (java.net.Proxy)14 InetSocketAddress (java.net.InetSocketAddress)12 HttpClient (java.net.http.HttpClient)11 Field (java.lang.reflect.Field)10 HttpURLConnection (java.net.HttpURLConnection)10 IOException (java.io.IOException)8 Test (org.junit.Test)7 Method (java.lang.reflect.Method)6 File (java.io.File)5 SocketAddress (java.net.SocketAddress)5 InputStream (java.io.InputStream)4 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ServerSocket (java.net.ServerSocket)3 URISyntaxException (java.net.URISyntaxException)3 ArrayList (java.util.ArrayList)3 Content (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content)3