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 org.apache.http.HttpConnectionFactory;
31  import org.apache.http.HttpRequest;
32  import org.apache.http.HttpResponse;
33  import org.apache.http.annotation.Immutable;
34  import org.apache.http.config.ConnectionConfig;
35  import org.apache.http.entity.ContentLengthStrategy;
36  import org.apache.http.io.HttpMessageParserFactory;
37  import org.apache.http.io.HttpMessageWriterFactory;
38  
39  import java.io.IOException;
40  import java.net.Socket;
41  
42  /**
43   * Default factory for {@link org.apache.http.HttpServerConnection}s.
44   *
45   * @since 4.3
46   */
47  @Immutable
48  public class DefaultBHttpServerConnectionFactory
49          implements HttpConnectionFactory<DefaultBHttpServerConnection> {
50  
51      public static final DefaultBHttpServerConnectionFactory INSTANCE = new DefaultBHttpServerConnectionFactory();
52  
53      private final ConnectionConfig cconfig;
54      private final ContentLengthStrategy incomingContentStrategy;
55      private final ContentLengthStrategy outgoingContentStrategy;
56      private final HttpMessageParserFactory<HttpRequest> requestParserFactory;
57      private final HttpMessageWriterFactory<HttpResponse> responseWriterFactory;
58  
59      public DefaultBHttpServerConnectionFactory(
60              final ConnectionConfig cconfig,
61              final ContentLengthStrategy incomingContentStrategy,
62              final ContentLengthStrategy outgoingContentStrategy,
63              final HttpMessageParserFactory<HttpRequest> requestParserFactory,
64              final HttpMessageWriterFactory<HttpResponse> responseWriterFactory) {
65          super();
66          this.cconfig = cconfig != null ? cconfig : ConnectionConfig.DEFAULT;
67          this.incomingContentStrategy = incomingContentStrategy;
68          this.outgoingContentStrategy = outgoingContentStrategy;
69          this.requestParserFactory = requestParserFactory;
70          this.responseWriterFactory = responseWriterFactory;
71      }
72  
73      public DefaultBHttpServerConnectionFactory(
74              final ConnectionConfig cconfig,
75              final HttpMessageParserFactory<HttpRequest> requestParserFactory,
76              final HttpMessageWriterFactory<HttpResponse> responseWriterFactory) {
77          this(cconfig, null, null, requestParserFactory, responseWriterFactory);
78      }
79  
80      public DefaultBHttpServerConnectionFactory(final ConnectionConfig cconfig) {
81          this(cconfig, null, null, null, null);
82      }
83  
84      public DefaultBHttpServerConnectionFactory() {
85          this(null, null, null, null, null);
86      }
87  
88      public DefaultBHttpServerConnection createConnection(final Socket socket) throws IOException {
89          final DefaultBHttpServerConnection conn = new DefaultBHttpServerConnection(
90                  this.cconfig.getBufferSize(),
91                  this.cconfig.getFragmentSizeHint(),
92                  ConnSupport.createDecoder(this.cconfig),
93                  ConnSupport.createEncoder(this.cconfig),
94                  this.cconfig.getMessageConstraints(),
95                  this.incomingContentStrategy,
96                  this.outgoingContentStrategy,
97                  this.requestParserFactory,
98                  this.responseWriterFactory);
99          conn.bind(socket);
100         return conn;
101     }
102 
103 }