Skip to content

Commit

Permalink
[JAVA][apache-httpclient] Use EntityUtils#toString instead of ``S…
Browse files Browse the repository at this point in the history
…canner``

``EntityUtils#toString`` automatically selects the correct encoding based on the received request.
Scanner currently uses the JVM default encoding, which doesn't always work.
  • Loading branch information
AB-xdev committed Mar 1, 2024
1 parent d4e1050 commit 844661b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -853,8 +853,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
if (mimeType == null || isJsonMime(mimeType)) {
// Assume json if no mime type
// convert input stream to string
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
String content = (String) (s.hasNext() ? s.next() : "");
String content = EntityUtils.toString(entity);
if ("".equals(content)) { // returns null for empty body
return null;
Expand All @@ -863,8 +862,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
return objectMapper.readValue(content, valueType);
} else if ("text/plain".equalsIgnoreCase(mimeType)) {
// convert input stream to string
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
return (T) (s.hasNext() ? s.next() : "");
return (T) EntityUtils.toString(entity);
} else {
throw new ApiException(
"Deserialization for content type '" + mimeType + "' not supported for type '" + valueType + "'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -771,8 +771,7 @@ public <T> T deserialize(CloseableHttpResponse response, TypeReference<T> valueT
if (mimeType == null || isJsonMime(mimeType)) {
// Assume json if no mime type
// convert input stream to string
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
String content = (String) (s.hasNext() ? s.next() : "");
String content = EntityUtils.toString(entity);

if ("".equals(content)) { // returns null for empty body
return null;
Expand All @@ -781,8 +780,7 @@ public <T> T deserialize(CloseableHttpResponse response, TypeReference<T> valueT
return objectMapper.readValue(content, valueType);
} else if ("text/plain".equalsIgnoreCase(mimeType)) {
// convert input stream to string
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
return (T) (s.hasNext() ? s.next() : "");
return (T) EntityUtils.toString(entity);
} else {
throw new ApiException(
"Deserialization for content type '" + mimeType + "' not supported for type '" + valueType + "'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,8 +864,7 @@ public <T> T deserialize(CloseableHttpResponse response, TypeReference<T> valueT
if (mimeType == null || isJsonMime(mimeType)) {
// Assume json if no mime type
// convert input stream to string
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
String content = (String) (s.hasNext() ? s.next() : "");
String content = EntityUtils.toString(entity);

if ("".equals(content)) { // returns null for empty body
return null;
Expand All @@ -874,8 +873,7 @@ public <T> T deserialize(CloseableHttpResponse response, TypeReference<T> valueT
return objectMapper.readValue(content, valueType);
} else if ("text/plain".equalsIgnoreCase(mimeType)) {
// convert input stream to string
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
return (T) (s.hasNext() ? s.next() : "");
return (T) EntityUtils.toString(entity);
} else {
throw new ApiException(
"Deserialization for content type '" + mimeType + "' not supported for type '" + valueType + "'",
Expand Down

0 comments on commit 844661b

Please sign in to comment.