Skip to content

Commit

Permalink
CHE-5505: Fix NPE in DockerConnector (#5510)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmorhun authored Jun 30, 2017
1 parent 003804d commit f0717e0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1346,15 +1346,25 @@ private String getBuildImageId(ProgressStatus progressStatus) {

protected <T> T parseResponseStreamAndClose(InputStream inputStream, Class<T> clazz) throws IOException {
try (InputStreamReader reader = new InputStreamReader(inputStream)) {
return GSON.fromJson(reader, clazz);
T objectFromJson = GSON.fromJson(reader, clazz);
if (objectFromJson == null) {
LOG.error("Docker response doesn't contain any data, though it was expected to contain some.");
throw new IOException("Internal server error. Unexpected response body received from Docker.");
}
return objectFromJson;
} catch (JsonParseException e) {
throw new IOException(e.getLocalizedMessage(), e);
}
}

protected <T> T parseResponseStreamAndClose(InputStream inputStream, TypeToken<T> tt) throws IOException {
try (InputStreamReader reader = new InputStreamReader(inputStream)) {
return GSON.fromJson(reader, tt.getType());
T objectFromJson = GSON.fromJson(reader, tt.getType());
if (objectFromJson == null) {
LOG.error("Docker response doesn't contain any data, though it was expected to contain some.");
throw new IOException("Internal server error. Unexpected response body received from Docker.");
}
return objectFromJson;
} catch (JsonParseException e) {
throw new IOException(e.getLocalizedMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,19 @@ public void shouldBeAbleToParseResponseStreamAsEmptyListOfContainersAndClose() t
assertEquals(containers.size(), 0);
}

@Test(expectedExceptions = IOException.class,
expectedExceptionsMessageRegExp = "Internal server error. Unexpected response body received from Docker.")
public void shouldThrowIOExceptionWhenParseEmptyResponseStringByClass() throws IOException {
dockerConnector.parseResponseStreamAndClose(new ByteArrayInputStream("".getBytes()), Version.class);
}

@Test(expectedExceptions = IOException.class,
expectedExceptionsMessageRegExp = "Internal server error. Unexpected response body received from Docker.")
public void shouldThrowIOExceptionWhenParseEmptyResponseStringByTypeToken() throws IOException {
dockerConnector.parseResponseStreamAndClose(new ByteArrayInputStream("".getBytes()),
new TypeToken<List<ContainerListEntry>>() {});
}

@Test
public void shouldBeAbleToGetNetworks() throws Exception {
// given
Expand Down

0 comments on commit f0717e0

Please sign in to comment.