/*
 *    Copyright (C) 2012 Hai Bison
 *
 *    See the file LICENSE at the root directory of this project for copying
 *    permission.
 */

package group.pals.desktop.app.apksigner.utils;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Network utilities.
 * 
 * @author Hai Bison
 * @since v1.6 beta
 */
public class Network {

    /**
     * The network timeout, in milliseconds.
     */
    public static final int NETWORK_TIMEOUT = 15000;

    /**
     * HTTP status code OK.
     */
    public static final int HTTP_STATUS_OK = 200;

    private static final String PROPERTY_SYS_PROXY_HOST = "http.proxyHost";
    private static final String PROPERTY_SYS_PROXY_PORT = "http.proxyPort";

    /**
     * Opens new connection to {@code url} with default settings.
     * 
     * @param url
     *            the URL.
     * @return the connection. Or {@code null} if an error occurred.
     */
    public static HttpURLConnection openHttpConnection(String url) {
        if (Texts.isEmpty(url))
            return null;

        if (Preferences.getInstance().isUsingProxy()) {
            System.setProperty(PROPERTY_SYS_PROXY_HOST, Preferences
                    .getInstance().getProxyHost());
            System.setProperty(PROPERTY_SYS_PROXY_PORT,
                    Integer.toString(Preferences.getInstance().getProxyPort()));
        } else {
            System.clearProperty(PROPERTY_SYS_PROXY_HOST);
            System.clearProperty(PROPERTY_SYS_PROXY_PORT);
        }

        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(url)
                    .openConnection();
            conn.setConnectTimeout(NETWORK_TIMEOUT);
            conn.setReadTimeout(NETWORK_TIMEOUT);
            return conn;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            /*
             * Perhaps there is no available Internet connections.
             */
            e.printStackTrace();
            return null;
        }
    }// openHttpConnection()
}

@@ -307,16 +307,64 @@
 t.URL;%0A%0A
+import org.apache.commons.codec.binary.Base64;%0A%0A
 /**%0A * N
@@ -701,32 +701,37 @@
 ng PROPERTY_SYS_
+HTTP_
 PROXY_HOST = %22ht
@@ -782,32 +782,37 @@
 ng PROPERTY_SYS_
+HTTP_
 PROXY_PORT = %22ht
@@ -827,16 +827,183 @@
 Port%22;%0A%0A
+    private static final String PROPERTY_SYS_HTTPS_PROXY_HOST = %22https.proxyHost%22;%0A    private static final String PROPERTY_SYS_HTTPS_PROXY_PORT = %22https.proxyPort%22;%0A%0A
     /**%0A
@@ -1377,24 +1377,75 @@
 gProxy()) %7B%0A
+            /*%0A             * HTTP%0A             */%0A
             
@@ -1468,32 +1468,37 @@
 ty(PROPERTY_SYS_
+HTTP_
 PROXY_HOST, Pref
@@ -1593,32 +1593,37 @@
 ty(PROPERTY_SYS_
+HTTP_
 PROXY_PORT,%0A    
@@ -1699,24 +1699,25 @@
 rt()));%0A
+%0A
         
 %7D else %7B
@@ -1712,16 +1712,51 @@
     
-%7D else %7B
+    /*%0A             * HTTPS%0A             */
 %0A   
@@ -1763,37 +1763,35 @@
          System.
-clear
+set
 Property(PROPERT
@@ -1792,24 +1792,30 @@
 ROPERTY_SYS_
+HTTPS_
 PROXY_HOST);
@@ -1812,16 +1812,79 @@
 OXY_HOST
+, Preferences%0A                    .getInstance().getProxyHost()
 );%0A     
@@ -1901,13 +1901,11 @@
 tem.
-clear
+set
 Prop
@@ -1922,16 +1922,22 @@
 RTY_SYS_
+HTTPS_
 PROXY_PO
@@ -1942,144 +1942,1259 @@
 PORT
-);%0A        %7D%0A%0A        try %7B%0A            HttpURLConnection conn = (HttpURLConnection) new URL(url)%0A                    .openConnection();
+,%0A                    Integer.toString(Preferences.getInstance().getProxyPort()));%0A        %7D else %7B%0A            for (String s : new String%5B%5D %7B PROPERTY_SYS_HTTP_PROXY_HOST,%0A                    PROPERTY_SYS_HTTP_PROXY_PORT,%0A                    PROPERTY_SYS_HTTPS_PROXY_HOST,%0A                    PROPERTY_SYS_HTTPS_PROXY_PORT %7D)%0A                System.clearProperty(s);%0A        %7D%0A%0A        try %7B%0A            HttpURLConnection conn = (HttpURLConnection) new URL(url)%0A                    .openConnection();%0A%0A            if (Preferences.getInstance().isUsingProxy()%0A                    && !Texts.isEmpty(Preferences.getInstance()%0A                            .getProxyUsername())) %7B%0A                char%5B%5D password = Preferences.getInstance().getProxyPassword();%0A                String proxyAuthorization = Base64%0A                        .encodeBase64String((Preferences.getInstance()%0A                                .getProxyUsername() + %22:%22 + (password != null ? new String(%0A                                password) : %22%22)).getBytes(Texts.UTF8));%0A                // https://en.wikipedia.org/wiki/List_of_HTTP_header_fields%0A                conn.setRequestProperty(%22Proxy-Authorization%22, %22Basic %22%0A                        + proxyAuthorization);%0A            %7D%0A
 %0A   

Enable using HTTPS proxy and proxy username/ password.