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