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