Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fa5dcc36 authored by Fs00's avatar Fs00
Browse files

Remove duplicate code and unneeded special handling for HTTPS connections

parent 7ccf9f5f
Loading
Loading
Loading
Loading
+12 −50
Original line number Diff line number Diff line
@@ -11,11 +11,8 @@ import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLException;

/**
 * This class is used to get a PDF File from an URL
@@ -34,58 +31,23 @@ public class DownloadPDFFile extends AsyncTask<String, Void, Object> {
        File file;
        String url = strings [0];
        String filename = strings[1];
        InputStream inputStream;
        MainActivity activity = mainActivityWR.get();
        int responseCode;

        try {
            URL uri = new URL(url);

            if (url.startsWith("https")) {
                Log.i("DownloadPDFFile", "HTTPS CONNECTION");
                HttpsURLConnection urlConnection = (HttpsURLConnection) uri.openConnection();
                urlConnection.connect();
                responseCode = urlConnection.getResponseCode();
                if (responseCode == 200) {
                    Certificate[] certs = urlConnection.getServerCertificates();
                    for (Certificate cert : certs)
                        Log.i("DownloadPDFFile", "Certificate : " +
                                "\n - Type : " + cert.getType() +
                                "\n - Hash Code : " + cert.hashCode() +
                                "\n - Public Key Algorithm : " + cert.getPublicKey().getAlgorithm() +
                                "\n - Public Key Format : " + cert.getPublicKey().getFormat()
                        );
                    inputStream = new BufferedInputStream(urlConnection.getInputStream());
                    file = Utils.createFileFromInputStream(activity.getCacheDir(), filename, inputStream);
                } else {
                    Log.e("DownloadPDFFile", "Error request https, response code : " + responseCode);
                    return responseCode;
                }
                urlConnection.disconnect();

            } else {
                Log.i("DownloadPDFFile", "HTTP CONNECTION");
            HttpURLConnection urlConnection = (HttpURLConnection) uri.openConnection();
            urlConnection.connect();
                responseCode = urlConnection.getResponseCode();
            int responseCode = urlConnection.getResponseCode();
            if (responseCode == 200) {
                    inputStream = new BufferedInputStream(urlConnection.getInputStream());
                InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
                file = Utils.createFileFromInputStream(activity.getCacheDir(), filename, inputStream);
            } else {
                    Log.e("DownloadPDFFile", "Error request http, response code : " + responseCode);
                Log.e("DownloadPDFFile", "Error during http request, response code : " + responseCode);
                return responseCode;
            }
            urlConnection.disconnect();
            }
        } catch (SSLPeerUnverifiedException e) {
            Log.e("DownloadPDFFile", "Error SSL Peer Unverified Exception");
            return e;
        } catch (SSLHandshakeException e) {
            Log.e("DownloadPDFFile", "Error SSL Handshake Exception");
            return e;
        } catch (IOException e) {
            Log.e("DownloadPDFFile", "Error cannot get file at URL :" + url);
            e.printStackTrace();
            Log.e("DownloadPDFFile", "Error cannot get file at URL : " + url, e);
            return e;
        }

@@ -102,7 +64,7 @@ public class DownloadPDFFile extends AsyncTask<String, Void, Object> {
                Toast.makeText(activity.getApplicationContext(), R.string.toast_io_exception, Toast.LENGTH_SHORT).show();
            } else if (result instanceof Integer) {
                Toast.makeText(activity.getApplicationContext(), R.string.toast_http_code_error + String.valueOf(result), Toast.LENGTH_SHORT).show();
            } else if (result instanceof SSLPeerUnverifiedException || result instanceof SSLHandshakeException) {
            } else if (result instanceof SSLException) {
                Toast.makeText(activity.getApplicationContext(), R.string.toast_ssl_error, Toast.LENGTH_SHORT).show();
            } else if (result instanceof IOException) {
                Toast.makeText(activity.getApplicationContext(), R.string.toast_io_exception, Toast.LENGTH_SHORT).show();