Skip to content

Commit fedf1e5

Browse files
committed
ResponseStatusExceptionFilter: Fix unwrap of JSON message
Reading the entity stream a second time without resetting it was causing the ObjectMapper to return null then a NullPointerExceptions when calling get('message') method on it. Fix the issue by creating the ObjectMapper from the String representation of the entity stream instead of resetting the stream to read it a second time. Also check to make sure the returned JsonNode is not null and that the content is text. Closes #1222
1 parent 5c3ba9a commit fedf1e5

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

src/main/java/com/github/dockerjava/jaxrs/filter/ResponseStatusExceptionFilter.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,18 @@ private String getBodyAsMessage(ClientResponseContext responseContext) {
7979
String message = IOUtils.toString(entityStream, charset);
8080

8181
if (MediaType.APPLICATION_JSON_TYPE.equals(mediaType)) {
82-
ObjectMapper mapper = new ObjectMapper();
83-
JsonNode node = mapper.readTree(entityStream).get("message");
84-
if (node != null) {
85-
message = node.textValue();
82+
try {
83+
JsonNode node = new ObjectMapper().readTree(message);
84+
if (node != null) {
85+
JsonNode messageNode = node.get("message");
86+
if (messageNode != null && messageNode.isTextual()) {
87+
message = messageNode.textValue();
88+
}
89+
}
90+
} catch (IOException ignored) {
91+
//ignore parsing errors and return the message as is
8692
}
8793
}
88-
8994
return message;
9095
} catch (Exception ignored) { }
9196
}

0 commit comments

Comments
 (0)