summaryrefslogtreecommitdiffstats
path: root/src/com/android/dialer/lookup/LookupUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/android/dialer/lookup/LookupUtils.java')
-rw-r--r--src/com/android/dialer/lookup/LookupUtils.java140
1 files changed, 112 insertions, 28 deletions
diff --git a/src/com/android/dialer/lookup/LookupUtils.java b/src/com/android/dialer/lookup/LookupUtils.java
index c0b84dc38..9d2678944 100644
--- a/src/com/android/dialer/lookup/LookupUtils.java
+++ b/src/com/android/dialer/lookup/LookupUtils.java
@@ -18,15 +18,20 @@ package com.android.dialer.lookup;
import android.text.Html;
-import org.apache.http.Header;
-import org.apache.http.HttpResponse;
-import org.apache.http.HttpStatus;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.util.EntityUtils;
-
+import java.io.BufferedInputStream;
+import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStreamWriter;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
+import java.nio.charset.UnsupportedCharsetException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -34,35 +39,115 @@ public class LookupUtils {
private static final String USER_AGENT =
"Mozilla/5.0 (X11; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0";
- public static String httpGet(HttpGet request) throws IOException {
- HttpClient client = new DefaultHttpClient();
+ private static HttpURLConnection prepareHttpConnection(String url, Map<String, String> headers)
+ throws IOException {
+ // open connection
+ HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();
+ // set user agent (default value is null)
+ urlConnection.setRequestProperty("User-Agent", USER_AGENT);
+ // set all other headers if not null
+ if (headers != null) {
+ for (Map.Entry<String, String> header : headers.entrySet()) {
+ urlConnection.setRequestProperty(header.getKey(), header.getValue());
+ }
+ }
+
+ return urlConnection;
+ }
+
+ private static byte[] httpFetch(HttpURLConnection urlConnection) throws IOException {
+ // query url, read and return buffered response body
+ // we want to make sure that the connection gets closed here
+ InputStream is = new BufferedInputStream(urlConnection.getInputStream());
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ byte[] result = null;
+ try {
+ byte[] partial = new byte[4096];
+ int read;
+ while ((read = is.read(partial, 0, 4096)) != -1) {
+ baos.write(partial, 0, read);
+ }
+ result = baos.toByteArray();
+ } finally {
+ is.close();
+ baos.close();
+ }
+ return result;
+ }
+
+ private static Charset determineCharset(HttpURLConnection connection) {
+ String contentType = connection.getContentType();
+ if (contentType != null) {
+ String[] split = contentType.split(";");
+ for (int i = 0; i < split.length; i++) {
+ String trimmed = split[i].trim();
+ if (trimmed.startsWith("charset=")) {
+ try {
+ return Charset.forName(trimmed.substring(8));
+ } catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
+ // we don't know about this charset -> ignore
+ }
+ }
+ }
+ }
+ return Charset.defaultCharset();
+ }
- request.setHeader("User-Agent", USER_AGENT);
+ public static String httpGet(String url, Map<String, String> headers) throws IOException {
+ HttpURLConnection connection = prepareHttpConnection(url, headers);
+ try {
+ byte[] response = httpFetch(connection);
+ return new String(response, determineCharset(connection));
+ } finally {
+ connection.disconnect();
+ }
+ }
- HttpResponse response = client.execute(request);
- int status = response.getStatusLine().getStatusCode();
+ public static byte[] httpGetBytes(String url, Map<String, String> headers) throws IOException {
+ HttpURLConnection connection = prepareHttpConnection(url, headers);
+ try {
+ return httpFetch(connection);
+ } finally {
+ connection.disconnect();
+ }
+ }
- // Android's org.apache.http doesn't have the RedirectStrategy class
- if (status == HttpStatus.SC_MOVED_PERMANENTLY
- || status == HttpStatus.SC_MOVED_TEMPORARILY) {
- Header[] headers = response.getHeaders("Location");
+ public static String httpPost(String url, Map<String, String> headers, String postData)
+ throws IOException {
+ HttpURLConnection connection = prepareHttpConnection(url, headers);
- if (headers != null && headers.length != 0) {
- HttpGet newGet = new HttpGet(headers[headers.length - 1].getValue());
- for (Header header : request.getAllHeaders()) {
- newGet.addHeader(header);
+ try {
+ // write postData to buffered output stream
+ if (postData != null) {
+ connection.setDoOutput(true);
+ BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
+ connection.getOutputStream()));
+ try {
+ bw.write(postData, 0, postData.length());
+ // close connection and re-throw exception
+ } finally {
+ bw.close();
}
- return httpGet(newGet);
- } else {
- throw new IOException("Empty redirection header");
}
+ byte[] response = httpFetch(connection);
+ return new String(response, determineCharset(connection));
+ } finally {
+ connection.disconnect();
}
+ }
- if (status != HttpStatus.SC_OK) {
- throw new IOException("HTTP failure (status " + status + ")");
+ public static List<String> allRegexResults(String input, String regex, boolean dotall) {
+ if (input == null) {
+ return null;
}
+ Pattern pattern = Pattern.compile(regex, dotall ? Pattern.DOTALL : 0);
+ Matcher matcher = pattern.matcher(input);
- return EntityUtils.toString(response.getEntity());
+ List<String> regexResults = new ArrayList<String>();
+ while (matcher.find()) {
+ regexResults.add(matcher.group(1).trim());
+ }
+ return regexResults;
}
public static String firstRegexResult(String input, String regex, boolean dotall) {
@@ -81,4 +166,3 @@ public class LookupUtils {
return Html.fromHtml(input).toString().trim();
}
}
-