-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClient.java
More file actions
189 lines (160 loc) · 7.07 KB
/
HttpClient.java
File metadata and controls
189 lines (160 loc) · 7.07 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
package common;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.uid2.shared.util.Mapper;
import lombok.Getter;
import okhttp3.*;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import java.util.Map;
public final class HttpClient {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpClient.class);
private static final ObjectMapper OBJECT_MAPPER = Mapper.getInstance();
public static final OkHttpClient RAW_CLIENT = new OkHttpClient();
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
public enum HttpMethod {
GET,
POST
}
@Getter
public static class HttpException extends Exception {
private final HttpMethod method;
private final String url;
private final int code;
private final String codeMessage;
private final String response;
public HttpException(HttpMethod method, String url, int code, String codeMessage, String response) {
super(createErrorMessage(method, url, code, codeMessage, response));
this.method = method;
this.url = url;
this.code = code;
this.codeMessage = codeMessage;
this.response = response;
}
public JsonNode getResponseJson() throws Exception {
return OBJECT_MAPPER.readTree(response);
}
private static String createErrorMessage(HttpMethod method, String url, int code, String message, String response) {
return "Unsuccessful %s request - URL: %s - Code: %d %s - Response body: %s".formatted(
method, url, code, message, response
);
}
}
private HttpClient() {
}
public static String get(String url, String bearerToken, Map<String, String> additionalHeaders) throws Exception {
Request request = buildRequest(HttpMethod.GET, url, null, bearerToken, additionalHeaders);
return execute(request, HttpMethod.GET);
}
public static String get(String url, String bearerToken) throws Exception {
return get(url, bearerToken, null);
}
public static String get(String url) throws Exception {
return get(url, null);
}
public static String post(String url, String body, String bearerToken) throws Exception {
Request request = buildRequest(HttpMethod.POST, url, body, bearerToken);
return execute(request, HttpMethod.POST);
}
public static String execute(Request request, HttpMethod method) throws Exception {
final String url = request.url().toString();
final String requestBodyForLog = extractRequestBodyForLog(request);
final String authHeader = extractAuthHeader(request);
final Headers headers = request.headers();
LOGGER.info(() -> String.format(
"[HTTP REQUEST] %s %s%n" +
" Authorization: %s%n" +
" Request Body: %s%n" +
" Headers: %s",
method, url,
authHeader,
requestBodyForLog,
headers.toString()
));
try (Response response = RAW_CLIENT.newCall(request).execute()) {
final ResponseBody body = response.body();
// Read the FULL response body - don't truncate the actual data!
final String fullResponseBody = (body != null) ? body.string() : "";
final int statusCode = response.code();
final String statusMessage = response.message();
final Headers responseHeaders = response.headers();
// Only truncate for logging display
final String responseBodyForLog = truncateForLog(fullResponseBody, 1000);
LOGGER.info(() -> String.format(
"[HTTP RESPONSE] %s %s%n" +
" Status: %d %s%n" +
" Response Headers: %s%n" +
" Response Body: %s",
method, url,
statusCode, statusMessage,
responseHeaders.toString(),
responseBodyForLog
));
if (!response.isSuccessful()) {
LOGGER.error(() -> String.format(
"[HTTP ERROR] Request failed: %s %s - Status: %d %s - Response: %s",
method, url, statusCode, statusMessage, responseBodyForLog
));
throw new HttpException(method, url, statusCode, statusMessage, fullResponseBody);
}
// Return the FULL response body, not truncated
return fullResponseBody;
}
}
private static String truncateForLog(String text, int maxLength) {
if (text == null || text.isEmpty()) {
return "[empty]";
}
if (text.length() > maxLength) {
return text.substring(0, maxLength) + "... [truncated, total length=" + text.length() + "]";
}
return text;
}
private static String extractRequestBodyForLog(Request request) {
if (request.body() == null) {
return "[empty]";
}
try {
okio.Buffer buffer = new okio.Buffer();
request.body().writeTo(buffer);
String body = buffer.readUtf8();
return truncateForLog(body, 500);
} catch (Exception e) {
return "[unable to read request body: " + e.getMessage() + "]";
}
}
private static String extractAuthHeader(Request request) {
Headers headers = request.headers();
for (int i = 0; i < headers.size(); i++) {
if ("Authorization".equalsIgnoreCase(headers.name(i))) {
String authValue = headers.value(i);
// Mask the token for security, but show first/last few chars
if (authValue.length() > 20) {
return authValue.substring(0, 10) + "..." + authValue.substring(authValue.length() - 10);
} else {
return "[masked]";
}
}
}
return "[none]";
}
private static Request buildRequest(HttpMethod method, String url, String body, String bearerToken) {
return buildRequest(method, url, body, bearerToken, null);
}
private static Request buildRequest(HttpMethod method, String url, String body, String bearerToken, Map<String, String> additionalHeaders) {
Request.Builder requestBuilder = new Request.Builder()
.url(url);
if (bearerToken != null) {
requestBuilder = requestBuilder.addHeader("Authorization", "Bearer " + bearerToken);
}
if (additionalHeaders != null) {
for (Map.Entry<String, String> header : additionalHeaders.entrySet()) {
requestBuilder = requestBuilder.addHeader(header.getKey(), header.getValue());
}
}
return (switch (method) {
case GET -> requestBuilder.get();
case POST -> requestBuilder.post(RequestBody.create(body, JSON));
}).build();
}
}