|
1 /* |
|
2 * ==================================================================== |
|
3 * |
|
4 * Licensed to the Apache Software Foundation (ASF) under one or more |
|
5 * contributor license agreements. See the NOTICE file distributed with |
|
6 * this work for additional information regarding copyright ownership. |
|
7 * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
8 * (the "License"); you may not use this file except in compliance with |
|
9 * the License. You may obtain a copy of the License at |
|
10 * |
|
11 * http://www.apache.org/licenses/LICENSE-2.0 |
|
12 * |
|
13 * Unless required by applicable law or agreed to in writing, software |
|
14 * distributed under the License is distributed on an "AS IS" BASIS, |
|
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
16 * See the License for the specific language governing permissions and |
|
17 * limitations under the License. |
|
18 * ==================================================================== |
|
19 * |
|
20 * This software consists of voluntary contributions made by many |
|
21 * individuals on behalf of the Apache Software Foundation. For more |
|
22 * information on the Apache Software Foundation, please see |
|
23 * <http://www.apache.org/>. |
|
24 * |
|
25 */ |
|
26 |
|
27 package ch.boye.httpclientandroidlib.impl.conn; |
|
28 |
|
29 import java.io.IOException; |
|
30 |
|
31 import ch.boye.httpclientandroidlib.HttpHost; |
|
32 import ch.boye.httpclientandroidlib.params.HttpParams; |
|
33 import ch.boye.httpclientandroidlib.protocol.HttpContext; |
|
34 import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; |
|
35 import ch.boye.httpclientandroidlib.conn.routing.HttpRoute; |
|
36 import ch.boye.httpclientandroidlib.conn.routing.RouteTracker; |
|
37 import ch.boye.httpclientandroidlib.conn.ClientConnectionOperator; |
|
38 import ch.boye.httpclientandroidlib.conn.OperatedClientConnection; |
|
39 |
|
40 /** |
|
41 * A pool entry for use by connection manager implementations. |
|
42 * Pool entries work in conjunction with an |
|
43 * {@link AbstractClientConnAdapter adapter}. |
|
44 * The adapter is handed out to applications that obtain a connection. |
|
45 * The pool entry stores the underlying connection and tracks the |
|
46 * {@link HttpRoute route} established. |
|
47 * The adapter delegates methods for establishing the route to |
|
48 * its pool entry. |
|
49 * <p> |
|
50 * If the managed connections is released or revoked, the adapter |
|
51 * gets disconnected, but the pool entry still contains the |
|
52 * underlying connection and the established route. |
|
53 * |
|
54 * @since 4.0 |
|
55 */ |
|
56 @NotThreadSafe |
|
57 public abstract class AbstractPoolEntry { |
|
58 |
|
59 /** The connection operator. */ |
|
60 protected final ClientConnectionOperator connOperator; |
|
61 |
|
62 /** The underlying connection being pooled or used. */ |
|
63 protected final OperatedClientConnection connection; |
|
64 |
|
65 /** The route for which this entry gets allocated. */ |
|
66 //@@@ currently accessed from connection manager(s) as attribute |
|
67 //@@@ avoid that, derived classes should decide whether update is allowed |
|
68 //@@@ SCCM: yes, TSCCM: no |
|
69 protected volatile HttpRoute route; |
|
70 |
|
71 /** Connection state object */ |
|
72 protected volatile Object state; |
|
73 |
|
74 /** The tracked route, or <code>null</code> before tracking starts. */ |
|
75 protected volatile RouteTracker tracker; |
|
76 |
|
77 |
|
78 /** |
|
79 * Creates a new pool entry. |
|
80 * |
|
81 * @param connOperator the Connection Operator for this entry |
|
82 * @param route the planned route for the connection, |
|
83 * or <code>null</code> |
|
84 */ |
|
85 protected AbstractPoolEntry(ClientConnectionOperator connOperator, |
|
86 HttpRoute route) { |
|
87 super(); |
|
88 if (connOperator == null) { |
|
89 throw new IllegalArgumentException("Connection operator may not be null"); |
|
90 } |
|
91 this.connOperator = connOperator; |
|
92 this.connection = connOperator.createConnection(); |
|
93 this.route = route; |
|
94 this.tracker = null; |
|
95 } |
|
96 |
|
97 /** |
|
98 * Returns the state object associated with this pool entry. |
|
99 * |
|
100 * @return The state object |
|
101 */ |
|
102 public Object getState() { |
|
103 return state; |
|
104 } |
|
105 |
|
106 /** |
|
107 * Assigns a state object to this pool entry. |
|
108 * |
|
109 * @param state The state object |
|
110 */ |
|
111 public void setState(final Object state) { |
|
112 this.state = state; |
|
113 } |
|
114 |
|
115 /** |
|
116 * Opens the underlying connection. |
|
117 * |
|
118 * @param route the route along which to open the connection |
|
119 * @param context the context for opening the connection |
|
120 * @param params the parameters for opening the connection |
|
121 * |
|
122 * @throws IOException in case of a problem |
|
123 */ |
|
124 public void open(HttpRoute route, |
|
125 HttpContext context, HttpParams params) |
|
126 throws IOException { |
|
127 |
|
128 if (route == null) { |
|
129 throw new IllegalArgumentException |
|
130 ("Route must not be null."); |
|
131 } |
|
132 if (params == null) { |
|
133 throw new IllegalArgumentException |
|
134 ("Parameters must not be null."); |
|
135 } |
|
136 if ((this.tracker != null) && this.tracker.isConnected()) { |
|
137 throw new IllegalStateException("Connection already open."); |
|
138 } |
|
139 |
|
140 // - collect the arguments |
|
141 // - call the operator |
|
142 // - update the tracking data |
|
143 // In this order, we can be sure that only a successful |
|
144 // opening of the connection will be tracked. |
|
145 |
|
146 this.tracker = new RouteTracker(route); |
|
147 final HttpHost proxy = route.getProxyHost(); |
|
148 |
|
149 connOperator.openConnection |
|
150 (this.connection, |
|
151 (proxy != null) ? proxy : route.getTargetHost(), |
|
152 route.getLocalAddress(), |
|
153 context, params); |
|
154 |
|
155 RouteTracker localTracker = tracker; // capture volatile |
|
156 |
|
157 // If this tracker was reset while connecting, |
|
158 // fail early. |
|
159 if (localTracker == null) { |
|
160 throw new IOException("Request aborted"); |
|
161 } |
|
162 |
|
163 if (proxy == null) { |
|
164 localTracker.connectTarget(this.connection.isSecure()); |
|
165 } else { |
|
166 localTracker.connectProxy(proxy, this.connection.isSecure()); |
|
167 } |
|
168 |
|
169 } |
|
170 |
|
171 /** |
|
172 * Tracks tunnelling of the connection to the target. |
|
173 * The tunnel has to be established outside by sending a CONNECT |
|
174 * request to the (last) proxy. |
|
175 * |
|
176 * @param secure <code>true</code> if the tunnel should be |
|
177 * considered secure, <code>false</code> otherwise |
|
178 * @param params the parameters for tunnelling the connection |
|
179 * |
|
180 * @throws IOException in case of a problem |
|
181 */ |
|
182 public void tunnelTarget(boolean secure, HttpParams params) |
|
183 throws IOException { |
|
184 |
|
185 if (params == null) { |
|
186 throw new IllegalArgumentException |
|
187 ("Parameters must not be null."); |
|
188 } |
|
189 |
|
190 if ((this.tracker == null) || !this.tracker.isConnected()) { |
|
191 throw new IllegalStateException("Connection not open."); |
|
192 } |
|
193 if (this.tracker.isTunnelled()) { |
|
194 throw new IllegalStateException |
|
195 ("Connection is already tunnelled."); |
|
196 } |
|
197 |
|
198 this.connection.update(null, tracker.getTargetHost(), |
|
199 secure, params); |
|
200 this.tracker.tunnelTarget(secure); |
|
201 } |
|
202 |
|
203 /** |
|
204 * Tracks tunnelling of the connection to a chained proxy. |
|
205 * The tunnel has to be established outside by sending a CONNECT |
|
206 * request to the previous proxy. |
|
207 * |
|
208 * @param next the proxy to which the tunnel was established. |
|
209 * See {@link ch.boye.httpclientandroidlib.conn.ManagedClientConnection#tunnelProxy |
|
210 * ManagedClientConnection.tunnelProxy} |
|
211 * for details. |
|
212 * @param secure <code>true</code> if the tunnel should be |
|
213 * considered secure, <code>false</code> otherwise |
|
214 * @param params the parameters for tunnelling the connection |
|
215 * |
|
216 * @throws IOException in case of a problem |
|
217 */ |
|
218 public void tunnelProxy(HttpHost next, boolean secure, HttpParams params) |
|
219 throws IOException { |
|
220 |
|
221 if (next == null) { |
|
222 throw new IllegalArgumentException |
|
223 ("Next proxy must not be null."); |
|
224 } |
|
225 if (params == null) { |
|
226 throw new IllegalArgumentException |
|
227 ("Parameters must not be null."); |
|
228 } |
|
229 |
|
230 //@@@ check for proxy in planned route? |
|
231 if ((this.tracker == null) || !this.tracker.isConnected()) { |
|
232 throw new IllegalStateException("Connection not open."); |
|
233 } |
|
234 |
|
235 this.connection.update(null, next, secure, params); |
|
236 this.tracker.tunnelProxy(next, secure); |
|
237 } |
|
238 |
|
239 /** |
|
240 * Layers a protocol on top of an established tunnel. |
|
241 * |
|
242 * @param context the context for layering |
|
243 * @param params the parameters for layering |
|
244 * |
|
245 * @throws IOException in case of a problem |
|
246 */ |
|
247 public void layerProtocol(HttpContext context, HttpParams params) |
|
248 throws IOException { |
|
249 |
|
250 //@@@ is context allowed to be null? depends on operator? |
|
251 if (params == null) { |
|
252 throw new IllegalArgumentException |
|
253 ("Parameters must not be null."); |
|
254 } |
|
255 |
|
256 if ((this.tracker == null) || !this.tracker.isConnected()) { |
|
257 throw new IllegalStateException("Connection not open."); |
|
258 } |
|
259 if (!this.tracker.isTunnelled()) { |
|
260 //@@@ allow this? |
|
261 throw new IllegalStateException |
|
262 ("Protocol layering without a tunnel not supported."); |
|
263 } |
|
264 if (this.tracker.isLayered()) { |
|
265 throw new IllegalStateException |
|
266 ("Multiple protocol layering not supported."); |
|
267 } |
|
268 |
|
269 // - collect the arguments |
|
270 // - call the operator |
|
271 // - update the tracking data |
|
272 // In this order, we can be sure that only a successful |
|
273 // layering on top of the connection will be tracked. |
|
274 |
|
275 final HttpHost target = tracker.getTargetHost(); |
|
276 |
|
277 connOperator.updateSecureConnection(this.connection, target, |
|
278 context, params); |
|
279 |
|
280 this.tracker.layerProtocol(this.connection.isSecure()); |
|
281 |
|
282 } |
|
283 |
|
284 /** |
|
285 * Shuts down the entry. |
|
286 * |
|
287 * If {@link #open(HttpRoute, HttpContext, HttpParams)} is in progress, |
|
288 * this will cause that open to possibly throw an {@link IOException}. |
|
289 */ |
|
290 protected void shutdownEntry() { |
|
291 tracker = null; |
|
292 state = null; |
|
293 } |
|
294 |
|
295 } |
|
296 |