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
20d64419030e1fec1e751695dab3bd7236e2fb0214Roger Chenimport java.io.DataInputStream;
21d64419030e1fec1e751695dab3bd7236e2fb0214Roger Chenimport java.io.IOException;
22d64419030e1fec1e751695dab3bd7236e2fb0214Roger Chenimport java.net.SocketException;
23d64419030e1fec1e751695dab3bd7236e2fb0214Roger Chenimport java.net.URI;
24d64419030e1fec1e751695dab3bd7236e2fb0214Roger Chenimport java.net.URISyntaxException;
25d64419030e1fec1e751695dab3bd7236e2fb0214Roger Chenimport java.util.Locale;
26d64419030e1fec1e751695dab3bd7236e2fb0214Roger Chen
2772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.HttpEntity;
2872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.HttpHost;
2972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.HttpRequest;
3072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.HttpResponse;
3172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.StatusLine;
3272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.client.methods.HttpGet;
3372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.client.methods.HttpPost;
3472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.conn.params.ConnRouteParams;
35d64419030e1fec1e751695dab3bd7236e2fb0214Roger Chenimport org.apache.http.params.HttpConnectionParams;
3672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.params.HttpParams;
3772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport org.apache.http.params.HttpProtocolParams;
3886a1cf79bf92dbb2b9d09431379bff6de83c2581Tom Taylor
3972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport android.content.Context;
404d33cdcbb402a87ffeeaddb58237c70c4608c6f5Dianne Hackbornimport android.net.http.AndroidHttpClient;
4189e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafssonimport android.telephony.TelephonyManager;
4289e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafssonimport android.text.TextUtils;
43dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylorimport android.util.Config;
4472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport android.util.Log;
4572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
46d64419030e1fec1e751695dab3bd7236e2fb0214Roger Chenimport com.android.mms.LogTag;
47d64419030e1fec1e751695dab3bd7236e2fb0214Roger Chenimport com.android.mms.MmsConfig;
4872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
4972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectpublic class HttpUtils {
50812391ad832f3fdac054ad3a50af563da16e99b5Wei Huang    private static final String TAG = LogTag.TRANSACTION;
51812391ad832f3fdac054ad3a50af563da16e99b5Wei Huang
520ecc26df09777835cfa8dbfd3c48ca7b7fa7f011Tom Taylor    private static final boolean DEBUG = false;
530ecc26df09777835cfa8dbfd3c48ca7b7fa7f011Tom Taylor    private static final boolean LOCAL_LOGV = DEBUG ? Config.LOGD : Config.LOGV;
540ecc26df09777835cfa8dbfd3c48ca7b7fa7f011Tom Taylor
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
58208b7834ee01388a5fecacf00a9c1e1b8ffc5240Matthias Thomae    private static final int MMS_READ_BUFFER = 4096;
59208b7834ee01388a5fecacf00a9c1e1b8ffc5240Matthias Thomae
6072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    // This is the value to use for the "Accept-Language" header.
6172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    // Once it becomes possible for the user to change the locale
6272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    // setting, this should no longer be static.  We should call
6372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    // getHttpAcceptLanguage instead.
6472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    private static final String HDR_VALUE_ACCEPT_LANGUAGE;
6572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
6672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    static {
67c122ca5ebd2ccac240002425a567718a9577568cTom Taylor        HDR_VALUE_ACCEPT_LANGUAGE = getCurrentAcceptLanguage(Locale.getDefault());
6872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    }
6972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
7072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    // Definition for necessary HTTP headers.
7172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    private static final String HDR_KEY_ACCEPT = "Accept";
7272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    private static final String HDR_KEY_ACCEPT_LANGUAGE = "Accept-Language";
7372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
7472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    private static final String HDR_VALUE_ACCEPT =
7572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic";
7672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
7772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    private HttpUtils() {
7872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        // To forbidden instantiate this class.
7972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    }
8072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
8172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    /**
8272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * A helper method to send or retrieve data through HTTP protocol.
8372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     *
8472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * @param token The token to identify the sending progress.
8572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * @param url The URL used in a GET request. Null when the method is
8672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     *         HTTP_POST_METHOD.
8772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * @param pdu The data to be POST. Null when the method is HTTP_GET_METHOD.
8872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * @param method HTTP_POST_METHOD or HTTP_GET_METHOD.
8972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * @return A byte array which contains the response data.
9072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     *         If an HTTP error code is returned, an IOException will be thrown.
9172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * @throws IOException if any error occurred on network interface or
9272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     *         an HTTP error code(>=400) returned from the server.
9372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     */
9472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    protected static byte[] httpConnection(Context context, long token,
9572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            String url, byte[] pdu, int method, boolean isProxySet,
9672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            String proxyHost, int proxyPort) throws IOException {
9772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        if (url == null) {
9872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            throw new IllegalArgumentException("URL must not be null.");
9972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        }
10072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
101472cecc82e384f0c81d2a7885ade3db702213d82Tom Taylor        if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
10272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            Log.v(TAG, "httpConnection: params list");
10372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            Log.v(TAG, "\ttoken\t\t= " + token);
10472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            Log.v(TAG, "\turl\t\t= " + url);
10572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            Log.v(TAG, "\tmethod\t\t= "
10672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    + ((method == HTTP_POST_METHOD) ? "POST"
10772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                            : ((method == HTTP_GET_METHOD) ? "GET" : "UNKNOWN")));
10872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            Log.v(TAG, "\tisProxySet\t= " + isProxySet);
10972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            Log.v(TAG, "\tproxyHost\t= " + proxyHost);
11072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            Log.v(TAG, "\tproxyPort\t= " + proxyPort);
11172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            // TODO Print out binary data more readable.
11272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            //Log.v(TAG, "\tpdu\t\t= " + Arrays.toString(pdu));
11372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        }
11472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
11572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        AndroidHttpClient client = null;
11672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
11772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        try {
11872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            // Make sure to use a proxy which supports CONNECT.
11972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            URI hostUrl = new URI(url);
12072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            HttpHost target = new HttpHost(
12172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    hostUrl.getHost(), hostUrl.getPort(),
12272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    HttpHost.DEFAULT_SCHEME_NAME);
12372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
124d62a959cf2e7d261e296f82831942a9a99320175Dan Egnor            client = createHttpClient(context);
12572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            HttpRequest req = null;
12672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            switch(method) {
12772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                case HTTP_POST_METHOD:
12872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    ProgressCallbackEntity entity = new ProgressCallbackEntity(
12972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                                                        context, token, pdu);
13072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    // Set request content type.
13172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    entity.setContentType("application/vnd.wap.mms-message");
13272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
13372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    HttpPost post = new HttpPost(url);
13472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    post.setEntity(entity);
13572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    req = post;
13672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    break;
13772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                case HTTP_GET_METHOD:
13872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    req = new HttpGet(url);
13972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    break;
14072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                default:
14172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    Log.e(TAG, "Unknown HTTP method: " + method
14272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                            + ". Must be one of POST[" + HTTP_POST_METHOD
14372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                            + "] or GET[" + HTTP_GET_METHOD + "].");
14472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    return null;
14572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
14672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
14772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            // Set route parameters for the request.
14872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            HttpParams params = client.getParams();
14972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            if (isProxySet) {
15072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                ConnRouteParams.setDefaultProxy(
15172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                        params, new HttpHost(proxyHost, proxyPort));
15272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
15372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            req.setParams(params);
15472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
15572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            // Set necessary HTTP headers for MMS transmission.
15672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            req.addHeader(HDR_KEY_ACCEPT, HDR_VALUE_ACCEPT);
15772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            {
15889e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                String xWapProfileTagName = MmsConfig.getUaProfTagName();
15986a1cf79bf92dbb2b9d09431379bff6de83c2581Tom Taylor                String xWapProfileUrl = MmsConfig.getUaProfUrl();
16072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
16172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                if (xWapProfileUrl != null) {
1621b37c93a3b67483ee9b0ad604a2e1e9d8aaa84d9Wei Huang                    if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
1631b37c93a3b67483ee9b0ad604a2e1e9d8aaa84d9Wei Huang                        Log.d(LogTag.TRANSACTION,
1641b37c93a3b67483ee9b0ad604a2e1e9d8aaa84d9Wei Huang                                "[HttpUtils] httpConn: xWapProfUrl=" + xWapProfileUrl);
1651b37c93a3b67483ee9b0ad604a2e1e9d8aaa84d9Wei Huang                    }
16689e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                    req.addHeader(xWapProfileTagName, xWapProfileUrl);
16789e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                }
16889e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson            }
16989e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson
17089e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson            // Extra http parameters. Split by '|' to get a list of value pairs.
17189e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson            // Separate each pair by the first occurrence of ':' to obtain a name and
17289e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson            // value. Replace the occurrence of the string returned by
17389e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson            // MmsConfig.getHttpParamsLine1Key() with the users telephone number inside
17489e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson            // the value.
17589e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson            String extraHttpParams = MmsConfig.getHttpParams();
17689e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson
17789e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson            if (extraHttpParams != null) {
17889e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                String line1Number = ((TelephonyManager)context
17989e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                        .getSystemService(Context.TELEPHONY_SERVICE))
18089e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                        .getLine1Number();
18189e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                String line1Key = MmsConfig.getHttpParamsLine1Key();
18289e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                String paramList[] = extraHttpParams.split("\\|");
18389e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson
18489e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                for (String paramPair : paramList) {
18589e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                    String splitPair[] = paramPair.split(":", 2);
18689e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson
18789e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                    if (splitPair.length == 2) {
18889e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                        String name = splitPair[0].trim();
18989e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                        String value = splitPair[1].trim();
19089e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson
19189e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                        if (line1Key != null) {
19289e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                            value = value.replace(line1Key, line1Number);
19389e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                        }
19489e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                        if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(value)) {
19589e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                            req.addHeader(name, value);
19689e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                        }
19789e7af1acc0b62f2822ace8df6d56f4c6d37a40fChristian Gustafsson                    }
19872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                }
19972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
20072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            req.addHeader(HDR_KEY_ACCEPT_LANGUAGE, HDR_VALUE_ACCEPT_LANGUAGE);
20172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
20272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            HttpResponse response = client.execute(target, req);
20372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            StatusLine status = response.getStatusLine();
20472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            if (status.getStatusCode() != 200) { // HTTP 200 is success.
20572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                throw new IOException("HTTP error: " + status.getReasonPhrase());
20672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
20772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
20872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            HttpEntity entity = response.getEntity();
20972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            byte[] body = null;
21072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            if (entity != null) {
21172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                try {
212dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                    if (entity.getContentLength() > 0) {
213dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                        body = new byte[(int) entity.getContentLength()];
214dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                        DataInputStream dis = new DataInputStream(entity.getContent());
215dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                        try {
216dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                            dis.readFully(body);
217dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                        } finally {
218dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                            try {
219dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                dis.close();
220dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                            } catch (IOException e) {
221dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                Log.e(TAG, "Error closing input stream: " + e.getMessage());
222dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                            }
223208b7834ee01388a5fecacf00a9c1e1b8ffc5240Matthias Thomae                        }
224dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                    }
225dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                    if (entity.isChunked()) {
226dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                        Log.v(TAG, "httpConnection: transfer encoding is chunked");
227dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                        int bytesTobeRead = MmsConfig.getMaxMessageSize();
228dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                        byte[] tempBody = new byte[bytesTobeRead];
229dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                        DataInputStream dis = new DataInputStream(entity.getContent());
23072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                        try {
231dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                            int bytesRead = 0;
232dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                            int offset = 0;
233dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                            boolean readError = false;
234dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                            do {
235dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                try {
236dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                    bytesRead = dis.read(tempBody, offset, bytesTobeRead);
237dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                } catch (IOException e) {
238dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                    readError = true;
239dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                    Log.e(TAG, "httpConnection: error reading input stream"
240dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                        + e.getMessage());
241dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                    break;
242dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                }
243dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                if (bytesRead > 0) {
244dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                    bytesTobeRead -= bytesRead;
245dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                    offset += bytesRead;
246dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                }
247dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                            } while (bytesRead >= 0 && bytesTobeRead > 0);
248dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                            if (bytesRead == -1 && offset > 0 && !readError) {
249dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                // offset is same as total number of bytes read
250dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                // bytesRead will be -1 if the data was read till the eof
251dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                body = new byte[offset];
252dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                System.arraycopy(tempBody, 0, body, 0, offset);
253dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                Log.v(TAG, "httpConnection: Chunked response length ["
254dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                    + Integer.toString(offset) + "]");
255dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                            } else {
256dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                Log.e(TAG, "httpConnection: Response entity too large or empty");
257dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                            }
258dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                        } finally {
259dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                            try {
260dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                dis.close();
261dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                            } catch (IOException e) {
262dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                                Log.e(TAG, "Error closing input stream: " + e.getMessage());
263dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                            }
26472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                        }
26572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                    }
26672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                } finally {
267dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                    if (entity != null) {
268dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                        entity.consumeContent();
269dd374f47c61f4fed8e6702c1f9d3f33ac5b77d02Tom Taylor                    }
27072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                }
27172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
27272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            return body;
27372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        } catch (URISyntaxException e) {
2747a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor            handleHttpConnectionException(e, url);
27572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        } catch (IllegalStateException e) {
2767a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor            handleHttpConnectionException(e, url);
27772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        } catch (IllegalArgumentException e) {
2787a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor            handleHttpConnectionException(e, url);
27972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        } catch (SocketException e) {
2807a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor            handleHttpConnectionException(e, url);
28172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        } catch (Exception e) {
2827a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor            handleHttpConnectionException(e, url);
28372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        }
28472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        finally {
28572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            if (client != null) {
28672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                client.close();
28772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
28872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        }
28972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        return null;
29072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    }
29172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
2927a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor    private static void handleHttpConnectionException(Exception exception, String url)
29372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            throws IOException {
29472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        // Inner exception should be logged to make life easier.
2957a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor        Log.e(TAG, "Url: " + url + "\n" + exception.getMessage());
2967a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor        IOException e = new IOException(exception.getMessage());
2977a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor        e.initCause(exception);
2987a17eaedae4c54b72f420380ec7129a15442cbbcTom Taylor        throw e;
29972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    }
30072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
301d62a959cf2e7d261e296f82831942a9a99320175Dan Egnor    private static AndroidHttpClient createHttpClient(Context context) {
3021a0ec0c1576cab49e02a1dab796ed2be33f0a0a5Wei Huang        String userAgent = MmsConfig.getUserAgent();
303d62a959cf2e7d261e296f82831942a9a99320175Dan Egnor        AndroidHttpClient client = AndroidHttpClient.newInstance(userAgent, context);
30472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        HttpParams params = client.getParams();
30572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        HttpProtocolParams.setContentCharset(params, "UTF-8");
306293000e15498b71a4986d7247e60fe8bdaa05a4bWei Huang
307293000e15498b71a4986d7247e60fe8bdaa05a4bWei Huang        // set the socket timeout
308293000e15498b71a4986d7247e60fe8bdaa05a4bWei Huang        int soTimeout = MmsConfig.getHttpSocketTimeout();
309293000e15498b71a4986d7247e60fe8bdaa05a4bWei Huang
3101a0ec0c1576cab49e02a1dab796ed2be33f0a0a5Wei Huang        if (Log.isLoggable(LogTag.TRANSACTION, Log.DEBUG)) {
3111a0ec0c1576cab49e02a1dab796ed2be33f0a0a5Wei Huang            Log.d(TAG, "[HttpUtils] createHttpClient w/ socket timeout " + soTimeout + " ms, "
3121a0ec0c1576cab49e02a1dab796ed2be33f0a0a5Wei Huang                    + ", UA=" + userAgent);
313293000e15498b71a4986d7247e60fe8bdaa05a4bWei Huang        }
314293000e15498b71a4986d7247e60fe8bdaa05a4bWei Huang        HttpConnectionParams.setSoTimeout(params, soTimeout);
31572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        return client;
31672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    }
31772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
318c122ca5ebd2ccac240002425a567718a9577568cTom Taylor    private static final String ACCEPT_LANG_FOR_US_LOCALE = "en-US";
319c122ca5ebd2ccac240002425a567718a9577568cTom Taylor
32072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    /**
32172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * Return the Accept-Language header.  Use the current locale plus
32272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * US if we are in a different locale than US.
323c122ca5ebd2ccac240002425a567718a9577568cTom Taylor     * This code copied from the browser's WebSettings.java
324c122ca5ebd2ccac240002425a567718a9577568cTom Taylor     * @return Current AcceptLanguage String.
32572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     */
326c122ca5ebd2ccac240002425a567718a9577568cTom Taylor    public static String getCurrentAcceptLanguage(Locale locale) {
327c122ca5ebd2ccac240002425a567718a9577568cTom Taylor        StringBuilder buffer = new StringBuilder();
328c122ca5ebd2ccac240002425a567718a9577568cTom Taylor        addLocaleToHttpAcceptLanguage(buffer, locale);
329c122ca5ebd2ccac240002425a567718a9577568cTom Taylor
330c122ca5ebd2ccac240002425a567718a9577568cTom Taylor        if (!Locale.US.equals(locale)) {
331c122ca5ebd2ccac240002425a567718a9577568cTom Taylor            if (buffer.length() > 0) {
332c122ca5ebd2ccac240002425a567718a9577568cTom Taylor                buffer.append(", ");
33372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
334c122ca5ebd2ccac240002425a567718a9577568cTom Taylor            buffer.append(ACCEPT_LANG_FOR_US_LOCALE);
33572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        }
336c122ca5ebd2ccac240002425a567718a9577568cTom Taylor
337c122ca5ebd2ccac240002425a567718a9577568cTom Taylor        return buffer.toString();
33872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    }
33972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
340c122ca5ebd2ccac240002425a567718a9577568cTom Taylor    /**
341c122ca5ebd2ccac240002425a567718a9577568cTom Taylor     * Convert obsolete language codes, including Hebrew/Indonesian/Yiddish,
342c122ca5ebd2ccac240002425a567718a9577568cTom Taylor     * to new standard.
343c122ca5ebd2ccac240002425a567718a9577568cTom Taylor     */
344c122ca5ebd2ccac240002425a567718a9577568cTom Taylor    private static String convertObsoleteLanguageCodeToNew(String langCode) {
345c122ca5ebd2ccac240002425a567718a9577568cTom Taylor        if (langCode == null) {
346c122ca5ebd2ccac240002425a567718a9577568cTom Taylor            return null;
347c122ca5ebd2ccac240002425a567718a9577568cTom Taylor        }
348c122ca5ebd2ccac240002425a567718a9577568cTom Taylor        if ("iw".equals(langCode)) {
349c122ca5ebd2ccac240002425a567718a9577568cTom Taylor            // Hebrew
350c122ca5ebd2ccac240002425a567718a9577568cTom Taylor            return "he";
351c122ca5ebd2ccac240002425a567718a9577568cTom Taylor        } else if ("in".equals(langCode)) {
352c122ca5ebd2ccac240002425a567718a9577568cTom Taylor            // Indonesian
353c122ca5ebd2ccac240002425a567718a9577568cTom Taylor            return "id";
354c122ca5ebd2ccac240002425a567718a9577568cTom Taylor        } else if ("ji".equals(langCode)) {
355c122ca5ebd2ccac240002425a567718a9577568cTom Taylor            // Yiddish
356c122ca5ebd2ccac240002425a567718a9577568cTom Taylor            return "yi";
357c122ca5ebd2ccac240002425a567718a9577568cTom Taylor        }
358c122ca5ebd2ccac240002425a567718a9577568cTom Taylor        return langCode;
359c122ca5ebd2ccac240002425a567718a9577568cTom Taylor    }
36072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
361c122ca5ebd2ccac240002425a567718a9577568cTom Taylor    private static void addLocaleToHttpAcceptLanguage(StringBuilder builder,
362c122ca5ebd2ccac240002425a567718a9577568cTom Taylor                                                      Locale locale) {
363c122ca5ebd2ccac240002425a567718a9577568cTom Taylor        String language = convertObsoleteLanguageCodeToNew(locale.getLanguage());
36472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        if (language != null) {
36572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            builder.append(language);
36672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            String country = locale.getCountry();
36772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            if (country != null) {
36872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                builder.append("-");
36972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                builder.append(country);
37072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
37172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        }
37272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    }
37372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project}
374