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 javax.net.ssl.SSLSocket;
34  
35  import org.apache.hc.core5.annotation.Contract;
36  import org.apache.hc.core5.annotation.ThreadingBehavior;
37  import org.apache.hc.core5.http.ClassicHttpRequest;
38  import org.apache.hc.core5.http.ClassicHttpResponse;
39  import org.apache.hc.core5.http.ContentLengthStrategy;
40  import org.apache.hc.core5.http.URIScheme;
41  import org.apache.hc.core5.http.config.CharCodingConfig;
42  import org.apache.hc.core5.http.config.Http1Config;
43  import org.apache.hc.core5.http.impl.CharCodingSupport;
44  import org.apache.hc.core5.http.io.HttpConnectionFactory;
45  import org.apache.hc.core5.http.io.HttpMessageParserFactory;
46  import org.apache.hc.core5.http.io.HttpMessageWriterFactory;
47  
48  /**
49   * Default factory for {@link org.apache.hc.core5.http.io.HttpServerConnection}s.
50   *
51   * @since 4.3
52   */
53  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
54  public class DefaultBHttpServerConnectionFactory implements HttpConnectionFactory<DefaultBHttpServerConnection> {
55  
56      private final String scheme;
57      private final Http1Config http1Config;
58      private final CharCodingConfig charCodingConfig;
59      private final ContentLengthStrategy incomingContentStrategy;
60      private final ContentLengthStrategy outgoingContentStrategy;
61      private final HttpMessageParserFactory<ClassicHttpRequest> requestParserFactory;
62      private final HttpMessageWriterFactory<ClassicHttpResponse> responseWriterFactory;
63  
64      public DefaultBHttpServerConnectionFactory(
65              final String scheme,
66              final Http1Config http1Config,
67              final CharCodingConfig charCodingConfig,
68              final ContentLengthStrategy incomingContentStrategy,
69              final ContentLengthStrategy outgoingContentStrategy,
70              final HttpMessageParserFactory<ClassicHttpRequest> requestParserFactory,
71              final HttpMessageWriterFactory<ClassicHttpResponse> responseWriterFactory) {
72          super();
73          this.scheme = scheme;
74          this.http1Config = http1Config != null ? http1Config : Http1Config.DEFAULT;
75          this.charCodingConfig = charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT;
76          this.incomingContentStrategy = incomingContentStrategy;
77          this.outgoingContentStrategy = outgoingContentStrategy;
78          this.requestParserFactory = requestParserFactory != null ? requestParserFactory :
79              new DefaultHttpRequestParserFactory(this.http1Config);
80          this.responseWriterFactory = responseWriterFactory != null ? responseWriterFactory :
81              new DefaultHttpResponseWriterFactory(this.http1Config);
82      }
83  
84      public DefaultBHttpServerConnectionFactory(
85              final String scheme,
86              final Http1Config http1Config,
87              final CharCodingConfig charCodingConfig,
88              final HttpMessageParserFactory<ClassicHttpRequest> requestParserFactory,
89              final HttpMessageWriterFactory<ClassicHttpResponse> responseWriterFactory) {
90          this(scheme, http1Config, charCodingConfig, null, null, requestParserFactory, responseWriterFactory);
91      }
92  
93      public DefaultBHttpServerConnectionFactory(
94              final String scheme,
95              final Http1Config http1Config,
96              final CharCodingConfig charCodingConfig) {
97          this(scheme, http1Config, charCodingConfig, null, null, null, null);
98      }
99  
100     DefaultBHttpServerConnection createDetached(final Socket socket) {
101         return new DefaultBHttpServerConnection(
102                 scheme != null ? scheme : (socket instanceof SSLSocket ? URIScheme.HTTPS.id : URIScheme.HTTP.id),
103                 this.http1Config,
104                 CharCodingSupport.createDecoder(this.charCodingConfig),
105                 CharCodingSupport.createEncoder(this.charCodingConfig),
106                 this.incomingContentStrategy,
107                 this.outgoingContentStrategy,
108                 this.requestParserFactory,
109                 this.responseWriterFactory);
110     }
111 
112     @Override
113     public DefaultBHttpServerConnection createConnection(final Socket socket) throws IOException {
114         final DefaultBHttpServerConnection conn = createDetached(socket);
115         conn.bind(socket);
116         return conn;
117     }
118 
119     /**
120      * @since 5.3
121      */
122     @Override
123     public DefaultBHttpServerConnection createConnection(final SSLSocket sslSocket, final Socket socket) throws IOException {
124         final DefaultBHttpServerConnection conn = createDetached(sslSocket);
125         conn.bind(sslSocket, socket);
126         return conn;
127     }
128 
129     /**
130      * Create a new {@link Builder}.
131      *
132      * @since 5.1
133      */
134     public static Builder builder() {
135         return new Builder();
136     }
137 
138     /**
139      * Builder for {@link DefaultBHttpServerConnectionFactory}.
140      *
141      * @since 5.1
142      */
143     public static final class Builder {
144         private String scheme;
145         private Http1Config http1Config;
146         private CharCodingConfig charCodingConfig;
147         private ContentLengthStrategy incomingContentLengthStrategy;
148         private ContentLengthStrategy outgoingContentLengthStrategy;
149         private HttpMessageParserFactory<ClassicHttpRequest> requestParserFactory;
150         private HttpMessageWriterFactory<ClassicHttpResponse> responseWriterFactory;
151 
152         private Builder() {}
153 
154         public Builder scheme(final String scheme) {
155             this.scheme = scheme;
156             return this;
157         }
158 
159         public Builder http1Config(final Http1Config http1Config) {
160             this.http1Config = http1Config;
161             return this;
162         }
163 
164         public Builder charCodingConfig(final CharCodingConfig charCodingConfig) {
165             this.charCodingConfig = charCodingConfig;
166             return this;
167         }
168 
169         public Builder incomingContentLengthStrategy(final ContentLengthStrategy incomingContentLengthStrategy) {
170             this.incomingContentLengthStrategy = incomingContentLengthStrategy;
171             return this;
172         }
173 
174         public Builder outgoingContentLengthStrategy(final ContentLengthStrategy outgoingContentLengthStrategy) {
175             this.outgoingContentLengthStrategy = outgoingContentLengthStrategy;
176             return this;
177         }
178 
179         public Builder requestParserFactory(
180                 final HttpMessageParserFactory<ClassicHttpRequest> requestParserFactory) {
181             this.requestParserFactory = requestParserFactory;
182             return this;
183         }
184 
185         public Builder responseWriterFactory(
186                 final HttpMessageWriterFactory<ClassicHttpResponse> responseWriterFactory) {
187             this.responseWriterFactory = responseWriterFactory;
188             return this;
189         }
190 
191         public DefaultBHttpServerConnectionFactory build() {
192             return new DefaultBHttpServerConnectionFactory(
193                     scheme,
194                     http1Config,
195                     charCodingConfig,
196                     incomingContentLengthStrategy,
197                     outgoingContentLengthStrategy,
198                     requestParserFactory,
199                     responseWriterFactory);
200         }
201     }
202 }