172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project/*
272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * Copyright (C) 2008 Esmertec AG.
372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project *
572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * you may not use this file except in compliance with the License.
772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * You may obtain a copy of the License at
872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project *
972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
1072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project *
1172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software
1272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
1372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * See the License for the specific language governing permissions and
1572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * limitations under the License.
1672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project */
1772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
1872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectpackage com.android.mms.transaction;
19812391ad832f3fdac054ad3a50af563da16e99b5Wei Huang
2072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.HttpEntity;
2172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.HttpHost;
2272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.HttpRequest;
2372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.HttpResponse;
2472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.StatusLine;
2572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.client.methods.HttpGet;
2672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.client.methods.HttpPost;
2772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.conn.params.ConnRouteParams;
2872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.params.HttpParams;
2972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.params.HttpProtocolParams;
30293000e15498b71a4986d7247e60fe8bdaa05a4bWei Huangimport org.apache.http.params.HttpConnectionParams;
3172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
3286a1cf79bf92dbb2b9d09431379bff6de83c2581Tom Taylorimport com.android.mms.MmsConfig;
33812391ad832f3fdac054ad3a50af563da16e99b5Wei Huangimport com.android.mms.LogTag;
3486a1cf79bf92dbb2b9d09431379bff6de83c2581Tom Taylor
3572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport android.content.Context;
364d33cdcbb402a87ffeeaddb58237c70c4608c6f5Dianne Hackbornimport android.net.http.AndroidHttpClient;
3789e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafssonimport android.telephony.TelephonyManager;
3889e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafssonimport android.text.TextUtils;
3972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport android.util.Config;
4072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport android.util.Log;
4172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
4272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport java.io.DataInputStream;
4372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport java.io.IOException;
4472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport java.net.SocketException;
4572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport java.net.URI;
4672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport java.net.URISyntaxException;
4772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport java.util.Locale;
4872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
4972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectpublic class HttpUtils {
50812391ad832f3fdac054ad3a50af563da16e99b5Wei Huang    private static final String TAG = LogTag.TRANSACTION;
51812391ad832f3fdac054ad3a50af563da16e99b5Wei Huang
5272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    private static final boolean DEBUG = false;
5372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    private static final boolean LOCAL_LOGV = DEBUG ? Config.LOGD : Config.LOGV;
5472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
5572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    public static final int HTTP_POST_METHOD = 1;
5672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    public static final int HTTP_GET_METHOD = 2;
5772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
5872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    // This is the value to use for the "Accept-Language" header.
5972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    // Once it becomes possible for the user to change the locale
6072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    // setting, this should no longer be static.  We should call
6172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    // getHttpAcceptLanguage instead.
6272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    private static final String HDR_VALUE_ACCEPT_LANGUAGE;
6372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
6472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    static {
6572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        HDR_VALUE_ACCEPT_LANGUAGE = getHttpAcceptLanguage();
6672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    }
6772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
6872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    // Definition for necessary HTTP headers.
6972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    private static final String HDR_KEY_ACCEPT = "Accept";
7072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    private static final String HDR_KEY_ACCEPT_LANGUAGE = "Accept-Language";
7172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
7272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    private static final String HDR_VALUE_ACCEPT =
7372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic";
7472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
7572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    private HttpUtils() {
7672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        // To forbidden instantiate this class.
7772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    }
7872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
7972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    /**
8072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * A helper method to send or retrieve data through HTTP protocol.
8172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     *
8272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * @param token The token to identify the sending progress.
8372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * @param url The URL used in a GET request. Null when the method is
8472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     *         HTTP_POST_METHOD.
8572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * @param pdu The data to be POST. Null when the method is HTTP_GET_METHOD.
8672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * @param method HTTP_POST_METHOD or HTTP_GET_METHOD.
8772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * @return A byte array which contains the response data.
8872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     *         If an HTTP error code is returned, an IOException will be thrown.
8972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * @throws IOException if any error occurred on network interface or
9072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     *         an HTTP error code(>=400) returned from the server.
9172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     */
9272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    protected static byte[] httpConnection(Context context, long token,
9372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            String url, byte[] pdu, int method, boolean isProxySet,
9472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            String proxyHost, int proxyPort) throws IOException {
9572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        if (url == null) {
9672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            throw new IllegalArgumentException("URL must not be null.");
9772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        }
9872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
9972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        if (LOCAL_LOGV) {
10072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            Log.v(TAG, "httpConnection: params list");
10172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            Log.v(TAG, "\ttoken\t\t= " + token);
10272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            Log.v(TAG, "\turl\t\t= " + url);
10372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            Log.v(TAG, "\tmethod\t\t= "
10472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    + ((method == HTTP_POST_METHOD) ? "POST"
10572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                            : ((method == HTTP_GET_METHOD) ? "GET" : "UNKNOWN")));
10672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            Log.v(TAG, "\tisProxySet\t= " + isProxySet);
10772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            Log.v(TAG, "\tproxyHost\t= " + proxyHost);
10872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            Log.v(TAG, "\tproxyPort\t= " + proxyPort);
10972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            // TODO Print out binary data more readable.
11072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            //Log.v(TAG, "\tpdu\t\t= " + Arrays.toString(pdu));
11172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        }
11272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
11372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        AndroidHttpClient client = null;
11472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
11572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        try {
11672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            // Make sure to use a proxy which supports CONNECT.
11772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            URI hostUrl = new URI(url);
11872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            HttpHost target = new HttpHost(
11972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    hostUrl.getHost(), hostUrl.getPort(),
12072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    HttpHost.DEFAULT_SCHEME_NAME);
12172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
122d62a959cf2e7d261e296f82831942a9a99320175Dan Egnor            client = createHttpClient(context);
12372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            HttpRequest req = null;
12472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            switch(method) {
12572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                case HTTP_POST_METHOD:
12672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    ProgressCallbackEntity entity = new ProgressCallbackEntity(
12772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                                                        context, token, pdu);
12872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    // Set request content type.
12972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    entity.setContentType("application/vnd.wap.mms-message");
13072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
13172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    HttpPost post = new HttpPost(url);
13272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    post.setEntity(entity);
13372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    req = post;
13472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    break;
13572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                case HTTP_GET_METHOD:
13672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    req = new HttpGet(url);
13772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    break;
13872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                default:
13972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    Log.e(TAG, "Unknown HTTP method: " + method
14072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                            + ". Must be one of POST[" + HTTP_POST_METHOD
14172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                            + "] or GET[" + HTTP_GET_METHOD + "].");
14272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    return null;
14372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
14472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
14572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            // Set route parameters for the request.
14672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            HttpParams params = client.getParams();
14772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            if (isProxySet) {
14872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                ConnRouteParams.setDefaultProxy(
14972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                        params, new HttpHost(proxyHost, proxyPort));
15072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
15172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            req.setParams(params);
15272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
15372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            // Set necessary HTTP headers for MMS transmission.
15472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            req.addHeader(HDR_KEY_ACCEPT, HDR_VALUE_ACCEPT);
15572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            {
15689e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                String xWapProfileTagName = MmsConfig.getUaProfTagName();
15786a1cf79bf92dbb2b9d09431379bff6de83c2581Tom Taylor                String xWapProfileUrl = MmsConfig.getUaProfUrl();
15872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
15972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                if (xWapProfileUrl != null) {
1601b37c93a3b67483ee9b0ad604a2e1e9d8aaa84d9Wei Huang                    if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
1611b37c93a3b67483ee9b0ad604a2e1e9d8aaa84d9Wei Huang                        Log.d(LogTag.TRANSACTION,
1621b37c93a3b67483ee9b0ad604a2e1e9d8aaa84d9Wei Huang                                "[HttpUtils] httpConn: xWapProfUrl=" + xWapProfileUrl);
1631b37c93a3b67483ee9b0ad604a2e1e9d8aaa84d9Wei Huang                    }
16489e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                    req.addHeader(xWapProfileTagName, xWapProfileUrl);
16589e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                }
16689e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson            }
16789e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson
16889e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson            // Extra http parameters. Split by '|' to get a list of value pairs.
16989e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson            // Separate each pair by the first occurrence of ':' to obtain a name and
17089e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson            // value. Replace the occurrence of the string returned by
17189e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson            // MmsConfig.getHttpParamsLine1Key() with the users telephone number inside
17289e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson            // the value.
17389e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson            String extraHttpParams = MmsConfig.getHttpParams();
17489e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson
17589e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson            if (extraHttpParams != null) {
17689e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                String line1Number = ((TelephonyManager)context
17789e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                        .getSystemService(Context.TELEPHONY_SERVICE))
17889e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                        .getLine1Number();
17989e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                String line1Key = MmsConfig.getHttpParamsLine1Key();
18089e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                String paramList[] = extraHttpParams.split("\\|");
18189e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson
18289e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                for (String paramPair : paramList) {
18389e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                    String splitPair[] = paramPair.split(":", 2);
18489e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson
18589e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                    if (splitPair.length == 2) {
18689e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                        String name = splitPair[0].trim();
18789e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                        String value = splitPair[1].trim();
18889e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson
18989e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                        if (line1Key != null) {
19089e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                            value = value.replace(line1Key, line1Number);
19189e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                        }
19289e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                        if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(value)) {
19389e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                            req.addHeader(name, value);
19489e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                        }
19589e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                    }
19672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                }
19772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
19872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            req.addHeader(HDR_KEY_ACCEPT_LANGUAGE, HDR_VALUE_ACCEPT_LANGUAGE);
19972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
20072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            HttpResponse response = client.execute(target, req);
20172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            StatusLine status = response.getStatusLine();
20272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            if (status.getStatusCode() != 200) { // HTTP 200 is success.
20372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                throw new IOException("HTTP error: " + status.getReasonPhrase());
20472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
20572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
20672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            HttpEntity entity = response.getEntity();
20772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            byte[] body = null;
20872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            if (entity != null) {
20972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                try {
21072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    if (entity.getContentLength() > 0) {
21172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                        body = new byte[(int) entity.getContentLength()];
21272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                        DataInputStream dis = new DataInputStream(entity.getContent());
21372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                        try {
21472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                            dis.readFully(body);
21572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                        } finally {
21672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                            try {
21772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                                dis.close();
21872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                            } catch (IOException e) {
21972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                                Log.e(TAG, "Error closing input stream: " + e.getMessage());
22072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                            }
22172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                        }
22272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    }
22372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                } finally {
22472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    if (entity != null) {
22572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                        entity.consumeContent();
22672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    }
22772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                }
22872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
22972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            return body;
23072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        } catch (URISyntaxException e) {
2317a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor            handleHttpConnectionException(e, url);
23272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        } catch (IllegalStateException e) {
2337a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor            handleHttpConnectionException(e, url);
23472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        } catch (IllegalArgumentException e) {
2357a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor            handleHttpConnectionException(e, url);
23672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        } catch (SocketException e) {
2377a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor            handleHttpConnectionException(e, url);
23872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        } catch (Exception e) {
2397a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor            handleHttpConnectionException(e, url);
24072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        }
24172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        finally {
24272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            if (client != null) {
24372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                client.close();
24472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
24572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        }
24672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        return null;
24772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    }
24872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
2497a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor    private static void handleHttpConnectionException(Exception exception, String url)
25072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            throws IOException {
25172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        // Inner exception should be logged to make life easier.
2527a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor        Log.e(TAG, "Url: " + url + "\n" + exception.getMessage());
2537a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor        IOException e = new IOException(exception.getMessage());
2547a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor        e.initCause(exception);
2557a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor        throw e;
25672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    }
25772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
258d62a959cf2e7d261e296f82831942a9a99320175Dan Egnor    private static AndroidHttpClient createHttpClient(Context context) {
2591a0ec0c1576cab49e02a1dab796ed2be33f0a0a5Wei Huang        String userAgent = MmsConfig.getUserAgent();
260d62a959cf2e7d261e296f82831942a9a99320175Dan Egnor        AndroidHttpClient client = AndroidHttpClient.newInstance(userAgent, context);
26172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        HttpParams params = client.getParams();
26272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        HttpProtocolParams.setContentCharset(params, "UTF-8");
263293000e15498b71a4986d7247e60fe8bdaa05a4bWei Huang
264293000e15498b71a4986d7247e60fe8bdaa05a4bWei Huang        // set the socket timeout
265293000e15498b71a4986d7247e60fe8bdaa05a4bWei Huang        int soTimeout = MmsConfig.getHttpSocketTimeout();
266293000e15498b71a4986d7247e60fe8bdaa05a4bWei Huang
2671a0ec0c1576cab49e02a1dab796ed2be33f0a0a5Wei Huang        if (Log.isLoggable(LogTag.TRANSACTION, Log.DEBUG)) {
2681a0ec0c1576cab49e02a1dab796ed2be33f0a0a5Wei Huang            Log.d(TAG, "[HttpUtils] createHttpClient w/ socket timeout " + soTimeout + " ms, "
2691a0ec0c1576cab49e02a1dab796ed2be33f0a0a5Wei Huang                    + ", UA=" + userAgent);
270293000e15498b71a4986d7247e60fe8bdaa05a4bWei Huang        }
271293000e15498b71a4986d7247e60fe8bdaa05a4bWei Huang        HttpConnectionParams.setSoTimeout(params, soTimeout);
27272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        return client;
27372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    }
27472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
27572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    /**
27672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * Return the Accept-Language header.  Use the current locale plus
27772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * US if we are in a different locale than US.
27872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     */
27972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    private static String getHttpAcceptLanguage() {
28072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        Locale locale = Locale.getDefault();
28172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        StringBuilder builder = new StringBuilder();
28272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
28372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        addLocaleToHttpAcceptLanguage(builder, locale);
28472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        if (!locale.equals(Locale.US)) {
28572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            if (builder.length() > 0) {
28672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                builder.append(", ");
28772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
28872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            addLocaleToHttpAcceptLanguage(builder, Locale.US);
28972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        }
29072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        return builder.toString();
29172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    }
29272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
29372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    private static void addLocaleToHttpAcceptLanguage(
29472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            StringBuilder builder, Locale locale) {
29572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        String language = locale.getLanguage();
29672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
29772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        if (language != null) {
29872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            builder.append(language);
29972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
30072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            String country = locale.getCountry();
30172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
30272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            if (country != null) {
30372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                builder.append("-");
30472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                builder.append(country);
30572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
30672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        }
30772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    }
30872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project}
309