Skip to content

Commit 2010582

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 2010582

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import javax.ws.rs.core.MediaType;
1111

1212
import org.apache.commons.io.IOUtils;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
1315

1416
import com.fasterxml.jackson.databind.JsonNode;
1517
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -31,6 +33,8 @@
3133
*/
3234
public class ResponseStatusExceptionFilter implements ClientResponseFilter {
3335

36+
private static final Logger LOG = LoggerFactory.getLogger(ResponseStatusExceptionFilter.class);
37+
3438
@Override
3539
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
3640
int status = responseContext.getStatus();
@@ -79,13 +83,19 @@ private String getBodyAsMessage(ClientResponseContext responseContext) {
7983
String message = IOUtils.toString(entityStream, charset);
8084

8185
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();
86+
try {
87+
JsonNode node = new ObjectMapper().readTree(message);
88+
if (node != null) {
89+
JsonNode messageNode = node.get("message");
90+
if (messageNode != null && messageNode.isTextual()) {
91+
message = messageNode.textValue();
92+
}
93+
}
94+
} catch (IOException e) {
95+
// ignore parsing errors and return the message as is
96+
LOG.debug("Failed to unwrap error message: {}", e.getMessage(), e);
8697
}
8798
}
88-
8999
return message;
90100
} catch (Exception ignored) { }
91101
}

0 commit comments

Comments
 (0)