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
19 changes: 19 additions & 0 deletions src/main/java/com/github/dockerjava/api/command/BuildImageCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.InputStream;
import java.net.URI;
import java.util.Map;
import java.util.Set;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -34,10 +35,21 @@ public interface BuildImageCmd extends AsyncDockerCmd<BuildImageCmd, BuildRespon

/**
* "t" in API
*
* @deprecated since docker API version 1.21 there can be multiple tags
* specified so use {@link #getTags()}
*/
@CheckForNull
@Deprecated
String getTag();

/**
* Multple "t" tags.
* @since {@link RemoteApiVersion#VERSION_1_21}
*/
@CheckForNull
Set<String> getTags();

/**
* "remote" in API
*/
Expand Down Expand Up @@ -109,8 +121,15 @@ public interface BuildImageCmd extends AsyncDockerCmd<BuildImageCmd, BuildRespon

// setters

/**
* @deprecated since docker API version 1.21 there can be multiple tags
* specified so use {@link #withTags(Set<String>)}
*/
@Deprecated
BuildImageCmd withTag(String tag);

BuildImageCmd withTags(Set<String> tags);

BuildImageCmd withRemote(URI remote);

BuildImageCmd withBaseDirectory(File baseDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,28 @@
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.github.dockerjava.api.command.BuildImageCmd;
import com.github.dockerjava.api.model.AuthConfigurations;
import com.github.dockerjava.api.model.BuildResponseItem;
import com.github.dockerjava.core.dockerfile.Dockerfile;
import com.github.dockerjava.core.util.FilePathUtil;

import javax.annotation.CheckForNull;

/**
*
* Build an image from Dockerfile.
*
*/
public class BuildImageCmdImpl extends AbstrAsyncDockerCmd<BuildImageCmd, BuildResponseItem> implements BuildImageCmd {

private InputStream tarInputStream;

@Deprecated
private String tag;

private Set<String> tags;

private Boolean noCache;

private Boolean remove = true;
Expand Down Expand Up @@ -82,11 +86,17 @@ public BuildImageCmdImpl(BuildImageCmd.Exec exec, InputStream tarInputStream) {

// getters API

@Deprecated
@Override
public String getTag() {
return tag;
}

@CheckForNull
public Set<String> getTags() {
return tags;
}

@Override
public URI getRemote() {
return remote;
Expand Down Expand Up @@ -178,13 +188,23 @@ public Long getShmsize() {

// setters

/**
* @deprecated use #withTags()
*/
@Deprecated
@Override
public BuildImageCmdImpl withTag(String tag) {
checkNotNull(tag, "Tag is null");
this.tag = tag;
return this;
}

@Override
public BuildImageCmd withTags(Set<String> tags) {
this.tags = tags;
return this;
}

@Override
public BuildImageCmd withRemote(URI remote) {
this.remote = remote;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.dockerjava.jaxrs;

import static javax.ws.rs.client.Entity.entity;
import static org.apache.commons.lang.StringUtils.isNotBlank;

import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
Expand Down Expand Up @@ -65,9 +66,15 @@ protected AbstractCallbackNotifier<BuildResponseItem> callbackNotifier(BuildImag
if (dockerFilePath != null && command.getRemote() == null && !"Dockerfile".equals(dockerFilePath)) {
webTarget = webTarget.queryParam("dockerfile", dockerFilePath);
}
if (command.getTag() != null) {

if (command.getTags() != null && !command.getTags().isEmpty()) {
for (String t : command.getTags()) {
webTarget = webTarget.queryParam("t", t);
}
} else if (isNotBlank(command.getTag())) {
webTarget = webTarget.queryParam("t", command.getTag());
}

if (command.getRemote() != null) {
webTarget = webTarget.queryParam("remote", command.getRemote().toString());
}
Expand Down
85 changes: 77 additions & 8 deletions src/main/java/com/github/dockerjava/netty/WebTarget.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,59 @@
package com.github.dockerjava.netty;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableSet;
import io.netty.handler.codec.http.HttpConstants;
import org.apache.commons.lang.StringUtils;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

/**
* This class is basically a replacement of javax.ws.rs.client.WebTarget to allow simpler migration of JAX-RS code to a netty based
* This class is basically a replacement of {@link javax.ws.rs.client.WebTarget} to allow simpler migration of JAX-RS code to a netty based
* implementation.
*
* @author Marcus Linke
*/
public class WebTarget {
private static final ObjectMapper MAPPER = new ObjectMapper();

private final ChannelProvider channelProvider;

private final ImmutableList<String> path;

private final ImmutableMap<String, String> queryParams;

/**
* Multiple values for the same name param.
*/
private final ImmutableMap<String, Set<String>> queryParamsSet;

private static final String PATH_SEPARATOR = "/";

public WebTarget(ChannelProvider channelProvider) {
this(channelProvider, ImmutableList.<String>of(), ImmutableMap.<String, String>of());
this(channelProvider, ImmutableList.<String>of(), ImmutableMap.<String, String>of(),
ImmutableMap.<String, Set<String>>of());
}

private WebTarget(ChannelProvider channelProvider,
ImmutableList<String> path,
ImmutableMap<String, String> queryParams) {
ImmutableMap<String, String> queryParams,
ImmutableMap<String, Set<String>> queryParamsSet) {
this.channelProvider = channelProvider;
this.path = path;
this.queryParams = queryParams;
this.queryParamsSet = queryParamsSet;
}

public WebTarget path(String... components) {
Expand All @@ -45,15 +63,21 @@ public WebTarget path(String... components) {
newPath.addAll(Arrays.asList(StringUtils.split(component, PATH_SEPARATOR)));
}

return new WebTarget(channelProvider, newPath.build(), queryParams);
return new WebTarget(channelProvider, newPath.build(), queryParams, queryParamsSet);
}

public InvocationBuilder request() {
String resource = PATH_SEPARATOR + StringUtils.join(path, PATH_SEPARATOR);

List<String> params = new ArrayList<>();
for (Map.Entry<String, String> entry : queryParams.entrySet()) {
params.add(entry.getKey() + "=" + entry.getValue());
params.add(entry.getKey() + "=" + encodeComponent(entry.getValue(), HttpConstants.DEFAULT_CHARSET));
}

for (Map.Entry<String, Set<String>> entry : queryParamsSet.entrySet()) {
for (String entryValueValue : entry.getValue()) {
params.add(entry.getKey() + "=" + encodeComponent(entryValueValue, HttpConstants.DEFAULT_CHARSET));
}
}

if (!params.isEmpty()) {
Expand All @@ -63,21 +87,58 @@ public InvocationBuilder request() {
return new InvocationBuilder(channelProvider, resource);
}

/**
* @see io.netty.handler.codec.http.QueryStringEncoder
*/
private static String encodeComponent(String s, Charset charset) {
// TODO: Optimize me.
try {
return URLEncoder.encode(s, charset.name()).replace("+", "%20");
} catch (UnsupportedEncodingException ignored) {
throw new UnsupportedCharsetException(charset.name());
}
}

public WebTarget resolveTemplate(String name, Object value) {
ImmutableList.Builder<String> newPath = ImmutableList.builder();
for (String component : path) {
component = component.replaceAll("\\{" + name + "\\}", value.toString());
newPath.add(component);
}
return new WebTarget(channelProvider, newPath.build(), queryParams);
return new WebTarget(channelProvider, newPath.build(), queryParams, queryParamsSet);
}

public WebTarget queryParam(String name, Object value) {
ImmutableMap.Builder<String, String> builder = ImmutableMap.<String, String>builder().putAll(queryParams);
if (value != null) {
builder.put(name, value.toString());
}
return new WebTarget(channelProvider, path, builder.build());
return new WebTarget(channelProvider, path, builder.build(), queryParamsSet);
}

public WebTarget queryParamsSet(String name, Set<?> values) {
ImmutableMap.Builder<String, Set<String>> builder = ImmutableMap.<String, Set<String>>builder().putAll(queryParamsSet);
if (values != null) {
ImmutableSet.Builder<String> valueBuilder = ImmutableSet.builder();
for (Object value : values) {
valueBuilder.add(value.toString());
}
builder.put(name, valueBuilder.build());
}
return new WebTarget(channelProvider, path, queryParams, builder.build());
}

public WebTarget queryParamsJsonMap(String name, Map<String, String> values) {
if (values != null && !values.isEmpty()) {
try {
// when param value is JSON string
return queryParam(name, MAPPER.writeValueAsString(values));
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
return this;
}
}

@Override
Expand All @@ -97,14 +158,22 @@ public boolean equals(Object o) {
if (path != null ? !path.equals(webTarget.path) : webTarget.path != null) {
return false;
}
return queryParams != null ? queryParams.equals(webTarget.queryParams) : webTarget.queryParams == null;
if (queryParams != null ? !queryParams.equals(webTarget.queryParams) : webTarget.queryParams != null) {
return false;
}
if (queryParamsSet != null ? !queryParamsSet.equals(webTarget.queryParamsSet) : webTarget.queryParamsSet != null) {
return false;
}

return true;
}

@Override
public int hashCode() {
int result = channelProvider != null ? channelProvider.hashCode() : 0;
result = 31 * result + (path != null ? path.hashCode() : 0);
result = 31 * result + (queryParams != null ? queryParams.hashCode() : 0);
result = 31 * result + (queryParamsSet != null ? queryParamsSet.hashCode() : 0);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.dockerjava.netty.exec;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -14,15 +13,12 @@
import com.github.dockerjava.netty.MediaType;
import com.github.dockerjava.netty.WebTarget;

import java.io.IOException;
import java.util.Map;
import static org.apache.commons.lang.StringUtils.isNotBlank;

public class BuildImageCmdExec extends AbstrAsyncDockerCmdExec<BuildImageCmd, BuildResponseItem> implements
BuildImageCmd.Exec {
private static final Logger LOGGER = LoggerFactory.getLogger(BuildImageCmdExec.class);

private static final ObjectMapper MAPPER = new ObjectMapper();

public BuildImageCmdExec(WebTarget baseResource, DockerClientConfig dockerClientConfig) {
super(baseResource, dockerClientConfig);
}
Expand Down Expand Up @@ -55,9 +51,13 @@ protected Void execute0(BuildImageCmd command, ResultCallback<BuildResponseItem>
if (dockerFilePath != null && command.getRemote() == null && !"Dockerfile".equals(dockerFilePath)) {
webTarget = webTarget.queryParam("dockerfile", dockerFilePath);
}
if (command.getTag() != null) {
webTarget = webTarget.queryParam("t", command.getTag());

if (command.getTags() != null && !command.getTags().isEmpty()) {
webTarget = webTarget.queryParamsSet("t", command.getTags());
} else if (isNotBlank(command.getTag())) {
webTarget = webTarget.queryParam("t", command.getTags());
Copy link
Member Author

Choose a reason for hiding this comment

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

!

Copy link
Contributor

Choose a reason for hiding this comment

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

O_o

}

if (command.getRemote() != null) {
webTarget = webTarget.queryParam("remote", command.getRemote().toString());
}
Expand Down Expand Up @@ -86,13 +86,17 @@ protected Void execute0(BuildImageCmd command, ResultCallback<BuildResponseItem>
webTarget = webTarget.queryParam("cpusetcpus", command.getCpusetcpus());
}

webTarget = writeMap(webTarget, "buildargs", command.getBuildArgs());
if (command.getBuildArgs() != null) {
webTarget = webTarget.queryParamsJsonMap("buildargs", command.getBuildArgs());
}

if (command.getShmsize() != null) {
webTarget = webTarget.queryParam("shmsize", command.getShmsize());
}

webTarget = writeMap(webTarget, "labels", command.getLabels());
if (command.getLabels() != null) {
webTarget = webTarget.queryParamsJsonMap("labels", command.getLabels());
}

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

Expand All @@ -106,16 +110,4 @@ protected Void execute0(BuildImageCmd command, ResultCallback<BuildResponseItem>

return null;
}

private WebTarget writeMap(WebTarget webTarget, String name, Map<String, String> value) {
if (value != null && !value.isEmpty()) {
try {
return webTarget.queryParam(name, MAPPER.writeValueAsString(value));
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
return webTarget;
}
}
}
Loading