Search in sources :

Example 46 with Authenticator

use of java.net.Authenticator in project linuxtools by eclipse.

the class RegistryAccountInfo method restoreAuthenticator.

@Override
protected void restoreAuthenticator() {
    IExtension[] extensions = RegistryFactory.getRegistry().getExtensionPoint("org.eclipse.core.net", // $NON-NLS-1$ //$NON-NLS-2$
    "authenticator").getExtensions();
    if (extensions.length == 0) {
        return;
    }
    IExtension extension = extensions[0];
    IConfigurationElement[] configs = extension.getConfigurationElements();
    if (configs.length == 0) {
        return;
    }
    try {
        IConfigurationElement config = configs[0];
        Authenticator original = (Authenticator) config.createExecutableExtension(// $NON-NLS-1$
        "class");
        Authenticator.setDefault(original);
    } catch (CoreException ex) {
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) IExtension(org.eclipse.core.runtime.IExtension) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) Authenticator(java.net.Authenticator)

Example 47 with Authenticator

use of java.net.Authenticator in project cxf by apache.

the class AbstractCodegenMoho method configureProxyServerSettings.

protected void configureProxyServerSettings() throws MojoExecutionException {
    Proxy proxy = mavenSession.getSettings().getActiveProxy();
    if (proxy != null) {
        getLog().info("Using proxy server configured in maven.");
        if (proxy.getHost() == null) {
            throw new MojoExecutionException("Proxy in settings.xml has no host");
        }
        if (proxy.getHost() != null) {
            System.setProperty(HTTP_PROXY_HOST, proxy.getHost());
        }
        if (String.valueOf(proxy.getPort()) != null) {
            System.setProperty(HTTP_PROXY_PORT, String.valueOf(proxy.getPort()));
        }
        if (proxy.getNonProxyHosts() != null) {
            System.setProperty(HTTP_NON_PROXY_HOSTS, proxy.getNonProxyHosts());
        }
        if (!StringUtils.isEmpty(proxy.getUsername()) && !StringUtils.isEmpty(proxy.getPassword())) {
            final String authUser = proxy.getUsername();
            final String authPassword = proxy.getPassword();
            Authenticator.setDefault(new Authenticator() {

                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(authUser, authPassword.toCharArray());
                }
            });
            System.setProperty(HTTP_PROXY_USER, authUser);
            System.setProperty(HTTP_PROXY_PORT, authPassword);
        }
    }
}
Also used : Proxy(org.apache.maven.settings.Proxy) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 48 with Authenticator

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

the class ApplicationWorkbenchAdvisor method initialize.

/* (non-Javadoc)
	 * @see org.eclipse.ui.application.WorkbenchAdvisor#initialize(org.eclipse.ui.application.IWorkbenchConfigurer)
	 */
@Override
public void initialize(IWorkbenchConfigurer configurer) {
    super.initialize(configurer);
    // Early creation of progress service to prevent NPE in Eclipse 4.2
    PlatformUI.getWorkbench().getProgressService();
    TweakletManager.initTweaklets();
    BrandingManager.create();
    final IPreferenceStore ps = Activator.getDefault().getPreferenceStore();
    // $NON-NLS-1$
    configurer.setSaveAndRestore(ps.getBoolean("SAVE_AND_RESTORE"));
    if (// $NON-NLS-1$
    ps.getBoolean("HTTP_PROXY_ENABLED")) {
        // $NON-NLS-1$ //$NON-NLS-2$
        System.setProperty("http.proxyHost", ps.getString("HTTP_PROXY_SERVER"));
        // $NON-NLS-1$ //$NON-NLS-2$
        System.setProperty("http.proxyPort", ps.getString("HTTP_PROXY_PORT"));
        // $NON-NLS-1$ //$NON-NLS-2$
        System.setProperty("http.noProxyHosts", ps.getString("HTTP_PROXY_EXCLUSIONS"));
        if (// $NON-NLS-1$
        ps.getBoolean("HTTP_PROXY_AUTH")) {
            Authenticator.setDefault(new Authenticator() {

                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    return new PasswordAuthentication(ps.getString("HTTP_PROXY_LOGIN"), ps.getString("HTTP_PROXY_PASSWORD").toCharArray());
                }
            });
        }
    } else {
        // $NON-NLS-1$
        System.clearProperty("http.proxyHost");
        // $NON-NLS-1$
        System.clearProperty("http.proxyPort");
        // $NON-NLS-1$
        System.clearProperty("http.noProxyHosts");
    }
}
Also used : IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 49 with Authenticator

use of java.net.Authenticator in project Bytecoder by mirkosertic.

the class EmptyInputStream method openConnectionCheckRedirects.

/**
 * opens a stream allowing redirects only to the same host.
 */
public static InputStream openConnectionCheckRedirects(URLConnection c) throws IOException {
    boolean redir;
    int redirects = 0;
    InputStream in;
    Authenticator a = null;
    do {
        if (c instanceof HttpURLConnection) {
            ((HttpURLConnection) c).setInstanceFollowRedirects(false);
            if (a == null) {
                a = ((HttpURLConnection) c).authenticator;
            }
        }
        // We want to open the input stream before
        // getting headers, because getHeaderField()
        // et al swallow IOExceptions.
        in = c.getInputStream();
        redir = false;
        if (c instanceof HttpURLConnection) {
            HttpURLConnection http = (HttpURLConnection) c;
            int stat = http.getResponseCode();
            if (stat >= 300 && stat <= 307 && stat != 306 && stat != HttpURLConnection.HTTP_NOT_MODIFIED) {
                URL base = http.getURL();
                String loc = http.getHeaderField("Location");
                URL target = null;
                if (loc != null) {
                    target = new URL(base, loc);
                }
                http.disconnect();
                if (target == null || !base.getProtocol().equals(target.getProtocol()) || base.getPort() != target.getPort() || !hostsEqual(base, target) || redirects >= 5) {
                    throw new SecurityException("illegal URL redirect");
                }
                redir = true;
                c = target.openConnection();
                if (a != null && c instanceof HttpURLConnection) {
                    ((HttpURLConnection) c).setAuthenticator(a);
                }
                redirects++;
            }
        }
    } while (redir);
    return in;
}
Also used : ChunkedInputStream(sun.net.www.http.ChunkedInputStream) Authenticator(java.net.Authenticator) URL(java.net.URL)

Example 50 with Authenticator

use of java.net.Authenticator 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)

Aggregations

Authenticator (java.net.Authenticator)80 PasswordAuthentication (java.net.PasswordAuthentication)50 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)9 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 ArrayList (java.util.ArrayList)3 Content (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content)3 Debug (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug)3 JCommander (com.beust.jcommander.JCommander)2