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 package org.apache.hc.core5.http.impl.bootstrap;
28
29 import org.apache.hc.core5.annotation.Experimental;
30 import org.apache.hc.core5.function.Callback;
31 import org.apache.hc.core5.function.Decorator;
32 import org.apache.hc.core5.http.ConnectionReuseStrategy;
33 import org.apache.hc.core5.http.HttpHost;
34 import org.apache.hc.core5.http.config.CharCodingConfig;
35 import org.apache.hc.core5.http.config.Http1Config;
36 import org.apache.hc.core5.http.impl.Http1StreamListener;
37 import org.apache.hc.core5.http.impl.HttpProcessors;
38 import org.apache.hc.core5.http.impl.nio.ClientHttp1IOEventHandlerFactory;
39 import org.apache.hc.core5.http.impl.nio.ClientHttp1StreamDuplexerFactory;
40 import org.apache.hc.core5.http.nio.ssl.BasicClientTlsStrategy;
41 import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
42 import org.apache.hc.core5.http.protocol.HttpProcessor;
43 import org.apache.hc.core5.pool.ConnPoolListener;
44 import org.apache.hc.core5.pool.DefaultDisposalCallback;
45 import org.apache.hc.core5.pool.LaxConnPool;
46 import org.apache.hc.core5.pool.ManagedConnPool;
47 import org.apache.hc.core5.pool.PoolConcurrencyPolicy;
48 import org.apache.hc.core5.pool.PoolReusePolicy;
49 import org.apache.hc.core5.pool.StrictConnPool;
50 import org.apache.hc.core5.reactor.IOEventHandlerFactory;
51 import org.apache.hc.core5.reactor.IOReactorConfig;
52 import org.apache.hc.core5.reactor.IOSession;
53 import org.apache.hc.core5.reactor.IOSessionListener;
54 import org.apache.hc.core5.util.Timeout;
55
56
57
58
59
60
61 public class AsyncRequesterBootstrap {
62
63 private IOReactorConfig ioReactorConfig;
64 private Http1Config http1Config;
65 private CharCodingConfig charCodingConfig;
66 private HttpProcessor httpProcessor;
67 private ConnectionReuseStrategy connStrategy;
68 private int defaultMaxPerRoute;
69 private int maxTotal;
70 private Timeout timeToLive;
71 private PoolReusePolicy poolReusePolicy;
72 private PoolConcurrencyPolicy poolConcurrencyPolicy;
73 private TlsStrategy tlsStrategy;
74 private Timeout handshakeTimeout;
75 private Decorator<IOSession> ioSessionDecorator;
76 private Callback<Exception> exceptionCallback;
77 private IOSessionListener sessionListener;
78 private Http1StreamListener streamListener;
79 private ConnPoolListener<HttpHost> connPoolListener;
80
81 private AsyncRequesterBootstrap() {
82 }
83
84 public static AsyncRequesterBootstrap bootstrap() {
85 return new AsyncRequesterBootstrap();
86 }
87
88
89
90
91 public final AsyncRequesterBootstrap setIOReactorConfig(final IOReactorConfig ioReactorConfig) {
92 this.ioReactorConfig = ioReactorConfig;
93 return this;
94 }
95
96
97
98
99 public final AsyncRequesterBootstrap setHttp1Config(final Http1Config http1Config) {
100 this.http1Config = http1Config;
101 return this;
102 }
103
104
105
106
107 public final AsyncRequesterBootstrap setCharCodingConfig(final CharCodingConfig charCodingConfig) {
108 this.charCodingConfig = charCodingConfig;
109 return this;
110 }
111
112
113
114
115 public final AsyncRequesterBootstrap setHttpProcessor(final HttpProcessor httpProcessor) {
116 this.httpProcessor = httpProcessor;
117 return this;
118 }
119
120
121
122
123 public final AsyncRequesterBootstrap setConnectionReuseStrategy(final ConnectionReuseStrategy connStrategy) {
124 this.connStrategy = connStrategy;
125 return this;
126 }
127
128 public final AsyncRequesterBootstrap setDefaultMaxPerRoute(final int defaultMaxPerRoute) {
129 this.defaultMaxPerRoute = defaultMaxPerRoute;
130 return this;
131 }
132
133 public final AsyncRequesterBootstrap setMaxTotal(final int maxTotal) {
134 this.maxTotal = maxTotal;
135 return this;
136 }
137
138 public final AsyncRequesterBootstrap setTimeToLive(final Timeout timeToLive) {
139 this.timeToLive = timeToLive;
140 return this;
141 }
142
143
144
145
146 public final AsyncRequesterBootstrap setPoolReusePolicy(final PoolReusePolicy poolReusePolicy) {
147 this.poolReusePolicy = poolReusePolicy;
148 return this;
149 }
150
151
152
153
154 @Experimental
155 public final AsyncRequesterBootstrap setPoolConcurrencyPolicy(final PoolConcurrencyPolicy poolConcurrencyPolicy) {
156 this.poolConcurrencyPolicy = poolConcurrencyPolicy;
157 return this;
158 }
159
160
161
162
163 public final AsyncRequesterBootstrap setTlsStrategy(final TlsStrategy tlsStrategy) {
164 this.tlsStrategy = tlsStrategy;
165 return this;
166 }
167
168 public final AsyncRequesterBootstrap setTlsHandshakeTimeout(final Timeout handshakeTimeout) {
169 this.handshakeTimeout = handshakeTimeout;
170 return this;
171 }
172
173
174
175
176 public final AsyncRequesterBootstrap setIOSessionDecorator(final Decorator<IOSession> ioSessionDecorator) {
177 this.ioSessionDecorator = ioSessionDecorator;
178 return this;
179 }
180
181
182
183
184 public final AsyncRequesterBootstrap setExceptionCallback(final Callback<Exception> exceptionCallback) {
185 this.exceptionCallback = exceptionCallback;
186 return this;
187 }
188
189
190
191
192 public final AsyncRequesterBootstrap setIOSessionListener(final IOSessionListener sessionListener) {
193 this.sessionListener = sessionListener;
194 return this;
195 }
196
197
198
199
200 public final AsyncRequesterBootstrap setStreamListener(final Http1StreamListener streamListener) {
201 this.streamListener = streamListener;
202 return this;
203 }
204
205
206
207
208 public final AsyncRequesterBootstrap setConnPoolListener(final ConnPoolListener<HttpHost> connPoolListener) {
209 this.connPoolListener = connPoolListener;
210 return this;
211 }
212
213 public HttpAsyncRequester create() {
214 final ManagedConnPool<HttpHost, IOSession> connPool;
215 switch (poolConcurrencyPolicy != null ? poolConcurrencyPolicy : PoolConcurrencyPolicy.STRICT) {
216 case LAX:
217 connPool = new LaxConnPool<>(
218 defaultMaxPerRoute > 0 ? defaultMaxPerRoute : 20,
219 timeToLive,
220 poolReusePolicy,
221 new DefaultDisposalCallback<IOSession>(),
222 connPoolListener);
223 break;
224 case STRICT:
225 default:
226 connPool = new StrictConnPool<>(
227 defaultMaxPerRoute > 0 ? defaultMaxPerRoute : 20,
228 maxTotal > 0 ? maxTotal : 50,
229 timeToLive,
230 poolReusePolicy,
231 new DefaultDisposalCallback<IOSession>(),
232 connPoolListener);
233 break;
234 }
235 final ClientHttp1StreamDuplexerFactoryactory.html#ClientHttp1StreamDuplexerFactory">ClientHttp1StreamDuplexerFactory streamDuplexerFactory = new ClientHttp1StreamDuplexerFactory(
236 httpProcessor != null ? httpProcessor : HttpProcessors.client(),
237 http1Config != null ? http1Config : Http1Config.DEFAULT,
238 charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT,
239 connStrategy,
240 null,
241 null,
242 streamListener);
243 final IOEventHandlerFactory ioEventHandlerFactory = new ClientHttp1IOEventHandlerFactory(
244 streamDuplexerFactory,
245 tlsStrategy != null ? tlsStrategy : new BasicClientTlsStrategy(),
246 handshakeTimeout);
247 return new HttpAsyncRequester(
248 ioReactorConfig,
249 ioEventHandlerFactory,
250 ioSessionDecorator,
251 exceptionCallback,
252 sessionListener,
253 connPool);
254 }
255
256 }