1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
48
49
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
134
135
136
137 public static Builder builder() {
138 return new Builder();
139 }
140
141
142
143
144
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 }