mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/AbstractHttpServerConnection.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * ====================================================================
michael@0 3 * Licensed to the Apache Software Foundation (ASF) under one
michael@0 4 * or more contributor license agreements. See the NOTICE file
michael@0 5 * distributed with this work for additional information
michael@0 6 * regarding copyright ownership. The ASF licenses this file
michael@0 7 * to you under the Apache License, Version 2.0 (the
michael@0 8 * "License"); you may not use this file except in compliance
michael@0 9 * with 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,
michael@0 14 * software distributed under the License is distributed on an
michael@0 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0 16 * KIND, either express or implied. See the License for the
michael@0 17 * specific language governing permissions and limitations
michael@0 18 * under the License.
michael@0 19 * ====================================================================
michael@0 20 *
michael@0 21 * This software consists of voluntary contributions made by many
michael@0 22 * individuals on behalf of the Apache Software Foundation. For more
michael@0 23 * information on the Apache Software Foundation, please see
michael@0 24 * <http://www.apache.org/>.
michael@0 25 *
michael@0 26 */
michael@0 27
michael@0 28 package ch.boye.httpclientandroidlib.impl;
michael@0 29
michael@0 30 import java.io.IOException;
michael@0 31
michael@0 32 import ch.boye.httpclientandroidlib.HttpConnectionMetrics;
michael@0 33 import ch.boye.httpclientandroidlib.HttpEntity;
michael@0 34 import ch.boye.httpclientandroidlib.HttpEntityEnclosingRequest;
michael@0 35 import ch.boye.httpclientandroidlib.HttpException;
michael@0 36 import ch.boye.httpclientandroidlib.HttpRequest;
michael@0 37 import ch.boye.httpclientandroidlib.HttpRequestFactory;
michael@0 38 import ch.boye.httpclientandroidlib.HttpResponse;
michael@0 39 import ch.boye.httpclientandroidlib.HttpServerConnection;
michael@0 40 import ch.boye.httpclientandroidlib.entity.ContentLengthStrategy;
michael@0 41 import ch.boye.httpclientandroidlib.impl.entity.EntityDeserializer;
michael@0 42 import ch.boye.httpclientandroidlib.impl.entity.EntitySerializer;
michael@0 43 import ch.boye.httpclientandroidlib.impl.entity.LaxContentLengthStrategy;
michael@0 44 import ch.boye.httpclientandroidlib.impl.entity.StrictContentLengthStrategy;
michael@0 45 import ch.boye.httpclientandroidlib.impl.io.HttpRequestParser;
michael@0 46 import ch.boye.httpclientandroidlib.impl.io.HttpResponseWriter;
michael@0 47 import ch.boye.httpclientandroidlib.io.EofSensor;
michael@0 48 import ch.boye.httpclientandroidlib.io.HttpMessageParser;
michael@0 49 import ch.boye.httpclientandroidlib.io.HttpMessageWriter;
michael@0 50 import ch.boye.httpclientandroidlib.io.HttpTransportMetrics;
michael@0 51 import ch.boye.httpclientandroidlib.io.SessionInputBuffer;
michael@0 52 import ch.boye.httpclientandroidlib.io.SessionOutputBuffer;
michael@0 53 import ch.boye.httpclientandroidlib.message.LineFormatter;
michael@0 54 import ch.boye.httpclientandroidlib.message.LineParser;
michael@0 55 import ch.boye.httpclientandroidlib.params.HttpParams;
michael@0 56
michael@0 57 /**
michael@0 58 * Abstract server-side HTTP connection capable of transmitting and receiving
michael@0 59 * data using arbitrary {@link SessionInputBuffer} and
michael@0 60 * {@link SessionOutputBuffer} implementations.
michael@0 61 * <p>
michael@0 62 * The following parameters can be used to customize the behavior of this
michael@0 63 * class:
michael@0 64 * <ul>
michael@0 65 * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#STRICT_TRANSFER_ENCODING}</li>
michael@0 66 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
michael@0 67 * </ul>
michael@0 68 *
michael@0 69 * @since 4.0
michael@0 70 */
michael@0 71 public abstract class AbstractHttpServerConnection implements HttpServerConnection {
michael@0 72
michael@0 73 private final EntitySerializer entityserializer;
michael@0 74 private final EntityDeserializer entitydeserializer;
michael@0 75
michael@0 76 private SessionInputBuffer inbuffer = null;
michael@0 77 private SessionOutputBuffer outbuffer = null;
michael@0 78 private EofSensor eofSensor = null;
michael@0 79 private HttpMessageParser requestParser = null;
michael@0 80 private HttpMessageWriter responseWriter = null;
michael@0 81 private HttpConnectionMetricsImpl metrics = null;
michael@0 82
michael@0 83 /**
michael@0 84 * Creates an instance of this class.
michael@0 85 * <p>
michael@0 86 * This constructor will invoke {@link #createEntityDeserializer()}
michael@0 87 * and {@link #createEntitySerializer()} methods in order to initialize
michael@0 88 * HTTP entity serializer and deserializer implementations for this
michael@0 89 * connection.
michael@0 90 */
michael@0 91 public AbstractHttpServerConnection() {
michael@0 92 super();
michael@0 93 this.entityserializer = createEntitySerializer();
michael@0 94 this.entitydeserializer = createEntityDeserializer();
michael@0 95 }
michael@0 96
michael@0 97 /**
michael@0 98 * Asserts if the connection is open.
michael@0 99 *
michael@0 100 * @throws IllegalStateException if the connection is not open.
michael@0 101 */
michael@0 102 protected abstract void assertOpen() throws IllegalStateException;
michael@0 103
michael@0 104 /**
michael@0 105 * Creates an instance of {@link EntityDeserializer} with the
michael@0 106 * {@link LaxContentLengthStrategy} implementation to be used for
michael@0 107 * de-serializing entities received over this connection.
michael@0 108 * <p>
michael@0 109 * This method can be overridden in a super class in order to create
michael@0 110 * instances of {@link EntityDeserializer} using a custom
michael@0 111 * {@link ContentLengthStrategy}.
michael@0 112 *
michael@0 113 * @return HTTP entity deserializer
michael@0 114 */
michael@0 115 protected EntityDeserializer createEntityDeserializer() {
michael@0 116 return new EntityDeserializer(new LaxContentLengthStrategy());
michael@0 117 }
michael@0 118
michael@0 119 /**
michael@0 120 * Creates an instance of {@link EntitySerializer} with the
michael@0 121 * {@link StrictContentLengthStrategy} implementation to be used for
michael@0 122 * serializing HTTP entities sent over this connection.
michael@0 123 * <p>
michael@0 124 * This method can be overridden in a super class in order to create
michael@0 125 * instances of {@link EntitySerializer} using a custom
michael@0 126 * {@link ContentLengthStrategy}.
michael@0 127 *
michael@0 128 * @return HTTP entity serialzier.
michael@0 129 */
michael@0 130 protected EntitySerializer createEntitySerializer() {
michael@0 131 return new EntitySerializer(new StrictContentLengthStrategy());
michael@0 132 }
michael@0 133
michael@0 134 /**
michael@0 135 * Creates an instance of {@link DefaultHttpRequestFactory} to be used
michael@0 136 * for creating {@link HttpRequest} objects received by over this
michael@0 137 * connection.
michael@0 138 * <p>
michael@0 139 * This method can be overridden in a super class in order to provide
michael@0 140 * a different implementation of the {@link HttpRequestFactory} interface.
michael@0 141 *
michael@0 142 * @return HTTP request factory.
michael@0 143 */
michael@0 144 protected HttpRequestFactory createHttpRequestFactory() {
michael@0 145 return new DefaultHttpRequestFactory();
michael@0 146 }
michael@0 147
michael@0 148 /**
michael@0 149 * Creates an instance of {@link HttpMessageParser} to be used for parsing
michael@0 150 * HTTP requests received over this connection.
michael@0 151 * <p>
michael@0 152 * This method can be overridden in a super class in order to provide
michael@0 153 * a different implementation of the {@link HttpMessageParser} interface or
michael@0 154 * to pass a different implementation of {@link LineParser} to the
michael@0 155 * the default implementation {@link HttpRequestParser}.
michael@0 156 *
michael@0 157 * @param buffer the session input buffer.
michael@0 158 * @param requestFactory the HTTP request factory.
michael@0 159 * @param params HTTP parameters.
michael@0 160 * @return HTTP message parser.
michael@0 161 */
michael@0 162 protected HttpMessageParser createRequestParser(
michael@0 163 final SessionInputBuffer buffer,
michael@0 164 final HttpRequestFactory requestFactory,
michael@0 165 final HttpParams params) {
michael@0 166 return new HttpRequestParser(buffer, null, requestFactory, params);
michael@0 167 }
michael@0 168
michael@0 169 /**
michael@0 170 * Creates an instance of {@link HttpMessageWriter} to be used for
michael@0 171 * writing out HTTP responses sent over this connection.
michael@0 172 * <p>
michael@0 173 * This method can be overridden in a super class in order to provide
michael@0 174 * a different implementation of the {@link HttpMessageWriter} interface or
michael@0 175 * to pass a different implementation of {@link LineFormatter} to the
michael@0 176 * the default implementation {@link HttpResponseWriter}.
michael@0 177 *
michael@0 178 * @param buffer the session output buffer
michael@0 179 * @param params HTTP parameters
michael@0 180 * @return HTTP message writer
michael@0 181 */
michael@0 182 protected HttpMessageWriter createResponseWriter(
michael@0 183 final SessionOutputBuffer buffer,
michael@0 184 final HttpParams params) {
michael@0 185 return new HttpResponseWriter(buffer, null, params);
michael@0 186 }
michael@0 187
michael@0 188 /**
michael@0 189 * @since 4.1
michael@0 190 */
michael@0 191 protected HttpConnectionMetricsImpl createConnectionMetrics(
michael@0 192 final HttpTransportMetrics inTransportMetric,
michael@0 193 final HttpTransportMetrics outTransportMetric) {
michael@0 194 return new HttpConnectionMetricsImpl(inTransportMetric, outTransportMetric);
michael@0 195 }
michael@0 196
michael@0 197 /**
michael@0 198 * Initializes this connection object with {@link SessionInputBuffer} and
michael@0 199 * {@link SessionOutputBuffer} instances to be used for sending and
michael@0 200 * receiving data. These session buffers can be bound to any arbitrary
michael@0 201 * physical output medium.
michael@0 202 * <p>
michael@0 203 * This method will invoke {@link #createHttpRequestFactory},
michael@0 204 * {@link #createRequestParser(SessionInputBuffer, HttpRequestFactory, HttpParams)}
michael@0 205 * and {@link #createResponseWriter(SessionOutputBuffer, HttpParams)}
michael@0 206 * methods to initialize HTTP request parser and response writer for this
michael@0 207 * connection.
michael@0 208 *
michael@0 209 * @param inbuffer the session input buffer.
michael@0 210 * @param outbuffer the session output buffer.
michael@0 211 * @param params HTTP parameters.
michael@0 212 */
michael@0 213 protected void init(
michael@0 214 final SessionInputBuffer inbuffer,
michael@0 215 final SessionOutputBuffer outbuffer,
michael@0 216 final HttpParams params) {
michael@0 217 if (inbuffer == null) {
michael@0 218 throw new IllegalArgumentException("Input session buffer may not be null");
michael@0 219 }
michael@0 220 if (outbuffer == null) {
michael@0 221 throw new IllegalArgumentException("Output session buffer may not be null");
michael@0 222 }
michael@0 223 this.inbuffer = inbuffer;
michael@0 224 this.outbuffer = outbuffer;
michael@0 225 if (inbuffer instanceof EofSensor) {
michael@0 226 this.eofSensor = (EofSensor) inbuffer;
michael@0 227 }
michael@0 228 this.requestParser = createRequestParser(
michael@0 229 inbuffer,
michael@0 230 createHttpRequestFactory(),
michael@0 231 params);
michael@0 232 this.responseWriter = createResponseWriter(
michael@0 233 outbuffer, params);
michael@0 234 this.metrics = createConnectionMetrics(
michael@0 235 inbuffer.getMetrics(),
michael@0 236 outbuffer.getMetrics());
michael@0 237 }
michael@0 238
michael@0 239 public HttpRequest receiveRequestHeader()
michael@0 240 throws HttpException, IOException {
michael@0 241 assertOpen();
michael@0 242 HttpRequest request = (HttpRequest) this.requestParser.parse();
michael@0 243 this.metrics.incrementRequestCount();
michael@0 244 return request;
michael@0 245 }
michael@0 246
michael@0 247 public void receiveRequestEntity(final HttpEntityEnclosingRequest request)
michael@0 248 throws HttpException, IOException {
michael@0 249 if (request == null) {
michael@0 250 throw new IllegalArgumentException("HTTP request may not be null");
michael@0 251 }
michael@0 252 assertOpen();
michael@0 253 HttpEntity entity = this.entitydeserializer.deserialize(this.inbuffer, request);
michael@0 254 request.setEntity(entity);
michael@0 255 }
michael@0 256
michael@0 257 protected void doFlush() throws IOException {
michael@0 258 this.outbuffer.flush();
michael@0 259 }
michael@0 260
michael@0 261 public void flush() throws IOException {
michael@0 262 assertOpen();
michael@0 263 doFlush();
michael@0 264 }
michael@0 265
michael@0 266 public void sendResponseHeader(final HttpResponse response)
michael@0 267 throws HttpException, IOException {
michael@0 268 if (response == null) {
michael@0 269 throw new IllegalArgumentException("HTTP response may not be null");
michael@0 270 }
michael@0 271 assertOpen();
michael@0 272 this.responseWriter.write(response);
michael@0 273 if (response.getStatusLine().getStatusCode() >= 200) {
michael@0 274 this.metrics.incrementResponseCount();
michael@0 275 }
michael@0 276 }
michael@0 277
michael@0 278 public void sendResponseEntity(final HttpResponse response)
michael@0 279 throws HttpException, IOException {
michael@0 280 if (response.getEntity() == null) {
michael@0 281 return;
michael@0 282 }
michael@0 283 this.entityserializer.serialize(
michael@0 284 this.outbuffer,
michael@0 285 response,
michael@0 286 response.getEntity());
michael@0 287 }
michael@0 288
michael@0 289 protected boolean isEof() {
michael@0 290 return this.eofSensor != null && this.eofSensor.isEof();
michael@0 291 }
michael@0 292
michael@0 293 public boolean isStale() {
michael@0 294 if (!isOpen()) {
michael@0 295 return true;
michael@0 296 }
michael@0 297 if (isEof()) {
michael@0 298 return true;
michael@0 299 }
michael@0 300 try {
michael@0 301 this.inbuffer.isDataAvailable(1);
michael@0 302 return isEof();
michael@0 303 } catch (IOException ex) {
michael@0 304 return true;
michael@0 305 }
michael@0 306 }
michael@0 307
michael@0 308 public HttpConnectionMetrics getMetrics() {
michael@0 309 return this.metrics;
michael@0 310 }
michael@0 311
michael@0 312 }

mercurial