Search in sources :

Example 16 with IProxyService

use of org.eclipse.core.net.proxy.IProxyService in project liferay-ide by liferay.

the class RemoteConnection method _getHttpClient.

private HttpClient _getHttpClient() {
    if (_httpClient != null) {
        return _httpClient;
    }
    DefaultHttpClient newDefaultHttpClient = null;
    if ((getUsername() != null) || (getPassword() != null)) {
        try {
            IProxyService proxyService = LiferayCore.getProxyService();
            URI uri = new URI("http://" + getHost() + ":" + getHttpPort());
            IProxyData[] proxyDataForHost = proxyService.select(uri);
            for (IProxyData data : proxyDataForHost) {
                if ((data.getHost() == null) || (data.getPort() == 0)) {
                    continue;
                }
                SchemeRegistry schemeRegistry = new SchemeRegistry();
                schemeRegistry.register(new Scheme("http", data.getPort(), PlainSocketFactory.getSocketFactory()));
                PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
                cm.setMaxTotal(200);
                cm.setDefaultMaxPerRoute(20);
                DefaultHttpClient newHttpClient = new DefaultHttpClient(cm);
                HttpHost proxy = new HttpHost(data.getHost(), data.getPort());
                newHttpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
                newDefaultHttpClient = newHttpClient;
                break;
            }
            if (newDefaultHttpClient == null) {
                uri = new URI("SOCKS://" + getHost() + ":" + getHttpPort());
                proxyDataForHost = proxyService.select(uri);
                for (IProxyData data : proxyDataForHost) {
                    if (data.getHost() == null) {
                        continue;
                    }
                    DefaultHttpClient newHttpClient = new DefaultHttpClient();
                    newHttpClient.getParams().setParameter("socks.host", data.getHost());
                    newHttpClient.getParams().setParameter("socks.port", data.getPort());
                    SchemeRegistry registry = newHttpClient.getConnectionManager().getSchemeRegistry();
                    Scheme scheme = new Scheme("socks", data.getPort(), PlainSocketFactory.getSocketFactory());
                    registry.register(scheme);
                    newDefaultHttpClient = newHttpClient;
                    break;
                }
            }
        } catch (URISyntaxException urise) {
            LiferayCore.logError("Unable to read proxy data", urise);
        }
        if (newDefaultHttpClient == null) {
            newDefaultHttpClient = new DefaultHttpClient();
        }
        _httpClient = newDefaultHttpClient;
    } else {
        _httpClient = new DefaultHttpClient();
    }
    return _httpClient;
}
Also used : PoolingClientConnectionManager(org.apache.http.impl.conn.PoolingClientConnectionManager) IProxyData(org.eclipse.core.net.proxy.IProxyData) Scheme(org.apache.http.conn.scheme.Scheme) HttpHost(org.apache.http.HttpHost) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) IProxyService(org.eclipse.core.net.proxy.IProxyService)

Example 17 with IProxyService

use of org.eclipse.core.net.proxy.IProxyService in project eclipse.jdt.ls by eclipse.

the class JavaLanguageServerPlugin method configureProxy.

private void configureProxy() {
    // 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);
            }
        }
    }
}
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 18 with IProxyService

use of org.eclipse.core.net.proxy.IProxyService in project ecf by eclipse.

the class Activator method getProxyService.

public IProxyService getProxyService() {
    try {
        if (proxyServiceTracker == null) {
            proxyServiceTracker = new ServiceTracker(this.context, IProxyService.class.getName(), null);
            proxyServiceTracker.open();
        }
        return (IProxyService) proxyServiceTracker.getService();
    } catch (Exception e) {
        logNoProxyWarning(e);
    } catch (NoClassDefFoundError e) {
        logNoProxyWarning(e);
    }
    return null;
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) IProxyService(org.eclipse.core.net.proxy.IProxyService) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException)

Aggregations

IProxyService (org.eclipse.core.net.proxy.IProxyService)18 IProxyData (org.eclipse.core.net.proxy.IProxyData)11 URI (java.net.URI)7 URISyntaxException (java.net.URISyntaxException)7 BundleContext (org.osgi.framework.BundleContext)4 HttpHost (org.apache.http.HttpHost)3 CoreException (org.eclipse.core.runtime.CoreException)3 PasswordAuthentication (java.net.PasswordAuthentication)2 ServiceReference (org.osgi.framework.ServiceReference)2 ServiceTracker (org.osgi.util.tracker.ServiceTracker)2 IOException (java.io.IOException)1 Authenticator (java.net.Authenticator)1 InetAddress (java.net.InetAddress)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 UnknownHostException (java.net.UnknownHostException)1 Inject (javax.inject.Inject)1 ProxyHost (org.apache.commons.httpclient.ProxyHost)1 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)1 AuthScope (org.apache.http.auth.AuthScope)1