mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/conn/AbstractPoolEntry.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/conn/AbstractPoolEntry.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,296 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + *
     1.7 + *  Licensed to the Apache Software Foundation (ASF) under one or more
     1.8 + *  contributor license agreements.  See the NOTICE file distributed with
     1.9 + *  this work for additional information regarding copyright ownership.
    1.10 + *  The ASF licenses this file to You under the Apache License, Version 2.0
    1.11 + *  (the "License"); you may not use this file except in compliance with
    1.12 + *  the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + *  Unless required by applicable law or agreed to in writing, software
    1.17 + *  distributed under the License is distributed on an "AS IS" BASIS,
    1.18 + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.19 + *  See the License for the specific language governing permissions and
    1.20 + *  limitations under the License.
    1.21 + * ====================================================================
    1.22 + *
    1.23 + * This software consists of voluntary contributions made by many
    1.24 + * individuals on behalf of the Apache Software Foundation.  For more
    1.25 + * information on the Apache Software Foundation, please see
    1.26 + * <http://www.apache.org/>.
    1.27 + *
    1.28 + */
    1.29 +
    1.30 +package ch.boye.httpclientandroidlib.impl.conn;
    1.31 +
    1.32 +import java.io.IOException;
    1.33 +
    1.34 +import ch.boye.httpclientandroidlib.HttpHost;
    1.35 +import ch.boye.httpclientandroidlib.params.HttpParams;
    1.36 +import ch.boye.httpclientandroidlib.protocol.HttpContext;
    1.37 +import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
    1.38 +import ch.boye.httpclientandroidlib.conn.routing.HttpRoute;
    1.39 +import ch.boye.httpclientandroidlib.conn.routing.RouteTracker;
    1.40 +import ch.boye.httpclientandroidlib.conn.ClientConnectionOperator;
    1.41 +import ch.boye.httpclientandroidlib.conn.OperatedClientConnection;
    1.42 +
    1.43 +/**
    1.44 + * A pool entry for use by connection manager implementations.
    1.45 + * Pool entries work in conjunction with an
    1.46 + * {@link AbstractClientConnAdapter adapter}.
    1.47 + * The adapter is handed out to applications that obtain a connection.
    1.48 + * The pool entry stores the underlying connection and tracks the
    1.49 + * {@link HttpRoute route} established.
    1.50 + * The adapter delegates methods for establishing the route to
    1.51 + * its pool entry.
    1.52 + * <p>
    1.53 + * If the managed connections is released or revoked, the adapter
    1.54 + * gets disconnected, but the pool entry still contains the
    1.55 + * underlying connection and the established route.
    1.56 + *
    1.57 + * @since 4.0
    1.58 + */
    1.59 +@NotThreadSafe
    1.60 +public abstract class AbstractPoolEntry {
    1.61 +
    1.62 +    /** The connection operator. */
    1.63 +    protected final ClientConnectionOperator connOperator;
    1.64 +
    1.65 +    /** The underlying connection being pooled or used. */
    1.66 +    protected final OperatedClientConnection connection;
    1.67 +
    1.68 +    /** The route for which this entry gets allocated. */
    1.69 +    //@@@ currently accessed from connection manager(s) as attribute
    1.70 +    //@@@ avoid that, derived classes should decide whether update is allowed
    1.71 +    //@@@ SCCM: yes, TSCCM: no
    1.72 +    protected volatile HttpRoute route;
    1.73 +
    1.74 +    /** Connection state object */
    1.75 +    protected volatile Object state;
    1.76 +
    1.77 +    /** The tracked route, or <code>null</code> before tracking starts. */
    1.78 +    protected volatile RouteTracker tracker;
    1.79 +
    1.80 +
    1.81 +    /**
    1.82 +     * Creates a new pool entry.
    1.83 +     *
    1.84 +     * @param connOperator     the Connection Operator for this entry
    1.85 +     * @param route   the planned route for the connection,
    1.86 +     *                or <code>null</code>
    1.87 +     */
    1.88 +    protected AbstractPoolEntry(ClientConnectionOperator connOperator,
    1.89 +                                HttpRoute route) {
    1.90 +        super();
    1.91 +        if (connOperator == null) {
    1.92 +            throw new IllegalArgumentException("Connection operator may not be null");
    1.93 +        }
    1.94 +        this.connOperator = connOperator;
    1.95 +        this.connection = connOperator.createConnection();
    1.96 +        this.route = route;
    1.97 +        this.tracker = null;
    1.98 +    }
    1.99 +
   1.100 +    /**
   1.101 +     * Returns the state object associated with this pool entry.
   1.102 +     *
   1.103 +     * @return The state object
   1.104 +     */
   1.105 +    public Object getState() {
   1.106 +        return state;
   1.107 +    }
   1.108 +
   1.109 +    /**
   1.110 +     * Assigns a state object to this pool entry.
   1.111 +     *
   1.112 +     * @param state The state object
   1.113 +     */
   1.114 +    public void setState(final Object state) {
   1.115 +        this.state = state;
   1.116 +    }
   1.117 +
   1.118 +    /**
   1.119 +     * Opens the underlying connection.
   1.120 +     *
   1.121 +     * @param route         the route along which to open the connection
   1.122 +     * @param context       the context for opening the connection
   1.123 +     * @param params        the parameters for opening the connection
   1.124 +     *
   1.125 +     * @throws IOException  in case of a problem
   1.126 +     */
   1.127 +    public void open(HttpRoute route,
   1.128 +                     HttpContext context, HttpParams params)
   1.129 +        throws IOException {
   1.130 +
   1.131 +        if (route == null) {
   1.132 +            throw new IllegalArgumentException
   1.133 +                ("Route must not be null.");
   1.134 +        }
   1.135 +        if (params == null) {
   1.136 +            throw new IllegalArgumentException
   1.137 +                ("Parameters must not be null.");
   1.138 +        }
   1.139 +        if ((this.tracker != null) && this.tracker.isConnected()) {
   1.140 +            throw new IllegalStateException("Connection already open.");
   1.141 +        }
   1.142 +
   1.143 +        // - collect the arguments
   1.144 +        // - call the operator
   1.145 +        // - update the tracking data
   1.146 +        // In this order, we can be sure that only a successful
   1.147 +        // opening of the connection will be tracked.
   1.148 +
   1.149 +        this.tracker = new RouteTracker(route);
   1.150 +        final HttpHost proxy  = route.getProxyHost();
   1.151 +
   1.152 +        connOperator.openConnection
   1.153 +            (this.connection,
   1.154 +             (proxy != null) ? proxy : route.getTargetHost(),
   1.155 +             route.getLocalAddress(),
   1.156 +             context, params);
   1.157 +
   1.158 +        RouteTracker localTracker = tracker; // capture volatile
   1.159 +
   1.160 +        // If this tracker was reset while connecting,
   1.161 +        // fail early.
   1.162 +        if (localTracker == null) {
   1.163 +            throw new IOException("Request aborted");
   1.164 +        }
   1.165 +
   1.166 +        if (proxy == null) {
   1.167 +            localTracker.connectTarget(this.connection.isSecure());
   1.168 +        } else {
   1.169 +            localTracker.connectProxy(proxy, this.connection.isSecure());
   1.170 +        }
   1.171 +
   1.172 +    }
   1.173 +
   1.174 +    /**
   1.175 +     * Tracks tunnelling of the connection to the target.
   1.176 +     * The tunnel has to be established outside by sending a CONNECT
   1.177 +     * request to the (last) proxy.
   1.178 +     *
   1.179 +     * @param secure    <code>true</code> if the tunnel should be
   1.180 +     *                  considered secure, <code>false</code> otherwise
   1.181 +     * @param params    the parameters for tunnelling the connection
   1.182 +     *
   1.183 +     * @throws IOException  in case of a problem
   1.184 +     */
   1.185 +    public void tunnelTarget(boolean secure, HttpParams params)
   1.186 +        throws IOException {
   1.187 +
   1.188 +        if (params == null) {
   1.189 +            throw new IllegalArgumentException
   1.190 +                ("Parameters must not be null.");
   1.191 +        }
   1.192 +
   1.193 +        if ((this.tracker == null) || !this.tracker.isConnected()) {
   1.194 +            throw new IllegalStateException("Connection not open.");
   1.195 +        }
   1.196 +        if (this.tracker.isTunnelled()) {
   1.197 +            throw new IllegalStateException
   1.198 +                ("Connection is already tunnelled.");
   1.199 +        }
   1.200 +
   1.201 +        this.connection.update(null, tracker.getTargetHost(),
   1.202 +                               secure, params);
   1.203 +        this.tracker.tunnelTarget(secure);
   1.204 +    }
   1.205 +
   1.206 +    /**
   1.207 +     * Tracks tunnelling of the connection to a chained proxy.
   1.208 +     * The tunnel has to be established outside by sending a CONNECT
   1.209 +     * request to the previous proxy.
   1.210 +     *
   1.211 +     * @param next      the proxy to which the tunnel was established.
   1.212 +     *  See {@link ch.boye.httpclientandroidlib.conn.ManagedClientConnection#tunnelProxy
   1.213 +     *                                  ManagedClientConnection.tunnelProxy}
   1.214 +     *                  for details.
   1.215 +     * @param secure    <code>true</code> if the tunnel should be
   1.216 +     *                  considered secure, <code>false</code> otherwise
   1.217 +     * @param params    the parameters for tunnelling the connection
   1.218 +     *
   1.219 +     * @throws IOException  in case of a problem
   1.220 +     */
   1.221 +    public void tunnelProxy(HttpHost next, boolean secure, HttpParams params)
   1.222 +        throws IOException {
   1.223 +
   1.224 +        if (next == null) {
   1.225 +            throw new IllegalArgumentException
   1.226 +                ("Next proxy must not be null.");
   1.227 +        }
   1.228 +        if (params == null) {
   1.229 +            throw new IllegalArgumentException
   1.230 +                ("Parameters must not be null.");
   1.231 +        }
   1.232 +
   1.233 +        //@@@ check for proxy in planned route?
   1.234 +        if ((this.tracker == null) || !this.tracker.isConnected()) {
   1.235 +            throw new IllegalStateException("Connection not open.");
   1.236 +        }
   1.237 +
   1.238 +        this.connection.update(null, next, secure, params);
   1.239 +        this.tracker.tunnelProxy(next, secure);
   1.240 +    }
   1.241 +
   1.242 +    /**
   1.243 +     * Layers a protocol on top of an established tunnel.
   1.244 +     *
   1.245 +     * @param context   the context for layering
   1.246 +     * @param params    the parameters for layering
   1.247 +     *
   1.248 +     * @throws IOException  in case of a problem
   1.249 +     */
   1.250 +    public void layerProtocol(HttpContext context, HttpParams params)
   1.251 +        throws IOException {
   1.252 +
   1.253 +        //@@@ is context allowed to be null? depends on operator?
   1.254 +        if (params == null) {
   1.255 +            throw new IllegalArgumentException
   1.256 +                ("Parameters must not be null.");
   1.257 +        }
   1.258 +
   1.259 +        if ((this.tracker == null) || !this.tracker.isConnected()) {
   1.260 +            throw new IllegalStateException("Connection not open.");
   1.261 +        }
   1.262 +        if (!this.tracker.isTunnelled()) {
   1.263 +            //@@@ allow this?
   1.264 +            throw new IllegalStateException
   1.265 +                ("Protocol layering without a tunnel not supported.");
   1.266 +        }
   1.267 +        if (this.tracker.isLayered()) {
   1.268 +            throw new IllegalStateException
   1.269 +                ("Multiple protocol layering not supported.");
   1.270 +        }
   1.271 +
   1.272 +        // - collect the arguments
   1.273 +        // - call the operator
   1.274 +        // - update the tracking data
   1.275 +        // In this order, we can be sure that only a successful
   1.276 +        // layering on top of the connection will be tracked.
   1.277 +
   1.278 +        final HttpHost target = tracker.getTargetHost();
   1.279 +
   1.280 +        connOperator.updateSecureConnection(this.connection, target,
   1.281 +                                             context, params);
   1.282 +
   1.283 +        this.tracker.layerProtocol(this.connection.isSecure());
   1.284 +
   1.285 +    }
   1.286 +
   1.287 +    /**
   1.288 +     * Shuts down the entry.
   1.289 +     *
   1.290 +     * If {@link #open(HttpRoute, HttpContext, HttpParams)} is in progress,
   1.291 +     * this will cause that open to possibly throw an {@link IOException}.
   1.292 +     */
   1.293 +    protected void shutdownEntry() {
   1.294 +        tracker = null;
   1.295 +        state = null;
   1.296 +    }
   1.297 +
   1.298 +}
   1.299 +

mercurial