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