-
Notifications
You must be signed in to change notification settings - Fork 38
/
Java11HttpPoster.java
86 lines (72 loc) · 2.61 KB
/
Java11HttpPoster.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
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package com.newrelic.telemetry;
import com.newrelic.telemetry.http.HttpPoster;
import com.newrelic.telemetry.http.HttpResponse;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.nio.charset.Charset;
import java.time.Duration;
import java.util.Map;
/** Implementation of the HttpPoster interface using an Java 11 JDK Http client. */
public class Java11HttpPoster implements HttpPoster {
private final HttpClient httpClient;
/**
* Create a Java11HttpPoster with your own object.
*
* @param httpClient - the preconstructed HTTP client object
*/
public Java11HttpPoster(HttpClient httpClient) {
this.httpClient = httpClient;
}
/**
* Create a default Java11HttpPoster with a custom call timeout.
*
* @param callTimeout - the timeout for HTTP calls
*/
public Java11HttpPoster(Duration callTimeout) {
this.httpClient = HttpClient.newBuilder().connectTimeout(callTimeout).build();
}
/** Create a default Java11HttpPoster with a connect timeout set to 2 seconds. */
public Java11HttpPoster() {
this(Duration.ofSeconds(2));
}
@Override
public HttpResponse post(URL url, Map<String, String> headers, byte[] body, String mediaType)
throws IOException {
try {
var builder =
HttpRequest.newBuilder(url.toURI()).POST(HttpRequest.BodyPublishers.ofByteArray(body));
headers.forEach(builder::header);
builder.header("Content-Type", mediaType);
var req = builder.build();
var response =
httpClient.send(
req, java.net.http.HttpResponse.BodyHandlers.ofString(Charset.defaultCharset()));
return toSdkResponse(response);
} catch (URISyntaxException | InterruptedException e) {
throw new IOException(e);
}
}
public static HttpResponse toSdkResponse(java.net.http.HttpResponse actual) {
return new HttpResponse(
actual.body().toString(),
actual.statusCode(),
"" + actual.statusCode(),
actual.headers().map());
}
public static MetricBatchSenderFactory metricSenderFactory() {
return MetricBatchSenderFactory.fromHttpImplementation(Java11HttpPoster::new);
}
public static SpanBatchSenderFactory spanSenderFactory() {
return SpanBatchSenderFactory.fromHttpImplementation(Java11HttpPoster::new);
}
public static EventBatchSenderFactory eventSenderFactory() {
return EventBatchSenderFactory.fromHttpImplementation(Java11HttpPoster::new);
}
}