Skip to content

Commit 7ac5484

Browse files
authored
Fix a bug where DetailedLoggingListener does not print text request body (#999)
1 parent cb4da1e commit 7ac5484

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

slack-api-client/src/main/java/com/slack/api/methods/impl/MethodsClientImpl.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3173,7 +3173,7 @@ protected <T extends SlackApiTextResponse> T postMultipartAndParseResponse(
31733173
metricsDatastore.addToLastMinuteRequests(executorName, teamId, methodName, System.currentTimeMillis());
31743174
}
31753175
Response response = runPostMultipart(form, methodName, token);
3176-
T apiResponse = parseJsonResponseAndRunListeners(teamId, methodName, response, clazz);
3176+
T apiResponse = parseJsonResponseAndRunListeners(teamId, methodName, response, clazz, true);
31773177
return apiResponse;
31783178

31793179
} catch (IOException e) {
@@ -3221,7 +3221,18 @@ <T extends SlackApiTextResponse> T parseJsonResponseAndRunListeners(
32213221
String teamId,
32223222
String methodName,
32233223
Response response,
3224-
Class<T> clazz) throws IOException, SlackApiException {
3224+
Class<T> clazz
3225+
) throws IOException, SlackApiException {
3226+
return parseJsonResponseAndRunListeners(teamId, methodName, response, clazz, false);
3227+
}
3228+
3229+
<T extends SlackApiTextResponse> T parseJsonResponseAndRunListeners(
3230+
String teamId,
3231+
String methodName,
3232+
Response response,
3233+
Class<T> clazz,
3234+
boolean isRequestBodyBinary
3235+
) throws IOException, SlackApiException {
32253236
String body = response.body().string();
32263237
if (response.isSuccessful()) {
32273238
try {
@@ -3236,7 +3247,7 @@ <T extends SlackApiTextResponse> T parseJsonResponseAndRunListeners(
32363247
apiResponse.setHttpResponseHeaders(toLowerCasedKeyMap(response.headers()));
32373248
return apiResponse;
32383249
} finally {
3239-
slackHttpClient.runHttpResponseListeners(response, body);
3250+
slackHttpClient.runHttpResponseListeners(response, body, isRequestBodyBinary);
32403251
}
32413252
} else {
32423253
throw new SlackApiException(slackHttpClient.getConfig(), response, body);

slack-api-client/src/main/java/com/slack/api/util/http/SlackHttpClient.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,11 @@ private String toCamelCaseJsonString(Object obj) {
253253
}
254254

255255
public void runHttpResponseListeners(Response response, String body) {
256-
HttpResponseListener.State state = new HttpResponseListener.State(config, response, body);
256+
runHttpResponseListeners(response, body, false);
257+
}
258+
259+
public void runHttpResponseListeners(Response response, String body, boolean isRequestBodyBinary) {
260+
HttpResponseListener.State state = new HttpResponseListener.State(config, response, body, isRequestBodyBinary);
257261
for (HttpResponseListener responseListener : config.getHttpClientResponseHandlers()) {
258262
responseListener.accept(state);
259263
}

slack-api-client/src/main/java/com/slack/api/util/http/listener/DetailedLoggingListener.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,16 @@ public void accept(State state) {
4747

4848
String textRequestBody = null;
4949
try {
50-
String contentType = "";
51-
if (requestBodyObj.contentType() != null
52-
&& requestBodyObj.contentType().type() != null
53-
) {
54-
contentType = requestBodyObj.contentType().type().toLowerCase(Locale.ENGLISH);
55-
}
56-
if (contentType.equals("application/json") ||
57-
contentType.equals("text/html")) {
50+
if (state.isRequestBodyBinary()) {
51+
textRequestBody = "(binary)";
52+
} else {
5853
textRequestBody = requestBody.buffer().readUtf8();
5954
if (!state.getConfig().isLibraryMaintainerMode()) {
6055
if (textRequestBody.contains("refresh_token=")) {
6156
textRequestBody = textRequestBody.replaceFirst(
6257
"refresh_token=[^\\&]+", "refresh_token=(redacted)");
6358
}
6459
}
65-
} else {
66-
textRequestBody = "(binary)";
6760
}
6861
} catch (Exception e) {
6962
log.debug("Failed to read request body because {}, error: {}", e.getMessage(), e.getClass().getCanonicalName());

slack-api-client/src/main/java/com/slack/api/util/http/listener/HttpResponseListener.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ public abstract class HttpResponseListener implements Consumer<HttpResponseListe
1818
@AllArgsConstructor
1919
@Data
2020
public static class State {
21+
public State(SlackConfig config, Response response, String parsedResponseBody) {
22+
this(config, response, parsedResponseBody, false);
23+
}
24+
2125
private SlackConfig config;
2226
private Response response;
2327
private String parsedResponseBody;
28+
private boolean requestBodyBinary;
2429
}
2530

2631
}

0 commit comments

Comments
 (0)