HttpConnection.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net.http;
18
19import android.content.Context;
20
21import java.net.Socket;
22import java.io.IOException;
23
24import org.apache.http.HttpClientConnection;
25import org.apache.http.HttpHost;
26import org.apache.http.impl.DefaultHttpClientConnection;
27import org.apache.http.params.BasicHttpParams;
28import org.apache.http.params.HttpConnectionParams;
29
30/**
31 * A requestConnection connecting to a normal (non secure) http server
32 *
33 * {@hide}
34 */
35class HttpConnection extends Connection {
36
37    HttpConnection(Context context, HttpHost host,
38                   RequestQueue.ConnectionManager connectionManager,
39                   RequestFeeder requestFeeder) {
40        super(context, host, connectionManager, requestFeeder);
41    }
42
43    /**
44     * Opens the connection to a http server
45     *
46     * @return the opened low level connection
47     * @throws IOException if the connection fails for any reason.
48     */
49    @Override
50    AndroidHttpClientConnection openConnection(Request req) throws IOException {
51
52        // Update the certificate info (connection not secure - set to null)
53        EventHandler eventHandler = req.getEventHandler();
54        mCertificate = null;
55        eventHandler.certificate(mCertificate);
56
57        AndroidHttpClientConnection conn = new AndroidHttpClientConnection();
58        BasicHttpParams params = new BasicHttpParams();
59        Socket sock = new Socket(mHost.getHostName(), mHost.getPort());
60        params.setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8192);
61        conn.bind(sock, params);
62        return conn;
63    }
64
65    /**
66     * Closes the low level connection.
67     *
68     * If an exception is thrown then it is assumed that the
69     * connection will have been closed (to the extent possible)
70     * anyway and the caller does not need to take any further action.
71     *
72     */
73    void closeConnection() {
74        try {
75            if (mHttpClientConnection != null && mHttpClientConnection.isOpen()) {
76                mHttpClientConnection.close();
77            }
78        } catch (IOException e) {
79            if (HttpLog.LOGV) HttpLog.v(
80                    "closeConnection(): failed closing connection " +
81                    mHost);
82            e.printStackTrace();
83        }
84    }
85
86    /**
87     * Restart a secure connection suspended waiting for user interaction.
88     */
89    void restartConnection(boolean abort) {
90        // not required for plain http connections
91    }
92
93    String getScheme() {
94        return "http";
95    }
96}
97