Skip to content
Closed
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
@@ -1,11 +1,13 @@
package com.github.dockerjava.api.command;

import com.github.dockerjava.api.model.LoadImageResponseItem;

import java.io.InputStream;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

public interface LoadImageCmd extends SyncDockerCmd<Void> {
public interface LoadImageCmd extends AsyncDockerCmd<LoadImageCmd, LoadImageResponseItem> {

@CheckForNull
InputStream getImageStream();
Expand All @@ -16,6 +18,11 @@ public interface LoadImageCmd extends SyncDockerCmd<Void> {
*/
LoadImageCmd withImageStream(@Nonnull InputStream imageStream);

interface Exec extends DockerCmdSyncExec<LoadImageCmd, Void> {
@CheckForNull
Boolean getQuiet();

LoadImageCmd withQuiet(Boolean quiet);

interface Exec extends DockerCmdAsyncExec<LoadImageCmd, LoadImageResponseItem> {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import javax.annotation.CheckForNull;

/**
* Represents a build response stream item
*/
Expand All @@ -25,6 +27,7 @@ public boolean isBuildSuccessIndicated() {
return getStream().contains(BUILD_SUCCESS);
}

@CheckForNull
@JsonIgnore
public String getImageId() {
if (!isBuildSuccessIndicated()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.dockerjava.api.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
* @author Kanstantsin Shautsou
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class LoadImageResponseItem extends ResponseItem {
private static final long serialVersionUID = 1L;

private static final String LOAD_SUCCESS = "Loaded image:";

@JsonIgnore
public boolean isLoadedOK() {
if (isErrorIndicated() || getStream() == null) {
return false;
}

return getStream().contains(LOAD_SUCCESS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.dockerjava.core.command;

import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.model.LoadImageResponseItem;
import com.github.dockerjava.core.async.ResultCallbackTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.CheckForNull;

/**
* @author Kanstantsin Shautsou
*/
public class LoadImageCallback extends ResultCallbackTemplate<LoadImageCallback, LoadImageResponseItem> {
private static final Logger LOG = LoggerFactory.getLogger(LoadImageCallback.class);

@CheckForNull
protected LoadImageResponseItem latestItem = null;

@Override
public void onNext(LoadImageResponseItem responseItem) {
this.latestItem = responseItem;
LOG.debug(responseItem.toString());
}

public void awaitSuccess() {
try {
awaitCompletion();
} catch (InterruptedException e) {
throw new DockerClientException("Interrupted", e);
}

if (latestItem == null) {
throw new DockerClientException("Could not load image");
} else if (latestItem.isErrorIndicated()) {
throw new DockerClientException("Could not load image: " + latestItem.getErrorDetail());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
import java.io.InputStream;

import com.github.dockerjava.api.command.LoadImageCmd;
import com.github.dockerjava.api.model.LoadImageResponseItem;

import javax.annotation.Nonnull;

public class LoadImageCmdImpl extends AbstrDockerCmd<LoadImageCmd, Void> implements LoadImageCmd {
public class LoadImageCmdImpl extends AbstrAsyncDockerCmd<LoadImageCmd, LoadImageResponseItem> implements LoadImageCmd {

private InputStream imageStream;

// set false because in quiet mode it returns not JSON!
private Boolean quiet = false;

/**
* @param imageStream
* the InputStream of the tar file
* @param imageStream the InputStream of the tar file
*/
public LoadImageCmdImpl(LoadImageCmd.Exec exec, InputStream imageStream) {
super(exec);
Expand All @@ -26,9 +29,19 @@ public InputStream getImageStream() {
return imageStream;
}

@Override
public Boolean getQuiet() {
return quiet;
}

@Override
public LoadImageCmd withQuiet(Boolean quiet) {
this.quiet = quiet;
return this;
}

/**
* @param imageStream
* the InputStream of the tar file
* @param imageStream the InputStream of the tar file
*/
@Override
public LoadImageCmdImpl withImageStream(@Nonnull InputStream imageStream) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void awaitSuccess() {
if (latestItem == null) {
throw new DockerClientException("Could not push image");
} else if (latestItem.isErrorIndicated()) {
throw new DockerClientException("Could not push image: " + latestItem.getError());
throw new DockerClientException("Could not push image: " + latestItem.getErrorDetail());
}
}
}
29 changes: 24 additions & 5 deletions src/main/java/com/github/dockerjava/jaxrs/LoadImageCmdExec.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.model.LoadImageResponseItem;
import com.github.dockerjava.core.async.JsonStreamProcessor;
import com.github.dockerjava.jaxrs.async.AbstractCallbackNotifier;
import com.github.dockerjava.jaxrs.async.POSTCallbackNotifier;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.RequestEntityProcessing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.dockerjava.api.command.LoadImageCmd;
import com.github.dockerjava.core.DockerClientConfig;

public class LoadImageCmdExec extends AbstrSyncDockerCmdExec<LoadImageCmd, Void> implements LoadImageCmd.Exec {
public class LoadImageCmdExec extends AbstrAsyncDockerCmdExec<LoadImageCmd, LoadImageResponseItem> implements LoadImageCmd.Exec {

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

Expand All @@ -20,12 +27,24 @@ public LoadImageCmdExec(WebTarget baseResource, DockerClientConfig dockerClientC
}

@Override
protected Void execute(LoadImageCmd command) {
protected AbstractCallbackNotifier<LoadImageResponseItem> callbackNotifier(LoadImageCmd command, ResultCallback<LoadImageResponseItem> resultCallback) {
WebTarget webTarget = getBaseResource().path("/images/load");

LOGGER.trace("POST: {}", webTarget);
webTarget.request().post(entity(command.getImageStream(), MediaType.APPLICATION_OCTET_STREAM));
if (command.getQuiet() != null) {
webTarget = webTarget.queryParam("quiet", command.getQuiet());
}

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

return null;
LOGGER.trace("POST: {}", webTarget);
// webTarget.request()
// .post(entity(command.getImageStream(), MediaType.APPLICATION_OCTET_STREAM));

return new POSTCallbackNotifier<>(new JsonStreamProcessor<>(LoadImageResponseItem.class),
resultCallback,
webTarget.request().accept(MediaType.TEXT_PLAIN),
entity(command.getImageStream(), "application/tar")
);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.github.dockerjava.netty.exec;

import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.model.LoadImageResponseItem;
import com.github.dockerjava.netty.InvocationBuilder;
import com.github.dockerjava.netty.MediaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -8,7 +12,7 @@
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.netty.WebTarget;

public class LoadImageCmdExec extends AbstrSyncDockerCmdExec<LoadImageCmd, Void> implements
public class LoadImageCmdExec extends AbstrAsyncDockerCmdExec<LoadImageCmd, LoadImageResponseItem> implements
LoadImageCmd.Exec {

private static final Logger LOGGER = LoggerFactory.getLogger(LoadImageCmdExec.class);
Expand All @@ -18,12 +22,22 @@ public LoadImageCmdExec(WebTarget baseResource, DockerClientConfig dockerClientC
}

@Override
protected Void execute(LoadImageCmd command) {
protected Void execute0(LoadImageCmd command, ResultCallback<LoadImageResponseItem> resultCallback) {
WebTarget webResource = getBaseResource().path("/images/load");

if (command.getQuiet() != null) {
webResource = webResource.queryParam("quiet", command.getQuiet());
}

LOGGER.trace("POST: {}", webResource);
return webResource.request()
.post(new TypeReference<Void>() {
}, command.getImageStream());
final InvocationBuilder builder = webResource.request()
.accept(MediaType.APPLICATION_JSON)
.header("Content-Type", "application/tar")
.header("encoding", "gzip");

builder.post(new TypeReference<LoadImageResponseItem>() {
}, resultCallback, command.getImageStream());

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
import com.github.dockerjava.api.command.VersionCmd;
import com.github.dockerjava.api.command.WaitContainerCmd;
import com.github.dockerjava.api.model.BuildResponseItem;
import com.github.dockerjava.api.model.LoadImageResponseItem;
import com.github.dockerjava.core.command.LoadImageCallback;

import java.io.IOException;
import java.security.SecureRandom;
Expand Down Expand Up @@ -136,8 +138,8 @@ public CreateImageResponse exec(CreateImageCmd command) {
public LoadImageCmd.Exec createLoadImageCmdExec() {
return new LoadImageCmd.Exec() {
@Override
public Void exec(LoadImageCmd command) {
delegate.createLoadImageCmdExec().exec(command);
public Void exec(LoadImageCmd command, ResultCallback<LoadImageResponseItem> resultCallback) {
delegate.createLoadImageCmdExec().exec(command, new LoadImageCallback());
return null;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void afterMethod(ITestResult result) {
@Test
public void loadImageFromTar() throws Exception {
try (InputStream uploadStream = Files.newInputStream(TestResources.getApiImagesLoadTestTarball())) {
dockerClient.loadImageCmd(uploadStream).exec();
dockerClient.loadImageCmd(uploadStream).exec(new LoadImageCallback()).awaitSuccess();
}

final Image image = findImageWithId(expectedImageId, dockerClient.listImagesCmd().exec());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.dockerjava.api.model.Image;

import com.github.dockerjava.core.command.LoadImageCallback;
import com.github.dockerjava.netty.AbstractNettyDockerClientTest;
import com.github.dockerjava.utils.TestResources;
import org.testng.ITestResult;
Expand Down Expand Up @@ -55,7 +56,7 @@ public void afterMethod(ITestResult result) {
@Test
public void loadImageFromTar() throws Exception {
try (InputStream uploadStream = Files.newInputStream(TestResources.getApiImagesLoadTestTarball())) {
dockerClient.loadImageCmd(uploadStream).exec();
dockerClient.loadImageCmd(uploadStream).exec(new LoadImageCallback()).awaitSuccess();
}

final Image image = findImageWithId(expectedImageId, dockerClient.listImagesCmd().exec());
Expand Down