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