1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/AbstractHttpServerConnection.java $
3 * $Revision: 618017 $
4 * $Date: 2008-02-03 08:42:22 -0800 (Sun, 03 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
32package org.apache.http.impl;
33
34import java.io.IOException;
35
36import org.apache.http.HttpConnectionMetrics;
37import org.apache.http.HttpEntity;
38import org.apache.http.HttpEntityEnclosingRequest;
39import org.apache.http.HttpException;
40import org.apache.http.HttpRequest;
41import org.apache.http.HttpRequestFactory;
42import org.apache.http.HttpResponse;
43import org.apache.http.HttpServerConnection;
44import org.apache.http.impl.entity.EntityDeserializer;
45import org.apache.http.impl.entity.EntitySerializer;
46import org.apache.http.impl.entity.LaxContentLengthStrategy;
47import org.apache.http.impl.entity.StrictContentLengthStrategy;
48import org.apache.http.impl.io.HttpRequestParser;
49import org.apache.http.impl.io.HttpResponseWriter;
50import org.apache.http.io.HttpMessageParser;
51import org.apache.http.io.HttpMessageWriter;
52import org.apache.http.io.SessionInputBuffer;
53import org.apache.http.io.SessionOutputBuffer;
54import org.apache.http.params.HttpParams;
55
56/**
57 * Abstract server-side HTTP connection capable of transmitting and receiving data
58 * using arbitrary {@link SessionInputBuffer} and {@link SessionOutputBuffer}
59 *
60 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
61 *
62 * @version $Revision: 618017 $
63 *
64 * @since 4.0
65 *
66 * @deprecated Please use {@link java.net.URL#openConnection} instead.
67 *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
68 *     for further details.
69 */
70@Deprecated
71public abstract class AbstractHttpServerConnection implements HttpServerConnection {
72
73    private final EntitySerializer entityserializer;
74    private final EntityDeserializer entitydeserializer;
75
76    private SessionInputBuffer inbuffer = null;
77    private SessionOutputBuffer outbuffer = null;
78    private HttpMessageParser requestParser = null;
79    private HttpMessageWriter responseWriter = null;
80    private HttpConnectionMetricsImpl metrics = null;
81
82
83
84    public AbstractHttpServerConnection() {
85        super();
86        this.entityserializer = createEntitySerializer();
87        this.entitydeserializer = createEntityDeserializer();
88    }
89
90    protected abstract void assertOpen() throws IllegalStateException;
91
92    protected EntityDeserializer createEntityDeserializer() {
93        return new EntityDeserializer(new LaxContentLengthStrategy());
94    }
95
96    protected EntitySerializer createEntitySerializer() {
97        return new EntitySerializer(new StrictContentLengthStrategy());
98    }
99
100    protected HttpRequestFactory createHttpRequestFactory() {
101        return new DefaultHttpRequestFactory();
102    }
103
104    protected HttpMessageParser createRequestParser(
105            final SessionInputBuffer buffer,
106            final HttpRequestFactory requestFactory,
107            final HttpParams params) {
108        // override in derived class to specify a line parser
109        return new HttpRequestParser(buffer, null, requestFactory, params);
110    }
111
112    protected HttpMessageWriter createResponseWriter(
113            final SessionOutputBuffer buffer,
114            final HttpParams params) {
115        // override in derived class to specify a line formatter
116        return new HttpResponseWriter(buffer, null, params);
117    }
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.requestParser = createRequestParser(
133                inbuffer,
134                createHttpRequestFactory(),
135                params);
136        this.responseWriter = createResponseWriter(
137                outbuffer, params);
138        this.metrics = new HttpConnectionMetricsImpl(
139                inbuffer.getMetrics(),
140                outbuffer.getMetrics());
141    }
142
143    public HttpRequest receiveRequestHeader()
144            throws HttpException, IOException {
145        assertOpen();
146        HttpRequest request = (HttpRequest) this.requestParser.parse();
147        this.metrics.incrementRequestCount();
148        return request;
149    }
150
151    public void receiveRequestEntity(final HttpEntityEnclosingRequest request)
152            throws HttpException, IOException {
153        if (request == null) {
154            throw new IllegalArgumentException("HTTP request may not be null");
155        }
156        assertOpen();
157        HttpEntity entity = this.entitydeserializer.deserialize(this.inbuffer, request);
158        request.setEntity(entity);
159    }
160
161    protected void doFlush() throws IOException  {
162        this.outbuffer.flush();
163    }
164
165    public void flush() throws IOException {
166        assertOpen();
167        doFlush();
168    }
169
170    public void sendResponseHeader(final HttpResponse response)
171            throws HttpException, IOException {
172        if (response == null) {
173            throw new IllegalArgumentException("HTTP response may not be null");
174        }
175        assertOpen();
176        this.responseWriter.write(response);
177        if (response.getStatusLine().getStatusCode() >= 200) {
178            this.metrics.incrementResponseCount();
179        }
180    }
181
182    public void sendResponseEntity(final HttpResponse response)
183            throws HttpException, IOException {
184        if (response.getEntity() == null) {
185            return;
186        }
187        this.entityserializer.serialize(
188                this.outbuffer,
189                response,
190                response.getEntity());
191    }
192
193    public boolean isStale() {
194        assertOpen();
195        try {
196            this.inbuffer.isDataAvailable(1);
197            return false;
198        } catch (IOException ex) {
199            return true;
200        }
201    }
202
203    public HttpConnectionMetrics getMetrics() {
204        return this.metrics;
205    }
206
207}
208