1 /* 2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/AbstractHttpClientConnection.java $ 3 * $Revision: 627457 $ 4 * $Date: 2008-02-13 07:14:19 -0800 (Wed, 13 Feb 2008) $ 5 * 6 * ==================================================================== 7 * Licensed to the Apache Software Foundation (ASF) under one 8 * or more contributor license agreements. See the NOTICE file 9 * distributed with this work for additional information 10 * regarding copyright ownership. The ASF licenses this file 11 * to you under the Apache License, Version 2.0 (the 12 * "License"); you may not use this file except in compliance 13 * with the License. You may obtain a copy of the License at 14 * 15 * http://www.apache.org/licenses/LICENSE-2.0 16 * 17 * Unless required by applicable law or agreed to in writing, 18 * software distributed under the License is distributed on an 19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 20 * KIND, either express or implied. See the License for the 21 * specific language governing permissions and limitations 22 * under the License. 23 * ==================================================================== 24 * 25 * This software consists of voluntary contributions made by many 26 * individuals on behalf of the Apache Software Foundation. For more 27 * information on the Apache Software Foundation, please see 28 * <http://www.apache.org/>. 29 * 30 */ 31 32 package org.apache.http.impl; 33 34 import java.io.IOException; 35 36 import org.apache.http.HttpClientConnection; 37 import org.apache.http.HttpConnectionMetrics; 38 import org.apache.http.HttpEntity; 39 import org.apache.http.HttpEntityEnclosingRequest; 40 import org.apache.http.HttpException; 41 import org.apache.http.HttpRequest; 42 import org.apache.http.HttpResponse; 43 import org.apache.http.HttpResponseFactory; 44 import org.apache.http.impl.entity.EntityDeserializer; 45 import org.apache.http.impl.entity.EntitySerializer; 46 import org.apache.http.impl.entity.LaxContentLengthStrategy; 47 import org.apache.http.impl.entity.StrictContentLengthStrategy; 48 import org.apache.http.impl.io.HttpRequestWriter; 49 import org.apache.http.impl.io.HttpResponseParser; 50 import org.apache.http.impl.io.SocketInputBuffer; 51 import org.apache.http.io.HttpMessageParser; 52 import org.apache.http.io.HttpMessageWriter; 53 import org.apache.http.io.SessionInputBuffer; 54 import org.apache.http.io.SessionOutputBuffer; 55 import org.apache.http.params.HttpParams; 56 57 /** 58 * Abstract client-side HTTP connection capable of transmitting and receiving data 59 * using arbitrary {@link SessionInputBuffer} and {@link SessionOutputBuffer} 60 * 61 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> 62 * 63 * @version $Revision: 627457 $ 64 * 65 * @since 4.0 66 * 67 * @deprecated Please use {@link java.net.URL#openConnection} instead. 68 * Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a> 69 * for further details. 70 */ 71 @Deprecated 72 public abstract class AbstractHttpClientConnection implements HttpClientConnection { 73 74 private final EntitySerializer entityserializer; 75 private final EntityDeserializer entitydeserializer; 76 77 private SessionInputBuffer inbuffer = null; 78 private SessionOutputBuffer outbuffer = null; 79 private HttpMessageParser responseParser = null; 80 private HttpMessageWriter requestWriter = null; 81 private HttpConnectionMetricsImpl metrics = null; 82 83 84 85 public AbstractHttpClientConnection() { 86 super(); 87 this.entityserializer = createEntitySerializer(); 88 this.entitydeserializer = createEntityDeserializer(); 89 } 90 91 protected abstract void assertOpen() throws IllegalStateException; 92 93 protected EntityDeserializer createEntityDeserializer() { 94 return new EntityDeserializer(new LaxContentLengthStrategy()); 95 } 96 97 protected EntitySerializer createEntitySerializer() { 98 return new EntitySerializer(new StrictContentLengthStrategy()); 99 } 100 101 protected HttpResponseFactory createHttpResponseFactory() { 102 return new DefaultHttpResponseFactory(); 103 } 104 105 protected HttpMessageParser createResponseParser( 106 final SessionInputBuffer buffer, 107 final HttpResponseFactory responseFactory, 108 final HttpParams params) { 109 // override in derived class to specify a line parser 110 return new HttpResponseParser(buffer, null, responseFactory, params); 111 } 112 113 protected HttpMessageWriter createRequestWriter( 114 final SessionOutputBuffer buffer, 115 final HttpParams params) { 116 // override in derived class to specify a line formatter 117 return new HttpRequestWriter(buffer, null, params); 118 } 119 120 protected void init( 121 final SessionInputBuffer inbuffer, 122 final SessionOutputBuffer outbuffer, 123 final HttpParams params) { 124 if (inbuffer == null) { 125 throw new IllegalArgumentException("Input session buffer may not be null"); 126 } 127 if (outbuffer == null) { 128 throw new IllegalArgumentException("Output session buffer may not be null"); 129 } 130 this.inbuffer = inbuffer; 131 this.outbuffer = outbuffer; 132 this.responseParser = createResponseParser( 133 inbuffer, 134 createHttpResponseFactory(), 135 params); 136 this.requestWriter = createRequestWriter( 137 outbuffer, params); 138 this.metrics = new HttpConnectionMetricsImpl( 139 inbuffer.getMetrics(), 140 outbuffer.getMetrics()); 141 } 142 143 public boolean isResponseAvailable(int timeout) throws IOException { 144 assertOpen(); 145 return this.inbuffer.isDataAvailable(timeout); 146 } 147 148 public void sendRequestHeader(final HttpRequest request) 149 throws HttpException, IOException { 150 if (request == null) { 151 throw new IllegalArgumentException("HTTP request may not be null"); 152 } 153 assertOpen(); 154 this.requestWriter.write(request); 155 this.metrics.incrementRequestCount(); 156 } 157 158 public void sendRequestEntity(final HttpEntityEnclosingRequest request) 159 throws HttpException, IOException { 160 if (request == null) { 161 throw new IllegalArgumentException("HTTP request may not be null"); 162 } 163 assertOpen(); 164 if (request.getEntity() == null) { 165 return; 166 } 167 this.entityserializer.serialize( 168 this.outbuffer, 169 request, 170 request.getEntity()); 171 } 172 173 protected void doFlush() throws IOException { 174 this.outbuffer.flush(); 175 } 176 177 public void flush() throws IOException { 178 assertOpen(); 179 doFlush(); 180 } 181 182 public HttpResponse receiveResponseHeader() 183 throws HttpException, IOException { 184 assertOpen(); 185 HttpResponse response = (HttpResponse) this.responseParser.parse(); 186 if (response.getStatusLine().getStatusCode() >= 200) { 187 this.metrics.incrementResponseCount(); 188 } 189 return response; 190 } 191 192 public void receiveResponseEntity(final HttpResponse response) 193 throws HttpException, IOException { 194 if (response == null) { 195 throw new IllegalArgumentException("HTTP response may not be null"); 196 } 197 assertOpen(); 198 HttpEntity entity = this.entitydeserializer.deserialize(this.inbuffer, response); 199 response.setEntity(entity); 200 } 201 202 public boolean isStale() { 203 if (!isOpen()) { 204 return true; 205 } 206 try { 207 // BEGIN android-added 208 // don't reuse connections when the socket input stream is exhausted 209 if (inbuffer instanceof SocketInputBuffer) { 210 return ((SocketInputBuffer) inbuffer).isStale(); 211 } 212 // END android-added 213 this.inbuffer.isDataAvailable(1); 214 return false; 215 } catch (IOException ex) { 216 return true; 217 } 218 } 219 220 public HttpConnectionMetrics getMetrics() { 221 return this.metrics; 222 } 223 224 } 225