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.annotation.NotThreadSafe;
43  import org.apache.http.config.MessageConstraints;
44  import org.apache.http.entity.ContentLengthStrategy;
45  import org.apache.http.impl.entity.DisallowIdentityContentLengthStrategy;
46  import org.apache.http.impl.io.DefaultHttpRequestParserFactory;
47  import org.apache.http.impl.io.DefaultHttpResponseWriterFactory;
48  import org.apache.http.io.HttpMessageParser;
49  import org.apache.http.io.HttpMessageParserFactory;
50  import org.apache.http.io.HttpMessageWriter;
51  import org.apache.http.io.HttpMessageWriterFactory;
52  import org.apache.http.util.Args;
53  
54  /**
55   * Default implementation of {@link HttpServerConnection}.
56   *
57   * @since 4.3
58   */
59  @NotThreadSafe
60  public class DefaultBHttpServerConnection extends BHttpConnectionBase
61                                                     implements HttpServerConnection {
62  
63      private final HttpMessageParser<HttpRequest> requestParser;
64      private final HttpMessageWriter<HttpResponse> responseWriter;
65  
66      /**
67       * Creates new instance of DefaultBHttpServerConnection.
68       *
69       * @param buffersize buffer size. Must be a positive number.
70       * @param fragmentSizeHint fragment size hint.
71       * @param chardecoder decoder to be used for decoding HTTP protocol elements.
72       *   If {@code null} simple type cast will be used for byte to char conversion.
73       * @param charencoder encoder to be used for encoding HTTP protocol elements.
74       *   If {@code null} simple type cast will be used for char to byte conversion.
75       * @param constraints Message constraints. If {@code null}
76       *   {@link MessageConstraints#DEFAULT} will be used.
77       * @param incomingContentStrategy incoming content length strategy. If {@code null}
78       *   {@link DisallowIdentityContentLengthStrategy#INSTANCE} will be used.
79       * @param outgoingContentStrategy outgoing content length strategy. If {@code null}
80       *   {@link org.apache.http.impl.entity.StrictContentLengthStrategy#INSTANCE} will be used.
81       * @param requestParserFactory request parser factory. If {@code null}
82       *   {@link DefaultHttpRequestParserFactory#INSTANCE} will be used.
83       * @param responseWriterFactory response writer factory. If {@code null}
84       *   {@link DefaultHttpResponseWriterFactory#INSTANCE} will be used.
85       */
86      public DefaultBHttpServerConnection(
87              final int buffersize,
88              final int fragmentSizeHint,
89              final CharsetDecoder chardecoder,
90              final CharsetEncoder charencoder,
91              final MessageConstraints constraints,
92              final ContentLengthStrategy incomingContentStrategy,
93              final ContentLengthStrategy outgoingContentStrategy,
94              final HttpMessageParserFactory<HttpRequest> requestParserFactory,
95              final HttpMessageWriterFactory<HttpResponse> responseWriterFactory) {
96          super(buffersize, fragmentSizeHint, chardecoder, charencoder, constraints,
97                  incomingContentStrategy != null ? incomingContentStrategy :
98                      DisallowIdentityContentLengthStrategy.INSTANCE, outgoingContentStrategy);
99          this.requestParser = (requestParserFactory != null ? requestParserFactory :
100             DefaultHttpRequestParserFactory.INSTANCE).create(getSessionInputBuffer(), constraints);
101         this.responseWriter = (responseWriterFactory != null ? responseWriterFactory :
102             DefaultHttpResponseWriterFactory.INSTANCE).create(getSessionOutputBuffer());
103     }
104 
105     public DefaultBHttpServerConnection(
106             final int buffersize,
107             final CharsetDecoder chardecoder,
108             final CharsetEncoder charencoder,
109             final MessageConstraints constraints) {
110         this(buffersize, buffersize, chardecoder, charencoder, constraints, null, null, null, null);
111     }
112 
113     public DefaultBHttpServerConnection(final int buffersize) {
114         this(buffersize, buffersize, null, null, null, null, null, null, null);
115     }
116 
117     protected void onRequestReceived(final HttpRequest request) {
118     }
119 
120     protected void onResponseSubmitted(final HttpResponse response) {
121     }
122 
123     @Override
124     public void bind(final Socket socket) throws IOException {
125         super.bind(socket);
126     }
127 
128     @Override
129     public HttpRequest receiveRequestHeader()
130             throws HttpException, IOException {
131         ensureOpen();
132         final HttpRequest request = this.requestParser.parse();
133         onRequestReceived(request);
134         incrementRequestCount();
135         return request;
136     }
137 
138     @Override
139     public void receiveRequestEntity(final HttpEntityEnclosingRequest request)
140             throws HttpException, IOException {
141         Args.notNull(request, "HTTP request");
142         ensureOpen();
143         final HttpEntity entity = prepareInput(request);
144         request.setEntity(entity);
145     }
146 
147     @Override
148     public void sendResponseHeader(final HttpResponse response)
149             throws HttpException, IOException {
150         Args.notNull(response, "HTTP response");
151         ensureOpen();
152         this.responseWriter.write(response);
153         onResponseSubmitted(response);
154         if (response.getStatusLine().getStatusCode() >= 200) {
155             incrementResponseCount();
156         }
157     }
158 
159     @Override
160     public void sendResponseEntity(final HttpResponse response)
161             throws HttpException, IOException {
162         Args.notNull(response, "HTTP response");
163         ensureOpen();
164         final HttpEntity entity = response.getEntity();
165         if (entity == null) {
166             return;
167         }
168         final OutputStream outstream = prepareOutput(response);
169         entity.writeTo(outstream);
170         outstream.close();
171     }
172 
173     @Override
174     public void flush() throws IOException {
175         ensureOpen();
176         doFlush();
177     }
178 
179 }