Search in sources :

Example 11 with IProxyService

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

Example 12 with IProxyService

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

the class AbstractRetrieveTestCase method addProxy.

protected void addProxy(final String proxyHost, final int port, final String username, final String password) throws Exception {
    IProxyService proxyService = Activator.getDefault().getProxyService();
    proxyService.setProxiesEnabled(true);
    proxyService.setSystemProxiesEnabled(false);
    IProxyData proxyData = new IProxyData() {

        public void disable() {
        }

        public String getHost() {
            return proxyHost;
        }

        public String getPassword() {
            return password;
        }

        public int getPort() {
            return port;
        }

        public String getType() {
            return "HTTP";
        }

        public String getUserId() {
            return username;
        }

        public boolean isRequiresAuthentication() {
            return (username != null);
        }

        public void setHost(String host) {
        }

        public void setPassword(String password) {
        }

        public void setPort(int port) {
        }

        public void setUserid(String userid) {
        }

        // TODO: What is the current expected target
        public String getSource() {
            // TODO Auto-generated method stub
            return null;
        }

        public void setSource(String source) {
        // TODO Auto-generated method stub
        }
    };
    proxyService.setProxyData(new IProxyData[] { proxyData });
}
Also used : IProxyData(org.eclipse.core.net.proxy.IProxyData) IProxyService(org.eclipse.core.net.proxy.IProxyService)

Example 13 with IProxyService

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

the class ProxyAddon method setupProxyService.

@Inject
public void setupProxyService(@Preference(value = UIConstants.Preferences.PROXY_HOST) String proxyHost, @Preference(value = UIConstants.Preferences.PROXY_PORT) int proxyPort) {
    BundleContext bc = FrameworkUtil.getBundle(ProxyAddon.class).getBundleContext();
    ServiceReference<IProxyService> serviceReference = bc.getServiceReference(IProxyService.class);
    IProxyService proxyService = bc.getService(serviceReference);
    setupProxy(proxyService, proxyHost, proxyPort);
    bc.ungetService(serviceReference);
}
Also used : IProxyService(org.eclipse.core.net.proxy.IProxyService) BundleContext(org.osgi.framework.BundleContext) Inject(javax.inject.Inject)

Example 14 with IProxyService

use of org.eclipse.core.net.proxy.IProxyService in project gemoc-studio by eclipse.

the class Activator method prepareProxySettings.

public void prepareProxySettings(String uriString) {
    URI uri;
    try {
        uri = new URI(uriString);
        IProxyService proxyService = Activator.getDefault().getProxyService();
        IProxyData[] proxyDataForHost = proxyService.select(uri);
        for (IProxyData data : proxyDataForHost) {
            if (data.getHost() != null) {
                System.setProperty("http.proxySet", "true");
                System.setProperty("http.proxyHost", data.getHost());
            }
            if (data.getHost() != null) {
                System.setProperty("http.proxyPort", String.valueOf(data.getPort()));
            }
        }
        // Close the service and close the service tracker
        proxyService = null;
    } catch (URISyntaxException e) {
        getLog().log(new Status(IStatus.WARNING, PLUGIN_ID, e.getMessage(), e));
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IProxyData(org.eclipse.core.net.proxy.IProxyData) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) IProxyService(org.eclipse.core.net.proxy.IProxyService)

Example 15 with IProxyService

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

the class ProxyAuthenticator method getPasswordAuthentication.

@Override
protected PasswordAuthentication getPasswordAuthentication() {
    IProxyService proxyService = ProxyHelper.getProxyService();
    if (proxyService != null && proxyService.isProxiesEnabled()) {
        URL requestingURL = getRequestingURL();
        IProxyData[] proxies;
        if (requestingURL == null) {
            proxies = proxyService.getProxyData();
        } else {
            try {
                proxies = proxyService.select(requestingURL.toURI());
            } catch (URISyntaxException e) {
                proxies = proxyService.getProxyData();
            }
        }
        for (IProxyData proxyData : proxies) {
            // make sure we don't hand out credentials to the wrong proxy
            if (proxyData.isRequiresAuthentication() && proxyData.getPort() == getRequestingPort() && hostMatches(proxyData)) {
                String userId = proxyData.getUserId();
                String password = proxyData.getPassword();
                if (userId != null && password != null) {
                    return new PasswordAuthentication(userId, password.toCharArray());
                }
            }
        }
    }
    if (delegate != null) {
        // Eclipse UI bundle registers one to query credentials from user
        try {
            Authenticator.setDefault(delegate);
            String requestingHost = getRequestingHost();
            InetAddress requestingSite = getRequestingSite();
            int requestingPort = getRequestingPort();
            String requestingProtocol = getRequestingProtocol();
            String requestingPrompt = getRequestingPrompt();
            String requestingScheme = getRequestingScheme();
            URL requestingURL = getRequestingURL();
            RequestorType requestorType = getRequestorType();
            if (requestingSite == null) {
                try {
                    requestingSite = InetAddress.getByName(requestingHost);
                } catch (Exception ex) {
                // ignore
                }
            }
            if (requestingPrompt == null) {
                // Help the Eclipse UI password dialog with its prompt
                String promptHost = // $NON-NLS-1$
                requestingSite == null ? // $NON-NLS-1$
                String.format("%s:%s", requestingHost, requestingPort) : requestingHost == null ? requestingSite.getHostName() : requestingHost;
                String promptType = requestorType.toString().toLowerCase();
                requestingPrompt = MessageFormat.format(Messages.ProxyAuthenticator_prompt, requestingScheme, promptType, promptHost);
            }
            return Authenticator.requestPasswordAuthentication(requestingHost, requestingSite, requestingPort, requestingProtocol, requestingPrompt, requestingScheme, requestingURL, requestorType);
        } finally {
            Authenticator.setDefault(this);
        }
    }
    return null;
}
Also used : IProxyData(org.eclipse.core.net.proxy.IProxyData) URISyntaxException(java.net.URISyntaxException) InetAddress(java.net.InetAddress) IProxyService(org.eclipse.core.net.proxy.IProxyService) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) CoreException(org.eclipse.core.runtime.CoreException) UnknownHostException(java.net.UnknownHostException) PasswordAuthentication(java.net.PasswordAuthentication)

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