Search in sources :

Example 1 with IProxyData

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

the class RestClient method getProxyConfiguration.

private ProxyConfiguration getProxyConfiguration() {
    IProxyService proxyService = getProxyService();
    IProxyData[] proxyDataForHost = proxyService.select(java.net.URI.create(connectionInfo.getUrl()));
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    RequestConfig.Builder configBuilder = RequestConfig.custom();
    for (IProxyData data : proxyDataForHost) {
        if (!Strings.isNullOrEmpty(data.getHost())) {
            HttpHost proxyConfig = new HttpHost(data.getHost(), data.getPort(), data.getType());
            configBuilder.setProxy(proxyConfig);
            if (!Strings.isNullOrEmpty(data.getUserId())) {
                credsProvider.setCredentials(new AuthScope(data.getHost(), data.getPort()), new UsernamePasswordCredentials(data.getUserId(), data.getPassword()));
            }
        }
    }
    return new ProxyConfiguration(configBuilder.build(), credsProvider);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) IProxyData(org.eclipse.core.net.proxy.IProxyData) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) IProxyService(org.eclipse.core.net.proxy.IProxyService) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 2 with IProxyData

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

the class RemoteLogStream method openInputStream.

protected static InputStream openInputStream(IServerManagerConnection remote, URL url) throws IOException {
    String username = remote.getUsername();
    String password = remote.getPassword();
    // $NON-NLS-1$
    String authString = username + ":" + password;
    byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
    String authStringEnc = new String(authEncBytes);
    final IProxyService proxyService = LiferayCore.getProxyService();
    URLConnection conn = null;
    try {
        // $NON-NLS-1$ //$NON-NLS-2$
        URI uri = new URI("HTTP://" + url.getHost() + ":" + url.getPort());
        IProxyData[] proxyDataForHost = proxyService.select(uri);
        for (IProxyData data : proxyDataForHost) {
            if (data.getHost() != null) {
                // $NON-NLS-1$
                System.setProperty("http.proxyHost", data.getHost());
                // $NON-NLS-1$
                System.setProperty("http.proxyPort", String.valueOf(data.getPort()));
                break;
            }
        }
        // $NON-NLS-1$ //$NON-NLS-2$
        uri = new URI("SOCKS://" + url.getHost() + ":" + url.getPort());
        proxyDataForHost = proxyService.select(uri);
        for (IProxyData data : proxyDataForHost) {
            if (data.getHost() != null) {
                // $NON-NLS-1$
                System.setProperty("socksProxyHost", data.getHost());
                // $NON-NLS-1$
                System.setProperty("socksProxyPort", String.valueOf(data.getPort()));
                break;
            }
        }
    } catch (URISyntaxException e) {
        // $NON-NLS-1$
        LiferayServerCore.logError("Could not read proxy data", e);
    }
    conn = url.openConnection();
    // $NON-NLS-1$ //$NON-NLS-2$
    conn.setRequestProperty("Authorization", "Basic " + authStringEnc);
    Authenticator.setDefault(null);
    conn.setAllowUserInteraction(false);
    return conn.getInputStream();
}
Also used : IProxyData(org.eclipse.core.net.proxy.IProxyData) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) IProxyService(org.eclipse.core.net.proxy.IProxyService) URLConnection(java.net.URLConnection)

Example 3 with IProxyData

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

the class SocketUtil method canConnectProxy.

public static IStatus canConnectProxy(Socket socket, String host, String port) {
    IProxyService proxyService = LiferayCore.getProxyService();
    try {
        // $NON-NLS-1$ //$NON-NLS-2$
        URI uri = new URI("http://" + host + ":" + port);
        IProxyData[] proxyDataForHost = proxyService.select(uri);
        for (IProxyData data : proxyDataForHost) {
            if (data.getHost() != null) {
                return SocketUtil.canConnect(socket, data.getHost(), String.valueOf(data.getPort()));
            }
        }
        // $NON-NLS-1$ //$NON-NLS-2$
        uri = new URI("SOCKS://" + host + ":" + port);
        for (IProxyData data : proxyDataForHost) {
            if (data.getHost() != null) {
                return SocketUtil.canConnect(socket, data.getHost(), String.valueOf(data.getPort()));
            }
        }
    } catch (URISyntaxException e) {
        // $NON-NLS-1$
        LiferayServerCore.logError("Could not read proxy data", e);
    }
    return null;
}
Also used : IProxyData(org.eclipse.core.net.proxy.IProxyData) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) IProxyService(org.eclipse.core.net.proxy.IProxyService)

Example 4 with IProxyData

use of org.eclipse.core.net.proxy.IProxyData in project tdq-studio-se by Talend.

the class EcosystemProxyAdapter method adapt.

/**
 * DOC bZhou Comment method "adapt".
 *
 * @param httpclient
 * @param url
 */
public static void adapt(HttpClient httpclient, String url) {
    IProxyService proxyService = EcosPlugin.getDefault().getProxyService();
    IProxyData proxyData = null;
    try {
        IProxyData[] proxyDatas = proxyService.select(new URI(url));
        if (proxyDatas != null && proxyDatas.length > 0) {
            proxyData = proxyDatas[0];
        }
    } catch (URISyntaxException e) {
        log.error(e, e);
    }
    if (proxyData == null) {
        proxyData = proxyService.getProxyData(IProxyData.HTTP_PROXY_TYPE);
    }
    if (proxyData != null & StringUtils.isNotEmpty(proxyData.getHost())) {
        // use proxy to connect
        ProxyHost host = new ProxyHost(proxyData.getHost(), proxyData.getPort());
        httpclient.getHostConfiguration().setProxyHost(host);
        httpclient.getParams().setAuthenticationPreemptive(true);
        String userId = proxyData.getUserId();
        if (StringUtils.isNotEmpty(userId)) {
            UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userId, proxyData.getPassword());
            httpclient.getState().setProxyCredentials(AuthScope.ANY, credentials);
        }
    }
}
Also used : IProxyData(org.eclipse.core.net.proxy.IProxyData) URISyntaxException(java.net.URISyntaxException) ProxyHost(org.apache.commons.httpclient.ProxyHost) URI(java.net.URI) IProxyService(org.eclipse.core.net.proxy.IProxyService) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 5 with IProxyData

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

the class ProxySetupHelper method getProxy.

public static Proxy getProxy(String url) {
    Proxy proxy = null;
    try {
        IProxyService proxyService = Activator.getDefault().getProxyService();
        // Only do this if platform service exists
        if (proxyService != null && proxyService.isProxiesEnabled()) {
            // Setup via proxyService entry
            URI uri = new URI(url);
            final IProxyData[] proxies = proxyService.select(uri);
            IProxyData selectedProxy = selectProxyFromProxies(uri.getScheme(), proxies);
            if (selectedProxy != null) {
                proxy = new Proxy(((selectedProxy.getType().equalsIgnoreCase(IProxyData.SOCKS_PROXY_TYPE)) ? Proxy.Type.SOCKS : Proxy.Type.HTTP), new ProxyAddress(selectedProxy.getHost(), selectedProxy.getPort()), selectedProxy.getUserId(), selectedProxy.getPassword());
            }
        }
    } catch (Exception e) {
        // If we don't even have the classes for this (i.e. the org.eclipse.core.net plugin not available)
        // then we simply log and ignore
        Activator.logNoProxyWarning(e);
    } catch (NoClassDefFoundError e) {
        Activator.logNoProxyWarning(e);
    }
    return proxy;
}
Also used : Proxy(org.eclipse.ecf.core.util.Proxy) IProxyData(org.eclipse.core.net.proxy.IProxyData) ProxyAddress(org.eclipse.ecf.core.util.ProxyAddress) URI(java.net.URI) IProxyService(org.eclipse.core.net.proxy.IProxyService)

Aggregations

IProxyData (org.eclipse.core.net.proxy.IProxyData)19 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