9
9
import com .exceptionless .exceptionlessclient .utils .Utils ;
10
10
import com .exceptionless .exceptionlessclient .utils .VisibleForTesting ;
11
11
import lombok .Builder ;
12
+ import okhttp3 .*;
12
13
import org .slf4j .Logger ;
13
14
import org .slf4j .LoggerFactory ;
14
15
15
- import java .net .URI ;
16
16
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 ;
21
17
import java .nio .charset .StandardCharsets ;
22
18
import java .time .Duration ;
23
19
import java .util .List ;
24
- import java .util .OptionalLong ;
20
+ import java .util .Optional ;
25
21
import java .util .stream .Collectors ;
26
22
27
23
public class DefaultSubmissionClient implements SubmissionClientIF {
@@ -30,18 +26,22 @@ public class DefaultSubmissionClient implements SubmissionClientIF {
30
26
31
27
private final Configuration configuration ;
32
28
private final SettingsManager settingsManager ;
33
- private final HttpClient httpClient ;
29
+ private final OkHttpClient httpClient ;
34
30
35
31
@ Builder
36
32
public DefaultSubmissionClient (Configuration configuration , SettingsManager settingsManager ) {
37
33
this .configuration = configuration ;
38
34
this .settingsManager = settingsManager ;
39
- this .httpClient = HttpClient .newHttpClient ();
35
+ this .httpClient =
36
+ new OkHttpClient ()
37
+ .newBuilder ()
38
+ .connectTimeout (Duration .ofMillis (configuration .getSubmissionClientTimeoutInMillis ()))
39
+ .build ();
40
40
}
41
41
42
42
@ VisibleForTesting
43
43
DefaultSubmissionClient (
44
- Configuration configuration , SettingsManager settingsManager , HttpClient httpClient ) {
44
+ Configuration configuration , SettingsManager settingsManager , OkHttpClient httpClient ) {
45
45
this .configuration = configuration ;
46
46
this .settingsManager = settingsManager ;
47
47
this .httpClient = httpClient ;
@@ -67,38 +67,33 @@ public SubmissionResponse postUserDescription(String referenceId, UserDescriptio
67
67
68
68
private SubmissionResponse postSubmission (String url , Object data ) {
69
69
try {
70
- URI uri = new URI (url );
71
70
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" )))
80
75
.build ();
81
76
82
- HttpResponse <String > response =
83
- httpClient .send (request , HttpResponse .BodyHandlers .ofString ());
77
+ Response response = httpClient .newCall (request ).execute ();
84
78
85
- if (response .statusCode () == 200 ) {
79
+ if (response .code () / 100 == 2 ) {
86
80
updateSettingsFromHeaders (response .headers ());
87
81
}
88
82
89
83
return SubmissionResponse .builder ()
90
- .statusCode (response .statusCode ())
91
- .message (response . body ( ))
84
+ .statusCode (response .code ())
85
+ .message (Utils . addCodeToResponseBodyStr ( response ))
92
86
.build ();
93
87
} catch (Exception e ) {
94
88
throw new SubmissionException (e );
95
89
}
96
90
}
97
91
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 ));
100
95
if (maybeSettingsVersion .isPresent ()) {
101
- settingsManager .checkVersion (maybeSettingsVersion .getAsLong ( ));
96
+ settingsManager .checkVersion (Long . parseLong ( maybeSettingsVersion .get () ));
102
97
} else {
103
98
LOG .error ("No config version header was returned" );
104
99
}
@@ -107,26 +102,21 @@ private void updateSettingsFromHeaders(HttpHeaders headers) {
107
102
@ Override
108
103
public void sendHeartBeat (String sessionIdOrUserId , boolean closeSession ) {
109
104
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 ()
124
115
.build ();
125
116
126
- HttpResponse <String > response =
127
- httpClient .send (request , HttpResponse .BodyHandlers .ofString ());
117
+ Response response = httpClient .newCall (request ).execute ();
128
118
129
- if (response .statusCode () != 200 ) {
119
+ if (response .code () / 100 != 2 ) {
130
120
LOG .error (
131
121
String .format (
132
122
"Error in submitting heartbeat to the server for sessionOrUserId: %s" ,
0 commit comments