1069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/*
2069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/AbstractPoolEntry.java $
3069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Revision: 658775 $
4069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Date: 2008-05-21 10:30:45 -0700 (Wed, 21 May 2008) $
5069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
6069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
7069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
8069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  Licensed to the Apache Software Foundation (ASF) under one or more
9069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  contributor license agreements.  See the NOTICE file distributed with
10069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  this work for additional information regarding copyright ownership.
11069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  The ASF licenses this file to You under the Apache License, Version 2.0
12069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  (the "License"); you may not use this file except in compliance with
13069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  the License.  You may obtain a copy of the License at
14069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
15069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
16069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
17069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  Unless required by applicable law or agreed to in writing, software
18069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  distributed under the License is distributed on an "AS IS" BASIS,
19069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  See the License for the specific language governing permissions and
21069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  limitations under the License.
22069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
23069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
24069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This software consists of voluntary contributions made by many
25069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * individuals on behalf of the Apache Software Foundation.  For more
26069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * information on the Apache Software Foundation, please see
27069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <http://www.apache.org/>.
28069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
29069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
30069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
31069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpackage org.apache.http.impl.conn;
32069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
33069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
34069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.io.IOException;
35069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
36069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.HttpHost;
37069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.params.HttpParams;
38069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.protocol.HttpContext;
39069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.conn.routing.HttpRoute;
40069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.conn.routing.RouteTracker;
41069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.conn.ClientConnectionOperator;
42069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.conn.OperatedClientConnection;
43069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
44069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
45069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
46069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/**
47069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * A pool entry for use by connection manager implementations.
48069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Pool entries work in conjunction with an
49069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * {@link AbstractClientConnAdapter adapter}.
50069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * The adapter is handed out to applications that obtain a connection.
51069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * The pool entry stores the underlying connection and tracks the
52069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * {@link HttpRoute route} established.
53069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * The adapter delegates methods for establishing the route to
54069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * it's pool entry.
55069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <br/>
56069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * If the managed connections is released or revoked, the adapter
57069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * gets disconnected, but the pool entry still contains the
58069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * underlying connection and the established route.
59069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
60069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
61069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
62069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
63069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
64069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <!-- empty lines to avoid svn diff problems -->
65069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @version   $Revision: 658775 $
66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
67069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @since 4.0
68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
69069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic abstract class AbstractPoolEntry {
70069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
71069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The connection operator. */
72069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected final ClientConnectionOperator connOperator;
73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
74069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The underlying connection being pooled or used. */
75069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected final OperatedClientConnection connection;
76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The route for which this entry gets allocated. */
78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    //@@@ currently accessed from connection manager(s) as attribute
79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    //@@@ avoid that, derived classes should decide whether update is allowed
80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    //@@@ SCCM: yes, TSCCM: no
81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected volatile HttpRoute route;
82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Connection state object */
84069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected volatile Object state;
85069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
86069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The tracked route, or <code>null</code> before tracking starts. */
87069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected volatile RouteTracker tracker;
88069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
89069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
90069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
91069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Creates a new pool entry.
92069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
93069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param connOperator     the Connection Operator for this entry
94069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param route   the planned route for the connection,
95069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                or <code>null</code>
96069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
97069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected AbstractPoolEntry(ClientConnectionOperator connOperator,
98069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                                HttpRoute route) {
99069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        super();
100069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (connOperator == null) {
101069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException("Connection operator may not be null");
102069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
103069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.connOperator = connOperator;
104069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.connection = connOperator.createConnection();
105069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.route = route;
106069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.tracker = null;
107069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
108069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
109069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
110069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns the state object associated with this pool entry.
111069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
112069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return The state object
113069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
114069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public Object getState() {
115069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return state;
116069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
117069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
118069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
119069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Assigns a state object to this pool entry.
120069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
121069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param state The state object
122069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
123069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void setState(final Object state) {
124069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.state = state;
125069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
126069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
127069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
128069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Opens the underlying connection.
129069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
130069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param route         the route along which to open the connection
131069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param context       the context for opening the connection
132069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param params        the parameters for opening the connection
133069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
134069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws IOException  in case of a problem
135069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
136069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void open(HttpRoute route,
137069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                     HttpContext context, HttpParams params)
138069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        throws IOException {
139069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
140069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (route == null) {
141069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException
142069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("Route must not be null.");
143069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
144069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        //@@@ is context allowed to be null? depends on operator?
145069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (params == null) {
146069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException
147069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("Parameters must not be null.");
148069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
149069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if ((this.tracker != null) && this.tracker.isConnected()) {
150069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalStateException("Connection already open.");
151069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
152069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
153069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // - collect the arguments
154069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // - call the operator
155069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // - update the tracking data
156069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // In this order, we can be sure that only a successful
157069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // opening of the connection will be tracked.
158069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
159069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        //@@@ verify route against planned route?
160069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
161069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.tracker = new RouteTracker(route);
162069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        final HttpHost proxy  = route.getProxyHost();
163069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
164069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        connOperator.openConnection
165069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            (this.connection,
166069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project             (proxy != null) ? proxy : route.getTargetHost(),
167069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project             route.getLocalAddress(),
168069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project             context, params);
169069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
170069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        RouteTracker localTracker = tracker; // capture volatile
171069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
172069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // If this tracker was reset while connecting,
173069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // fail early.
174069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (localTracker == null) {
175069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IOException("Request aborted");
176069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
177069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
178069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (proxy == null) {
179069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            localTracker.connectTarget(this.connection.isSecure());
180069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } else {
181069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            localTracker.connectProxy(proxy, this.connection.isSecure());
182069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
183069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
184069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    } // open
185069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
186069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
187069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
188069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Tracks tunnelling of the connection to the target.
189069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * The tunnel has to be established outside by sending a CONNECT
190069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * request to the (last) proxy.
191069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
192069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param secure    <code>true</code> if the tunnel should be
193069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  considered secure, <code>false</code> otherwise
194069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param params    the parameters for tunnelling the connection
195069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
196069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws IOException  in case of a problem
197069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
198069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void tunnelTarget(boolean secure, HttpParams params)
199069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        throws IOException {
200069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
201069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (params == null) {
202069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException
203069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("Parameters must not be null.");
204069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
205069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
206069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        //@@@ check for proxy in planned route?
207069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if ((this.tracker == null) || !this.tracker.isConnected()) {
208069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalStateException("Connection not open.");
209069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
210069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (this.tracker.isTunnelled()) {
211069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalStateException
212069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("Connection is already tunnelled.");
213069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
214069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
215069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // LOG.debug?
216069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
217069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.connection.update(null, tracker.getTargetHost(),
218069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                               secure, params);
219069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.tracker.tunnelTarget(secure);
220069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
221069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    } // tunnelTarget
222069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
223069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
224069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
225069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Tracks tunnelling of the connection to a chained proxy.
226069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * The tunnel has to be established outside by sending a CONNECT
227069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * request to the previous proxy.
228069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
229069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param next      the proxy to which the tunnel was established.
230069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *  See {@link org.apache.http.conn.ManagedClientConnection#tunnelProxy
231069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                                  ManagedClientConnection.tunnelProxy}
232069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  for details.
233069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param secure    <code>true</code> if the tunnel should be
234069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  considered secure, <code>false</code> otherwise
235069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param params    the parameters for tunnelling the connection
236069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
237069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws IOException  in case of a problem
238069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
239069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void tunnelProxy(HttpHost next, boolean secure, HttpParams params)
240069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        throws IOException {
241069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
242069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (next == null) {
243069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException
244069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("Next proxy must not be null.");
245069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
246069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (params == null) {
247069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException
248069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("Parameters must not be null.");
249069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
250069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
251069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        //@@@ check for proxy in planned route?
252069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if ((this.tracker == null) || !this.tracker.isConnected()) {
253069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalStateException("Connection not open.");
254069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
255069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
256069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // LOG.debug?
257069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
258069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.connection.update(null, next, secure, params);
259069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.tracker.tunnelProxy(next, secure);
260069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
261069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    } // tunnelProxy
262069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
263069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
264069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
265069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Layers a protocol on top of an established tunnel.
266069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
267069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param context   the context for layering
268069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param params    the parameters for layering
269069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
270069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws IOException  in case of a problem
271069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
272069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void layerProtocol(HttpContext context, HttpParams params)
273069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        throws IOException {
274069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
275069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        //@@@ is context allowed to be null? depends on operator?
276069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (params == null) {
277069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException
278069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("Parameters must not be null.");
279069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
280069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
281069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if ((this.tracker == null) || !this.tracker.isConnected()) {
282069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalStateException("Connection not open.");
283069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
284069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (!this.tracker.isTunnelled()) {
285069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            //@@@ allow this?
286069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalStateException
287069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("Protocol layering without a tunnel not supported.");
288069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
289069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (this.tracker.isLayered()) {
290069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalStateException
291069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("Multiple protocol layering not supported.");
292069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
293069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
294069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // - collect the arguments
295069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // - call the operator
296069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // - update the tracking data
297069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // In this order, we can be sure that only a successful
298069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // layering on top of the connection will be tracked.
299069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
300069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        final HttpHost target = tracker.getTargetHost();
301069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
302069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        connOperator.updateSecureConnection(this.connection, target,
303069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                                             context, params);
304069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
305069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.tracker.layerProtocol(this.connection.isSecure());
306069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
307069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    } // layerProtocol
308069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
309069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
310069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
311069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Shuts down the entry.
312069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
313069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * If {@link #open(HttpRoute, HttpContext, HttpParams)} is in progress,
314069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * this will cause that open to possibly throw an {@link IOException}.
315069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
316069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected void shutdownEntry() {
317069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        tracker = null;
318069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
319069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
320069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
321069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project} // class AbstractPoolEntry
322069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
323