-
Notifications
You must be signed in to change notification settings - Fork 30
/
ClientOptions.java
247 lines (216 loc) · 8.81 KB
/
ClientOptions.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
package com.puppetlabs.http.client;
import com.codahale.metrics.MetricRegistry;
import com.puppetlabs.ssl_utils.SSLUtils;
import javax.net.ssl.SSLContext;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.Security;
/**
* This class is a wrapper around a number of options for use
* in configuring a persistent HTTP Client.
*
* @see com.puppetlabs.http.client.Async#createClient(ClientOptions)
* @see com.puppetlabs.http.client.Sync#createClient(ClientOptions)
*/
public class ClientOptions {
public static final String[] DEFAULT_SSL_PROTOCOLS =
new String[] {"TLSv1.3", "TLSv1.2"};
private SSLContext sslContext;
private String sslCert;
private String sslKey;
private String sslCaCert;
private String[] sslProtocols;
private String[] sslCipherSuites;
private boolean insecure = false;
private boolean forceRedirects = false;
private boolean followRedirects = true;
private int connectTimeoutMilliseconds = -1;
private int socketTimeoutMilliseconds = -1;
private MetricRegistry metricRegistry;
private String metricPrefix;
private String serverId;
private boolean enableURLMetrics = true;
// defaults from apache connection manager
private int maxConnectionsTotal = 20;
private int maxConnectionsPerRoute = 2;
/**
* Constructor for the ClientOptions class. When this constructor is called,
* insecure and forceRedirects will default to false, and followRedirects will default
* to true.
*/
public ClientOptions() { }
/**
* Constructor for the ClientOptions class.
* @param sslContext The SSL context for the client.
* @param sslCert The path to a PEM file containing the client cert
* @param sslKey The path to a PEM file containing the client private key
* @param sslCaCert The path to a PEM file containing the CA cert
* @param sslProtocols The SSL protocols that the client can select from when talking to the server
* @param sslCipherSuites The cipher suites that the client can select from when talking to the server
* @param insecure Whether or not the client should use an insecure connection.
* @param forceRedirects Whether or not the client should follow redirects on POST or PUT requests.
* @param followRedirects Whether or not the client should follow redirects in general.
* @param connectTimeoutMilliseconds Maximum number of milliseconds that
* the client will wait for a
* connection to be established. A
* value of zero is interpreted as
* infinite. A negative value is
* interpreted as undefined (system
* default).
* @param socketTimeoutMilliseconds Maximum number of milliseconds that
* the client will allow for no data to
* be available on the socket before
* closing the underlying connection,
* <code>SO_TIMEOUT</code> in socket
* terms. A timeout of zero is
* interpreted as an infinite timeout.
* A negative value is interpreted as
* undefined (system default).
* @param maxConnectionsPerRoute Maxiumum number of connections allowed for a given route for a client instance, where
* a route is a host / port combination. The apache default is 2
* @param maxConnectionsTotal Maximum number of connections allowed for a given client instance. The apache default
* is 20.
*
*/
public ClientOptions(SSLContext sslContext,
String sslCert,
String sslKey,
String sslCaCert,
String[] sslProtocols,
String[] sslCipherSuites,
boolean insecure,
boolean forceRedirects,
boolean followRedirects,
int connectTimeoutMilliseconds,
int socketTimeoutMilliseconds,
int maxConnectionsPerRoute,
int maxConnectionsTotal) {
this.sslContext = sslContext;
this.sslCert = sslCert;
this.sslKey = sslKey;
this.sslCaCert = sslCaCert;
this.sslProtocols = sslProtocols;
this.sslCipherSuites = sslCipherSuites;
this.insecure = insecure;
this.forceRedirects = forceRedirects;
this.followRedirects = followRedirects;
this.connectTimeoutMilliseconds = connectTimeoutMilliseconds;
this.socketTimeoutMilliseconds = socketTimeoutMilliseconds;
this.maxConnectionsPerRoute = maxConnectionsPerRoute;
this.maxConnectionsTotal = maxConnectionsTotal;
}
public SSLContext getSslContext() {
return sslContext;
}
public ClientOptions setSslContext(SSLContext sslContext) {
this.sslContext = sslContext;
return this;
}
public String getSslCert() {
return sslCert;
}
public ClientOptions setSslCert(String sslCert) {
this.sslCert = sslCert;
return this;
}
public String getSslKey() {
return sslKey;
}
public ClientOptions setSslKey(String sslKey) {
this.sslKey = sslKey;
return this;
}
public String getSslCaCert() {
return sslCaCert;
}
public ClientOptions setSslCaCert(String sslCaCert) {
this.sslCaCert = sslCaCert;
return this;
}
public String[] getSslProtocols() {
return sslProtocols;
}
public ClientOptions setSslProtocols(String[] sslProtocols) {
this.sslProtocols = sslProtocols;
return this;
}
public String[] getSslCipherSuites() {
return sslCipherSuites;
}
public ClientOptions setSslCipherSuites(String[] sslCipherSuites) {
this.sslCipherSuites = sslCipherSuites;
return this;
}
public boolean getInsecure() {
return insecure;
}
public ClientOptions setInsecure(boolean insecure) {
this.insecure = insecure;
return this;
}
public boolean getForceRedirects() { return forceRedirects; }
public ClientOptions setForceRedirects(boolean forceRedirects) {
this.forceRedirects = forceRedirects;
return this;
}
public boolean getFollowRedirects() { return followRedirects; }
public ClientOptions setFollowRedirects(boolean followRedirects) {
this.followRedirects = followRedirects;
return this;
}
public int getConnectTimeoutMilliseconds() {
return connectTimeoutMilliseconds;
}
public ClientOptions setConnectTimeoutMilliseconds(
int connectTimeoutMilliseconds) {
this.connectTimeoutMilliseconds = connectTimeoutMilliseconds;
return this;
}
public int getSocketTimeoutMilliseconds() {
return socketTimeoutMilliseconds;
}
public ClientOptions setSocketTimeoutMilliseconds(
int socketTimeoutMilliseconds) {
this.socketTimeoutMilliseconds = socketTimeoutMilliseconds;
return this;
}
public MetricRegistry getMetricRegistry() {
return metricRegistry;
}
public ClientOptions setMetricRegistry(MetricRegistry metricRegistry) {
this.metricRegistry = metricRegistry;
return this;
}
public String getMetricPrefix() {
return metricPrefix;
}
public ClientOptions setMetricPrefix(String metricPrefix) {
this.metricPrefix = metricPrefix;
return this;
}
public String getServerId() {
return serverId;
}
public ClientOptions setServerId(String serverId) {
this.serverId = serverId;
return this;
}
public boolean isEnableURLMetrics() {
return enableURLMetrics;
}
public ClientOptions setEnableURLMetrics(boolean enableURLMetrics) {
this.enableURLMetrics = enableURLMetrics;
return this;
}
public int getMaxConnectionsTotal() {return this.maxConnectionsTotal; }
public ClientOptions setMaxConnectionsTotal(int maxConnectionsTotal) {
this.maxConnectionsTotal = maxConnectionsTotal;
return this;
}
public int getMaxConnectionsPerRoute() {return this.maxConnectionsPerRoute; }
public ClientOptions setMaxConnectionsPerRoute(int maxConnectionsPerRoute) {
this.maxConnectionsPerRoute = maxConnectionsPerRoute;
return this;
}
}