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