summaryrefslogtreecommitdiffstats
path: root/java/com/android/dialer/lookup/LookupUtils.java
blob: f13a33e54201e897df276129decbe85a867ac4c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright (C) 2014 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dialer.lookup;

import android.text.Html;

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;

public class LookupUtils {
    private static final String USER_AGENT =
            "Mozilla/5.0 (X11; Linux x86_64; rv:42.0) Gecko/20100101 Firefox/42.0";

    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();
    }

    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();
        }
    }

    public static byte[] httpGetBytes(String url, Map<String, String> headers) throws IOException {
        HttpURLConnection connection = prepareHttpConnection(url, headers);
        try {
            return httpFetch(connection);
        } finally {
            connection.disconnect();
        }
    }

    public static String httpPost(String url, Map<String, String> headers, String postData)
            throws IOException {
        HttpURLConnection connection = prepareHttpConnection(url, headers);

        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();
                }
            }
            byte[] response = httpFetch(connection);
            return new String(response, determineCharset(connection));
        } finally {
            connection.disconnect();
        }
    }

    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);

        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) {
        if (input == null) {
            return null;
        }
        Pattern pattern = Pattern.compile(regex, dotall ? Pattern.DOTALL : 0);
        Matcher m = pattern.matcher(input);
        return m.find() ? m.group(1).trim() : null;
    }

    public static String fromHtml(String input) {
        if (input == null) {
            return null;
        }
        return Html.fromHtml(input).toString().trim();
    }
}