001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/impl/AbstractHttpClientConnection.java $
003: * $Revision: 573829 $
004: * $Date: 2007-09-08 14:41:25 +0200 (Sat, 08 Sep 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.impl;
033:
034: import java.io.IOException;
035:
036: import org.apache.http.HttpClientConnection;
037: import org.apache.http.HttpConnectionMetrics;
038: import org.apache.http.HttpEntity;
039: import org.apache.http.HttpEntityEnclosingRequest;
040: import org.apache.http.HttpException;
041: import org.apache.http.HttpRequest;
042: import org.apache.http.HttpResponse;
043: import org.apache.http.HttpResponseFactory;
044: import org.apache.http.impl.entity.EntityDeserializer;
045: import org.apache.http.impl.entity.EntitySerializer;
046: import org.apache.http.impl.entity.LaxContentLengthStrategy;
047: import org.apache.http.impl.entity.StrictContentLengthStrategy;
048: import org.apache.http.impl.io.HttpRequestWriter;
049: import org.apache.http.impl.io.HttpResponseParser;
050: import org.apache.http.io.HttpMessageParser;
051: import org.apache.http.io.HttpMessageWriter;
052: import org.apache.http.io.SessionInputBuffer;
053: import org.apache.http.io.SessionOutputBuffer;
054: import org.apache.http.params.HttpParams;
055:
056: /**
057: * Abstract client-side HTTP connection capable of transmitting and receiving data
058: * using arbitrary {@link SessionInputBuffer} and {@link SessionOutputBuffer}
059: *
060: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
061: *
062: * @version $Revision: 573829 $
063: *
064: * @since 4.0
065: */
066: public abstract class AbstractHttpClientConnection implements
067: HttpClientConnection {
068:
069: private final EntitySerializer entityserializer;
070: private final EntityDeserializer entitydeserializer;
071:
072: private SessionInputBuffer inbuffer = null;
073: private SessionOutputBuffer outbuffer = null;
074: private HttpMessageParser responseParser = null;
075: private HttpMessageWriter requestWriter = null;
076: private HttpConnectionMetricsImpl metrics = null;
077:
078: public AbstractHttpClientConnection() {
079: super ();
080: this .entityserializer = createEntitySerializer();
081: this .entitydeserializer = createEntityDeserializer();
082: }
083:
084: protected abstract void assertOpen() throws IllegalStateException;
085:
086: protected EntityDeserializer createEntityDeserializer() {
087: return new EntityDeserializer(new LaxContentLengthStrategy());
088: }
089:
090: protected EntitySerializer createEntitySerializer() {
091: return new EntitySerializer(new StrictContentLengthStrategy());
092: }
093:
094: protected HttpResponseFactory createHttpResponseFactory() {
095: return new DefaultHttpResponseFactory();
096: }
097:
098: protected HttpMessageParser createResponseParser(
099: final SessionInputBuffer buffer,
100: final HttpResponseFactory responseFactory,
101: final HttpParams params) {
102: // override in derived class to specify a line parser
103: return new HttpResponseParser(buffer, null, responseFactory,
104: params);
105: }
106:
107: protected HttpMessageWriter createRequestWriter(
108: final SessionOutputBuffer buffer, final HttpParams params) {
109: // override in derived class to specify a line formatter
110: return new HttpRequestWriter(buffer, null, params);
111: }
112:
113: protected void init(final SessionInputBuffer inbuffer,
114: final SessionOutputBuffer outbuffer, final HttpParams params) {
115: if (inbuffer == null) {
116: throw new IllegalArgumentException(
117: "Input session buffer may not be null");
118: }
119: if (outbuffer == null) {
120: throw new IllegalArgumentException(
121: "Output session buffer may not be null");
122: }
123: this .inbuffer = inbuffer;
124: this .outbuffer = outbuffer;
125: this .responseParser = createResponseParser(inbuffer,
126: createHttpResponseFactory(), params);
127: this .requestWriter = createRequestWriter(outbuffer, params);
128: this .metrics = new HttpConnectionMetricsImpl(inbuffer
129: .getMetrics(), outbuffer.getMetrics());
130: }
131:
132: public boolean isResponseAvailable(int timeout) throws IOException {
133: assertOpen();
134: return this .inbuffer.isDataAvailable(timeout);
135: }
136:
137: public void sendRequestHeader(final HttpRequest request)
138: throws HttpException, IOException {
139: if (request == null) {
140: throw new IllegalArgumentException(
141: "HTTP request may not be null");
142: }
143: assertOpen();
144: this .requestWriter.write(request);
145: this .metrics.incrementRequestCount();
146: }
147:
148: public void sendRequestEntity(
149: final HttpEntityEnclosingRequest request)
150: throws HttpException, IOException {
151: if (request == null) {
152: throw new IllegalArgumentException(
153: "HTTP request may not be null");
154: }
155: assertOpen();
156: if (request.getEntity() == null) {
157: return;
158: }
159: this .entityserializer.serialize(this .outbuffer, request,
160: request.getEntity());
161: }
162:
163: protected void doFlush() throws IOException {
164: this .outbuffer.flush();
165: }
166:
167: public void flush() throws IOException {
168: assertOpen();
169: doFlush();
170: }
171:
172: public HttpResponse receiveResponseHeader() throws HttpException,
173: IOException {
174: assertOpen();
175: HttpResponse response = (HttpResponse) this .responseParser
176: .parse();
177: if (response.getStatusLine().getStatusCode() >= 200) {
178: this .metrics.incrementResponseCount();
179: }
180: return response;
181: }
182:
183: public void receiveResponseEntity(final HttpResponse response)
184: throws HttpException, IOException {
185: if (response == null) {
186: throw new IllegalArgumentException(
187: "HTTP response may not be null");
188: }
189: assertOpen();
190: HttpEntity entity = this .entitydeserializer.deserialize(
191: this .inbuffer, response);
192: response.setEntity(entity);
193: }
194:
195: public boolean isStale() {
196: assertOpen();
197: try {
198: this .inbuffer.isDataAvailable(1);
199: return false;
200: } catch (IOException ex) {
201: return true;
202: }
203: }
204:
205: public HttpConnectionMetrics getMetrics() {
206: return this.metrics;
207: }
208:
209: }
|