-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathHttpClientWrapper.java
More file actions
687 lines (623 loc) · 24.4 KB
/
HttpClientWrapper.java
File metadata and controls
687 lines (623 loc) · 24.4 KB
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
package com.dtsx.astra.sdk.utils;
import com.dtsx.astra.sdk.exception.AuthenticationException;
import com.dtsx.astra.sdk.utils.observability.ApiExecutionInfos;
import com.dtsx.astra.sdk.utils.observability.ApiRequestObserver;
import com.dtsx.astra.sdk.utils.observability.CompletableFutures;
import org.apache.hc.client5.http.auth.StandardAuthScheme;
import org.apache.hc.client5.http.classic.methods.HttpDelete;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpHead;
import org.apache.hc.client5.http.classic.methods.HttpPatch;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpPut;
import org.apache.hc.client5.http.classic.methods.HttpTrace;
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.cookie.StandardCookieSpec;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.Method;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.HttpURLConnection;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.stream.Collectors;
/**
* Helper to forge Http Requests to interact with Devops API.
*/
public class HttpClientWrapper {
/** Logger for our Client. */
private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientWrapper.class);
/** Value for the requested with. */
private static final String REQUEST_WITH = "AstraJavaSDK " + HttpClientWrapper.class.getPackage().getImplementationVersion();
/** Default settings in Request and Retry */
private static final int DEFAULT_TIMEOUT_REQUEST = 20;
/** Default settings in Request and Retry */
private static final int DEFAULT_TIMEOUT_CONNECT = 20;
/** Default retry settings */
private static final int DEFAULT_MAX_RETRIES = 3;
/** Default retry settings */
private static final int DEFAULT_RETRY_INITIAL_DELAY_MS = 1000;
/** Default retry settings */
private static final double DEFAULT_RETRY_BACKOFF_MULTIPLIER = 2.0;
/** Headers, Api is using JSON */
private static final String CONTENT_TYPE_JSON = "application/json";
/** Header param. */
private static final String HEADER_ACCEPT = "Accept";
/** Headers param to insert the conte type. */
private static final String HEADER_CONTENT_TYPE = "Content-Type";
/** Headers param to insert the token for devops API. */
private static final String HEADER_AUTHORIZATION = "Authorization";
/** Headers name to insert the user agent identifying the client. */
private static final String HEADER_USER_AGENT = "User-Agent";
/** Headers param to insert the user agent identifying the client. */
private static final String HEADER_REQUESTED_WITH = "X-Requested-With";
/** Current organization identifier. */
private static final String HEADER_CURRENT_ORG = "X-DataStax-Current-Org";
/** Current pulsar cluster. */
private static final String HEADER_CURRENT_PULSAR_CLUSTER = "X-DataStax-Pulsar-Cluster";
/** Singleton pattern. */
private static HttpClientWrapper _instance = null;
/** HttpComponent5. */
protected CloseableHttpClient httpClient = null;
/** Observers. */
protected static Map<String, ApiRequestObserver> observers = new LinkedHashMap<>();
/** Observers. */
protected String operationName= "n/a";
/** Default request configuration. */
protected static RequestConfig requestConfig = RequestConfig.custom()
.setCookieSpec(StandardCookieSpec.STRICT)
.setExpectContinueEnabled(true)
.setConnectionRequestTimeout(Timeout.ofSeconds(DEFAULT_TIMEOUT_REQUEST))
.setConnectTimeout(Timeout.ofSeconds(DEFAULT_TIMEOUT_CONNECT))
.setTargetPreferredAuthSchemes(Arrays.asList(StandardAuthScheme.NTLM, StandardAuthScheme.DIGEST))
.build();
/** Retry configuration. */
protected static int maxRetries = DEFAULT_MAX_RETRIES;
/** Retry configuration. */
protected static int retryInitialDelayMs = DEFAULT_RETRY_INITIAL_DELAY_MS;
/** Retry configuration. */
protected static double retryBackoffMultiplier = DEFAULT_RETRY_BACKOFF_MULTIPLIER;
// -------------------------------------------
// ----------------- Singleton ---------------
// -------------------------------------------
/**
* Hide default constructor
*/
private HttpClientWrapper() {}
/**
* Singleton Pattern.
*
* @return
* singleton for the class
*/
private static synchronized HttpClientWrapper getInstance() {
if (_instance == null) {
_instance = new HttpClientWrapper();
final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setValidateAfterInactivity(TimeValue.ofSeconds(10));
connManager.setMaxTotal(100);
connManager.setDefaultMaxPerRoute(10);
_instance.httpClient = HttpClients.custom().setConnectionManager(connManager).build();
}
return _instance;
}
/**
* Singleton Pattern.
*
* @param operation
* name of the operation
* @return
* singleton for the class
*/
public static synchronized HttpClientWrapper getInstance(String operation) {
if (_instance == null) {
_instance = new HttpClientWrapper();
final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setValidateAfterInactivity(TimeValue.ofSeconds(10));
connManager.setMaxTotal(100);
connManager.setDefaultMaxPerRoute(10);
_instance.httpClient = HttpClients.custom().setConnectionManager(connManager).build();
}
_instance.operationName = operation;
return _instance;
}
// -------------------------------------------
// ---------- Working with HTTP --------------
// -------------------------------------------
/**
* Helper to build the HTTP request.
*
* @param url
* target url
* @param token
* authentication token
* @return
* http request
*/
public ApiResponseHttp GET(String url, String token) {
return executeHttp(Method.GET, url, token, null, CONTENT_TYPE_JSON, false);
}
/**
* Helper to build the HTTP request.
*
* @param url
* target url
* @param token
* authentication token
* @param pulsarCluster
* pulsar cluster
* @param organizationId
* organization identifier
* @return
* http request
*/
public ApiResponseHttp GET_PULSAR(String url, String token, String pulsarCluster, String organizationId) {
HttpUriRequestBase request = buildRequest(Method.GET, url, token, null, CONTENT_TYPE_JSON);
updatePulsarHttpRequest(request, token, pulsarCluster, organizationId);
return executeHttp(request, false);
}
/**
* Helper to build the HTTP request.
*
* @param url
* target url
* @param token
* authentication token
* @param body
* request body
* @param pulsarCluster
* pulsar cluster
* @param organizationId
* organization identifier
* @return
* http request
*/
public ApiResponseHttp POST_PULSAR(String url, String token, String body, String pulsarCluster, String organizationId) {
HttpUriRequestBase request = buildRequest(Method.POST, url, token, body, CONTENT_TYPE_JSON);
updatePulsarHttpRequest(request, token, pulsarCluster, organizationId);
return executeHttp(request, false);
}
/**
* Helper to build the HTTP request.
*
* @param url
* target url
* @param token
* authentication token
* @param body
* request body
* @param pulsarCluster
* pulsar cluster
* @param organizationId
* organization identifier
* @return
* http request
*/
public ApiResponseHttp DELETE_PULSAR(String url, String token, String body, String pulsarCluster, String organizationId) {
HttpUriRequestBase request = buildRequest(Method.DELETE, url, token, body, CONTENT_TYPE_JSON);
updatePulsarHttpRequest(request, token, pulsarCluster, organizationId);
return executeHttp(request, false);
}
/**
* Add item for a pulsar request.
*
* @param request
* current request
* @param pulsarToken
* pulsar token
* @param pulsarCluster
* pulsar cluster
* @param organizationId
* organization
*/
private void updatePulsarHttpRequest(HttpUriRequestBase request, String pulsarToken, String pulsarCluster, String organizationId) {
request.addHeader(HEADER_AUTHORIZATION, pulsarToken);
request.addHeader(HEADER_CURRENT_ORG, organizationId);
request.addHeader(HEADER_CURRENT_PULSAR_CLUSTER, pulsarCluster);
}
/**
* Helper to build the HTTP request.
*
* @param url
* target url
* @param token
* authentication token
* @return
* http request
*/
public ApiResponseHttp HEAD(String url, String token) {
return executeHttp(Method.HEAD, url, token, null, CONTENT_TYPE_JSON, false);
}
/**
* Helper to build the HTTP request.
*
* @param url
* target url
* @param token
* authentication token
* @return
* http request
*/
public ApiResponseHttp POST(String url, String token) {
return executeHttp(Method.POST, url, token, null, CONTENT_TYPE_JSON, true);
}
/**
* Helper to build the HTTP request.
*
* @param url
* target url
* @param token
* authentication token
* @param body
* request body
* @return
* http request
*/
public ApiResponseHttp POST(String url, String token, String body) {
return executeHttp(Method.POST, url, token, body, CONTENT_TYPE_JSON, true);
}
/**
* Helper to build the HTTP request.
*
* @param url
* target url
* @param token
* authentication token
*/
public void DELETE(String url, String token) {
executeHttp(Method.DELETE, url, token, null, CONTENT_TYPE_JSON, true);
}
/**
* Helper to build the HTTP request.
*
* @param url
* target url
* @param token
* authentication token
* @param body
* request body
*/
public void PUT(String url, String token, String body) {
executeHttp(Method.PUT, url, token, body, CONTENT_TYPE_JSON, false);
}
/**
* Helper to build the HTTP request.
*
* @param url
* target url
* @param token
* authentication token
* @param body
* request body
*/
public void PATCH(String url, String token, String body) {
executeHttp(Method.PATCH, url, token, body, CONTENT_TYPE_JSON, false);
}
/**
* Main Method executing HTTP Request.
*
* @param method
* http method
* @param url
* url
* @param token
* authentication token
* @param contentType
* request content type
* @param reqBody
* request body
* @param mandatory
* allow 404 errors
* @return
* basic request
*/
public ApiResponseHttp executeHttp(final Method method, final String url, final String token, String reqBody, String contentType, boolean mandatory) {
return executeHttp(buildRequest(method, url, token, reqBody, contentType), mandatory);
}
/**
* Execute a request coming from elsewhere with retry logic.
*
* @param req
* current request
* @param mandatory
* mandatory
* @return
* api response
*/
public ApiResponseHttp executeHttp(HttpUriRequestBase req, boolean mandatory) {
return executeHttpWithRetry(req, mandatory, maxRetries, retryInitialDelayMs, retryBackoffMultiplier);
}
/**
* Execute a request with configurable retry logic.
*
* @param req
* current request
* @param mandatory
* mandatory
* @param maxRetries
* maximum number of retries
* @param initialDelayMs
* initial delay between retries in milliseconds
* @param backoffMultiplier
* multiplier for exponential backoff
* @return
* api response
*/
public ApiResponseHttp executeHttpWithRetry(HttpUriRequestBase req, boolean mandatory, int maxRetries, int initialDelayMs, double backoffMultiplier) {
// Execution Infos
ApiExecutionInfos.ApiExecutionInfoBuilder executionInfo = ApiExecutionInfos.builder()
.withOperationName(operationName)
.withHttpRequest(req);
int retryCount = 0;
long delayMs = initialDelayMs;
while (true) {
try (CloseableHttpResponse response = httpClient.execute(req)) {
ApiResponseHttp res;
if (response == null) {
res = new ApiResponseHttp("Response is empty, please check url",
HttpURLConnection.HTTP_UNAVAILABLE, null);
} else {
// Mapping response
String body = null;
if (null != response.getEntity()) {
body = EntityUtils.toString(response.getEntity());
EntityUtils.consume(response.getEntity());
}
Map<String, String> headers = new HashMap<>();
Arrays.stream(response.getHeaders()).forEach(h -> headers.put(h.getName(), h.getValue()));
res = new ApiResponseHttp(body, response.getCode(), headers);
}
// Error management
if (HttpURLConnection.HTTP_NOT_FOUND == res.getCode() && !mandatory) {
return res;
}
// Check if we should retry
if (res.getCode() >= 500 && retryCount < maxRetries) {
LOGGER.warn("Received HTTP {} error, retrying in {} ms (attempt {}/{})",
res.getCode(), delayMs, retryCount + 1, maxRetries);
Thread.sleep(delayMs);
delayMs = (long) (delayMs * backoffMultiplier);
retryCount++;
continue;
}
if (res.getCode() >= 300) {
String entity = "n/a";
if (req.getEntity() != null) {
entity = EntityUtils.toString(req.getEntity());
}
LOGGER.error("Error for request, url={}, method={}, body={}",
req.getUri().toString(), req.getMethod(), entity);
LOGGER.error("Response code={}, body={}", res.getCode(), res.getBody());
processErrors(res, mandatory);
LOGGER.error("An HTTP Error occurred. The HTTP CODE Return is {}", res.getCode());
}
executionInfo.withHttpResponse(res);
return res;
} catch (IllegalArgumentException | IllegalStateException e) {
throw e;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Request interrupted", e);
} catch (Exception e) {
if (retryCount < maxRetries) {
LOGGER.warn("Request failed with exception, retrying in {} ms (attempt {}/{})",
delayMs, retryCount + 1, maxRetries, e);
try {
Thread.sleep(delayMs);
delayMs = (long) (delayMs * backoffMultiplier);
retryCount++;
continue;
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new RuntimeException("Request interrupted", ie);
}
}
throw new RuntimeException("Error in HTTP Request: " + e.getMessage(), e);
} finally {
// Notify the observers
CompletableFuture.runAsync(() -> notifyASync(l -> l.onRequest(executionInfo.build()), observers.values()));
}
}
}
/**
* Initialize an HTTP request against Stargate.
*
* @param method
* http Method
* @param url
* target URL
* @param token
* current token
* @return
* default http with header
*/
private HttpUriRequestBase buildRequest(final Method method, final String url, final String token, String body, String contentType) {
HttpUriRequestBase req;
switch(method) {
case GET: req = new HttpGet(url); break;
case POST: req = new HttpPost(url); break;
case PUT: req = new HttpPut(url); break;
case DELETE: req = new HttpDelete(url); break;
case PATCH: req = new HttpPatch(url); break;
case HEAD: req = new HttpHead(url); break;
case TRACE: req = new HttpTrace(url); break;
case OPTIONS:
case CONNECT:
default:throw new IllegalArgumentException("Invalid HTTP Method");
}
req.addHeader(HEADER_CONTENT_TYPE, contentType);
req.addHeader(HEADER_ACCEPT, CONTENT_TYPE_JSON);
req.addHeader(HEADER_USER_AGENT, REQUEST_WITH);
req.addHeader(HEADER_REQUESTED_WITH, REQUEST_WITH);
req.addHeader(HEADER_AUTHORIZATION, "Bearer " + token);
req.setConfig(requestConfig);
if (null != body) {
req.setEntity(new StringEntity(body, ContentType.TEXT_PLAIN));
}
return req;
}
/**
* Process ERRORS.Anything above code 300 can be marked as an error Still something
* 404 is expected and should not result in throwing exception (=not find)
* @param res HttpResponse
*/
private void processErrors(ApiResponseHttp res, boolean mandatory) {
String body = res.getBody();
switch(res.getCode()) {
// 400
case HttpURLConnection.HTTP_BAD_REQUEST:
throw new IllegalArgumentException("HTTP_BAD_REQUEST (code=" + res.getCode() +
"): Invalid Parameters " + body);
// 401
case HttpURLConnection.HTTP_UNAUTHORIZED:
throw new AuthenticationException("HTTP_UNAUTHORIZED (code=" + res.getCode() +
"): Invalid Credentials. Your token is invalid for target environment.");
// 403
case HttpURLConnection.HTTP_FORBIDDEN:
throw new AuthenticationException("HTTP_FORBIDDEN (code=" + res.getCode() +
"): Invalid permissions. Your token may not have expected permissions to perform this actions.");
// 404
case HttpURLConnection.HTTP_NOT_FOUND:
if (mandatory) {
throw new IllegalArgumentException("HTTP_NOT_FOUND (code=" + res.getCode() +
") Object not found: " + body);
}
break;
// 409
case HttpURLConnection.HTTP_CONFLICT:
throw new AuthenticationException("HTTP_CONFLICT (code=" + res.getCode() +
"): Object may already exist with same name or id " +
body);
case 422:
throw new IllegalArgumentException("Error Code=" + res.getCode() +
"(422) Invalid information provided to create DB: "
+ body);
default:
if (res.getCode() == HttpURLConnection.HTTP_UNAVAILABLE) {
throw new IllegalStateException("(code=" + res.getCode() + ")" + body);
}
throw new RuntimeException(" (code=" + res.getCode() + ")" + body);
}
}
/**
* Allow to register a listener for the command.
* @param name
* name of the observer
* @param observer
* observer to register
*/
public static void registerObserver(String name, ApiRequestObserver observer) {
observers.put(name, observer);
}
/**
* Allow to register a listener for the command.
*
* @param observers
* observer sto register
*/
public static void registerObservers(Map<String, ApiRequestObserver> observers) {
if (observers != null) {
observers.forEach(HttpClientWrapper::registerObserver);
}
}
/**
* Register an observer with its className.
*
* @param observer
* command observer
*/
public static void registerObserver(ApiRequestObserver observer) {
registerObserver(observer.getClass().getSimpleName(), observer);
}
/**
* Remove a listener from the command.
*
* @param name
* name of the observer
*/
public static void unregisterObserver(String name) {
observers.remove(name);
}
/**
* Remove an observer by its class.
*
* @param observer
* observer to remove
*/
public static void unregisterObserver(Class<ApiRequestObserver> observer) {
unregisterObserver(observer.getSimpleName());
}
/**
* Asynchronously send calls to listener for tracing.
*
* @param lambda
* operations to execute
* @param observers
* list of observers to check
*
*/
private void notifyASync(Consumer<ApiRequestObserver> lambda, Collection<ApiRequestObserver> observers) {
if (observers != null) {
CompletableFutures.allDone(observers.stream()
.map(l -> CompletableFuture.runAsync(() -> lambda.accept(l)))
.collect(Collectors.toList()));
}
}
/**
* Configure retry settings.
*
* @param maxRetries
* maximum number of retries
* @param initialDelayMs
* initial delay between retries in milliseconds
* @param backoffMultiplier
* multiplier for exponential backoff
*/
public static void configureRetry(int maxRetries, int initialDelayMs, double backoffMultiplier) {
HttpClientWrapper.maxRetries = maxRetries;
HttpClientWrapper.retryInitialDelayMs = initialDelayMs;
HttpClientWrapper.retryBackoffMultiplier = backoffMultiplier;
}
/**
* Configure retry settings.
*
* @param maxRetries
* maximum number of retries
*/
public static void configureRetry(int maxRetries) {
configureRetry(maxRetries, DEFAULT_RETRY_INITIAL_DELAY_MS, DEFAULT_RETRY_BACKOFF_MULTIPLIER);
}
/**
* Configure retry settings.
*
* @param maxRetries
* maximum number of retries
* @param initialDelayMs
* initial delay between retries in milliseconds
*/
public static void configureRetry(int maxRetries, int initialDelayMs) {
configureRetry(maxRetries, initialDelayMs, DEFAULT_RETRY_BACKOFF_MULTIPLIER);
}
/**
* Reset retry settings to defaults.
*/
public static void resetRetryConfiguration() {
maxRetries = DEFAULT_MAX_RETRIES;
retryInitialDelayMs = DEFAULT_RETRY_INITIAL_DELAY_MS;
retryBackoffMultiplier = DEFAULT_RETRY_BACKOFF_MULTIPLIER;
}
}