1 package com.atlassian.httpclient.api.factory;
2
3 import com.atlassian.httpclient.api.Request;
4 import com.atlassian.httpclient.api.HostResolver;
5 import com.atlassian.util.concurrent.Effect;
6 import com.atlassian.util.concurrent.Effects;
7 import com.atlassian.util.concurrent.ThreadFactories;
8 import com.google.common.base.Preconditions;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import javax.annotation.Nonnull;
13 import java.util.Optional;
14 import java.util.concurrent.ExecutorService;
15 import java.util.concurrent.LinkedBlockingQueue;
16 import java.util.concurrent.ThreadFactory;
17 import java.util.concurrent.ThreadPoolExecutor;
18 import java.util.concurrent.TimeUnit;
19
20
21
22
23 public final class HttpClientOptions {
24 public static final String OPTION_PROPERTY_PREFIX = "com.atlassian.httpclient.options";
25 public static final String OPTION_THREAD_WORK_QUEUE_LIMIT = OPTION_PROPERTY_PREFIX + ".threadWorkQueueLimit";
26
27 private final Logger log = LoggerFactory.getLogger(this.getClass());
28
29 private String threadPrefix = "httpclient";
30 private boolean ignoreCookies = false;
31 private int ioThreadCount = Integer.getInteger(OPTION_PROPERTY_PREFIX + ".ioThreadCount", 10);
32 private long ioSelectInterval = Integer.getInteger(OPTION_PROPERTY_PREFIX + ".ioSelectInterval", 1000);
33 private int threadWorkQueueLimit = Integer.getInteger(OPTION_THREAD_WORK_QUEUE_LIMIT, 256);
34
35 private long connectionTimeout = 5 * 1000;
36 private long socketTimeout = 20 * 1000;
37 private long requestTimeout = 30 * 3000;
38
39 private int maxTotalConnections = 20;
40 private int maxConnectionsPerHost = 20;
41
42 private long connectionPoolTimeToLive = 30 * 1000;
43
44 private long maxCacheObjectSize = 100 * 1024L;
45 private int maxCacheEntries = 100;
46
47 private long maxEntitySize = 1024 * 1024 * 100;
48
49 private long leaseTimeout = 10 * 60 * 1000;
50 private int maxCallbackThreadPoolSize = 16;
51
52 private boolean trustSelfSignedCertificates = false;
53
54 private Effect<Request> requestPreparer = Effects.noop();
55
56 private String userAgent = "Default";
57
58 private ExecutorService callbackExecutor;
59
60 private ProxyOptions proxyOptions = ProxyOptions.ProxyOptionsBuilder.create().build();
61
62 private HostResolver hostResolver;
63
64
65
66
67
68
69 public boolean getIgnoreCookies() {
70 return ignoreCookies;
71 }
72
73
74
75
76 public void setIgnoreCookies(boolean ignoreCookies) {
77 this.ignoreCookies = ignoreCookies;
78 }
79
80
81
82
83
84
85 public int getIoThreadCount() {
86 return ioThreadCount;
87 }
88
89
90
91
92
93 public void setIoThreadCount(int ioThreadCount) {
94 this.ioThreadCount = ioThreadCount;
95 }
96
97
98
99
100
101
102
103 public long getIoSelectInterval() {
104 return ioSelectInterval;
105 }
106
107
108
109
110
111 public void setIoSelectInterval(int ioSelectInterval, TimeUnit timeUnit) {
112 this.ioSelectInterval = timeUnit.toMillis(ioSelectInterval);
113 }
114
115
116
117
118 public long getConnectionTimeout() {
119 return connectionTimeout;
120 }
121
122
123
124
125
126
127
128 public void setConnectionTimeout(int connectionTimeout, TimeUnit timeUnit) {
129 this.connectionTimeout = timeUnit.toMillis(connectionTimeout);
130 }
131
132
133
134
135 public long getSocketTimeout() {
136 return socketTimeout;
137 }
138
139
140
141
142
143 public void setSocketTimeout(int socketTimeout, TimeUnit timeUnit) {
144 this.socketTimeout = timeUnit.toMillis(socketTimeout);
145 }
146
147
148
149
150 public long getRequestTimeout() {
151 return requestTimeout;
152 }
153
154
155
156
157
158 public void setRequestTimeout(int requestTimeout, TimeUnit timeUnit) {
159 this.requestTimeout = timeUnit.toMillis(requestTimeout);
160 }
161
162
163
164
165 public String getUserAgent() {
166 return userAgent;
167 }
168
169
170
171
172 public void setUserAgent(String userAgent) {
173 this.userAgent = userAgent;
174 }
175
176
177
178
179 public String getThreadPrefix() {
180 return threadPrefix;
181 }
182
183
184
185
186 public Optional<HostResolver> getHostResolver() {
187 return Optional.ofNullable(hostResolver);
188 }
189
190 public void setHostResolver(HostResolver hostResolver) {
191 this.hostResolver = hostResolver;
192 }
193
194
195
196
197 public void setThreadPrefix(String threadPrefix) {
198 this.threadPrefix = threadPrefix;
199 }
200
201
202
203
204
205 public long getConnectionPoolTimeToLive() {
206 return connectionPoolTimeToLive;
207 }
208
209
210
211
212
213 public void setConnectionPoolTimeToLive(int connectionPoolTimeToLive, TimeUnit timeUnit) {
214 this.connectionPoolTimeToLive = timeUnit.toMillis(connectionPoolTimeToLive);
215 }
216
217
218
219
220 public int getMaxTotalConnections() {
221 return maxTotalConnections;
222 }
223
224
225
226
227 public void setMaxTotalConnections(int maxTotalConnections) {
228 this.maxTotalConnections = maxTotalConnections;
229 }
230
231
232
233
234 public int getMaxConnectionsPerHost() {
235 return maxConnectionsPerHost;
236 }
237
238
239
240
241 public void setMaxConnectionsPerHost(int maxConnectionsPerHost) {
242 this.maxConnectionsPerHost = maxConnectionsPerHost;
243 }
244
245
246
247
248 public long getMaxCacheObjectSize() {
249 return maxCacheObjectSize;
250 }
251
252
253
254
255 public void setMaxCacheObjectSize(long maxCacheObjectSize) {
256 this.maxCacheObjectSize = maxCacheObjectSize;
257 }
258
259
260
261
262 public int getMaxCacheEntries() {
263 return maxCacheEntries;
264 }
265
266
267
268
269 public void setMaxCacheEntries(int maxCacheEntries) {
270 this.maxCacheEntries = maxCacheEntries;
271 }
272
273
274
275
276 public Effect<Request> getRequestPreparer() {
277 return requestPreparer;
278 }
279
280
281
282
283 public void setRequestPreparer(Effect<Request> requestPreparer) {
284 this.requestPreparer = requestPreparer;
285 }
286
287
288
289
290 public long getMaxEntitySize() {
291 return maxEntitySize;
292 }
293
294
295
296
297 public long getLeaseTimeout() {
298 return leaseTimeout;
299 }
300
301
302
303
304 public void setLeaseTimeout(long leaseTimeout) {
305 this.leaseTimeout = leaseTimeout;
306 }
307
308
309
310
311 public void setMaxEntitySize(long maxEntitySize) {
312 this.maxEntitySize = maxEntitySize;
313 }
314
315
316
317
318 public int getMaxCallbackThreadPoolSize() {
319 return maxCallbackThreadPoolSize;
320 }
321
322
323
324
325 public void setMaxCallbackThreadPoolSize(final int maxCallbackThreadPoolSize) {
326 this.maxCallbackThreadPoolSize = maxCallbackThreadPoolSize;
327 }
328
329 public void setCallbackExecutor(ExecutorService callbackExecutor) {
330 this.callbackExecutor = callbackExecutor;
331 }
332
333 public ExecutorService getCallbackExecutor() {
334 return callbackExecutor != null ? callbackExecutor : defaultCallbackExecutor();
335 }
336
337 private ExecutorService defaultCallbackExecutor() {
338 ThreadFactory threadFactory = ThreadFactories.namedThreadFactory(getThreadPrefix() + "-callbacks", ThreadFactories.Type.DAEMON);
339 return new ThreadPoolExecutor(
340 0,
341 getMaxCallbackThreadPoolSize(),
342 60L,
343 TimeUnit.SECONDS,
344 new LinkedBlockingQueue<Runnable>(threadWorkQueueLimit),
345 threadFactory,
346 (r, e) -> log.warn(
347 "Exceeded the limit of requests waiting for execution. " +
348 " Increase the value of the system property {} to prevent these situations in the " +
349 "future. Current value of {} = {}.",
350 OPTION_THREAD_WORK_QUEUE_LIMIT,
351 OPTION_THREAD_WORK_QUEUE_LIMIT,
352 threadWorkQueueLimit)
353 );
354 }
355
356 public void setTrustSelfSignedCertificates(boolean trustSelfSignedCertificates) {
357 this.trustSelfSignedCertificates = trustSelfSignedCertificates;
358 }
359
360
361
362
363 public boolean trustSelfSignedCertificates() {
364 return trustSelfSignedCertificates;
365 }
366
367
368
369
370
371
372 public void setProxyOptions(final @Nonnull ProxyOptions proxyOptions) {
373 Preconditions.checkNotNull(proxyOptions, "Proxy options cannot be null");
374 this.proxyOptions = proxyOptions;
375 }
376
377
378
379
380 public ProxyOptions getProxyOptions() {
381 return this.proxyOptions;
382 }
383
384 public int getThreadWorkQueueLimit() {
385 return threadWorkQueueLimit;
386 }
387
388 public void setThreadWorkQueueLimit(int threadWorkQueueLimit) {
389 this.threadWorkQueueLimit = threadWorkQueueLimit;
390 }
391 }