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