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

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

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

import com.fitbur.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.fitbur.github.dockerjava.api.DockerClientException;
import com.fitbur.github.dockerjava.api.com.fitburmand.AttachContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.AuthCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.BuildImageCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.CommitCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.ContainerDiffCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.CopyFileFromContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.CreateContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.CreateImageCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.DockerCmdExecFactory;
import com.fitbur.github.dockerjava.api.com.fitburmand.EventsCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.ExecCreateCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.ExecStartCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.InfoCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.InspectContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.InspectExecCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.InspectImageCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.KillContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.ListContainersCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.ListImagesCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.LogContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.PauseContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.PingCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.PullImageCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.PushImageCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.RemoveContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.RemoveImageCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.RestartContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.SaveImageCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.SearchImagesCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.StartContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.StatsCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.StopContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.TagImageCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.TopContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.UnpauseContainerCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.VersionCmd;
import com.fitbur.github.dockerjava.api.com.fitburmand.WaitContainerCmd;
import com.fitbur.github.dockerjava.core.DockerClientConfig;
import com.fitbur.github.dockerjava.core.RemoteApiVersion;
import com.fitbur.github.dockerjava.jaxrs.connector.ApacheConnectorProvider;
import com.fitbur.github.dockerjava.jaxrs.filter.JsonClientFilter;
import com.fitbur.github.dockerjava.jaxrs.filter.ResponseStatusExceptionFilter;
import com.fitbur.github.dockerjava.jaxrs.filter.SelectiveLoggingFilter;

import com.fitbur.apache.http.config.RegistryBuilder;
import com.fitbur.apache.http.conn.socket.ConnectionSocketFactory;
import com.fitbur.apache.http.conn.socket.PlainConnectionSocketFactory;
import com.fitbur.apache.http.conn.ssl.SSLConnectionSocketFactory;
import com.fitbur.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import com.fitbur.glassfish.com.fitbur.CommonProperties;
import com.fitbur.glassfish.com.fitbur.apache.connector.ApacheClientProperties;
import com.fitbur.glassfish.com.fitbur.client.ClientConfig;
import com.fitbur.glassfish.com.fitbur.client.ClientProperties;
import com.fitbur.slf4j.Logger;
import com.fitbur.slf4j.LoggerFactory;

import javax.net.ssl.SSLContext;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.client.ClientResponseFilter;
import javax.ws.rs.client.WebTarget;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.util.List;

import static com.fitbur.google.com.fitburmon.base.Preconditions.checkNotNull;

//import com.fitbur.glassfish.com.fitbur.apache.connector.ApacheConnectorProvider;
// see https://github.com.fitbur/docker-java/docker-java/issues/196

public class DockerCmdExecFactoryImpl implements DockerCmdExecFactory {

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

    private Client client;

    private WebTarget baseResource;

    private Integer readTimeout = null;

    private Integer connectTimeout = null;

    private Integer maxTotalConnections = null;

    private Integer maxPerRouteConnections = null;

    private ClientRequestFilter[] clientRequestFilters = null;

    private ClientResponseFilter[] clientResponseFilters = null;

    private DockerClientConfig dockerClientConfig;

    @Override
    public void init(DockerClientConfig dockerClientConfig) {
        checkNotNull(dockerClientConfig, "config was not specified");
        this.dockerClientConfig = dockerClientConfig;

        ClientConfig clientConfig = new ClientConfig();
        clientConfig.connectorProvider(new ApacheConnectorProvider());
        clientConfig.property(CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true);

        clientConfig.register(ResponseStatusExceptionFilter.class);
        clientConfig.register(JsonClientFilter.class);
        clientConfig.register(JacksonJsonProvider.class);

        // logging may disabled via log level
        clientConfig.register(new SelectiveLoggingFilter(LOGGER, true));

        if (readTimeout != null) {
            clientConfig.property(ClientProperties.READ_TIMEOUT, readTimeout);
        }

        if (connectTimeout != null) {
            clientConfig.property(ClientProperties.CONNECT_TIMEOUT, connectTimeout);
        }

        if (clientResponseFilters != null) {
            for (ClientResponseFilter clientResponseFilter : clientResponseFilters) {
                if (clientResponseFilter != null)
                    clientConfig.register(clientResponseFilter);
            }
        }

        if (clientRequestFilters != null) {
            for (ClientRequestFilter clientRequestFilter : clientRequestFilters) {
                if (clientRequestFilter != null)
                    clientConfig.register(clientRequestFilter);
            }
        }

        URI originalUri = dockerClientConfig.getUri();

        SSLContext sslContext = null;

        if (dockerClientConfig.getSslConfig() != null) {
            configureProxy(clientConfig, "https");
            try {
                sslContext = dockerClientConfig.getSslConfig().getSSLContext();
            } catch (Exception ex) {
                throw new DockerClientException("Error in SSL Configuration", ex);
            }
        } else {
            configureProxy(clientConfig, "http");
        }

        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(getSchemeRegistry(
                originalUri, sslContext));

        if (maxTotalConnections != null)
            connManager.setMaxTotal(maxTotalConnections);
        if (maxPerRouteConnections != null)
            connManager.setDefaultMaxPerRoute(maxPerRouteConnections);

        clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connManager);

        // Configure connection pool timeout
        // clientConfig.property(ApacheClientProperties.REQUEST_CONFIG, RequestConfig.custom()
        // .setConnectionRequestTimeout(1000).build());

        ClientBuilder clientBuilder = ClientBuilder.newBuilder().withConfig(clientConfig);

        if (sslContext != null) {
            clientBuilder.sslContext(sslContext);
        }

        client = clientBuilder.build();

        if (originalUri.getScheme().equals("unix")) {
            dockerClientConfig.setUri(UnixConnectionSocketFactory.sanitizeUri(originalUri));
        }

        baseResource = client.target(dockerClientConfig.getUri()).path(dockerClientConfig.getVersion().asWebPathPart());

    }

    private void configureProxy(ClientConfig clientConfig, String protocol) {

        List proxies = ProxySelector.getDefault().select(dockerClientConfig.getUri());

        for (Proxy proxy : proxies) {
            InetSocketAddress address = (InetSocketAddress) proxy.address();
            if (address != null) {
                String hostname = address.getHostName();
                int port = address.getPort();

                clientConfig.property(ClientProperties.PROXY_URI, protocol + "://" + hostname + ":" + port);

                String httpProxyUser = System.getProperty(protocol + ".proxyUser");
                if (httpProxyUser != null) {
                    clientConfig.property(ClientProperties.PROXY_USERNAME, httpProxyUser);
                    String httpProxyPassword = System.getProperty(protocol + ".proxyPassword");
                    if (httpProxyPassword != null) {
                        clientConfig.property(ClientProperties.PROXY_PASSWORD, httpProxyPassword);
                    }
                }
            }
        }
    }

    private com.fitbur.apache.http.config.Registry getSchemeRegistry(final URI originalUri,
            SSLContext sslContext) {
        RegistryBuilder registryBuilder = RegistryBuilder.create();
        registryBuilder.register("http", PlainConnectionSocketFactory.getSocketFactory());
        if (sslContext != null) {
            registryBuilder.register("https", new SSLConnectionSocketFactory(sslContext));
        }
        registryBuilder.register("unix", new UnixConnectionSocketFactory(originalUri));
        return registryBuilder.build();
    }

    protected WebTarget getBaseResource() {
        checkNotNull(baseResource, "Factory not initialized, baseResource not set. You probably forgot to call init()!");
        return baseResource;
    }

    protected DockerClientConfig getDockerClientConfig() {
        checkNotNull(dockerClientConfig,
                "Factor not initialized, dockerClientConfig not set. You probably forgot to call init()!");
        return dockerClientConfig;
    }

    @Override
    public AuthCmd.Exec createAuthCmdExec() {
        return new AuthCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public InfoCmd.Exec createInfoCmdExec() {
        return new InfoCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public PingCmd.Exec createPingCmdExec() {
        return new PingCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public VersionCmd.Exec createVersionCmdExec() {
        return new VersionCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public PullImageCmd.Exec createPullImageCmdExec() {
        return new PullImageCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public PushImageCmd.Exec createPushImageCmdExec() {
        return new PushImageCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public SaveImageCmd.Exec createSaveImageCmdExec() {
        return new SaveImageCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public CreateImageCmd.Exec createCreateImageCmdExec() {
        return new CreateImageCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public SearchImagesCmd.Exec createSearchImagesCmdExec() {
        return new SearchImagesCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public RemoveImageCmd.Exec createRemoveImageCmdExec() {
        return new RemoveImageCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public ListImagesCmd.Exec createListImagesCmdExec() {
        return new ListImagesCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public InspectImageCmd.Exec createInspectImageCmdExec() {
        return new InspectImageCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public ListContainersCmd.Exec createListContainersCmdExec() {
        return new ListContainersCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public CreateContainerCmd.Exec createCreateContainerCmdExec() {
        return new CreateContainerCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public StartContainerCmd.Exec createStartContainerCmdExec() {
        return new StartContainerCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public InspectContainerCmd.Exec createInspectContainerCmdExec() {
        return new InspectContainerCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public ExecCreateCmd.Exec createExecCmdExec() {
        return new ExecCreateCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public RemoveContainerCmd.Exec createRemoveContainerCmdExec() {
        return new RemoveContainerCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public WaitContainerCmd.Exec createWaitContainerCmdExec() {
        return new WaitContainerCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public AttachContainerCmd.Exec createAttachContainerCmdExec() {
        return new AttachContainerCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public ExecStartCmd.Exec createExecStartCmdExec() {
        return new ExecStartCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public InspectExecCmd.Exec createInspectExecCmdExec() {
        return new InspectExecCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public LogContainerCmd.Exec createLogContainerCmdExec() {
        return new LogContainerCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public CopyFileFromContainerCmd.Exec createCopyFileFromContainerCmdExec() {
        return new CopyFileFromContainerCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public StopContainerCmd.Exec createStopContainerCmdExec() {
        return new StopContainerCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public ContainerDiffCmd.Exec createContainerDiffCmdExec() {
        return new ContainerDiffCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public KillContainerCmd.Exec createKillContainerCmdExec() {
        return new KillContainerCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public RestartContainerCmd.Exec createRestartContainerCmdExec() {
        return new RestartContainerCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public CommitCmd.Exec createCommitCmdExec() {
        return new CommitCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public BuildImageCmd.Exec createBuildImageCmdExec() {
        return new BuildImageCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public TopContainerCmd.Exec createTopContainerCmdExec() {
        return new TopContainerCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public TagImageCmd.Exec createTagImageCmdExec() {
        return new TagImageCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public PauseContainerCmd.Exec createPauseContainerCmdExec() {
        return new PauseContainerCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public UnpauseContainerCmd.Exec createUnpauseContainerCmdExec() {
        return new UnpauseContainerCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public EventsCmd.Exec createEventsCmdExec() {
        return new EventsCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public StatsCmd.Exec createStatsCmdExec() {
        return new StatsCmdExec(getBaseResource(), getDockerClientConfig());
    }

    @Override
    public void close() throws IOException {
        checkNotNull(client, "Factory not initialized. You probably forgot to call init()!");
        client.close();
    }

    public DockerCmdExecFactoryImpl withReadTimeout(Integer readTimeout) {
        this.readTimeout = readTimeout;
        return this;
    }

    public DockerCmdExecFactoryImpl withConnectTimeout(Integer connectTimeout) {
        this.connectTimeout = connectTimeout;
        return this;
    }

    public DockerCmdExecFactoryImpl withMaxTotalConnections(Integer maxTotalConnections) {
        this.maxTotalConnections = maxTotalConnections;
        return this;
    }

    public DockerCmdExecFactoryImpl withMaxPerRouteConnections(Integer maxPerRouteConnections) {
        this.maxPerRouteConnections = maxPerRouteConnections;
        return this;
    }

    public DockerCmdExecFactoryImpl withClientResponseFilters(ClientResponseFilter... clientResponseFilter) {
        this.clientResponseFilters = clientResponseFilter;
        return this;
    }

    public DockerCmdExecFactoryImpl withClientRequestFilters(ClientRequestFilter... clientRequestFilters) {
        this.clientRequestFilters = clientRequestFilters;
        return this;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy