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
@@ -1,8 +1,5 @@
package com.github.dockerjava.core.exec;

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

import com.fasterxml.jackson.core.type.TypeReference;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.BuildImageCmd;
Expand All @@ -12,6 +9,9 @@
import com.github.dockerjava.core.InvocationBuilder;
import com.github.dockerjava.core.MediaType;
import com.github.dockerjava.core.WebTarget;
import com.github.dockerjava.core.util.CacheFromEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.apache.commons.lang.StringUtils.isNotBlank;

Expand Down Expand Up @@ -59,7 +59,7 @@ protected Void execute0(BuildImageCmd command, ResultCallback<BuildResponseItem>
}

if (command.getCacheFrom() != null && !command.getCacheFrom().isEmpty()) {
webTarget = webTarget.queryParamsSet("cachefrom", command.getCacheFrom());
webTarget = webTarget.queryParam("cachefrom", CacheFromEncoder.jsonEncode(command.getCacheFrom()));
}

if (command.getRemote() != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.dockerjava.core.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;

import javax.ws.rs.core.MediaType;
import java.util.Collection;

/**
* JSON Encoder for the docker --cache-from parameter.
*/
public class CacheFromEncoder {
private CacheFromEncoder() {
}

private static final ObjectMapper OBJECT_MAPPER = new JacksonJaxbJsonProvider().locateMapper(Collection.class,
MediaType.APPLICATION_JSON_TYPE);

public static String jsonEncode(Collection<String> imageIds) {
try {
return OBJECT_MAPPER.writeValueAsString(imageIds);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You added new encoder, didn't existing fit that i referenced?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing one deals with maps

} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.github.dockerjava.core.util.CacheFromEncoder;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.RequestEntityProcessing;
import org.slf4j.Logger;
Expand Down Expand Up @@ -75,10 +77,8 @@ protected AbstractCallbackNotifier<BuildResponseItem> callbackNotifier(BuildImag
webTarget = webTarget.queryParam("t", command.getTag());
}

if (command.getCacheFrom() != null) {
for (String c: command.getCacheFrom()) {
webTarget = webTarget.queryParam("cachefrom", c);
}
if (command.getCacheFrom() != null && !command.getCacheFrom().isEmpty()) {
webTarget = webTarget.queryParam("cachefrom", CacheFromEncoder.jsonEncode(command.getCacheFrom()));
}

if (command.getRemote() != null) {
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/com/github/dockerjava/cmd/BuildImageCmdIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,7 @@ public void cacheFrom() throws Exception {
assertThat(inspectImageResponse1, not(nullValue()));

File baseDir2 = fileFromBuildTestResource("CacheFrom/test2");
String cacheImage = String.format("[\"%s\"]", imageId1);
String imageId2 = dockerRule.getClient().buildImageCmd(baseDir2).withCacheFrom(new HashSet<>(Arrays.asList(cacheImage)))
String imageId2 = dockerRule.getClient().buildImageCmd(baseDir2).withCacheFrom(new HashSet<>(Arrays.asList(imageId1)))
.exec(new BuildImageResultCallback())
.awaitImageId();
InspectImageResponse inspectImageResponse2 = dockerRule.getClient().inspectImageCmd(imageId2).exec();
Expand Down