/
Sync.java
308 lines (275 loc) · 11.6 KB
/
Sync.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
package com.puppetlabs.http.client;
import com.puppetlabs.http.client.impl.JavaClient;
import com.puppetlabs.http.client.impl.PersistentSyncHttpClient;
import com.puppetlabs.http.client.metrics.Metrics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
/**
* This class allows for the creation of a persistent synchronous HTTP client. It also allows
* for sending synchronous HTTP requests without a persistent HTTP client.
*/
public class Sync {
private static final Logger LOGGER = LoggerFactory.getLogger(Sync.class);
private static RequestOptions extractRequestOptions(SimpleRequestOptions simpleOptions) {
URI uri = simpleOptions.getUri();
Map<String, String> headers = simpleOptions.getHeaders();
Object body = simpleOptions.getBody();
boolean decompressBody = simpleOptions.getDecompressBody();
ResponseBodyType as = simpleOptions.getAs();
CompressType requestBodyDecompression =
simpleOptions.getCompressRequestBody();
RequestOptions requestOptions = new RequestOptions(
uri, headers, body, decompressBody, as);
requestOptions.setCompressRequestBody(requestBodyDecompression);
return requestOptions;
}
private static ClientOptions extractClientOptions(SimpleRequestOptions simpleOptions) {
SSLContext sslContext = simpleOptions.getSslContext();
String sslCert = simpleOptions.getSslCert();
String sslKey = simpleOptions.getSslKey();
String sslCaCert = simpleOptions.getSslCaCert();
String[] sslProtocols = simpleOptions.getSslProtocols();
String[] sslCipherSuites = simpleOptions.getSslCipherSuites();
boolean insecure = simpleOptions.getInsecure();
boolean forceRedirects = simpleOptions.getForceRedirects();
boolean followRedirects = simpleOptions.getFollowRedirects();
int connectTimeoutMilliseconds =
simpleOptions.getConnectTimeoutMilliseconds();
int socketTimeoutMilliseconds =
simpleOptions.getSocketTimeoutMilliseconds();
return new ClientOptions(sslContext, sslCert, sslKey, sslCaCert,
sslProtocols, sslCipherSuites, insecure,
forceRedirects, followRedirects, connectTimeoutMilliseconds,
socketTimeoutMilliseconds,
// this option set is only used for one-shot requests, so at the point of being explicit,
// only allow at most one connection at a time.
1, 1);
}
private static Response request(SimpleRequestOptions simpleRequestOptions,
HttpMethod method) {
// TODO: if we end up implementing an async version of the java API,
// we should refactor this implementation so that it is based on the
// async one, as Patrick has done in the clojure API.
final Response response;
final SyncHttpClient client = createClient(
extractClientOptions(simpleRequestOptions));
try {
response = client.request(
extractRequestOptions(simpleRequestOptions),
method);
}
finally {
try {
client.close();
}
catch (IOException e) {
LOGGER.error("Error closing client", e);
}
}
return response;
}
/**
* Creates a synchronous persistent HTTP client
* @param clientOptions
* @return A persistent synchronous HTTP client
*/
public static SyncHttpClient createClient(ClientOptions clientOptions) {
final String metricNamespace = Metrics.buildMetricNamespace(clientOptions.getMetricPrefix(),
clientOptions.getServerId());
return new PersistentSyncHttpClient(JavaClient.createClient(clientOptions),
clientOptions.getMetricRegistry(), metricNamespace, clientOptions.isEnableURLMetrics());
}
/**
* Makes a simple HTTP GET request
* @param url The URL against which to make the request
* @return The HTTP Response corresponding to the request
* @throws URISyntaxException
*/
public static Response get(String url) throws URISyntaxException {
return get(new URI(url));
}
/**
* Makes a simple HTTP GET request
* @param uri The URI against which to make the request
* @return The HTTP Response corresponding to the request
*/
public static Response get(URI uri) {
return get(new SimpleRequestOptions(uri));
}
/**
* Makes a simple HTTP GET request
* @param simpleRequestOptions The options to configure the request and the client making it
* @return The HTTP response corresponding to the request
*/
public static Response get(SimpleRequestOptions simpleRequestOptions) {
return request(simpleRequestOptions, HttpMethod.GET);
}
/**
* Makes a simple HTTP HEAD request
* @param url The URL against which to make the request
* @return The HTTP Response corresponding to the request
* @throws URISyntaxException
*/
public static Response head(String url) throws URISyntaxException {
return head(new URI(url));
}
/**
* Makes a simple HTTP HEAD request
* @param uri The URI against which to make the request
* @return The HTTP Response corresponding to the request
*/
public static Response head(URI uri) {
return head(new SimpleRequestOptions(uri));
}
/**
* Makes a simple HTTP HEAD request
* @param simpleRequestOptions The options to configure the request and the client making it
* @return The HTTP response corresponding to the request
*/
public static Response head(SimpleRequestOptions simpleRequestOptions) {
return request(simpleRequestOptions, HttpMethod.HEAD);
}
/**
* Makes a simple HTTP POST request
* @param url The URL against which to make the request
* @return The HTTP Response corresponding to the request
* @throws URISyntaxException
*/
public static Response post(String url) throws URISyntaxException {
return post(new URI(url));
}
/**
* Makes a simple HTTP POST request
* @param uri The URI against which to make the request
* @return The HTTP Response corresponding to the request
*/
public static Response post(URI uri) { return post(new SimpleRequestOptions(uri)); }
/**
* Makes a simple HTTP POST request
* @param simpleRequestOptions The options to configure the request and the client making it
* @return The HTTP response corresponding to the request
*/
public static Response post(SimpleRequestOptions simpleRequestOptions) {
return request(simpleRequestOptions, HttpMethod.POST);
}
/**
* Makes a simple HTTP PUT request
* @param url The URL against which to make the request
* @return The HTTP Response corresponding to the request
* @throws URISyntaxException
*/
public static Response put(String url) throws URISyntaxException {
return put(new URI(url));
}
/**
* Makes a simple HTTP PUT request
* @param uri The URI against which to make the request
* @return The HTTP Response corresponding to the request
*/
public static Response put(URI uri) { return put(new SimpleRequestOptions(uri)); }
/**
* Makes a simple HTTP PUT request
* @param simpleRequestOptions The options to configure the request and the client making it
* @return The HTTP response corresponding to the request
*/
public static Response put(SimpleRequestOptions simpleRequestOptions) {
return request(simpleRequestOptions, HttpMethod.PUT);
}
/**
* Makes a simple HTTP DELETE request
* @param url The URL against which to make the request
* @return The HTTP Response corresponding to the request
* @throws URISyntaxException
*/
public static Response delete(String url) throws URISyntaxException {
return delete(new URI(url));
}
/**
* Makes a simple HTTP DELETE request
* @param uri The URI against which to make the request
* @return The HTTP Response corresponding to the request
*/
public static Response delete(URI uri) { return delete(new SimpleRequestOptions(uri)); }
/**
* Makes a simple HTTP DELETE request
* @param simpleRequestOptions The options to configure the request and the client making it
* @return The HTTP response corresponding to the request
*/
public static Response delete(SimpleRequestOptions simpleRequestOptions) {
return request(simpleRequestOptions, HttpMethod.DELETE);
}
/**
* Makes a simple HTTP TRACE request
* @param url The URL against which to make the request
* @return The HTTP Response corresponding to the request
* @throws URISyntaxException
*/
public static Response trace(String url) throws URISyntaxException {
return trace(new URI(url));
}
/**
* Makes a simple HTTP TRACE request
* @param uri The URI against which to make the request
* @return The HTTP Response corresponding to the request
*/
public static Response trace(URI uri) { return trace(new SimpleRequestOptions(uri)); }
/**
* Makes a simple HTTP TRACE request
* @param simpleRequestOptions The options to configure the request and the client making it
* @return The HTTP response corresponding to the request
*/
public static Response trace(SimpleRequestOptions simpleRequestOptions) {
return request(simpleRequestOptions, HttpMethod.TRACE);
}
/**
* Makes a simple HTTP OPTIONS request
* @param url The URL against which to make the request
* @return The HTTP Response corresponding to the request
* @throws URISyntaxException
*/
public static Response options(String url) throws URISyntaxException {
return options(new URI(url));
}
/**
* Makes a simple HTTP OPTIONS request
* @param uri The URI against which to make the request
* @return The HTTP Response corresponding to the request
*/
public static Response options(URI uri) { return options(new SimpleRequestOptions(uri)); }
/**
* Makes a simple HTTP OPTIONS request
* @param simpleRequestOptions The options to configure the request and the client making it
* @return The HTTP response corresponding to the request
*/
public static Response options(SimpleRequestOptions simpleRequestOptions) {
return request(simpleRequestOptions, HttpMethod.OPTIONS);
}
/**
* Makes a simple HTTP PATCH request
* @param url The URL against which to make the request
* @return The HTTP Response corresponding to the request
* @throws URISyntaxException
*/
public static Response patch(String url) throws URISyntaxException {
return patch(new URI(url));
}
/**
* Makes a simple HTTP PATCH request
* @param uri The URI against which to make the request
* @return The HTTP Response corresponding to the request
*/
public static Response patch(URI uri) { return patch(new SimpleRequestOptions(uri)); }
/**
* Makes a simple HTTP PATCH request
* @param simpleRequestOptions The options to configure the request and the client making it
* @return The HTTP response corresponding to the request
*/
public static Response patch(SimpleRequestOptions simpleRequestOptions) {
return request(simpleRequestOptions, HttpMethod.PATCH);
}
}