use of org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream in project che by eclipse.
the class DockerConnector method getResource.
/**
* Gets files from the specified container.
*
* @return stream of resources from the specified container filesystem, with retention connection
* @throws IOException
* when a problem occurs with docker api calls
* @apiNote this method implements 1.20 docker API and requires docker not less than 1.8.0 version
*/
public InputStream getResource(final GetResourceParams params) throws IOException {
DockerConnection connection = null;
try {
connection = connectionFactory.openConnection(dockerDaemonUri).method("GET").path(apiVersionPathPrefix + "/containers/" + params.getContainer() + "/archive").query("path", params.getSourcePath());
final DockerResponse response = connection.request();
if (response.getStatus() != OK.getStatusCode()) {
throw getDockerException(response);
}
return new CloseConnectionInputStream(response.getInputStream(), connection);
} catch (IOException io) {
connection.close();
throw io;
}
}
use of org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream in project che by eclipse.
the class DockerConnectorTest method shouldProduceErrorWhenGetsResourcesFromContainerIfResponseCodeIsNotSuccess.
@Test(expectedExceptions = IOException.class, expectedExceptionsMessageRegExp = EXCEPTION_ERROR_MESSAGE)
public void shouldProduceErrorWhenGetsResourcesFromContainerIfResponseCodeIsNotSuccess() throws IOException {
GetResourceParams getResourceParams = GetResourceParams.create(CONTAINER, PATH_TO_FILE);
when(dockerResponse.getStatus()).thenReturn(RESPONSE_ERROR_CODE);
when(dockerResponse.getInputStream()).thenReturn(new CloseConnectionInputStream(new ByteArrayInputStream(ERROR_MESSAGE.getBytes()), dockerConnection));
dockerConnector.getResource(getResourceParams);
verify(dockerResponse).getStatus();
}
use of org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream in project che by eclipse.
the class DockerConnectorTest method shouldBeAbleToGetResourcesFromContainer.
@Test
public void shouldBeAbleToGetResourcesFromContainer() throws IOException {
GetResourceParams getResourceParams = GetResourceParams.create(CONTAINER, PATH_TO_FILE);
when(dockerResponse.getInputStream()).thenReturn(new CloseConnectionInputStream(new ByteArrayInputStream(STREAM_DATA_BYTES), dockerConnection));
String response = CharStreams.toString(new InputStreamReader(dockerConnector.getResource(getResourceParams)));
verify(dockerConnectionFactory).openConnection(any(URI.class));
verify(dockerConnection).method(REQUEST_METHOD_GET);
verify(dockerConnection).path("/containers/" + getResourceParams.getContainer() + "/archive");
verify(dockerConnection).query(eq("path"), eq(PATH_TO_FILE));
verify(dockerConnection).request();
verify(dockerResponse).getStatus();
verify(dockerResponse).getInputStream();
assertEquals(response, STREAM_DATA);
}
use of org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream in project che by eclipse.
the class DockerConnectorTest method shouldProduceErrorWhenPutsResourcesIntoContainerIfResponseCodeIsNotSuccess.
@Test(expectedExceptions = IOException.class, expectedExceptionsMessageRegExp = EXCEPTION_ERROR_MESSAGE)
public void shouldProduceErrorWhenPutsResourcesIntoContainerIfResponseCodeIsNotSuccess() throws IOException {
InputStream source = new CloseConnectionInputStream(new ByteArrayInputStream(ERROR_MESSAGE.getBytes()), dockerConnection);
PutResourceParams putResourceParams = PutResourceParams.create(CONTAINER, PATH_TO_FILE, source);
when(dockerResponse.getStatus()).thenReturn(RESPONSE_ERROR_CODE);
when(dockerResponse.getInputStream()).thenReturn(new ByteArrayInputStream(ERROR_MESSAGE.getBytes()));
dockerConnector.putResource(putResourceParams);
verify(dockerResponse).getStatus();
}
use of org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream in project che by eclipse.
the class DockerConnectorTest method shouldBeAbleToPutResourcesIntoContainer.
@Test
public void shouldBeAbleToPutResourcesIntoContainer() throws IOException {
InputStream source = new CloseConnectionInputStream(new ByteArrayInputStream(STREAM_DATA_BYTES), dockerConnection);
PutResourceParams putResourceParams = PutResourceParams.create(CONTAINER, PATH_TO_FILE, source);
dockerConnector.putResource(putResourceParams);
verify(dockerConnectionFactory).openConnection(any(URI.class));
verify(dockerConnection).method(REQUEST_METHOD_PUT);
verify(dockerConnection).path("/containers/" + putResourceParams.getContainer() + "/archive");
verify(dockerConnection).query(eq("path"), eq(PATH_TO_FILE));
verify(dockerConnection).header("Content-Type", ExtMediaType.APPLICATION_X_TAR);
verify(dockerConnection).header(eq("Content-Length"), anyInt());
verify(dockerConnection).entity(any(InputStream.class));
verify(dockerConnection).request();
verify(dockerResponse).getStatus();
}
Aggregations