Example usage for java.net Authenticator setDefault

List of usage examples for java.net Authenticator setDefault

Introduction

In this page you can find the example usage for java.net Authenticator setDefault.

Prototype

public static synchronized void setDefault(Authenticator a) 

Source Link

Document

Sets the authenticator that will be used by the networking code when a proxy or an HTTP server asks for authentication.

Usage

From source file:se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventor.java

/** Clear the default java.net.Authenticator (in case current one is loaded in web app) */
protected void clearDefaultAuthenticator() {
    final Authenticator defaultAuthenticator = getStaticFieldValue(Authenticator.class, "theAuthenticator");
    if (defaultAuthenticator == null || // Can both mean not set, or error retrieving, so unset anyway to be safe 
            isLoadedInWebApplication(defaultAuthenticator)) {
        Authenticator.setDefault(null);
    }//w w  w.j  ava  2 s.co m
}

From source file:fur.shadowdrake.minecraft.InstallPanel.java

public boolean downloadMojangLauncher() {
    URL u;/*w w w.j  a  v  a2s.c o m*/
    HttpURLConnection connection;
    Proxy p;
    InputStream is;
    FileOutputStream fos;

    if (new File(config.getInstallDir(), "Minecraft.jar").isFile()) {
        return true;
    }

    log.println("Connecting to Mojang server...");
    if (config.getHttpProxy().isEmpty()) {
        p = Proxy.NO_PROXY;
    } else {
        Authenticator.setDefault(new Authenticator() {
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                if (getRequestorType() == Authenticator.RequestorType.PROXY) {
                    return config.getHttpProxyCredentials();
                } else {
                    return super.getPasswordAuthentication();
                }
            }
        });
        p = new Proxy(Proxy.Type.HTTP, new ProxyAddress(config.getHttpProxy(), 3128).getSockaddr());
    }
    try {
        u = new URL("https://s3.amazonaws.com/Minecraft.Download/launcher/Minecraft.jar");
        connection = (HttpURLConnection) u.openConnection(p);
        connection.addRequestProperty("User-agent", "Minecraft Bootloader");
        connection.setUseCaches(false);
        connection.setDefaultUseCaches(false);
        connection.setConnectTimeout(10000);
        connection.setReadTimeout(10000);
        connection.connect();
        log.println("Mojang server returned " + connection.getResponseMessage());
        if (connection.getResponseCode() != 200) {
            connection.disconnect();
            return false;
        }
    } catch (MalformedURLException ex) {
        Logger.getLogger(InstallPanel.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    } catch (IOException ex) {
        Logger.getLogger(InstallPanel.class.getName()).log(Level.SEVERE, null, ex);
        log.println("Connection to Mojang server failed.");
        return false;
    }

    try {
        is = connection.getInputStream();
        fos = new FileOutputStream(new File(config.getInstallDir(), "Minecraft.jar"));
        log.println("Downloading Minecraft.jar");
        byte[] buffer = new byte[4096];
        for (int n = is.read(buffer); n > 0; n = is.read(buffer)) {
            fos.write(buffer, 0, n);
        }
        fos.close();
        is.close();
        connection.disconnect();
        log.println("Done.");
    } catch (IOException ex) {
        Logger.getLogger(InstallPanel.class.getName()).log(Level.SEVERE, "downloadMojangLauncher", ex);
        log.println("Faild to save file.");
        return false;
    }
    return true;
}

From source file:in.andres.kandroid.kanboard.KanboardAPI.java

public KanboardAPI(String serverURL, final String username, final String password) throws IOException {
    Authenticator.setDefault(new Authenticator() {
        @Override/*  www. j  ava 2s .  com*/
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password.toCharArray());
        }

    });
    serverURL = serverURL.trim();
    String tmpURL = serverURL;
    if (!serverURL.endsWith("jsonrpc.php")) {
        if (!serverURL.endsWith("/"))
            tmpURL += "/";
        tmpURL += "jsonrpc.php";
    }
    kanboardURL = new URL(tmpURL);
    Log.i(Constants.TAG, String.format("Host uses %s", kanboardURL.getProtocol()));
    //        threadPoolExecutor = new ThreadPoolExecutor(12, 12, 20, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(256));
    threadPoolExecutor = (ThreadPoolExecutor) AsyncTask.THREAD_POOL_EXECUTOR;
    threadPoolExecutor.setCorePoolSize(12);
    threadPoolExecutor.setMaximumPoolSize(12);
}

From source file:org.broad.igv.util.HttpUtils.java

/**
 * Provide override for unit tests
 */
public void setAuthenticator(Authenticator authenticator) {
    Authenticator.setDefault(authenticator);
}

From source file:org.broad.igv.util.HttpUtils.java

/**
 * For unit tests
 */
public void resetAuthenticator() {
    Authenticator.setDefault(new IGVAuthenticator());

}

From source file:org.openecomp.sdnc.uebclient.SdncUebCallback.java

private HttpURLConnection getRestXmlConnection(String urlString, String method) throws IOException {
    URL sdncUrl = new URL(urlString);
    Authenticator.setDefault(new SdncAuthenticator(config.getSdncUser(), config.getSdncPasswd()));

    HttpURLConnection conn = (HttpURLConnection) sdncUrl.openConnection();

    String authStr = config.getSdncUser() + ":" + config.getSdncPasswd();
    String encodedAuthStr = new String(Base64.encodeBase64(authStr.getBytes()));

    conn.addRequestProperty("Authentication", "Basic " + encodedAuthStr);

    conn.setRequestMethod(method);//from w  ww. j  a  v  a  2s . co  m
    conn.setRequestProperty("Content-Type", "application/xml");
    conn.setRequestProperty("Accept", "application/xml");

    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setUseCaches(false);

    return (conn);

}

From source file:com.adito.server.DefaultAditoServerFactory.java

private void configureProxyServers() throws Exception {
    getBootProgressMonitor().updateMessage("Configuring proxy servers");
    getBootProgressMonitor().updateProgress(5);

    String httpProxyHost = contextConfiguration.retrieveProperty(new ContextKey("proxies.http.proxyHost"));
    if (httpProxyHost.length() != 0) {
        if (LOG.isInfoEnabled()) {
            LOG.info("Configuring outgoing HTTP connections to use a proxy server.");
        }/* www .j  a v  a2s.c  o  m*/
        System.setProperty("http.proxyHost", httpProxyHost);
        System.setProperty("com.maverick.ssl.https.HTTPProxyHostname", httpProxyHost);
        String httpProxyPort = contextConfiguration.retrieveProperty(new ContextKey("proxies.http.proxyPort"));

        String httpProxyUsername = contextConfiguration
                .retrieveProperty(new ContextKey("proxies.http.proxyUser"));
        String httpProxyPassword = contextConfiguration
                .retrieveProperty(new ContextKey("proxies.http.proxyPassword"));

        System.setProperty("http.proxyPort", httpProxyPort);
        System.setProperty("com.maverick.ssl.https.HTTPProxyPort", httpProxyPort);

        if (httpProxyUsername.trim().length() != 0) {
            System.setProperty("com.maverick.ssl.https.HTTPProxyUsername", httpProxyUsername.trim());
        }

        if (httpProxyPassword.trim().length() != 0) {
            System.setProperty("com.maverick.ssl.https.HTTPProxyPassword", httpProxyPassword.trim());
        }

        System.setProperty("com.maverick.ssl.https.HTTPProxySecure", "false");

        PropertyList list = contextConfiguration
                .retrievePropertyList(new ContextKey("proxies.http.nonProxyHosts"));
        StringBuilder hosts = new StringBuilder();
        for (Iterator i = list.iterator(); i.hasNext();) {
            if (hosts.length() != 0) {
                hosts.append("|");
            }
            hosts.append(i.next());
        }
        System.setProperty("http.nonProxyHosts", hosts.toString());
        System.setProperty("com.maverick.ssl.https.HTTPProxyNonProxyHosts", hosts.toString());
    }
    String socksProxyHost = contextConfiguration.retrieveProperty(new ContextKey("proxies.socksProxyHost"));
    if (socksProxyHost.length() != 0) {
        if (LOG.isInfoEnabled()) {
            LOG.info("Configuring outgoing TCP/IP connections to use a SOCKS proxy server.");
        }
        System.setProperty("socksProxyHost", httpProxyHost);
        System.setProperty("socksProxyPort",
                contextConfiguration.retrieveProperty(new ContextKey("proxies.socksProxyPort")));
    }
    if (socksProxyHost.length() != 0 || httpProxyHost.length() != 0) {
        Authenticator.setDefault(new ProxyAuthenticator());
    }
}

From source file:com.adito.server.Main.java

private void configureProxyServers() throws Exception {
    getBootProgressMonitor().updateMessage("Configuring proxy servers");
    getBootProgressMonitor().updateProgress(5);

    String httpProxyHost = contextConfiguration.retrieveProperty(new ContextKey("proxies.http.proxyHost"));
    if (!httpProxyHost.equals("")) {
        if (log.isInfoEnabled())
            log.info("Configuring outgoing HTTP connections to use a proxy server.");
        System.setProperty("http.proxyHost", httpProxyHost);
        System.setProperty("com.maverick.ssl.https.HTTPProxyHostname", httpProxyHost);
        String httpProxyPort = contextConfiguration.retrieveProperty(new ContextKey("proxies.http.proxyPort"));

        String httpProxyUsername = contextConfiguration
                .retrieveProperty(new ContextKey("proxies.http.proxyUser"));
        String httpProxyPassword = contextConfiguration
                .retrieveProperty(new ContextKey("proxies.http.proxyPassword"));

        System.setProperty("http.proxyPort", httpProxyPort);
        System.setProperty("com.maverick.ssl.https.HTTPProxyPort", httpProxyPort);

        if (!httpProxyUsername.trim().equals(""))
            System.setProperty("com.maverick.ssl.https.HTTPProxyUsername", httpProxyUsername.trim());

        if (!httpProxyPassword.trim().equals(""))
            System.setProperty("com.maverick.ssl.https.HTTPProxyPassword", httpProxyPassword.trim());

        System.setProperty("com.maverick.ssl.https.HTTPProxySecure", "false");

        PropertyList list = contextConfiguration
                .retrievePropertyList(new ContextKey("proxies.http.nonProxyHosts"));
        StringBuffer hosts = new StringBuffer();
        for (Iterator i = list.iterator(); i.hasNext();) {
            if (hosts.length() != 0) {
                hosts.append("|");
            }// w w w.j  a  v  a2s .  co m
            hosts.append(i.next());
        }
        System.setProperty("http.nonProxyHosts", hosts.toString());
        System.setProperty("com.maverick.ssl.https.HTTPProxyNonProxyHosts", hosts.toString());
    }
    String socksProxyHost = contextConfiguration.retrieveProperty(new ContextKey("proxies.socksProxyHost"));
    if (!socksProxyHost.equals("")) {
        if (log.isInfoEnabled())
            log.info("Configuring outgoing TCP/IP connections to use a SOCKS proxy server.");
        System.setProperty("socksProxyHost", httpProxyHost);
        System.setProperty("socksProxyPort",
                contextConfiguration.retrieveProperty(new ContextKey("proxies.socksProxyPort")));
    }
    if (!socksProxyHost.equals("") || !httpProxyHost.equals("")) {
        Authenticator.setDefault(new ProxyAuthenticator());
    }
}

From source file:tvbrowser.TVBrowser.java

/**
 * Updates the proxy settings.//from   w w w.jav a  2 s  .  c  om
 */
public static void updateProxySettings() {
    String httpHost = "", httpPort = "", httpUser = "", httpPassword = "";

    if (Settings.propHttpProxyUseProxy.getBoolean()) {
        httpHost = Settings.propHttpProxyHost.getString();
        httpPort = Settings.propHttpProxyPort.getString();

        if (Settings.propHttpProxyAuthentifyAtProxy.getBoolean()) {
            httpUser = Settings.propHttpProxyUser.getString();
            httpPassword = Settings.propHttpProxyPassword.getString();
            if (httpPassword == null) {
                httpPassword = "";
            }

            final String user = httpUser;
            final String pw = httpPassword;
            Authenticator.setDefault(new Authenticator() {
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(user, pw.toCharArray());
                }
            });
        }
    }

    System.setProperty("http.proxyHost", httpHost);
    System.setProperty("http.proxyPort", httpPort);
    System.setProperty("http.proxyUser", httpUser);
    System.setProperty("http.proxyPassword", httpPassword);
    System.setProperty("https.proxyHost", httpHost);
}

From source file:fr.cls.atoll.motu.library.misc.intfce.Organizer.java

/**
 * Inits the proxy parameters.//  w ww  .ja v a2 s. co  m
 * 
 * @throws MotuException the motu exception
 */
public static void initProxyLogin() throws MotuException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("BEGIN Organizer.initProxyLogin()");
        // LOG.debug("proxyHost:");
        // LOG.debug(System.getProperty("proxyHost"));
        // LOG.debug("proxyPort:");
        // LOG.debug(System.getProperty("proxyPort"));
        // LOG.debug("socksProxyHost:");
        // LOG.debug(System.getProperty("socksProxyHost"));
        // LOG.debug("socksProxyPort:");
        // LOG.debug(System.getProperty("socksProxyPort"));
        // LOG.debug("properties:");
        // LOG.debug(System.getProperties().toString());
    }

    if (Organizer.getMotuConfigInstance().getUseProxy()) {
        String user = Organizer.getMotuConfigInstance().getProxyLogin();
        String pwd = Organizer.getMotuConfigInstance().getProxyPwd();
        System.setProperty("proxyHost", Organizer.getMotuConfigInstance().getProxyHost());
        System.setProperty("proxyPort", Organizer.getMotuConfigInstance().getProxyPort());
        System.setProperty("http.proxyHost", Organizer.getMotuConfigInstance().getProxyHost());
        System.setProperty("http.proxyPort", Organizer.getMotuConfigInstance().getProxyPort());

        if ((!Organizer.isNullOrEmpty(user)) && (!Organizer.isNullOrEmpty(pwd))) {
            Authenticator.setDefault(new SimpleAuthenticator(user, pwd));
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("END Organizer.initProxyLogin()");
    }
}