Search in sources :

Example 16 with IProxyData

use of org.eclipse.core.net.proxy.IProxyData 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 IProxyData

use of org.eclipse.core.net.proxy.IProxyData 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 IProxyData

use of org.eclipse.core.net.proxy.IProxyData in project egit by eclipse.

the class EclipseProxySelector method select.

@Override
public List<Proxy> select(final URI uri) {
    final ArrayList<Proxy> r = new ArrayList<Proxy>();
    final String host = uri.getHost();
    if (host != null) {
        String type = IProxyData.SOCKS_PROXY_TYPE;
        if (// $NON-NLS-1$
        "http".equals(uri.getScheme()))
            type = IProxyData.HTTP_PROXY_TYPE;
        else if (// $NON-NLS-1$
        "ftp".equals(uri.getScheme()))
            type = IProxyData.HTTP_PROXY_TYPE;
        else if (// $NON-NLS-1$
        "https".equals(uri.getScheme()))
            type = IProxyData.HTTPS_PROXY_TYPE;
        try {
            // $NON-NLS-1$
            URI queryUri = new URI(type, "//" + host, null);
            final IProxyData[] dataArray = service.select(queryUri);
            for (IProxyData data : dataArray) {
                if (IProxyData.HTTP_PROXY_TYPE.equals(data.getType()))
                    addProxy(r, Proxy.Type.HTTP, data);
                else if (IProxyData.HTTPS_PROXY_TYPE.equals(data.getType()))
                    addProxy(r, Proxy.Type.HTTP, data);
                else if (IProxyData.SOCKS_PROXY_TYPE.equals(data.getType()))
                    addProxy(r, Proxy.Type.SOCKS, data);
            }
        } catch (URISyntaxException e) {
        // just add nothing to r
        }
    }
    if (r.isEmpty())
        r.add(Proxy.NO_PROXY);
    return r;
}
Also used : Proxy(java.net.Proxy) IProxyData(org.eclipse.core.net.proxy.IProxyData) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Aggregations

IProxyData (org.eclipse.core.net.proxy.IProxyData)18 IProxyService (org.eclipse.core.net.proxy.IProxyService)11 URI (java.net.URI)8 URISyntaxException (java.net.URISyntaxException)8 HttpHost (org.apache.http.HttpHost)5 CoreException (org.eclipse.core.runtime.CoreException)4 AuthScope (org.apache.http.auth.AuthScope)3 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)3 PasswordAuthentication (java.net.PasswordAuthentication)2 Authenticator (java.net.Authenticator)1 InetAddress (java.net.InetAddress)1 Proxy (java.net.Proxy)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 ProxyHost (org.apache.commons.httpclient.ProxyHost)1 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)1 NTCredentials (org.apache.http.auth.NTCredentials)1 CredentialsProvider (org.apache.http.client.CredentialsProvider)1