summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNarayan Kamath <narayan@google.com>2014-12-16 17:03:02 +0000
committerNarayan Kamath <narayan@google.com>2014-12-16 17:08:25 +0000
commit3a2cf8007db4f3258b94fdcb74c147220350aa36 (patch)
treec8514f30328048f995b160ab82ed3a333343c2cc
parent3bf59298765b441ba3cd26f0731253962c190387 (diff)
downloadBrowser-3a2cf8007db4f3258b94fdcb74c147220350aa36.tar.gz
Move browser over to java.net.URLConnection.
bug: 18027885 Change-Id: Ie1db66432f0bd2b2eaaf2a7998828f69145645d9
-rw-r--r--src/com/android/browser/GoogleAccountLogin.java60
-rw-r--r--src/com/android/browser/search/OpenSearchSearchEngine.java49
2 files changed, 62 insertions, 47 deletions
diff --git a/src/com/android/browser/GoogleAccountLogin.java b/src/com/android/browser/GoogleAccountLogin.java
index 2bd3c8c7..93b10a6b 100644
--- a/src/com/android/browser/GoogleAccountLogin.java
+++ b/src/com/android/browser/GoogleAccountLogin.java
@@ -27,18 +27,22 @@ import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
-import android.net.http.AndroidHttpClient;
import android.os.Bundle;
import android.util.Log;
import android.webkit.CookieSyncManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
-import org.apache.http.HttpStatus;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.util.EntityUtils;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.Proxy;
+import java.net.URL;
+import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
+import java.nio.charset.UnsupportedCharsetException;
+import libcore.io.Streams;
+import libcore.net.http.ResponseUtils;
public class GoogleAccountLogin implements Runnable,
AccountManagerCallback<Bundle>, OnCancelListener {
@@ -104,25 +108,27 @@ public class GoogleAccountLogin implements Runnable,
// Runnable
@Override
public void run() {
- String url = ISSUE_AUTH_TOKEN_URL.buildUpon()
+ String urlString = ISSUE_AUTH_TOKEN_URL.buildUpon()
.appendQueryParameter("SID", mSid)
.appendQueryParameter("LSID", mLsid)
.build().toString();
- // Intentionally not using Proxy.
- AndroidHttpClient client = AndroidHttpClient.newInstance(mUserAgent);
- HttpPost request = new HttpPost(url);
- String result = null;
+ HttpURLConnection connection = null;
+ String authToken = null;
try {
- HttpResponse response = client.execute(request);
- int status = response.getStatusLine().getStatusCode();
- if (status != HttpStatus.SC_OK) {
+ URL url = new URL(urlString);
+ // Intentionally not using Proxy.
+ connection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
+ connection.setRequestMethod("POST");
+ connection.setRequestProperty("User-Agent", mUserAgent);
+
+ int status = connection.getResponseCode();
+ if (status != HttpURLConnection.HTTP_OK) {
Log.d(LOGTAG, "LOGIN_FAIL: Bad status from auth url "
- + status + ": "
- + response.getStatusLine().getReasonPhrase());
+ + status + ": " + connection.getResponseMessage());
// Invalidate the tokens once just in case the 403 was for other
// reasons.
- if (status == HttpStatus.SC_FORBIDDEN && !mTokensInvalidated) {
+ if (status == HttpURLConnection.HTTP_FORBIDDEN && !mTokensInvalidated) {
Log.d(LOGTAG, "LOGIN_FAIL: Invalidating tokens...");
// Need to regenerate the auth tokens and try again.
invalidateTokens();
@@ -134,24 +140,24 @@ public class GoogleAccountLogin implements Runnable,
done();
return;
}
- HttpEntity entity = response.getEntity();
- if (entity == null) {
- Log.d(LOGTAG, "LOGIN_FAIL: Null entity in response");
- done();
- return;
- }
- result = EntityUtils.toString(entity, "UTF-8");
+
+ final Charset responseCharset = ResponseUtils.responseCharset(
+ connection.getContentType());
+ byte[] responseBytes = Streams.readFully(connection.getInputStream());
+ authToken = new String(responseBytes, responseCharset);
} catch (Exception e) {
Log.d(LOGTAG, "LOGIN_FAIL: Exception acquiring uber token " + e);
- request.abort();
done();
return;
} finally {
- client.close();
+ if (connection != null) {
+ connection.disconnect();
+ }
}
+
final String newUrl = TOKEN_AUTH_URL.buildUpon()
.appendQueryParameter("source", "android-browser")
- .appendQueryParameter("auth", result)
+ .appendQueryParameter("auth", authToken)
.appendQueryParameter("continue",
BrowserSettings.getFactoryResetHomeUrl(mActivity))
.build().toString();
diff --git a/src/com/android/browser/search/OpenSearchSearchEngine.java b/src/com/android/browser/search/OpenSearchSearchEngine.java
index e600aa98..0343d6e4 100644
--- a/src/com/android/browser/search/OpenSearchSearchEngine.java
+++ b/src/com/android/browser/search/OpenSearchSearchEngine.java
@@ -17,10 +17,14 @@ package com.android.browser.search;
import com.android.browser.R;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.params.HttpParams;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
+import java.nio.charset.UnsupportedCharsetException;
+import libcore.io.Streams;
+import libcore.net.http.ResponseUtils;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
@@ -33,7 +37,6 @@ import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
-import android.net.http.AndroidHttpClient;
import android.os.Bundle;
import android.provider.Browser;
import android.text.TextUtils;
@@ -51,9 +54,6 @@ public class OpenSearchSearchEngine implements SearchEngine {
private static final String USER_AGENT = "Android/1.0";
private static final int HTTP_TIMEOUT_MS = 1000;
- // TODO: this should be defined somewhere
- private static final String HTTP_TIMEOUT = "http.connection-manager.timeout";
-
// Indices of the columns in the below arrays.
private static final int COLUMN_INDEX_ID = 0;
private static final int COLUMN_INDEX_QUERY = 1;
@@ -80,13 +80,8 @@ public class OpenSearchSearchEngine implements SearchEngine {
private final SearchEngineInfo mSearchEngineInfo;
- private final AndroidHttpClient mHttpClient;
-
public OpenSearchSearchEngine(Context context, SearchEngineInfo searchEngineInfo) {
mSearchEngineInfo = searchEngineInfo;
- mHttpClient = AndroidHttpClient.newInstance(USER_AGENT);
- HttpParams params = mHttpClient.getParams();
- params.setLongParameter(HTTP_TIMEOUT, HTTP_TIMEOUT_MS);
}
public String getName() {
@@ -167,16 +162,31 @@ public class OpenSearchSearchEngine implements SearchEngine {
/**
* Executes a GET request and returns the response content.
*
- * @param url Request URI.
+ * @param urlString Request URI.
* @return The response content. This is the empty string if the response
* contained no content.
*/
- public String readUrl(String url) {
+ public String readUrl(String urlString) {
try {
- HttpGet method = new HttpGet(url);
- HttpResponse response = mHttpClient.execute(method);
- if (response.getStatusLine().getStatusCode() == 200) {
- return EntityUtils.toString(response.getEntity());
+ URL url = new URL(urlString);
+ HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
+ urlConnection.setRequestProperty("User-Agent", USER_AGENT);
+ urlConnection.setConnectTimeout(HTTP_TIMEOUT_MS);
+
+ if (urlConnection.getResponseCode() == 200) {
+ final Charset responseCharset;
+ try {
+ responseCharset = ResponseUtils.responseCharset(urlConnection.getContentType());
+ } catch (UnsupportedCharsetException ucse) {
+ Log.i(TAG, "Unsupported response charset", ucse);
+ return null;
+ } catch (IllegalCharsetNameException icne) {
+ Log.i(TAG, "Illegal response charset", icne);
+ return null;
+ }
+
+ byte[] responseBytes = Streams.readFully(urlConnection.getInputStream());
+ return new String(responseBytes, responseCharset);
} else {
Log.i(TAG, "Suggestion request failed");
return null;
@@ -192,7 +202,6 @@ public class OpenSearchSearchEngine implements SearchEngine {
}
public void close() {
- mHttpClient.close();
}
private boolean isNetworkConnected(Context context) {