Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package io.fabric8.maven.docker.access.hc;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import io.fabric8.maven.docker.access.AuthConfig;
import io.fabric8.maven.docker.access.BuildOptions;
import io.fabric8.maven.docker.access.ContainerCreateConfig;
import io.fabric8.maven.docker.access.DockerAccess;
import io.fabric8.maven.docker.access.DockerAccessException;
import io.fabric8.maven.docker.access.NetworkCreateConfig;
import io.fabric8.maven.docker.access.UrlBuilder;
import io.fabric8.maven.docker.access.VolumeCreateConfig;
import io.fabric8.maven.docker.access.chunked.BuildJsonResponseHandler;
import io.fabric8.maven.docker.access.chunked.EntityStreamReaderUtil;
import io.fabric8.maven.docker.access.chunked.PullOrPushResponseJsonHandler;
import io.fabric8.maven.docker.access.hc.ApacheHttpClientDelegate.BodyAndStatusResponseHandler;
import io.fabric8.maven.docker.access.hc.ApacheHttpClientDelegate.HttpBodyAndStatus;
import io.fabric8.maven.docker.access.hc.http.HttpClientBuilder;
import io.fabric8.maven.docker.access.hc.unix.UnixSocketClientBuilder;
import io.fabric8.maven.docker.access.hc.util.ClientBuilder;
import io.fabric8.maven.docker.access.hc.win.NamedPipeClientBuilder;
import io.fabric8.maven.docker.access.log.LogCallback;
import io.fabric8.maven.docker.access.log.LogGetHandle;
import io.fabric8.maven.docker.access.log.LogRequestor;
import io.fabric8.maven.docker.config.ArchiveCompression;
import io.fabric8.maven.docker.config.Arguments;
import io.fabric8.maven.docker.log.DefaultLogCallback;
import io.fabric8.maven.docker.log.LogOutputSpec;
import io.fabric8.maven.docker.model.Container;
import io.fabric8.maven.docker.model.ContainerDetails;
import io.fabric8.maven.docker.model.ContainersListElement;
import io.fabric8.maven.docker.model.ExecDetails;
import io.fabric8.maven.docker.model.Network;
import io.fabric8.maven.docker.model.NetworksListElement;
import io.fabric8.maven.docker.util.EnvUtil;
import io.fabric8.maven.docker.util.JsonFactory;
import io.fabric8.maven.docker.util.ImageName;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.docker.util.Timestamp;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import static java.net.HttpURLConnection.HTTP_CREATED;
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static java.net.HttpURLConnection.HTTP_NOT_MODIFIED;
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
import static java.net.HttpURLConnection.HTTP_OK;
/**
* Implementation using Apache HttpComponents
* for remotely accessing the docker host.
*
* The design goal here is to provide only the functionality required for this plugin in order to
* make it as robust as possible against docker API changes (which happen quite frequently). That's
* also the reason, why no framework like JAX-RS or docker-java is used so that the dependencies are
* kept low.
*
* Of course, it's a bit more manual work, but it's worth the effort
* (as long as the Docker API functionality required is not too much).
*
* @author roland
* @since 26.03.14
*/
public class DockerAccessWithHcClient implements DockerAccess {
// Base URL which is given through when using UnixSocket communication but is not really used
private static final String UNIX_URL = "unix://127.0.0.1:1/";
// Base URL which is given through when using NamedPipe communication but is not really used
private static final String NPIPE_URL = "npipe://127.0.0.1:1/";
// Minimal API version, independent of any feature used
public static final String API_VERSION = "1.18";
// Logging
private final Logger log;
private final ApacheHttpClientDelegate delegate;
private final UrlBuilder urlBuilder;
/**
* Create a new access for the given URL
*
* @param baseUrl base URL for accessing the docker Daemon
* @param certPath used to build up a keystore with the given keys and certificates found in this
* directory
* @param maxConnections maximum parallel connections allowed to docker daemon (if a pool is used)
* @param log a log handler for printing out logging information
* @paran usePool whether to use a connection bool or not
*/
public DockerAccessWithHcClient(String baseUrl,
String certPath,
int maxConnections,
Logger log) throws IOException {
URI uri = URI.create(baseUrl);
if (uri.getScheme() == null) {
throw new IllegalArgumentException("The docker access url '" + baseUrl + "' must contain a schema tcp://, unix:// or npipe://");
}
if (uri.getScheme().equalsIgnoreCase("unix")) {
this.delegate = createHttpClient(new UnixSocketClientBuilder(uri.getPath(), maxConnections, log));
baseUrl = UNIX_URL;
} else if (uri.getScheme().equalsIgnoreCase("npipe")) {
this.delegate = createHttpClient(new NamedPipeClientBuilder(uri.getPath(), maxConnections, log), false);
baseUrl = NPIPE_URL;
} else {
this.delegate = createHttpClient(new HttpClientBuilder(isSSL(baseUrl) ? certPath : null, maxConnections));
}
// Strip trailing slashes if any
while(baseUrl.endsWith("/")) {
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
}
this.urlBuilder = new UrlBuilder(baseUrl, "v" + fetchApiVersionFromServer(baseUrl, this.delegate));
this.log = log;
}
/** {@inheritDoc} */
@Override
public String getServerApiVersion() throws DockerAccessException {
try {
String url = urlBuilder.version();
String response = delegate.get(url, 200);
JsonObject info = JsonFactory.newJsonObject(response);
return info.get("ApiVersion").getAsString();
} catch (Exception e) {
throw new DockerAccessException(e, "Cannot extract API version from server %s", urlBuilder.getBaseUrl());
}
}
@Override
public void startExecContainer(String containerId, LogOutputSpec outputSpec) throws DockerAccessException {
try {
String url = urlBuilder.startExecContainer(containerId);
JsonObject request = new JsonObject();
request.addProperty("Detach", false);
request.addProperty("Tty", true);
delegate.post(url, request.toString(), createExecResponseHandler(outputSpec), HTTP_OK);
} catch (Exception e) {
throw new DockerAccessException(e, "Unable to start container id [%s]", containerId);
}
}
private ResponseHandler