HttpConnection.java revision a8b46a3d3b6ed1488df10740653829283572903b
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.HttpHost;
25import org.apache.http.params.BasicHttpParams;
26import org.apache.http.params.HttpConnectionParams;
27
28/**
29 * A requestConnection connecting to a normal (non secure) http server
30 *
31 * {@hide}
32 */
33class HttpConnection extends Connection {
34
35    HttpConnection(Context context, HttpHost host,
36                   RequestFeeder requestFeeder) {
37        super(context, host, requestFeeder);
38    }
39
40    /**
41     * Opens the connection to a http server
42     *
43     * @return the opened low level connection
44     * @throws IOException if the connection fails for any reason.
45     */
46    @Override
47    AndroidHttpClientConnection openConnection(Request req) throws IOException {
48
49        // Update the certificate info (connection not secure - set to null)
50        EventHandler eventHandler = req.getEventHandler();
51        mCertificate = null;
52        eventHandler.certificate(mCertificate);
53
54        AndroidHttpClientConnection conn = new AndroidHttpClientConnection();
55        BasicHttpParams params = new BasicHttpParams();
56        Socket sock = new Socket(mHost.getHostName(), mHost.getPort());
57        params.setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8192);
58        conn.bind(sock, params);
59        return conn;
60    }
61
62    /**
63     * Closes the low level connection.
64     *
65     * If an exception is thrown then it is assumed that the
66     * connection will have been closed (to the extent possible)
67     * anyway and the caller does not need to take any further action.
68     *
69     */
70    void closeConnection() {
71        try {
72            if (mHttpClientConnection != null && mHttpClientConnection.isOpen()) {
73                mHttpClientConnection.close();
74            }
75        } catch (IOException e) {
76            if (HttpLog.LOGV) HttpLog.v(
77                    "closeConnection(): failed closing connection " +
78                    mHost);
79            e.printStackTrace();
80        }
81    }
82
83    /**
84     * Restart a secure connection suspended waiting for user interaction.
85     */
86    void restartConnection(boolean abort) {
87        // not required for plain http connections
88    }
89
90    String getScheme() {
91        return "http";
92    }
93}
94