
org.testifyproject.github.dockerjava.jaxrs.BuildImageCmdExec Maven / Gradle / Ivy
package org.testifyproject.testifyproject.github.dockerjava.jaxrs;
import static javax.ws.rs.client.Entity.entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import org.testifyproject.testifyproject.fasterxml.jackson.databind.ObjectMapper;
import org.testifyproject.glassfish.org.testifyproject.testifyproject.client.ClientProperties;
import org.testifyproject.glassfish.org.testifyproject.testifyproject.client.RequestEntityProcessing;
import org.testifyproject.slf4j.Logger;
import org.testifyproject.slf4j.LoggerFactory;
import org.testifyproject.testifyproject.github.dockerjava.api.async.ResultCallback;
import org.testifyproject.testifyproject.github.dockerjava.api.org.testifyproject.testifyprojectmand.BuildImageCmd;
import org.testifyproject.testifyproject.github.dockerjava.api.model.AuthConfigurations;
import org.testifyproject.testifyproject.github.dockerjava.api.model.BuildResponseItem;
import org.testifyproject.testifyproject.github.dockerjava.core.DockerClientConfig;
import org.testifyproject.testifyproject.github.dockerjava.core.async.JsonStreamProcessor;
import org.testifyproject.testifyproject.github.dockerjava.jaxrs.async.AbstractCallbackNotifier;
import org.testifyproject.testifyproject.github.dockerjava.jaxrs.async.POSTCallbackNotifier;
import java.org.testifyproject.testifyproject.IOException;
import java.net.URLEncoder;
public class BuildImageCmdExec extends AbstrAsyncDockerCmdExec 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);
}
private Invocation.Builder resourceWithOptionalAuthConfig(BuildImageCmd org.testifyproject.testifyprojectmand, Invocation.Builder request) {
final AuthConfigurations authConfigs = firstNonNull(org.testifyproject.testifyprojectmand.getBuildAuthConfigs(), getBuildAuthConfigs());
if (authConfigs != null && !authConfigs.getConfigs().isEmpty()) {
request = request.header("X-Registry-Config", registryConfigs(authConfigs));
}
return request;
}
private static AuthConfigurations firstNonNull(final AuthConfigurations fromCommand,
final AuthConfigurations fromConfig) {
if (fromCommand != null) {
return fromCommand;
}
if (fromConfig != null) {
return fromConfig;
}
return null;
}
@Override
protected AbstractCallbackNotifier callbackNotifier(BuildImageCmd org.testifyproject.testifyprojectmand,
ResultCallback resultCallback) {
WebTarget webTarget = getBaseResource().path("/build");
String dockerFilePath = org.testifyproject.testifyprojectmand.getPathToDockerfile();
if (dockerFilePath != null && org.testifyproject.testifyprojectmand.getRemote() == null && !"Dockerfile".equals(dockerFilePath)) {
webTarget = webTarget.queryParam("dockerfile", dockerFilePath);
}
if (org.testifyproject.testifyprojectmand.getTag() != null) {
webTarget = webTarget.queryParam("t", org.testifyproject.testifyprojectmand.getTag());
}
if (org.testifyproject.testifyprojectmand.getRemote() != null) {
webTarget = webTarget.queryParam("remote", org.testifyproject.testifyprojectmand.getRemote().toString());
}
webTarget = booleanQueryParam(webTarget, "q", org.testifyproject.testifyprojectmand.isQuiet());
webTarget = booleanQueryParam(webTarget, "nocache", org.testifyproject.testifyprojectmand.hasNoCacheEnabled());
webTarget = booleanQueryParam(webTarget, "pull", org.testifyproject.testifyprojectmand.hasPullEnabled());
webTarget = booleanQueryParam(webTarget, "rm", org.testifyproject.testifyprojectmand.hasRemoveEnabled());
webTarget = booleanQueryParam(webTarget, "forcerm", org.testifyproject.testifyprojectmand.isForcerm());
// this has to be handled differently as it should switch to 'false'
if (org.testifyproject.testifyprojectmand.hasRemoveEnabled() == null || !org.testifyproject.testifyprojectmand.hasRemoveEnabled()) {
webTarget = webTarget.queryParam("rm", "false");
}
if (org.testifyproject.testifyprojectmand.getMemory() != null) {
webTarget = webTarget.queryParam("memory", org.testifyproject.testifyprojectmand.getMemory());
}
if (org.testifyproject.testifyprojectmand.getMemswap() != null) {
webTarget = webTarget.queryParam("memswap", org.testifyproject.testifyprojectmand.getMemswap());
}
if (org.testifyproject.testifyprojectmand.getCpushares() != null) {
webTarget = webTarget.queryParam("cpushares", org.testifyproject.testifyprojectmand.getCpushares());
}
if (org.testifyproject.testifyprojectmand.getCpusetcpus() != null) {
webTarget = webTarget.queryParam("cpusetcpus", org.testifyproject.testifyprojectmand.getCpusetcpus());
}
if (org.testifyproject.testifyprojectmand.hasRemoveEnabled() == null || !org.testifyproject.testifyprojectmand.hasRemoveEnabled()) {
webTarget = webTarget.queryParam("rm", "false");
}
if (org.testifyproject.testifyprojectmand.getBuildArgs() != null && !org.testifyproject.testifyprojectmand.getBuildArgs().isEmpty()) {
try {
webTarget = webTarget.queryParam("buildargs",
URLEncoder.encode(MAPPER.writeValueAsString(org.testifyproject.testifyprojectmand.getBuildArgs()), "UTF-8"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (org.testifyproject.testifyprojectmand.getShmsize() != null) {
webTarget = webTarget.queryParam("shmsize", org.testifyproject.testifyprojectmand.getShmsize());
}
webTarget.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.CHUNKED);
webTarget.property(ClientProperties.CHUNKED_ENCODING_SIZE, 1024 * 1024);
LOGGER.trace("POST: {}", webTarget);
return new POSTCallbackNotifier<>(new JsonStreamProcessor<>(BuildResponseItem.class),
resultCallback,
resourceWithOptionalAuthConfig(org.testifyproject.testifyprojectmand, webTarget.request()).accept(MediaType.TEXT_PLAIN),
entity(org.testifyproject.testifyprojectmand.getTarInputStream(), "application/tar")
);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy