1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/AbstractHttpServerConnection.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,312 @@ 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.HttpConnectionMetrics; 1.36 +import ch.boye.httpclientandroidlib.HttpEntity; 1.37 +import ch.boye.httpclientandroidlib.HttpEntityEnclosingRequest; 1.38 +import ch.boye.httpclientandroidlib.HttpException; 1.39 +import ch.boye.httpclientandroidlib.HttpRequest; 1.40 +import ch.boye.httpclientandroidlib.HttpRequestFactory; 1.41 +import ch.boye.httpclientandroidlib.HttpResponse; 1.42 +import ch.boye.httpclientandroidlib.HttpServerConnection; 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.HttpRequestParser; 1.49 +import ch.boye.httpclientandroidlib.impl.io.HttpResponseWriter; 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 server-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 AbstractHttpServerConnection implements HttpServerConnection { 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 requestParser = null; 1.83 + private HttpMessageWriter responseWriter = 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 AbstractHttpServerConnection() { 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 DefaultHttpRequestFactory} to be used 1.139 + * for creating {@link HttpRequest} 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 HttpRequestFactory} interface. 1.144 + * 1.145 + * @return HTTP request factory. 1.146 + */ 1.147 + protected HttpRequestFactory createHttpRequestFactory() { 1.148 + return new DefaultHttpRequestFactory(); 1.149 + } 1.150 + 1.151 + /** 1.152 + * Creates an instance of {@link HttpMessageParser} to be used for parsing 1.153 + * HTTP requests 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 HttpRequestParser}. 1.159 + * 1.160 + * @param buffer the session input buffer. 1.161 + * @param requestFactory the HTTP request factory. 1.162 + * @param params HTTP parameters. 1.163 + * @return HTTP message parser. 1.164 + */ 1.165 + protected HttpMessageParser createRequestParser( 1.166 + final SessionInputBuffer buffer, 1.167 + final HttpRequestFactory requestFactory, 1.168 + final HttpParams params) { 1.169 + return new HttpRequestParser(buffer, null, requestFactory, params); 1.170 + } 1.171 + 1.172 + /** 1.173 + * Creates an instance of {@link HttpMessageWriter} to be used for 1.174 + * writing out HTTP responses 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 HttpResponseWriter}. 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 createResponseWriter( 1.186 + final SessionOutputBuffer buffer, 1.187 + final HttpParams params) { 1.188 + return new HttpResponseWriter(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 #createHttpRequestFactory}, 1.207 + * {@link #createRequestParser(SessionInputBuffer, HttpRequestFactory, HttpParams)} 1.208 + * and {@link #createResponseWriter(SessionOutputBuffer, HttpParams)} 1.209 + * methods to initialize HTTP request parser and response writer 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.requestParser = createRequestParser( 1.232 + inbuffer, 1.233 + createHttpRequestFactory(), 1.234 + params); 1.235 + this.responseWriter = createResponseWriter( 1.236 + outbuffer, params); 1.237 + this.metrics = createConnectionMetrics( 1.238 + inbuffer.getMetrics(), 1.239 + outbuffer.getMetrics()); 1.240 + } 1.241 + 1.242 + public HttpRequest receiveRequestHeader() 1.243 + throws HttpException, IOException { 1.244 + assertOpen(); 1.245 + HttpRequest request = (HttpRequest) this.requestParser.parse(); 1.246 + this.metrics.incrementRequestCount(); 1.247 + return request; 1.248 + } 1.249 + 1.250 + public void receiveRequestEntity(final HttpEntityEnclosingRequest request) 1.251 + throws HttpException, IOException { 1.252 + if (request == null) { 1.253 + throw new IllegalArgumentException("HTTP request may not be null"); 1.254 + } 1.255 + assertOpen(); 1.256 + HttpEntity entity = this.entitydeserializer.deserialize(this.inbuffer, request); 1.257 + request.setEntity(entity); 1.258 + } 1.259 + 1.260 + protected void doFlush() throws IOException { 1.261 + this.outbuffer.flush(); 1.262 + } 1.263 + 1.264 + public void flush() throws IOException { 1.265 + assertOpen(); 1.266 + doFlush(); 1.267 + } 1.268 + 1.269 + public void sendResponseHeader(final HttpResponse response) 1.270 + throws HttpException, IOException { 1.271 + if (response == null) { 1.272 + throw new IllegalArgumentException("HTTP response may not be null"); 1.273 + } 1.274 + assertOpen(); 1.275 + this.responseWriter.write(response); 1.276 + if (response.getStatusLine().getStatusCode() >= 200) { 1.277 + this.metrics.incrementResponseCount(); 1.278 + } 1.279 + } 1.280 + 1.281 + public void sendResponseEntity(final HttpResponse response) 1.282 + throws HttpException, IOException { 1.283 + if (response.getEntity() == null) { 1.284 + return; 1.285 + } 1.286 + this.entityserializer.serialize( 1.287 + this.outbuffer, 1.288 + response, 1.289 + response.getEntity()); 1.290 + } 1.291 + 1.292 + protected boolean isEof() { 1.293 + return this.eofSensor != null && this.eofSensor.isEof(); 1.294 + } 1.295 + 1.296 + public boolean isStale() { 1.297 + if (!isOpen()) { 1.298 + return true; 1.299 + } 1.300 + if (isEof()) { 1.301 + return true; 1.302 + } 1.303 + try { 1.304 + this.inbuffer.isDataAvailable(1); 1.305 + return isEof(); 1.306 + } catch (IOException ex) { 1.307 + return true; 1.308 + } 1.309 + } 1.310 + 1.311 + public HttpConnectionMetrics getMetrics() { 1.312 + return this.metrics; 1.313 + } 1.314 + 1.315 +}