diff --git a/client/src/main/java/org/elasticsearch/client/RequestLogger.java b/client/src/main/java/org/elasticsearch/client/RequestLogger.java index ce2e82ea1e5..dd9fb269717 100644 --- a/client/src/main/java/org/elasticsearch/client/RequestLogger.java +++ b/client/src/main/java/org/elasticsearch/client/RequestLogger.java @@ -28,11 +28,14 @@ import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.entity.BufferedHttpEntity; +import org.apache.http.entity.ContentType; import org.apache.http.util.EntityUtils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; /** * Helper class that exposes static methods to unify the way requests are logged. @@ -118,7 +121,12 @@ public final class RequestLogger { if (entity != null) { entity = new BufferedHttpEntity(entity); httpResponse.setEntity(entity); - try (BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()))) { + ContentType contentType = ContentType.get(entity); + Charset charset = StandardCharsets.UTF_8; + if (contentType != null) { + charset = contentType.getCharset(); + } + try (BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), charset))) { String line; while( (line = reader.readLine()) != null) { responseLine += "\n# " + line; diff --git a/client/src/test/java/org/elasticsearch/client/RequestLoggerTests.java b/client/src/test/java/org/elasticsearch/client/RequestLoggerTests.java index 84bc3c032d5..89fa30c7e08 100644 --- a/client/src/test/java/org/elasticsearch/client/RequestLoggerTests.java +++ b/client/src/test/java/org/elasticsearch/client/RequestLoggerTests.java @@ -20,7 +20,6 @@ package org.elasticsearch.client; import com.carrotsearch.randomizedtesting.generators.RandomInts; -import org.apache.commons.codec.Charsets; import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpHost; import org.apache.http.ProtocolVersion; @@ -36,6 +35,7 @@ import org.apache.lucene.util.LuceneTestCase; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; import static org.hamcrest.CoreMatchers.equalTo; @@ -72,7 +72,7 @@ public class RequestLoggerTests extends LuceneTestCase { if (request instanceof HttpEntityEnclosingRequest && random().nextBoolean()) { HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request; String requestBody = "{ \"field\": \"value\" }"; - enclosingRequest.setEntity(new StringEntity(requestBody, Charsets.UTF_8)); + enclosingRequest.setEntity(new StringEntity(requestBody, StandardCharsets.UTF_8)); expected += " -d '" + requestBody + "'"; } @@ -95,7 +95,7 @@ public class RequestLoggerTests extends LuceneTestCase { expected += "\n#"; if (random().nextBoolean()) { String responseBody = "{\n \"field\": \"value\"\n}"; - httpResponse.setEntity(new StringEntity(responseBody, Charsets.UTF_8)); + httpResponse.setEntity(new StringEntity(responseBody, StandardCharsets.UTF_8)); expected += "\n# {"; expected += "\n# \"field\": \"value\""; expected += "\n# }";