| 97 | } |
| 98 | |
| 99 | protected Response logAndRebufferResponse( |
| 100 | String configKey, Level logLevel, Response response, long elapsedTime) throws IOException { |
| 101 | String protocolVersion = resolveProtocolVersion(response.protocolVersion()); |
| 102 | String reason = |
| 103 | response.reason() != null && logLevel.compareTo(Level.NONE) > 0 |
| 104 | ? " " + response.reason() |
| 105 | : ""; |
| 106 | int status = response.status(); |
| 107 | log(configKey, "<--- %s %s%s (%sms)", protocolVersion, status, reason, elapsedTime); |
| 108 | if (logLevel.ordinal() >= Level.HEADERS.ordinal()) { |
| 109 | |
| 110 | for (String field : response.headers().keySet()) { |
| 111 | if (shouldLogResponseHeader(field)) { |
| 112 | for (String value : valuesOrEmpty(response.headers(), field)) { |
| 113 | log(configKey, "%s: %s", field, value); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | int bodyLength = 0; |
| 119 | if (response.body() != null && !(status == 204 || status == 205)) { |
| 120 | // HTTP 204 No Content "...response MUST NOT include a message-body" |
| 121 | // HTTP 205 Reset Content "...response MUST NOT include an entity" |
| 122 | if (logLevel.ordinal() >= Level.FULL.ordinal()) { |
| 123 | log(configKey, ""); // CRLF |
| 124 | } |
| 125 | byte[] bodyData = Util.toByteArray(response.body().asInputStream()); |
| 126 | ensureClosed(response.body()); |
| 127 | bodyLength = bodyData.length; |
| 128 | if (logLevel.ordinal() >= Level.FULL.ordinal() && bodyLength > 0) { |
| 129 | log(configKey, "%s", decodeOrDefault(bodyData, UTF_8, "Binary data")); |
| 130 | } |
| 131 | log(configKey, "<--- END HTTP (%s-byte body)", bodyLength); |
| 132 | return response.toBuilder().body(bodyData).build(); |
| 133 | } else { |
| 134 | log(configKey, "<--- END HTTP (%s-byte body)", bodyLength); |
| 135 | } |
| 136 | } |
| 137 | return response; |
| 138 | } |
| 139 | |
| 140 | protected IOException logIOException( |
| 141 | String configKey, Level logLevel, IOException ioe, long elapsedTime) { |