View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl;
29  
30  import java.io.IOException;
31  import java.io.OutputStream;
32  import java.net.Socket;
33  import java.nio.charset.CharsetDecoder;
34  import java.nio.charset.CharsetEncoder;
35  
36  import org.apache.http.HttpEntity;
37  import org.apache.http.HttpEntityEnclosingRequest;
38  import org.apache.http.HttpException;
39  import org.apache.http.HttpRequest;
40  import org.apache.http.HttpResponse;
41  import org.apache.http.HttpServerConnection;
42  import org.apache.http.config.MessageConstraints;
43  import org.apache.http.entity.ContentLengthStrategy;
44  import org.apache.http.impl.entity.DisallowIdentityContentLengthStrategy;
45  import org.apache.http.impl.io.DefaultHttpRequestParserFactory;
46  import org.apache.http.impl.io.DefaultHttpResponseWriterFactory;
47  import org.apache.http.io.HttpMessageParser;
48  import org.apache.http.io.HttpMessageParserFactory;
49  import org.apache.http.io.HttpMessageWriter;
50  import org.apache.http.io.HttpMessageWriterFactory;
51  import org.apache.http.util.Args;
52  
53  /**
54   * Default implementation of {@link HttpServerConnection}.
55   *
56   * @since 4.3
57   */
58  public class DefaultBHttpServerConnection extends BHttpConnectionBase implements HttpServerConnection {
59  
60      private final HttpMessageParser<HttpRequest> requestParser;
61      private final HttpMessageWriter<HttpResponse> responseWriter;
62  
63      /**
64       * Creates new instance of DefaultBHttpServerConnection.
65       *
66       * @param bufferSize buffer size. Must be a positive number.
67       * @param fragmentSizeHint fragment size hint.
68       * @param charDecoder decoder to be used for decoding HTTP protocol elements.
69       *   If {@code null} simple type cast will be used for byte to char conversion.
70       * @param charEncoder encoder to be used for encoding HTTP protocol elements.
71       *   If {@code null} simple type cast will be used for char to byte conversion.
72       * @param constraints Message constraints. If {@code null}
73       *   {@link MessageConstraints#DEFAULT} will be used.
74       * @param incomingContentStrategy incoming content length strategy. If {@code null}
75       *   {@link DisallowIdentityContentLengthStrategy#INSTANCE} will be used.
76       * @param outgoingContentStrategy outgoing content length strategy. If {@code null}
77       *   {@link org.apache.http.impl.entity.StrictContentLengthStrategy#INSTANCE} will be used.
78       * @param requestParserFactory request parser factory. If {@code null}
79       *   {@link DefaultHttpRequestParserFactory#INSTANCE} will be used.
80       * @param responseWriterFactory response writer factory. If {@code null}
81       *   {@link DefaultHttpResponseWriterFactory#INSTANCE} will be used.
82       */
83      public DefaultBHttpServerConnection(
84              final int bufferSize,
85              final int fragmentSizeHint,
86              final CharsetDecoder charDecoder,
87              final CharsetEncoder charEncoder,
88              final MessageConstraints constraints,
89              final ContentLengthStrategy incomingContentStrategy,
90              final ContentLengthStrategy outgoingContentStrategy,
91              final HttpMessageParserFactory<HttpRequest> requestParserFactory,
92              final HttpMessageWriterFactory<HttpResponse> responseWriterFactory) {
93          super(bufferSize, fragmentSizeHint, charDecoder, charEncoder, constraints,
94                  incomingContentStrategy != null ? incomingContentStrategy :
95                      DisallowIdentityContentLengthStrategy.INSTANCE, outgoingContentStrategy);
96          this.requestParser = (requestParserFactory != null ? requestParserFactory :
97              DefaultHttpRequestParserFactory.INSTANCE).create(getSessionInputBuffer(), constraints);
98          this.responseWriter = (responseWriterFactory != null ? responseWriterFactory :
99              DefaultHttpResponseWriterFactory.INSTANCE).create(getSessionOutputBuffer());
100     }
101 
102     public DefaultBHttpServerConnection(
103             final int bufferSize,
104             final CharsetDecoder charDecoder,
105             final CharsetEncoder charEncoder,
106             final MessageConstraints constraints) {
107         this(bufferSize, bufferSize, charDecoder, charEncoder, constraints, null, null, null, null);
108     }
109 
110     public DefaultBHttpServerConnection(final int bufferSize) {
111         this(bufferSize, bufferSize, null, null, null, null, null, null, null);
112     }
113 
114     protected void onRequestReceived(final HttpRequest request) {
115     }
116 
117     protected void onResponseSubmitted(final HttpResponse response) {
118     }
119 
120     @Override
121     public void bind(final Socket socket) throws IOException {
122         super.bind(socket);
123     }
124 
125     @Override
126     public HttpRequest receiveRequestHeader()
127             throws HttpException, IOException {
128         ensureOpen();
129         final HttpRequest request = this.requestParser.parse();
130         onRequestReceived(request);
131         incrementRequestCount();
132         return request;
133     }
134 
135     @Override
136     public void receiveRequestEntity(final HttpEntityEnclosingRequest request)
137             throws HttpException, IOException {
138         Args.notNull(request, "HTTP request");
139         ensureOpen();
140         final HttpEntity entity = prepareInput(request);
141         request.setEntity(entity);
142     }
143 
144     @Override
145     public void sendResponseHeader(final HttpResponse response)
146             throws HttpException, IOException {
147         Args.notNull(response, "HTTP response");
148         ensureOpen();
149         this.responseWriter.write(response);
150         onResponseSubmitted(response);
151         if (response.getStatusLine().getStatusCode() >= 200) {
152             incrementResponseCount();
153         }
154     }
155 
156     @Override
157     public void sendResponseEntity(final HttpResponse response)
158             throws HttpException, IOException {
159         Args.notNull(response, "HTTP response");
160         ensureOpen();
161         final HttpEntity entity = response.getEntity();
162         if (entity == null) {
163             return;
164         }
165         final OutputStream outStream = prepareOutput(response);
166         entity.writeTo(outStream);
167         outStream.close();
168     }
169 
170     @Override
171     public void flush() throws IOException {
172         ensureOpen();
173         doFlush();
174     }
175 
176 }