/
HttpClientOptions.java
441 lines (384 loc) · 12 KB
/
HttpClientOptions.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
* Copyright (c) 2011-2014 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package io.vertx.core.http;
import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.ClientOptionsBase;
import io.vertx.core.net.JksOptions;
import io.vertx.core.net.PemCaOptions;
import io.vertx.core.net.PemKeyCertOptions;
import io.vertx.core.net.PfxOptions;
import io.vertx.core.net.CaOptions;
import io.vertx.core.net.TCPSSLOptions;
/**
* Options describing how an {@link HttpClient} will make connections.
*
* @author <a href="http://tfox.org">Tim Fox</a>
*/
@DataObject
public class HttpClientOptions extends ClientOptionsBase {
/**
* The default maximum number of connections a client will pool = 5
*/
public static final int DEFAULT_MAX_POOL_SIZE = 5;
/**
* Default value of whether keep-alive is enabled = true
*/
public static final boolean DEFAULT_KEEP_ALIVE = true;
/**
* Default value of whether pipe-lining is enabled = false
*/
public static final boolean DEFAULT_PIPELINING = false;
/**
* Default value of whether the client will attempt to use compression = false
*/
public static final boolean DEFAULT_TRY_USE_COMPRESSION = false;
/**
* Default value of whether hostname verification (for SSL/TLS) is enabled = true
*/
public static final boolean DEFAULT_VERIFY_HOST = true;
/**
* The default value for maximum websocket frame size = 65536 bytes
*/
public static final int DEFAULT_MAX_WEBSOCKET_FRAME_SIZE = 65536;
/**
* The default value for host name = "localhost"
*/
public static final String DEFAULT_DEFAULT_HOST = "localhost";
/**
* The default value for port = 80
*/
public static final int DEFAULT_DEFAULT_PORT = 80;
private boolean verifyHost = true;
private int maxPoolSize;
private boolean keepAlive;
private boolean pipelining;
private boolean tryUseCompression;
private int maxWebsocketFrameSize;
private String defaultHost;
private int defaultPort;
/**
* Default constructor
*/
public HttpClientOptions() {
super();
verifyHost = DEFAULT_VERIFY_HOST;
maxPoolSize = DEFAULT_MAX_POOL_SIZE;
keepAlive = DEFAULT_KEEP_ALIVE;
pipelining = DEFAULT_PIPELINING;
tryUseCompression = DEFAULT_TRY_USE_COMPRESSION;
maxWebsocketFrameSize = DEFAULT_MAX_WEBSOCKET_FRAME_SIZE;
defaultHost = DEFAULT_DEFAULT_HOST;
defaultPort = DEFAULT_DEFAULT_PORT;
}
/**
* Copy constructor
*
* @param other the options to copy
*/
public HttpClientOptions(HttpClientOptions other) {
super(other);
this.verifyHost = other.isVerifyHost();
this.maxPoolSize = other.getMaxPoolSize();
this.keepAlive = other.isKeepAlive();
this.pipelining = other.isPipelining();
this.tryUseCompression = other.isTryUseCompression();
this.maxWebsocketFrameSize = other.maxWebsocketFrameSize;
this.defaultHost = other.defaultHost;
this.defaultPort = other.defaultPort;
}
/**
* Constructor to create an options from JSON
*
* @param json the JSON
*/
public HttpClientOptions(JsonObject json) {
super(json);
this.verifyHost = json.getBoolean("verifyHost", DEFAULT_VERIFY_HOST);
this.maxPoolSize = json.getInteger("maxPoolSize", DEFAULT_MAX_POOL_SIZE);
this.keepAlive = json.getBoolean("keepAlive", DEFAULT_KEEP_ALIVE);
this.pipelining = json.getBoolean("pipelining", DEFAULT_PIPELINING);
this.tryUseCompression = json.getBoolean("tryUseCompression", DEFAULT_TRY_USE_COMPRESSION);
this.maxWebsocketFrameSize = json.getInteger("maxWebsocketFrameSize", DEFAULT_MAX_WEBSOCKET_FRAME_SIZE);
this.defaultHost = json.getString("defaultHost", DEFAULT_DEFAULT_HOST);
this.defaultPort = json.getInteger("defaultPort", DEFAULT_DEFAULT_PORT);
}
@Override
public HttpClientOptions setSendBufferSize(int sendBufferSize) {
super.setSendBufferSize(sendBufferSize);
return this;
}
@Override
public HttpClientOptions setReceiveBufferSize(int receiveBufferSize) {
super.setReceiveBufferSize(receiveBufferSize);
return this;
}
@Override
public HttpClientOptions setReuseAddress(boolean reuseAddress) {
super.setReuseAddress(reuseAddress);
return this;
}
@Override
public HttpClientOptions setTrafficClass(int trafficClass) {
super.setTrafficClass(trafficClass);
return this;
}
@Override
public HttpClientOptions setTcpNoDelay(boolean tcpNoDelay) {
super.setTcpNoDelay(tcpNoDelay);
return this;
}
@Override
public HttpClientOptions setTcpKeepAlive(boolean tcpKeepAlive) {
super.setTcpKeepAlive(tcpKeepAlive);
return this;
}
@Override
public HttpClientOptions setSoLinger(int soLinger) {
super.setSoLinger(soLinger);
return this;
}
@Override
public HttpClientOptions setUsePooledBuffers(boolean usePooledBuffers) {
super.setUsePooledBuffers(usePooledBuffers);
return this;
}
@Override
public HttpClientOptions setIdleTimeout(int idleTimeout) {
super.setIdleTimeout(idleTimeout);
return this;
}
@Override
public HttpClientOptions setSsl(boolean ssl) {
super.setSsl(ssl);
return this;
}
@Override
public HttpClientOptions setKeyStoreOptions(JksOptions options) {
super.setKeyStoreOptions(options);
return this;
}
@Override
public HttpClientOptions setPfxKeyCertOptions(PfxOptions options) {
return (HttpClientOptions) super.setPfxKeyCertOptions(options);
}
@Override
public HttpClientOptions setPemKeyCertOptions(PemKeyCertOptions options) {
return (HttpClientOptions) super.setPemKeyCertOptions(options);
}
@Override
public HttpClientOptions setTrustStoreOptions(JksOptions options) {
super.setTrustStoreOptions(options);
return this;
}
@Override
public HttpClientOptions setPfxCaOptions(PfxOptions options) {
return (HttpClientOptions) super.setPfxCaOptions(options);
}
@Override
public HttpClientOptions setPemCaOptions(PemCaOptions options) {
return (HttpClientOptions) super.setPemCaOptions(options);
}
@Override
public HttpClientOptions addEnabledCipherSuite(String suite) {
super.addEnabledCipherSuite(suite);
return this;
}
@Override
public HttpClientOptions setConnectTimeout(int connectTimeout) {
super.setConnectTimeout(connectTimeout);
return this;
}
@Override
public HttpClientOptions setTrustAll(boolean trustAll) {
super.setTrustAll(trustAll);
return this;
}
/**
* Get the maximum pool size for connections
*
* @return the maximum pool size
*/
public int getMaxPoolSize() {
return maxPoolSize;
}
/**
* Set the maximum pool size for connections
*
* @param maxPoolSize the maximum pool size
* @return a reference to this, so the API can be used fluently
*/
public HttpClientOptions setMaxPoolSize(int maxPoolSize) {
if (maxPoolSize < 1) {
throw new IllegalArgumentException("maxPoolSize must be > 0");
}
this.maxPoolSize = maxPoolSize;
return this;
}
/**
* Is keep alive enabled on the client?
*
* @return true if enabled
*/
public boolean isKeepAlive() {
return keepAlive;
}
/**
* Set whether keep alive is enabled on the client
*
* @param keepAlive true if enabled
* @return a reference to this, so the API can be used fluently
*/
public HttpClientOptions setKeepAlive(boolean keepAlive) {
this.keepAlive = keepAlive;
return this;
}
/**
* Is pipe-lining enabled on the client
*
* @return true if pipe-lining is enabled
*/
public boolean isPipelining() {
return pipelining;
}
/**
* Set whether pipe-lining is enabled on the client
*
* @param pipelining true if enabled
* @return a reference to this, so the API can be used fluently
*/
public HttpClientOptions setPipelining(boolean pipelining) {
this.pipelining = pipelining;
return this;
}
/**
* Is hostname verification (for SSL/TLS) enabled?
*
* @return true if enabled
*/
public boolean isVerifyHost() {
return verifyHost;
}
/**
* Set whether hostname verification is enabled
*
* @param verifyHost true if enabled
* @return a reference to this, so the API can be used fluently
*/
public HttpClientOptions setVerifyHost(boolean verifyHost) {
this.verifyHost = verifyHost;
return this;
}
/**
* Is compression enabled on the client?
*
* @return true if enabled
*/
public boolean isTryUseCompression() {
return tryUseCompression;
}
/**
* Set whether compression is enabled
*
* @param tryUseCompression true if enabled
* @return a reference to this, so the API can be used fluently
*/
public HttpClientOptions setTryUseCompression(boolean tryUseCompression) {
this.tryUseCompression = tryUseCompression;
return this;
}
/**
* Get the maximum websocket framesize to use
*
* @return the max websocket framesize
*/
public int getMaxWebsocketFrameSize() {
return maxWebsocketFrameSize;
}
/**
* Set the max websocket frame size
*
* @param maxWebsocketFrameSize the max frame size, in bytes
* @return a reference to this, so the API can be used fluently
*/
public HttpClientOptions setMaxWebsocketFrameSize(int maxWebsocketFrameSize) {
this.maxWebsocketFrameSize = maxWebsocketFrameSize;
return this;
}
/**
* Get the default host name to be used by this client in requests if none is provided when making the request.
*
* @return the default host name
*/
public String getDefaultHost() {
return defaultHost;
}
/**
* Set the default host name to be used by this client in requests if none is provided when making the request.
*
* @return a reference to this, so the API can be used fluently
*/
public HttpClientOptions setDefaultHost(String defaultHost) {
this.defaultHost = defaultHost;
return this;
}
/**
* Get the default port to be used by this client in requests if none is provided when making the request.
*
* @return the default port
*/
public int getDefaultPort() {
return defaultPort;
}
/**
* Set the default port to be used by this client in requests if none is provided when making the request.
*
* @return a reference to this, so the API can be used fluently
*/
public HttpClientOptions setDefaultPort(int defaultPort) {
this.defaultPort = defaultPort;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof HttpClientOptions)) return false;
if (!super.equals(o)) return false;
HttpClientOptions that = (HttpClientOptions) o;
if (defaultPort != that.defaultPort) return false;
if (keepAlive != that.keepAlive) return false;
if (maxPoolSize != that.maxPoolSize) return false;
if (maxWebsocketFrameSize != that.maxWebsocketFrameSize) return false;
if (pipelining != that.pipelining) return false;
if (tryUseCompression != that.tryUseCompression) return false;
if (verifyHost != that.verifyHost) return false;
if (!defaultHost.equals(that.defaultHost)) return false;
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (verifyHost ? 1 : 0);
result = 31 * result + maxPoolSize;
result = 31 * result + (keepAlive ? 1 : 0);
result = 31 * result + (pipelining ? 1 : 0);
result = 31 * result + (tryUseCompression ? 1 : 0);
result = 31 * result + maxWebsocketFrameSize;
result = 31 * result + defaultHost.hashCode();
result = 31 * result + defaultPort;
return result;
}
}