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.Http1Config;
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 Http1Config http1Config;
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 Http1Config http1Config,
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.http1Config = http1Config != null ? http1Config : Http1Config.DEFAULT;
72          this.charCodingConfig = charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT;
73          this.incomingContentStrategy = incomingContentStrategy;
74          this.outgoingContentStrategy = outgoingContentStrategy;
75          this.requestParserFactory = requestParserFactory != null ? requestParserFactory :
76              new DefaultHttpRequestParserFactory(this.http1Config);
77          this.responseWriterFactory = responseWriterFactory != null ? responseWriterFactory :
78              new DefaultHttpResponseWriterFactory(this.http1Config);
79      }
80  
81      public DefaultBHttpServerConnectionFactory(
82              final String scheme,
83              final Http1Config http1Config,
84              final CharCodingConfig charCodingConfig,
85              final HttpMessageParserFactory<ClassicHttpRequest> requestParserFactory,
86              final HttpMessageWriterFactory<ClassicHttpResponse> responseWriterFactory) {
87          this(scheme, http1Config, charCodingConfig, null, null, requestParserFactory, responseWriterFactory);
88      }
89  
90      public DefaultBHttpServerConnectionFactory(
91              final String scheme,
92              final Http1Config http1Config,
93              final CharCodingConfig charCodingConfig) {
94          this(scheme, http1Config, charCodingConfig, null, null, null, null);
95      }
96  
97      @Override
98      public DefaultBHttpServerConnection createConnection(final Socket socket) throws IOException {
99          final DefaultBHttpServerConnection conn = new DefaultBHttpServerConnection(
100                 this.scheme,
101                 this.http1Config,
102                 CharCodingSupport.createDecoder(this.charCodingConfig),
103                 CharCodingSupport.createEncoder(this.charCodingConfig),
104                 this.incomingContentStrategy,
105                 this.outgoingContentStrategy,
106                 this.requestParserFactory,
107                 this.responseWriterFactory);
108         conn.bind(socket);
109         return conn;
110     }
111 
112     /**
113      * Create a new {@link Builder}.
114      *
115      * @since 5.1
116      */
117     public static Builder builder() {
118         return new Builder();
119     }
120 
121     /**
122      * Builder for {@link DefaultBHttpServerConnectionFactory}.
123      *
124      * @since 5.1
125      */
126     public static final class Builder {
127         private String scheme;
128         private Http1Config http1Config;
129         private CharCodingConfig charCodingConfig;
130         private ContentLengthStrategy incomingContentLengthStrategy;
131         private ContentLengthStrategy outgoingContentLengthStrategy;
132         private HttpMessageParserFactory<ClassicHttpRequest> requestParserFactory;
133         private HttpMessageWriterFactory<ClassicHttpResponse> responseWriterFactory;
134 
135         private Builder() {}
136 
137         public Builder scheme(final String scheme) {
138             this.scheme = scheme;
139             return this;
140         }
141 
142         public Builder http1Config(final Http1Config http1Config) {
143             this.http1Config = http1Config;
144             return this;
145         }
146 
147         public Builder charCodingConfig(final CharCodingConfig charCodingConfig) {
148             this.charCodingConfig = charCodingConfig;
149             return this;
150         }
151 
152         public Builder incomingContentLengthStrategy(final ContentLengthStrategy incomingContentLengthStrategy) {
153             this.incomingContentLengthStrategy = incomingContentLengthStrategy;
154             return this;
155         }
156 
157         public Builder outgoingContentLengthStrategy(final ContentLengthStrategy outgoingContentLengthStrategy) {
158             this.outgoingContentLengthStrategy = outgoingContentLengthStrategy;
159             return this;
160         }
161 
162         public Builder requestParserFactory(
163                 final HttpMessageParserFactory<ClassicHttpRequest> requestParserFactory) {
164             this.requestParserFactory = requestParserFactory;
165             return this;
166         }
167 
168         public Builder responseWriterFactory(
169                 final HttpMessageWriterFactory<ClassicHttpResponse> responseWriterFactory) {
170             this.responseWriterFactory = responseWriterFactory;
171             return this;
172         }
173 
174         public DefaultBHttpServerConnectionFactory build() {
175             return new DefaultBHttpServerConnectionFactory(
176                     scheme,
177                     http1Config,
178                     charCodingConfig,
179                     incomingContentLengthStrategy,
180                     outgoingContentLengthStrategy,
181                     requestParserFactory,
182                     responseWriterFactory);
183         }
184     }
185 }