Source code: org/apache/http/impl/DefaultHttpServerConnection.java
1 /*
2 * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/impl/DefaultHttpServerConnection.java $
3 * $Revision: 411100 $
4 * $Date: 2006-06-02 11:12:04 +0200 (Fri, 02 Jun 2006) $
5 *
6 * ====================================================================
7 *
8 * Copyright 1999-2006 The Apache Software Foundation
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ====================================================================
22 *
23 * This software consists of voluntary contributions made by many
24 * individuals on behalf of the Apache Software Foundation. For more
25 * information on the Apache Software Foundation, please see
26 * <http://www.apache.org/>.
27 *
28 */
29
30 package org.apache.http.impl;
31
32 import java.io.IOException;
33 import java.net.Socket;
34
35 import org.apache.http.ConnectionClosedException;
36 import org.apache.http.Header;
37 import org.apache.http.HttpEntity;
38 import org.apache.http.HttpEntityEnclosingRequest;
39 import org.apache.http.HttpException;
40 import org.apache.http.HttpRequest;
41 import org.apache.http.HttpRequestFactory;
42 import org.apache.http.HttpResponse;
43 import org.apache.http.HttpServerConnection;
44 import org.apache.http.RequestLine;
45 import org.apache.http.StatusLine;
46 import org.apache.http.entity.EntityDeserializer;
47 import org.apache.http.entity.EntitySerializer;
48 import org.apache.http.impl.entity.DefaultEntityDeserializer;
49 import org.apache.http.impl.entity.DefaultEntitySerializer;
50 import org.apache.http.io.CharArrayBuffer;
51 import org.apache.http.params.HttpConnectionParams;
52 import org.apache.http.params.HttpParams;
53 import org.apache.http.util.HeaderUtils;
54
55 /**
56 * Default implementation of a server-side HTTP connection.
57 *
58 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
59 *
60 * @version $Revision: 411100 $
61 *
62 * @since 4.0
63 */
64 public class DefaultHttpServerConnection
65 extends AbstractHttpConnection implements HttpServerConnection {
66
67 private int maxHeaderCount = -1;
68
69 private final CharArrayBuffer buffer;
70
71 /*
72 * Dependent interfaces
73 */
74 private HttpRequestFactory requestfactory = null;
75 private EntitySerializer entityserializer = null;
76 private EntityDeserializer entitydeserializer = null;
77
78 public DefaultHttpServerConnection() {
79 super();
80 this.requestfactory = new DefaultHttpRequestFactory();
81 this.buffer = new CharArrayBuffer(128);
82 this.entityserializer = new DefaultEntitySerializer();
83 this.entitydeserializer = new DefaultEntityDeserializer();
84 }
85
86 public void setRequestFactory(final HttpRequestFactory requestfactory) {
87 if (requestfactory == null) {
88 throw new IllegalArgumentException("Factory may not be null");
89 }
90 this.requestfactory = requestfactory;
91 }
92
93 public void setEntityDeserializer(final EntityDeserializer entitydeserializer) {
94 if (entitydeserializer == null) {
95 throw new IllegalArgumentException("Entity deserializer may not be null");
96 }
97 this.entitydeserializer = entitydeserializer;
98 }
99
100 public void setEntitySerializer(final EntitySerializer entityserializer) {
101 if (entityserializer == null) {
102 throw new IllegalArgumentException("Entity serializer may not be null");
103 }
104 this.entityserializer = entityserializer;
105 }
106
107 public void bind(final Socket socket, final HttpParams params) throws IOException {
108 super.bind(socket, params);
109 this.maxHeaderCount = params.getIntParameter(HttpConnectionParams.MAX_HEADER_COUNT, -1);
110 }
111
112 public HttpRequest receiveRequestHeader(final HttpParams params)
113 throws HttpException, IOException {
114 if (params == null) {
115 throw new IllegalArgumentException("HTTP parameters may not be null");
116 }
117 assertOpen();
118 HttpRequest request = receiveRequestLine(params);
119 receiveRequestHeaders(request);
120 return request;
121 }
122
123 public void receiveRequestEntity(final HttpEntityEnclosingRequest request)
124 throws HttpException, IOException {
125 if (request == null) {
126 throw new IllegalArgumentException("HTTP request may not be null");
127 }
128 assertOpen();
129 HttpEntity entity = this.entitydeserializer.deserialize(this.datareceiver, request);
130 request.setEntity(entity);
131 }
132
133 protected HttpRequest receiveRequestLine(final HttpParams params)
134 throws HttpException, IOException {
135 this.buffer.clear();
136 int i = this.datareceiver.readLine(this.buffer);
137 if (i == -1) {
138 throw new ConnectionClosedException("Client closed connection");
139 }
140 RequestLine requestline = RequestLine.parse(this.buffer, 0, this.buffer.length());
141 HttpRequest request = this.requestfactory.newHttpRequest(requestline);
142 request.getParams().setDefaults(params);
143 return request;
144 }
145
146 protected void receiveRequestHeaders(final HttpRequest request)
147 throws HttpException, IOException {
148 Header[] headers = HeaderUtils.parseHeaders(this.datareceiver, this.maxHeaderCount);
149 for (int i = 0; i < headers.length; i++) {
150 request.addHeader(headers[i]);
151 }
152 }
153
154 public void flush() throws IOException {
155 assertOpen();
156 this.datatransmitter.flush();
157 }
158
159 public void sendResponseHeader(final HttpResponse response)
160 throws HttpException, IOException {
161 if (response == null) {
162 throw new IllegalArgumentException("HTTP response may not be null");
163 }
164 assertOpen();
165 sendResponseStatusLine(response);
166 sendResponseHeaders(response);
167 }
168
169 public void sendResponseEntity(final HttpResponse response)
170 throws HttpException, IOException {
171 if (response.getEntity() == null) {
172 return;
173 }
174 this.entityserializer.serialize(
175 this.datatransmitter,
176 response,
177 response.getEntity());
178 }
179
180 protected void sendResponseStatusLine(final HttpResponse response)
181 throws HttpException, IOException {
182 this.buffer.clear();
183 StatusLine.format(this.buffer, response.getStatusLine());
184 this.datatransmitter.writeLine(this.buffer);
185 }
186
187 protected void sendResponseHeaders(final HttpResponse response)
188 throws HttpException, IOException {
189 Header[] headers = response.getAllHeaders();
190 for (int i = 0; i < headers.length; i++) {
191 this.buffer.clear();
192 Header.format(this.buffer, headers[i]);
193 this.datatransmitter.writeLine(this.buffer);
194 }
195 this.buffer.clear();
196 this.datatransmitter.writeLine(this.buffer);
197 }
198
199 }