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 | * 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.HttpClientConnection; |
michael@0 | 33 | import ch.boye.httpclientandroidlib.HttpConnectionMetrics; |
michael@0 | 34 | import ch.boye.httpclientandroidlib.HttpEntity; |
michael@0 | 35 | import ch.boye.httpclientandroidlib.HttpEntityEnclosingRequest; |
michael@0 | 36 | import ch.boye.httpclientandroidlib.HttpException; |
michael@0 | 37 | import ch.boye.httpclientandroidlib.HttpRequest; |
michael@0 | 38 | import ch.boye.httpclientandroidlib.HttpResponse; |
michael@0 | 39 | import ch.boye.httpclientandroidlib.HttpResponseFactory; |
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.HttpRequestWriter; |
michael@0 | 46 | import ch.boye.httpclientandroidlib.impl.io.HttpResponseParser; |
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 client-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 AbstractHttpClientConnection implements HttpClientConnection { |
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 responseParser = null; |
michael@0 | 80 | private HttpMessageWriter requestWriter = 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 AbstractHttpClientConnection() { |
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 DefaultHttpResponseFactory} to be used |
michael@0 | 136 | * for creating {@link HttpResponse} 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 HttpResponseFactory} interface. |
michael@0 | 141 | * |
michael@0 | 142 | * @return HTTP response factory. |
michael@0 | 143 | */ |
michael@0 | 144 | protected HttpResponseFactory createHttpResponseFactory() { |
michael@0 | 145 | return new DefaultHttpResponseFactory(); |
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 responses 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 HttpResponseParser}. |
michael@0 | 156 | * |
michael@0 | 157 | * @param buffer the session input buffer. |
michael@0 | 158 | * @param responseFactory the HTTP response factory. |
michael@0 | 159 | * @param params HTTP parameters. |
michael@0 | 160 | * @return HTTP message parser. |
michael@0 | 161 | */ |
michael@0 | 162 | protected HttpMessageParser createResponseParser( |
michael@0 | 163 | final SessionInputBuffer buffer, |
michael@0 | 164 | final HttpResponseFactory responseFactory, |
michael@0 | 165 | final HttpParams params) { |
michael@0 | 166 | return new HttpResponseParser(buffer, null, responseFactory, 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 requests 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 HttpRequestWriter}. |
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 createRequestWriter( |
michael@0 | 183 | final SessionOutputBuffer buffer, |
michael@0 | 184 | final HttpParams params) { |
michael@0 | 185 | return new HttpRequestWriter(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 #createHttpResponseFactory()}, |
michael@0 | 204 | * {@link #createRequestWriter(SessionOutputBuffer, HttpParams)} |
michael@0 | 205 | * and {@link #createResponseParser(SessionInputBuffer, HttpResponseFactory, HttpParams)} |
michael@0 | 206 | * methods to initialize HTTP request writer and response parser 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.responseParser = createResponseParser( |
michael@0 | 229 | inbuffer, |
michael@0 | 230 | createHttpResponseFactory(), |
michael@0 | 231 | params); |
michael@0 | 232 | this.requestWriter = createRequestWriter( |
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 boolean isResponseAvailable(int timeout) throws IOException { |
michael@0 | 240 | assertOpen(); |
michael@0 | 241 | return this.inbuffer.isDataAvailable(timeout); |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | public void sendRequestHeader(final HttpRequest request) |
michael@0 | 245 | throws HttpException, IOException { |
michael@0 | 246 | if (request == null) { |
michael@0 | 247 | throw new IllegalArgumentException("HTTP request may not be null"); |
michael@0 | 248 | } |
michael@0 | 249 | assertOpen(); |
michael@0 | 250 | this.requestWriter.write(request); |
michael@0 | 251 | this.metrics.incrementRequestCount(); |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | public void sendRequestEntity(final HttpEntityEnclosingRequest request) |
michael@0 | 255 | throws HttpException, IOException { |
michael@0 | 256 | if (request == null) { |
michael@0 | 257 | throw new IllegalArgumentException("HTTP request may not be null"); |
michael@0 | 258 | } |
michael@0 | 259 | assertOpen(); |
michael@0 | 260 | if (request.getEntity() == null) { |
michael@0 | 261 | return; |
michael@0 | 262 | } |
michael@0 | 263 | this.entityserializer.serialize( |
michael@0 | 264 | this.outbuffer, |
michael@0 | 265 | request, |
michael@0 | 266 | request.getEntity()); |
michael@0 | 267 | } |
michael@0 | 268 | |
michael@0 | 269 | protected void doFlush() throws IOException { |
michael@0 | 270 | this.outbuffer.flush(); |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | public void flush() throws IOException { |
michael@0 | 274 | assertOpen(); |
michael@0 | 275 | doFlush(); |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | public HttpResponse receiveResponseHeader() |
michael@0 | 279 | throws HttpException, IOException { |
michael@0 | 280 | assertOpen(); |
michael@0 | 281 | HttpResponse response = (HttpResponse) this.responseParser.parse(); |
michael@0 | 282 | if (response.getStatusLine().getStatusCode() >= 200) { |
michael@0 | 283 | this.metrics.incrementResponseCount(); |
michael@0 | 284 | } |
michael@0 | 285 | return response; |
michael@0 | 286 | } |
michael@0 | 287 | |
michael@0 | 288 | public void receiveResponseEntity(final HttpResponse response) |
michael@0 | 289 | throws HttpException, IOException { |
michael@0 | 290 | if (response == null) { |
michael@0 | 291 | throw new IllegalArgumentException("HTTP response may not be null"); |
michael@0 | 292 | } |
michael@0 | 293 | assertOpen(); |
michael@0 | 294 | HttpEntity entity = this.entitydeserializer.deserialize(this.inbuffer, response); |
michael@0 | 295 | response.setEntity(entity); |
michael@0 | 296 | } |
michael@0 | 297 | |
michael@0 | 298 | protected boolean isEof() { |
michael@0 | 299 | return this.eofSensor != null && this.eofSensor.isEof(); |
michael@0 | 300 | } |
michael@0 | 301 | |
michael@0 | 302 | public boolean isStale() { |
michael@0 | 303 | if (!isOpen()) { |
michael@0 | 304 | return true; |
michael@0 | 305 | } |
michael@0 | 306 | if (isEof()) { |
michael@0 | 307 | return true; |
michael@0 | 308 | } |
michael@0 | 309 | try { |
michael@0 | 310 | this.inbuffer.isDataAvailable(1); |
michael@0 | 311 | return isEof(); |
michael@0 | 312 | } catch (IOException ex) { |
michael@0 | 313 | return true; |
michael@0 | 314 | } |
michael@0 | 315 | } |
michael@0 | 316 | |
michael@0 | 317 | public HttpConnectionMetrics getMetrics() { |
michael@0 | 318 | return this.metrics; |
michael@0 | 319 | } |
michael@0 | 320 | |
michael@0 | 321 | } |