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