Skip to content

Commit 4245f18

Browse files
committed
Moved to okhttp client
1 parent 1965c12 commit 4245f18

File tree

7 files changed

+189
-205
lines changed

7 files changed

+189
-205
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
<artifactId>jackson-datatype-jsr310</artifactId>
4040
<version>2.10.0</version>
4141
</dependency>
42+
<dependency>
43+
<groupId>com.squareup.okhttp3</groupId>
44+
<artifactId>okhttp</artifactId>
45+
<version>4.9.1</version>
46+
</dependency>
4247
<!--Test Dependencies-->
4348
<dependency>
4449
<groupId>org.junit.jupiter</groupId>

samples/example-app/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
<groupId>com.exceptionless</groupId>
88
<artifactId>example-app</artifactId>
9-
<version>1.0-beta1</version>
9+
<version>1.0</version>
1010

1111
<dependencies>
1212
<dependency>
1313
<groupId>com.exceptionless</groupId>
1414
<artifactId>exceptionless-client</artifactId>
15-
<version>1.0</version>
15+
<version>1.0-beta1</version>
1616
</dependency>
1717
</dependencies>
1818

src/main/java/com/exceptionless/exceptionlessclient/settings/DefaultSettingsClient.java

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,64 @@
66
import com.exceptionless.exceptionlessclient.utils.VisibleForTesting;
77
import com.fasterxml.jackson.core.type.TypeReference;
88
import lombok.Builder;
9+
import okhttp3.OkHttpClient;
10+
import okhttp3.Request;
11+
import okhttp3.Response;
12+
import okhttp3.ResponseBody;
913

10-
import java.net.URI;
11-
import java.net.http.HttpClient;
12-
import java.net.http.HttpRequest;
13-
import java.net.http.HttpResponse;
1414
import java.time.Duration;
1515

1616
public class DefaultSettingsClient implements SettingsClientIF {
1717
private final Configuration configuration;
18-
private final HttpClient httpClient;
18+
private final OkHttpClient httpClient;
1919

2020
@Builder
2121
public DefaultSettingsClient(Configuration configuration) {
2222
this.configuration = configuration;
23-
this.httpClient = HttpClient.newHttpClient();
23+
this.httpClient =
24+
new OkHttpClient()
25+
.newBuilder()
26+
.connectTimeout(Duration.ofMillis(configuration.getSettingsClientTimeoutInMillis()))
27+
.build();
28+
;
2429
}
2530

2631
@VisibleForTesting
27-
DefaultSettingsClient(Configuration configuration, HttpClient httpClient) {
32+
DefaultSettingsClient(Configuration configuration, OkHttpClient httpClient) {
2833
this.configuration = configuration;
2934
this.httpClient = httpClient;
3035
}
3136

3237
@Override
3338
public SettingsResponse getSettings(long version) {
3439
try {
35-
URI uri =
36-
new URI(
37-
String.format(
38-
"%s/api/v2/projects/config?v=%s&access_token=%s",
39-
configuration.getServerUrl(), version, configuration.getApiKey()));
40-
41-
HttpRequest request =
42-
HttpRequest.newBuilder()
43-
.uri(uri)
44-
.GET()
45-
.header("User-Agent", Configuration.USER_AGENT)
46-
.timeout(Duration.ofMillis(configuration.getSettingsClientTimeoutInMillis()))
40+
Request request =
41+
new Request.Builder()
42+
.url(
43+
String.format(
44+
"%s/api/v2/projects/config?v=%s&access_token=%s",
45+
configuration.getServerUrl(), version, configuration.getApiKey()))
46+
.get()
4747
.build();
4848

49-
HttpResponse<String> response =
50-
httpClient.send(request, HttpResponse.BodyHandlers.ofString());
49+
Response response = httpClient.newCall(request).execute();
5150

52-
if (response.statusCode() != 200) {
53-
return SettingsResponse.builder().success(false).message(response.body()).build();
51+
ResponseBody body = response.body();
52+
if (response.code() / 100 != 2) {
53+
return SettingsResponse.builder()
54+
.success(false)
55+
.message(Utils.addCodeToResponseBodyStr(response))
56+
.build();
57+
}
58+
if (body == null) {
59+
return SettingsResponse.builder()
60+
.success(false)
61+
.message("No settings returned by server!")
62+
.build();
5463
}
5564

5665
ServerSettings serverSettings =
57-
Utils.JSON_MAPPER.readValue(response.body(), new TypeReference<ServerSettings>() {});
58-
66+
Utils.JSON_MAPPER.readValue(body.string(), new TypeReference<ServerSettings>() {});
5967
return SettingsResponse.builder().success(true).settings(serverSettings).build();
6068
} catch (Exception e) {
6169
return SettingsResponse.builder().success(false).exception(e).message(e.getMessage()).build();

src/main/java/com/exceptionless/exceptionlessclient/submission/DefaultSubmissionClient.java

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,15 @@
99
import com.exceptionless.exceptionlessclient.utils.Utils;
1010
import com.exceptionless.exceptionlessclient.utils.VisibleForTesting;
1111
import lombok.Builder;
12+
import okhttp3.*;
1213
import org.slf4j.Logger;
1314
import org.slf4j.LoggerFactory;
1415

15-
import java.net.URI;
1616
import java.net.URLEncoder;
17-
import java.net.http.HttpClient;
18-
import java.net.http.HttpHeaders;
19-
import java.net.http.HttpRequest;
20-
import java.net.http.HttpResponse;
2117
import java.nio.charset.StandardCharsets;
2218
import java.time.Duration;
2319
import java.util.List;
24-
import java.util.OptionalLong;
20+
import java.util.Optional;
2521
import java.util.stream.Collectors;
2622

2723
public class DefaultSubmissionClient implements SubmissionClientIF {
@@ -30,18 +26,22 @@ public class DefaultSubmissionClient implements SubmissionClientIF {
3026

3127
private final Configuration configuration;
3228
private final SettingsManager settingsManager;
33-
private final HttpClient httpClient;
29+
private final OkHttpClient httpClient;
3430

3531
@Builder
3632
public DefaultSubmissionClient(Configuration configuration, SettingsManager settingsManager) {
3733
this.configuration = configuration;
3834
this.settingsManager = settingsManager;
39-
this.httpClient = HttpClient.newHttpClient();
35+
this.httpClient =
36+
new OkHttpClient()
37+
.newBuilder()
38+
.connectTimeout(Duration.ofMillis(configuration.getSubmissionClientTimeoutInMillis()))
39+
.build();
4040
}
4141

4242
@VisibleForTesting
4343
DefaultSubmissionClient(
44-
Configuration configuration, SettingsManager settingsManager, HttpClient httpClient) {
44+
Configuration configuration, SettingsManager settingsManager, OkHttpClient httpClient) {
4545
this.configuration = configuration;
4646
this.settingsManager = settingsManager;
4747
this.httpClient = httpClient;
@@ -67,38 +67,33 @@ public SubmissionResponse postUserDescription(String referenceId, UserDescriptio
6767

6868
private SubmissionResponse postSubmission(String url, Object data) {
6969
try {
70-
URI uri = new URI(url);
7170
String requestJSON = Utils.JSON_MAPPER.writeValueAsString(data);
72-
73-
HttpRequest request =
74-
HttpRequest.newBuilder()
75-
.uri(uri)
76-
.POST(HttpRequest.BodyPublishers.ofString(requestJSON))
77-
.header("Content-Type", "application/json")
78-
.header("User-Agent", Configuration.USER_AGENT)
79-
.timeout(Duration.ofMillis(configuration.getSubmissionClientTimeoutInMillis()))
71+
Request request =
72+
new Request.Builder()
73+
.url(url)
74+
.post(RequestBody.create(requestJSON, MediaType.parse("application/json")))
8075
.build();
8176

82-
HttpResponse<String> response =
83-
httpClient.send(request, HttpResponse.BodyHandlers.ofString());
77+
Response response = httpClient.newCall(request).execute();
8478

85-
if (response.statusCode() == 200) {
79+
if (response.code() / 100 == 2) {
8680
updateSettingsFromHeaders(response.headers());
8781
}
8882

8983
return SubmissionResponse.builder()
90-
.statusCode(response.statusCode())
91-
.message(response.body())
84+
.statusCode(response.code())
85+
.message(Utils.addCodeToResponseBodyStr(response))
9286
.build();
9387
} catch (Exception e) {
9488
throw new SubmissionException(e);
9589
}
9690
}
9791

98-
private void updateSettingsFromHeaders(HttpHeaders headers) {
99-
OptionalLong maybeSettingsVersion = headers.firstValueAsLong(CONFIGURATION_VERSION_HEADER);
92+
private void updateSettingsFromHeaders(Headers headers) {
93+
Optional<String> maybeSettingsVersion =
94+
Optional.ofNullable(headers.get(CONFIGURATION_VERSION_HEADER));
10095
if (maybeSettingsVersion.isPresent()) {
101-
settingsManager.checkVersion(maybeSettingsVersion.getAsLong());
96+
settingsManager.checkVersion(Long.parseLong(maybeSettingsVersion.get()));
10297
} else {
10398
LOG.error("No config version header was returned");
10499
}
@@ -107,26 +102,21 @@ private void updateSettingsFromHeaders(HttpHeaders headers) {
107102
@Override
108103
public void sendHeartBeat(String sessionIdOrUserId, boolean closeSession) {
109104
try {
110-
URI uri =
111-
new URI(
112-
String.format(
113-
"%s/api/v2/events/session/heartbeat?id=%s&close=%s&access_token=%s",
114-
configuration.getHeartbeatServerUrl(),
115-
URLEncoder.encode(sessionIdOrUserId, StandardCharsets.UTF_8),
116-
closeSession,
117-
configuration.getApiKey()));
118-
HttpRequest request =
119-
HttpRequest.newBuilder()
120-
.uri(uri)
121-
.GET()
122-
.header("X-Exceptionless-Client", Configuration.USER_AGENT)
123-
.timeout(Duration.ofMillis(configuration.getSubmissionClientTimeoutInMillis()))
105+
Request request =
106+
new Request.Builder()
107+
.url(
108+
String.format(
109+
"%s/api/v2/events/session/heartbeat?id=%s&close=%s&access_token=%s",
110+
configuration.getHeartbeatServerUrl(),
111+
URLEncoder.encode(sessionIdOrUserId, StandardCharsets.UTF_8),
112+
closeSession,
113+
configuration.getApiKey()))
114+
.get()
124115
.build();
125116

126-
HttpResponse<String> response =
127-
httpClient.send(request, HttpResponse.BodyHandlers.ofString());
117+
Response response = httpClient.newCall(request).execute();
128118

129-
if (response.statusCode() != 200) {
119+
if (response.code() / 100 != 2) {
130120
LOG.error(
131121
String.format(
132122
"Error in submitting heartbeat to the server for sessionOrUserId: %s",

src/main/java/com/exceptionless/exceptionlessclient/utils/Utils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import com.fasterxml.jackson.databind.SerializationFeature;
55
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
6+
import okhttp3.Response;
7+
import okhttp3.ResponseBody;
68
import org.slf4j.Logger;
79
import org.slf4j.LoggerFactory;
810

11+
import java.io.IOException;
912
import java.net.URI;
1013
import java.net.http.HttpRequest;
1114
import java.util.*;
1215

1316
public final class Utils {
1417
public static final ObjectMapper JSON_MAPPER;
18+
1519
static {
1620
JSON_MAPPER = new ObjectMapper();
1721
JSON_MAPPER.registerModule(new JavaTimeModule());
@@ -86,4 +90,13 @@ public static boolean match(String value, String pattern) {
8690
}
8791
return false;
8892
}
93+
94+
public static String addCodeToResponseBodyStr(Response response) throws IOException {
95+
ResponseBody body = response.body();
96+
if (body == null) {
97+
return String.format("Code: %s", response.code());
98+
}
99+
100+
return String.format("Code: %s, Body: %s", response.code(), body.string());
101+
}
89102
}

0 commit comments

Comments
 (0)