001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/impl/AbstractHttpServerConnection.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.HttpConnectionMetrics;
037: import org.apache.http.HttpEntity;
038: import org.apache.http.HttpEntityEnclosingRequest;
039: import org.apache.http.HttpException;
040: import org.apache.http.HttpRequest;
041: import org.apache.http.HttpRequestFactory;
042: import org.apache.http.HttpResponse;
043: import org.apache.http.HttpServerConnection;
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.HttpRequestParser;
049: import org.apache.http.impl.io.HttpResponseWriter;
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 server-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 AbstractHttpServerConnection implements
067: HttpServerConnection {
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 requestParser = null;
075: private HttpMessageWriter responseWriter = null;
076: private HttpConnectionMetricsImpl metrics = null;
077:
078: public AbstractHttpServerConnection() {
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 HttpRequestFactory createHttpRequestFactory() {
095: return new DefaultHttpRequestFactory();
096: }
097:
098: protected HttpMessageParser createRequestParser(
099: final SessionInputBuffer buffer,
100: final HttpRequestFactory requestFactory,
101: final HttpParams params) {
102: // override in derived class to specify a line parser
103: return new HttpRequestParser(buffer, null, requestFactory,
104: params);
105: }
106:
107: protected HttpMessageWriter createResponseWriter(
108: final SessionOutputBuffer buffer, final HttpParams params) {
109: // override in derived class to specify a line formatter
110: return new HttpResponseWriter(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 .requestParser = createRequestParser(inbuffer,
126: createHttpRequestFactory(), params);
127: this .responseWriter = createResponseWriter(outbuffer, params);
128: this .metrics = new HttpConnectionMetricsImpl(inbuffer
129: .getMetrics(), outbuffer.getMetrics());
130: }
131:
132: public HttpRequest receiveRequestHeader() throws HttpException,
133: IOException {
134: assertOpen();
135: HttpRequest request = (HttpRequest) this .requestParser.parse();
136: this .metrics.incrementRequestCount();
137: return request;
138: }
139:
140: public void receiveRequestEntity(
141: final HttpEntityEnclosingRequest request)
142: throws HttpException, IOException {
143: if (request == null) {
144: throw new IllegalArgumentException(
145: "HTTP request may not be null");
146: }
147: assertOpen();
148: HttpEntity entity = this .entitydeserializer.deserialize(
149: this .inbuffer, request);
150: request.setEntity(entity);
151: }
152:
153: protected void doFlush() throws IOException {
154: this .outbuffer.flush();
155: }
156:
157: public void flush() throws IOException {
158: assertOpen();
159: doFlush();
160: }
161:
162: public void sendResponseHeader(final HttpResponse response)
163: throws HttpException, IOException {
164: if (response == null) {
165: throw new IllegalArgumentException(
166: "HTTP response may not be null");
167: }
168: assertOpen();
169: this .responseWriter.write(response);
170: if (response.getStatusLine().getStatusCode() >= 200) {
171: this .metrics.incrementResponseCount();
172: }
173: }
174:
175: public void sendResponseEntity(final HttpResponse response)
176: throws HttpException, IOException {
177: if (response.getEntity() == null) {
178: return;
179: }
180: this .entityserializer.serialize(this .outbuffer, response,
181: response.getEntity());
182: }
183:
184: public boolean isStale() {
185: assertOpen();
186: try {
187: this .inbuffer.isDataAvailable(1);
188: return false;
189: } catch (IOException ex) {
190: return true;
191: }
192: }
193:
194: public HttpConnectionMetrics getMetrics() {
195: return this.metrics;
196: }
197:
198: }
|