All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.github.dockerjava.jaxrs.BuildImageCmdExec Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.fitbur.github.dockerjava.jaxrs;

import com.fitbur.github.dockerjava.api.async.ResultCallback;
import com.fitbur.github.dockerjava.api.com.fitburmand.BuildImageCmd;
import com.fitbur.github.dockerjava.api.model.AuthConfigurations;
import com.fitbur.github.dockerjava.api.model.BuildResponseItem;
import com.fitbur.github.dockerjava.core.DockerClientConfig;
import com.fitbur.github.dockerjava.core.async.JsonStreamProcessor;
import com.fitbur.github.dockerjava.jaxrs.async.AbstractCallbackNotifier;
import com.fitbur.github.dockerjava.jaxrs.async.POSTCallbackNotifier;
import com.fitbur.glassfish.com.fitbur.client.ClientProperties;
import com.fitbur.glassfish.com.fitbur.client.RequestEntityProcessing;
import com.fitbur.slf4j.Logger;
import com.fitbur.slf4j.LoggerFactory;

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

import static javax.ws.rs.client.Entity.entity;

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

    public BuildImageCmdExec(WebTarget baseResource, DockerClientConfig dockerClientConfig) {
        super(baseResource, dockerClientConfig);
    }

    private Invocation.Builder resourceWithOptionalAuthConfig(BuildImageCmd com.fitburmand, Invocation.Builder request) {
        final AuthConfigurations authConfigs = firstNonNull(com.fitburmand.getBuildAuthConfigs(), getBuildAuthConfigs());
        if (authConfigs != null) {
            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 com.fitburmand,
            ResultCallback resultCallback) {

        WebTarget webTarget = getBaseResource().path("/build");
        String dockerFilePath = com.fitburmand.getPathToDockerfile();

        if (dockerFilePath != null && com.fitburmand.getRemote() == null && !"Dockerfile".equals(dockerFilePath)) {
            webTarget = webTarget.queryParam("dockerfile", dockerFilePath);
        }
        if (com.fitburmand.getTag() != null) {
            webTarget = webTarget.queryParam("t", com.fitburmand.getTag());
        }
        if (com.fitburmand.getRemote() != null) {
            webTarget = webTarget.queryParam("remote", com.fitburmand.getRemote().toString());
        }
        if (com.fitburmand.isQuiet()) {
            webTarget = webTarget.queryParam("q", "true");
        }
        if (com.fitburmand.hasNoCacheEnabled()) {
            webTarget = webTarget.queryParam("nocache", "true");
        }
        if (com.fitburmand.hasPullEnabled()) {
            webTarget = webTarget.queryParam("pull", "true");
        }
        if (!com.fitburmand.hasRemoveEnabled()) {
            webTarget = webTarget.queryParam("rm", "false");
        }
        if (com.fitburmand.isForcerm()) {
            webTarget = webTarget.queryParam("forcerm", "true");
        }
        if (com.fitburmand.getMemory() != null) {
            webTarget = webTarget.queryParam("memory", com.fitburmand.getMemory());
        }
        if (com.fitburmand.getMemswap() != null) {
            webTarget = webTarget.queryParam("memswap", com.fitburmand.getMemswap());
        }
        if (com.fitburmand.getCpushares() != null) {
            webTarget = webTarget.queryParam("cpushares", com.fitburmand.getCpushares());
        }
        if (com.fitburmand.getCpusetcpus() != null) {
            webTarget = webTarget.queryParam("cpusetcpus", com.fitburmand.getCpusetcpus());
        }

        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(com.fitburmand, webTarget.request()).accept(MediaType.TEXT_PLAIN), entity(
                        com.fitburmand.getTarInputStream(), "application/tar"));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy