mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/AbstractHttpClientConnection.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/AbstractHttpClientConnection.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,321 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + * Licensed to the Apache Software Foundation (ASF) under one
     1.7 + * or more contributor license agreements.  See the NOTICE file
     1.8 + * distributed with this work for additional information
     1.9 + * regarding copyright ownership.  The ASF licenses this file
    1.10 + * to you under the Apache License, Version 2.0 (the
    1.11 + * "License"); you may not use this file except in compliance
    1.12 + * with 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,
    1.17 + * software distributed under the License is distributed on an
    1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    1.19 + * KIND, either express or implied.  See the License for the
    1.20 + * specific language governing permissions and limitations
    1.21 + * under the License.
    1.22 + * ====================================================================
    1.23 + *
    1.24 + * This software consists of voluntary contributions made by many
    1.25 + * individuals on behalf of the Apache Software Foundation.  For more
    1.26 + * information on the Apache Software Foundation, please see
    1.27 + * <http://www.apache.org/>.
    1.28 + *
    1.29 + */
    1.30 +
    1.31 +package ch.boye.httpclientandroidlib.impl;
    1.32 +
    1.33 +import java.io.IOException;
    1.34 +
    1.35 +import ch.boye.httpclientandroidlib.HttpClientConnection;
    1.36 +import ch.boye.httpclientandroidlib.HttpConnectionMetrics;
    1.37 +import ch.boye.httpclientandroidlib.HttpEntity;
    1.38 +import ch.boye.httpclientandroidlib.HttpEntityEnclosingRequest;
    1.39 +import ch.boye.httpclientandroidlib.HttpException;
    1.40 +import ch.boye.httpclientandroidlib.HttpRequest;
    1.41 +import ch.boye.httpclientandroidlib.HttpResponse;
    1.42 +import ch.boye.httpclientandroidlib.HttpResponseFactory;
    1.43 +import ch.boye.httpclientandroidlib.entity.ContentLengthStrategy;
    1.44 +import ch.boye.httpclientandroidlib.impl.entity.EntityDeserializer;
    1.45 +import ch.boye.httpclientandroidlib.impl.entity.EntitySerializer;
    1.46 +import ch.boye.httpclientandroidlib.impl.entity.LaxContentLengthStrategy;
    1.47 +import ch.boye.httpclientandroidlib.impl.entity.StrictContentLengthStrategy;
    1.48 +import ch.boye.httpclientandroidlib.impl.io.HttpRequestWriter;
    1.49 +import ch.boye.httpclientandroidlib.impl.io.HttpResponseParser;
    1.50 +import ch.boye.httpclientandroidlib.io.EofSensor;
    1.51 +import ch.boye.httpclientandroidlib.io.HttpMessageParser;
    1.52 +import ch.boye.httpclientandroidlib.io.HttpMessageWriter;
    1.53 +import ch.boye.httpclientandroidlib.io.HttpTransportMetrics;
    1.54 +import ch.boye.httpclientandroidlib.io.SessionInputBuffer;
    1.55 +import ch.boye.httpclientandroidlib.io.SessionOutputBuffer;
    1.56 +import ch.boye.httpclientandroidlib.message.LineFormatter;
    1.57 +import ch.boye.httpclientandroidlib.message.LineParser;
    1.58 +import ch.boye.httpclientandroidlib.params.HttpParams;
    1.59 +
    1.60 +/**
    1.61 + * Abstract client-side HTTP connection capable of transmitting and receiving
    1.62 + * data using arbitrary {@link SessionInputBuffer} and
    1.63 + * {@link SessionOutputBuffer} implementations.
    1.64 + * <p>
    1.65 + * The following parameters can be used to customize the behavior of this
    1.66 + * class:
    1.67 + * <ul>
    1.68 + *  <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING}</li>
    1.69 + *  <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
    1.70 + * </ul>
    1.71 + *
    1.72 + * @since 4.0
    1.73 + */
    1.74 +public abstract class AbstractHttpClientConnection implements HttpClientConnection {
    1.75 +
    1.76 +    private final EntitySerializer entityserializer;
    1.77 +    private final EntityDeserializer entitydeserializer;
    1.78 +
    1.79 +    private SessionInputBuffer inbuffer = null;
    1.80 +    private SessionOutputBuffer outbuffer = null;
    1.81 +    private EofSensor eofSensor = null;
    1.82 +    private HttpMessageParser responseParser = null;
    1.83 +    private HttpMessageWriter requestWriter = null;
    1.84 +    private HttpConnectionMetricsImpl metrics = null;
    1.85 +
    1.86 +    /**
    1.87 +     * Creates an instance of this class.
    1.88 +     * <p>
    1.89 +     * This constructor will invoke {@link #createEntityDeserializer()}
    1.90 +     * and {@link #createEntitySerializer()} methods in order to initialize
    1.91 +     * HTTP entity serializer and deserializer implementations for this
    1.92 +     * connection.
    1.93 +     */
    1.94 +    public AbstractHttpClientConnection() {
    1.95 +        super();
    1.96 +        this.entityserializer = createEntitySerializer();
    1.97 +        this.entitydeserializer = createEntityDeserializer();
    1.98 +    }
    1.99 +
   1.100 +    /**
   1.101 +     * Asserts if the connection is open.
   1.102 +     *
   1.103 +     * @throws IllegalStateException if the connection is not open.
   1.104 +     */
   1.105 +    protected abstract void assertOpen() throws IllegalStateException;
   1.106 +
   1.107 +    /**
   1.108 +     * Creates an instance of {@link EntityDeserializer} with the
   1.109 +     * {@link LaxContentLengthStrategy} implementation to be used for
   1.110 +     * de-serializing entities received over this connection.
   1.111 +     * <p>
   1.112 +     * This method can be overridden in a super class in order to create
   1.113 +     * instances of {@link EntityDeserializer} using a custom
   1.114 +     * {@link ContentLengthStrategy}.
   1.115 +     *
   1.116 +     * @return HTTP entity deserializer
   1.117 +     */
   1.118 +    protected EntityDeserializer createEntityDeserializer() {
   1.119 +        return new EntityDeserializer(new LaxContentLengthStrategy());
   1.120 +    }
   1.121 +
   1.122 +    /**
   1.123 +     * Creates an instance of {@link EntitySerializer} with the
   1.124 +     * {@link StrictContentLengthStrategy} implementation to be used for
   1.125 +     * serializing HTTP entities sent over this connection.
   1.126 +     * <p>
   1.127 +     * This method can be overridden in a super class in order to create
   1.128 +     * instances of {@link EntitySerializer} using a custom
   1.129 +     * {@link ContentLengthStrategy}.
   1.130 +     *
   1.131 +     * @return HTTP entity serialzier.
   1.132 +     */
   1.133 +    protected EntitySerializer createEntitySerializer() {
   1.134 +        return new EntitySerializer(new StrictContentLengthStrategy());
   1.135 +    }
   1.136 +
   1.137 +    /**
   1.138 +     * Creates an instance of {@link DefaultHttpResponseFactory} to be used
   1.139 +     * for creating {@link HttpResponse} objects received by over this
   1.140 +     * connection.
   1.141 +     * <p>
   1.142 +     * This method can be overridden in a super class in order to provide
   1.143 +     * a different implementation of the {@link HttpResponseFactory} interface.
   1.144 +     *
   1.145 +     * @return HTTP response factory.
   1.146 +     */
   1.147 +    protected HttpResponseFactory createHttpResponseFactory() {
   1.148 +        return new DefaultHttpResponseFactory();
   1.149 +    }
   1.150 +
   1.151 +    /**
   1.152 +     * Creates an instance of {@link HttpMessageParser} to be used for parsing
   1.153 +     * HTTP responses received over this connection.
   1.154 +     * <p>
   1.155 +     * This method can be overridden in a super class in order to provide
   1.156 +     * a different implementation of the {@link HttpMessageParser} interface or
   1.157 +     * to pass a different implementation of {@link LineParser} to the
   1.158 +     * the default implementation {@link HttpResponseParser}.
   1.159 +     *
   1.160 +     * @param buffer the session input buffer.
   1.161 +     * @param responseFactory the HTTP response factory.
   1.162 +     * @param params HTTP parameters.
   1.163 +     * @return HTTP message parser.
   1.164 +     */
   1.165 +    protected HttpMessageParser createResponseParser(
   1.166 +            final SessionInputBuffer buffer,
   1.167 +            final HttpResponseFactory responseFactory,
   1.168 +            final HttpParams params) {
   1.169 +        return new HttpResponseParser(buffer, null, responseFactory, params);
   1.170 +    }
   1.171 +
   1.172 +    /**
   1.173 +     * Creates an instance of {@link HttpMessageWriter} to be used for
   1.174 +     * writing out HTTP requests sent over this connection.
   1.175 +     * <p>
   1.176 +     * This method can be overridden in a super class in order to provide
   1.177 +     * a different implementation of the {@link HttpMessageWriter} interface or
   1.178 +     * to pass a different implementation of {@link LineFormatter} to the
   1.179 +     * the default implementation {@link HttpRequestWriter}.
   1.180 +     *
   1.181 +     * @param buffer the session output buffer
   1.182 +     * @param params HTTP parameters
   1.183 +     * @return HTTP message writer
   1.184 +     */
   1.185 +    protected HttpMessageWriter createRequestWriter(
   1.186 +            final SessionOutputBuffer buffer,
   1.187 +            final HttpParams params) {
   1.188 +        return new HttpRequestWriter(buffer, null, params);
   1.189 +    }
   1.190 +
   1.191 +    /**
   1.192 +     * @since 4.1
   1.193 +     */
   1.194 +    protected HttpConnectionMetricsImpl createConnectionMetrics(
   1.195 +            final HttpTransportMetrics inTransportMetric,
   1.196 +            final HttpTransportMetrics outTransportMetric) {
   1.197 +        return new HttpConnectionMetricsImpl(inTransportMetric, outTransportMetric);
   1.198 +    }
   1.199 +
   1.200 +    /**
   1.201 +     * Initializes this connection object with {@link SessionInputBuffer} and
   1.202 +     * {@link SessionOutputBuffer} instances to be used for sending and
   1.203 +     * receiving data. These session buffers can be bound to any arbitrary
   1.204 +     * physical output medium.
   1.205 +     * <p>
   1.206 +     * This method will invoke {@link #createHttpResponseFactory()},
   1.207 +     * {@link #createRequestWriter(SessionOutputBuffer, HttpParams)}
   1.208 +     * and {@link #createResponseParser(SessionInputBuffer, HttpResponseFactory, HttpParams)}
   1.209 +     * methods to initialize HTTP request writer and response parser for this
   1.210 +     * connection.
   1.211 +     *
   1.212 +     * @param inbuffer the session input buffer.
   1.213 +     * @param outbuffer the session output buffer.
   1.214 +     * @param params HTTP parameters.
   1.215 +     */
   1.216 +    protected void init(
   1.217 +            final SessionInputBuffer inbuffer,
   1.218 +            final SessionOutputBuffer outbuffer,
   1.219 +            final HttpParams params) {
   1.220 +        if (inbuffer == null) {
   1.221 +            throw new IllegalArgumentException("Input session buffer may not be null");
   1.222 +        }
   1.223 +        if (outbuffer == null) {
   1.224 +            throw new IllegalArgumentException("Output session buffer may not be null");
   1.225 +        }
   1.226 +        this.inbuffer = inbuffer;
   1.227 +        this.outbuffer = outbuffer;
   1.228 +        if (inbuffer instanceof EofSensor) {
   1.229 +            this.eofSensor = (EofSensor) inbuffer;
   1.230 +        }
   1.231 +        this.responseParser = createResponseParser(
   1.232 +                inbuffer,
   1.233 +                createHttpResponseFactory(),
   1.234 +                params);
   1.235 +        this.requestWriter = createRequestWriter(
   1.236 +                outbuffer, params);
   1.237 +        this.metrics = createConnectionMetrics(
   1.238 +                inbuffer.getMetrics(),
   1.239 +                outbuffer.getMetrics());
   1.240 +    }
   1.241 +
   1.242 +    public boolean isResponseAvailable(int timeout) throws IOException {
   1.243 +        assertOpen();
   1.244 +        return this.inbuffer.isDataAvailable(timeout);
   1.245 +    }
   1.246 +
   1.247 +    public void sendRequestHeader(final HttpRequest request)
   1.248 +            throws HttpException, IOException {
   1.249 +        if (request == null) {
   1.250 +            throw new IllegalArgumentException("HTTP request may not be null");
   1.251 +        }
   1.252 +        assertOpen();
   1.253 +        this.requestWriter.write(request);
   1.254 +        this.metrics.incrementRequestCount();
   1.255 +    }
   1.256 +
   1.257 +    public void sendRequestEntity(final HttpEntityEnclosingRequest request)
   1.258 +            throws HttpException, IOException {
   1.259 +        if (request == null) {
   1.260 +            throw new IllegalArgumentException("HTTP request may not be null");
   1.261 +        }
   1.262 +        assertOpen();
   1.263 +        if (request.getEntity() == null) {
   1.264 +            return;
   1.265 +        }
   1.266 +        this.entityserializer.serialize(
   1.267 +                this.outbuffer,
   1.268 +                request,
   1.269 +                request.getEntity());
   1.270 +    }
   1.271 +
   1.272 +    protected void doFlush() throws IOException {
   1.273 +        this.outbuffer.flush();
   1.274 +    }
   1.275 +
   1.276 +    public void flush() throws IOException {
   1.277 +        assertOpen();
   1.278 +        doFlush();
   1.279 +    }
   1.280 +
   1.281 +    public HttpResponse receiveResponseHeader()
   1.282 +            throws HttpException, IOException {
   1.283 +        assertOpen();
   1.284 +        HttpResponse response = (HttpResponse) this.responseParser.parse();
   1.285 +        if (response.getStatusLine().getStatusCode() >= 200) {
   1.286 +            this.metrics.incrementResponseCount();
   1.287 +        }
   1.288 +        return response;
   1.289 +    }
   1.290 +
   1.291 +    public void receiveResponseEntity(final HttpResponse response)
   1.292 +            throws HttpException, IOException {
   1.293 +        if (response == null) {
   1.294 +            throw new IllegalArgumentException("HTTP response may not be null");
   1.295 +        }
   1.296 +        assertOpen();
   1.297 +        HttpEntity entity = this.entitydeserializer.deserialize(this.inbuffer, response);
   1.298 +        response.setEntity(entity);
   1.299 +    }
   1.300 +
   1.301 +    protected boolean isEof() {
   1.302 +        return this.eofSensor != null && this.eofSensor.isEof();
   1.303 +    }
   1.304 +
   1.305 +    public boolean isStale() {
   1.306 +        if (!isOpen()) {
   1.307 +            return true;
   1.308 +        }
   1.309 +        if (isEof()) {
   1.310 +            return true;
   1.311 +        }
   1.312 +        try {
   1.313 +            this.inbuffer.isDataAvailable(1);
   1.314 +            return isEof();
   1.315 +        } catch (IOException ex) {
   1.316 +            return true;
   1.317 +        }
   1.318 +    }
   1.319 +
   1.320 +    public HttpConnectionMetrics getMetrics() {
   1.321 +        return this.metrics;
   1.322 +    }
   1.323 +
   1.324 +}

mercurial