Example usage for java.net Authenticator Authenticator

List of usage examples for java.net Authenticator Authenticator

Introduction

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

Prototype

Authenticator

Source Link

Usage

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

public boolean downloadMojangLauncher() {
    URL u;/* www  .java  2 s.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//from w  w  w  .  j  a  v a 2s  .  c  om
        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:tvbrowser.TVBrowser.java

/**
 * Updates the proxy settings.//from   ww w.jav a2 s.co m
 */
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);
}