Bug 1324427 - Switchboard: Set user agent and proxy; Using proxy and setting user agent from ProxySelector and BaseAction classes respectively. r=sebastian
authorSwaroop Rao <swaroop.rao@gmail.com>
Wed, 01 Feb 2017 18:12:54 -0500
changeset 469925 bcabeb8c81a266a76bea170b3482bde25ca65c8a
parent 469924 f10e4c47689971e36b8d38525043cfd353db0172
child 469926 42cd21f10830c493837d80facdfd0234124f9948
push id43881
push userbmo:gps@mozilla.com
push dateThu, 02 Feb 2017 23:49:03 +0000
reviewerssebastian
bugs1324427
milestone54.0a1
Bug 1324427 - Switchboard: Set user agent and proxy; Using proxy and setting user agent from ProxySelector and BaseAction classes respectively. r=sebastian MozReview-Commit-ID: 4L22dZd4VX5
mobile/android/base/java/org/mozilla/gecko/switchboard/SwitchBoard.java
--- a/mobile/android/base/java/org/mozilla/gecko/switchboard/SwitchBoard.java
+++ b/mobile/android/base/java/org/mozilla/gecko/switchboard/SwitchBoard.java
@@ -16,26 +16,30 @@
 package org.mozilla.gecko.switchboard;
 
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
 import java.util.MissingResourceException;
 import java.util.zip.CRC32;
 
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.json.JSONArray;
+import org.mozilla.gecko.AppConstants;
+import org.mozilla.gecko.util.HardwareUtils;
+import org.mozilla.gecko.util.ProxySelector;
 
 import android.content.Context;
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.os.Build;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 import android.text.TextUtils;
 import android.util.Log;
@@ -370,32 +374,35 @@ public class SwitchBoard {
 
     /**
      * Returns a String containing the server response from a GET request
      * @param url URL for GET request.
      * @return Returns String from server or null when failed.
      */
     @Nullable private static String readFromUrlGET(URL url) {
         try {
-            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+            HttpURLConnection connection = (HttpURLConnection) ProxySelector.openConnectionWithProxy(url.toURI());
+            connection.setRequestProperty("User-Agent", HardwareUtils.isTablet() ?
+                    AppConstants.USER_AGENT_FENNEC_TABLET :
+                    AppConstants.USER_AGENT_FENNEC_MOBILE);
             connection.setRequestMethod("GET");
             connection.setUseCaches(false);
 
             InputStream is = connection.getInputStream();
             InputStreamReader inputStreamReader = new InputStreamReader(is);
             BufferedReader bufferReader = new BufferedReader(inputStreamReader, 8192);
             String line;
             StringBuilder resultContent = new StringBuilder();
             while ((line = bufferReader.readLine()) != null) {
                 resultContent.append(line);
             }
             bufferReader.close();
 
             return resultContent.toString();
-        } catch (IOException e) {
+        } catch (IOException | URISyntaxException e) {
             e.printStackTrace();
         }
 
         return null;
     }
 
     /**
      * Return the bucket number of the user. There are 100 possible buckets.