Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public interface AttachContainerCmd extends DockerCmd<InputStream>{
public AttachContainerCmd withLogs();

/**
* Its the responsibility of the caller to consume and/or close the {@link InputStream} to prevent
* connection leaks.
*
* @throws NotFoundException No such container
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public interface CopyFileFromContainerCmd extends DockerCmd<InputStream> {
public CopyFileFromContainerCmd withHostPath(String hostPath);

/**
* Its the responsibility of the caller to consume and/or close the {@link InputStream} to prevent
* connection leaks.
*
* @throws NotFoundException No such container
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public interface ExecStartCmd extends DockerCmd<InputStream> {
public ExecStartCmd withTty();

/**
* Its the responsibility of the caller to consume and/or close the {@link InputStream} to prevent
* connection leaks.
*
* @throws com.github.dockerjava.api.NotFoundException
* No such exec instance
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public interface LogContainerCmd extends DockerCmd<InputStream>{
public LogContainerCmd withTail(int tail);

/**
* Its the responsibility of the caller to consume and/or close the {@link InputStream} to prevent
* connection leaks.
*
* @throws NotFoundException No such container
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,12 @@ public interface PullImageCmd extends DockerCmd<InputStream>{

public static interface Exec extends DockerCmdExec<PullImageCmd, InputStream> {
}

/**
* Its the responsibility of the caller to consume and/or close the {@link InputStream} to prevent
* connection leaks.
*/
@Override
public InputStream exec();

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public interface SaveImageCmd extends DockerCmd<InputStream>{
public SaveImageCmd withTag(String tag);

/**
* Its the responsibility of the caller to consume and/or close the {@link InputStream} to prevent
* connection leaks.
*
* @throws com.github.dockerjava.api.NotFoundException No such image
*/
public InputStream exec() throws NotFoundException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.LoggerFactory;

import com.github.dockerjava.api.command.AttachContainerCmd;
import com.github.dockerjava.jaxrs.util.WrappedResponseInputStream;

public class AttachContainerCmdExec extends
AbstrDockerCmdExec<AttachContainerCmd, InputStream> implements
Expand All @@ -36,9 +37,11 @@ protected InputStream execute(AttachContainerCmd command) {

LOGGER.trace("POST: {}", webResource);

return webResource.request()
Response response = webResource.request()
.accept(MediaType.APPLICATION_OCTET_STREAM_TYPE)
.post(null, Response.class).readEntity(InputStream.class);
.post(null, Response.class);

return new WrappedResponseInputStream(response);
}

}
131 changes: 69 additions & 62 deletions src/main/java/com/github/dockerjava/jaxrs/BuildImageCmdExec.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,86 +21,93 @@
import com.github.dockerjava.api.command.BuildImageCmd;
import com.github.dockerjava.api.model.AuthConfigurations;
import com.github.dockerjava.api.model.EventStreamItem;
import com.github.dockerjava.jaxrs.util.WrappedResponseInputStream;
import com.google.common.collect.ImmutableList;

public class BuildImageCmdExec extends AbstrDockerCmdExec<BuildImageCmd, BuildImageCmd.Response> implements BuildImageCmd.Exec {

public class BuildImageCmdExec extends
AbstrDockerCmdExec<BuildImageCmd, BuildImageCmd.Response> implements
BuildImageCmd.Exec {

private static final Logger LOGGER = LoggerFactory
.getLogger(BuildImageCmdExec.class);

public BuildImageCmdExec(WebTarget baseResource) {
super(baseResource);
}

@Override
protected ResponseImpl execute(BuildImageCmd command) {
WebTarget webResource = getBaseResource().path("/build");
String dockerFilePath = command.getPathToDockerfile();
if(command.getTag() != null) {
String dockerFilePath = command.getPathToDockerfile();

if (command.getTag() != null) {
webResource = webResource.queryParam("t", command.getTag());
}
if (command.hasNoCacheEnabled()) {
webResource = webResource.queryParam("nocache", "true");
}
if (command.hasRemoveEnabled()) {
webResource = webResource.queryParam("rm", "true");
}
if (command.isQuiet()) {
webResource = webResource.queryParam("q", "true");
}
if( dockerFilePath != null && !"Dockerfile".equals(dockerFilePath)) {
webResource = webResource.queryParam("dockerfile", dockerFilePath);
}


webResource.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.CHUNKED);
webResource.property(ClientProperties.CHUNKED_ENCODING_SIZE, 1024*1024);


LOGGER.debug("POST: {}", webResource);
InputStream is = resourceWithOptionalAuthConfig(command, webResource.request())
.accept(MediaType.TEXT_PLAIN)
.post(entity(command.getTarInputStream(), "application/tar"), Response.class).readEntity(InputStream.class);

return new ResponseImpl(is);

if (command.hasNoCacheEnabled()) {
webResource = webResource.queryParam("nocache", "true");
}
if (command.hasRemoveEnabled()) {
webResource = webResource.queryParam("rm", "true");
}
if (command.isQuiet()) {
webResource = webResource.queryParam("q", "true");
}
if (dockerFilePath != null && !"Dockerfile".equals(dockerFilePath)) {
webResource = webResource.queryParam("dockerfile", dockerFilePath);
}

webResource.property(ClientProperties.REQUEST_ENTITY_PROCESSING,
RequestEntityProcessing.CHUNKED);
webResource.property(ClientProperties.CHUNKED_ENCODING_SIZE,
1024 * 1024);

LOGGER.debug("POST: {}", webResource);
Response response = resourceWithOptionalAuthConfig(command,
webResource.request())
.accept(MediaType.TEXT_PLAIN)
.post(entity(command.getTarInputStream(), "application/tar"),
Response.class);

return new ResponseImpl(new WrappedResponseInputStream(response));

}

private Invocation.Builder resourceWithOptionalAuthConfig(BuildImageCmd command, Invocation.Builder request) {
private Invocation.Builder resourceWithOptionalAuthConfig(
BuildImageCmd command, Invocation.Builder request) {
AuthConfigurations authConfigs = command.getBuildAuthConfigs();
if (authConfigs != null) {
request = request.header("X-Registry-Config", registryConfigs(authConfigs));
request = request.header("X-Registry-Config",
registryConfigs(authConfigs));
}
return request;
}
public static class ResponseImpl extends BuildImageCmd.Response {

private final InputStream proxy;

public ResponseImpl(InputStream proxy) {
this.proxy = proxy;
}

@Override
public Iterable<EventStreamItem> getItems() throws IOException {
ObjectMapper mapper = new ObjectMapper();
// we'll be reading instances of MyBean
ObjectReader reader = mapper.reader(EventStreamItem.class);
// and then do other configuration, if any, and read:
Iterator<EventStreamItem> items = reader.readValues(proxy);
try {
return ImmutableList.copyOf(items);
} finally {
proxy.close();
}
}

@Override
public int read() throws IOException {
return proxy.read();
}
}

public static class ResponseImpl extends BuildImageCmd.Response {

private final InputStream proxy;

public ResponseImpl(InputStream proxy) {
this.proxy = proxy;
}

@Override
public Iterable<EventStreamItem> getItems() throws IOException {
ObjectMapper mapper = new ObjectMapper();
// we'll be reading instances of MyBean
ObjectReader reader = mapper.reader(EventStreamItem.class);
// and then do other configuration, if any, and read:
Iterator<EventStreamItem> items = reader.readValues(proxy);

try {
return ImmutableList.copyOf(items);
} finally {
proxy.close();
}
}

@Override
public int read() throws IOException {
return proxy.read();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.dockerjava.api.command.CopyFileFromContainerCmd;
import com.github.dockerjava.jaxrs.util.WrappedResponseInputStream;

public class CopyFileFromContainerCmdExec extends AbstrDockerCmdExec<CopyFileFromContainerCmd, InputStream> implements CopyFileFromContainerCmd.Exec {

Expand All @@ -29,7 +31,9 @@ protected InputStream execute(CopyFileFromContainerCmd command) {

LOGGER.trace("POST: " + webResource.toString());

return webResource.request().accept(MediaType.APPLICATION_OCTET_STREAM_TYPE).post(entity(command, MediaType.APPLICATION_JSON)).readEntity(InputStream.class);
Response response = webResource.request().accept(MediaType.APPLICATION_OCTET_STREAM_TYPE).post(entity(command, MediaType.APPLICATION_JSON));

return new WrappedResponseInputStream(response);
}

}
3 changes: 2 additions & 1 deletion src/main/java/com/github/dockerjava/jaxrs/EventsCmdExec.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.github.dockerjava.api.command.EventCallback;
import com.github.dockerjava.api.command.EventsCmd;
import com.github.dockerjava.api.model.Event;
import com.github.dockerjava.jaxrs.util.WrappedResponseInputStream;

public class EventsCmdExec extends AbstrDockerCmdExec<EventsCmd, ExecutorService> implements EventsCmd.Exec {
private static final Logger LOGGER = LoggerFactory.getLogger(EventsCmdExec.class);
Expand Down Expand Up @@ -66,7 +67,7 @@ public Void call() throws Exception {
Response response = null;
try {
response = webTarget.request().get(Response.class);
InputStream inputStream = response.readEntity(InputStream.class);
InputStream inputStream = new WrappedResponseInputStream(response);
JsonParser jp = JSON_FACTORY.createParser(inputStream);
while (jp.nextToken() != JsonToken.END_OBJECT && !jp.isClosed() && eventCallback.isReceiving()) {
eventCallback.onEvent(OBJECT_MAPPER.readValue(jp, Event.class));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.github.dockerjava.jaxrs;

import com.github.dockerjava.api.command.ExecStartCmd;
import com.github.dockerjava.jaxrs.util.WrappedResponseInputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import java.io.InputStream;

import static javax.ws.rs.client.Entity.entity;
Expand All @@ -26,9 +29,11 @@ protected InputStream execute(ExecStartCmd command) {

LOGGER.trace("POST: {}", webResource);

return webResource
Response response = webResource
.request()
.accept(MediaType.APPLICATION_JSON)
.post(entity(command, MediaType.APPLICATION_JSON), Response.class).readEntity(InputStream.class);
.post(entity(command, MediaType.APPLICATION_JSON), Response.class);

return new WrappedResponseInputStream(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.LoggerFactory;

import com.github.dockerjava.api.command.LogContainerCmd;
import com.github.dockerjava.jaxrs.util.WrappedResponseInputStream;

public class LogContainerCmdExec extends AbstrDockerCmdExec<LogContainerCmd, InputStream> implements LogContainerCmd.Exec {

Expand All @@ -28,7 +29,8 @@ protected InputStream execute(LogContainerCmd command) {
.queryParam("tail", command.getTail() < 0 ? "all" : "" + command.getTail());

LOGGER.trace("GET: {}", webResource);
return webResource.request().get().readEntity(InputStream.class);

return new WrappedResponseInputStream(webResource.request().get());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.dockerjava.api.command.PullImageCmd;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.jaxrs.util.WrappedResponseInputStream;

public class PullImageCmdExec extends
AbstrDockerCmdExec<PullImageCmd, InputStream> implements
Expand All @@ -31,9 +33,10 @@ protected InputStream execute(PullImageCmd command) {
.queryParam("registry", command.getRegistry());

LOGGER.trace("POST: {}", webResource);
return resourceWithOptionalAuthConfig(command, webResource.request())
.accept(MediaType.APPLICATION_OCTET_STREAM_TYPE).post(null)
.readEntity(InputStream.class);
Response response = resourceWithOptionalAuthConfig(command, webResource.request())
.accept(MediaType.APPLICATION_OCTET_STREAM_TYPE).post(null);

return new WrappedResponseInputStream(response);
}

private Invocation.Builder resourceWithOptionalAuthConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.api.model.PushEventStreamItem;

import com.github.dockerjava.jaxrs.util.WrappedResponseInputStream;
// Shaded, but imported
import com.google.common.collect.ImmutableList;

Expand All @@ -40,15 +41,14 @@ protected ResponseImpl execute(PushImageCmd command) {

final String registryAuth = registryAuth(command.getAuthConfig());
LOGGER.trace("POST: {}", webResource);
InputStream is = webResource
javax.ws.rs.core.Response response = webResource
.request()
.header("X-Registry-Auth", registryAuth)
.accept(MediaType.APPLICATION_JSON)
.post(
entity(Response.class, MediaType.APPLICATION_JSON)).readEntity(
InputStream.class);
entity(Response.class, MediaType.APPLICATION_JSON));

return new ResponseImpl(is);
return new ResponseImpl(new WrappedResponseInputStream(response));
}

private String name(PushImageCmd command) {
Expand Down
Loading