DefaultDockerClient extends DockerAbstractClient implements DockerClient { @Nonnull public static DefaultDockerClient newInstance(DockerClientConfig clientConfig) { DockerCloudUtils.requireNonNull(clientConfig, "Client config cannot be null."); URI dockerURI = clientConfig.getInstanceURI(); boolean usingTLS = clientConfig.isUsingTLS(); int connectionPoolSize = clientConfig.getConnectionPoolSize(); if (dockerURI.isOpaque()) { throw new IllegalArgumentException("Non opaque URI expected: " + dockerURI); } if (!dockerURI.isAbsolute()) { throw new IllegalArgumentException("Absolute URI expected: " + dockerURI); } ClientConfig config = new ClientConfig(); config.connectorProvider(new ApacheConnectorProvider()); SupportedScheme scheme; try { scheme = SupportedScheme.valueOf(dockerURI.getScheme().toUpperCase()); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Invalid scheme: " + dockerURI.getScheme() + ". Only 'tcp' or 'unix' supported.", e); } HttpClientConnectionOperator connectionOperator = null; ConnectionReuseStrategy connectionReuseStrategy = new UpgradeAwareConnectionReuseStrategy(); final URI effectiveURI; switch (scheme) { case TCP: if (StringUtil.isNotEmpty(dockerURI.getPath()) || dockerURI.getUserInfo() != null || dockerURI.getQuery() != null || dockerURI.getFragment() != null) { throw new IllegalArgumentException("Only host ip/name and port can be provided for tcp scheme."); } if (StringUtil.isEmpty(dockerURI.getHost())) { throw new IllegalArgumentException("Invalid hostname."); } int port = dockerURI.getPort(); if (port == -1) { port = usingTLS ? DEFAULT_TLS_PORT : DEFAULT_PORT; } try { TranslatedScheme translatedScheme = usingTLS ? TranslatedScheme.HTTPS : TranslatedScheme.HTTP; effectiveURI = new URI(translatedScheme.part(), dockerURI.getUserInfo(), dockerURI.getHost(), port, null, null, null); } catch (URISyntaxException e) { throw new IllegalArgumentException("Failed to build effective URI for TCP socket.", e); } break; case UNIX: if (DockerCloudUtils.isWindowsHost()) { throw new IllegalArgumentException("Unix sockets are not supported on Windows hosts."); } effectiveURI = validatePathBasedURI(dockerURI, usingTLS, SupportedScheme.UNIX, "Unix sockets"); connectionOperator = new UnixSocketClientConnectionOperator(Paths.get(dockerURI.getPath())); break; case NPIPE: if (!DockerCloudUtils.isWindowsHost()) { throw new IllegalArgumentException("Named pipes are only supported on Windows hosts."); } effectiveURI = validatePathBasedURI(dockerURI, usingTLS, SupportedScheme.NPIPE, "named pipes."); NPipeSocketAddress pipeAddress = NPipeSocketAddress.fromPath(Paths.get(dockerURI.getPath())); connectionOperator = new NPipeSocketClientConnectionOperator(pipeAddress); connectionReuseStrategy = NoConnectionReuseStrategy.INSTANCE; break; default: throw new AssertionError("Unknown enum member: " + scheme); } DockerHttpConnectionFactory connectionFactory = new DockerHttpConnectionFactory(); PoolingHttpClientConnectionManager connManager; if (connectionOperator != null) { connManager = new PoolingHttpClientConnectionManager(connectionOperator, connectionFactory, -1, TimeUnit.SECONDS); } else { connManager = new PoolingHttpClientConnectionManager( getDefaultRegistry(clientConfig.isVerifyingHostname()), connectionFactory, null); } connManager.setDefaultMaxPerRoute(connectionPoolSize); connManager.setMaxTotal(connectionPoolSize); config.property(ApacheClientProperties.CONNECTION_MANAGER, connManager); config.property(ClientProperties.CONNECT_TIMEOUT, (int) clientConfig.getConnectTimeout().toMillis()); config.property(ClientProperties.READ_TIMEOUT, (int) clientConfig.getTransferTimeout().toMillis()); config.property(ApacheConnectorProvider.CONNECTION_REUSE_STRATEGY_PROP, connectionReuseStrategy); return new DefaultDockerClient(connectionFactory, ClientBuilder.newClient(config), effectiveURI, clientConfig.getApiVersion()); } private  DefaultDockerClient(DockerHttpConnectionFactory connectionFactory, Client jerseyClient, URI targetUri,
                                DockerAPIVersion apiVersion); @Nonnull @Override DockerAPIVersion getApiVersion(); @Override void setApiVersion(@Nonnull DockerAPIVersion apiVersion); @Nonnull Node getVersion(); @Nonnull @Override Node getInfo(); @Nonnull @Override Node createContainer(@Nonnull Node containerSpec, @Nullable String name); @Override void startContainer(@Nonnull final String containerId); @Nonnull @Override Node createService(@Nonnull Node serviceSpec); @Nonnull @Override Node inspectService(@Nonnull String service); @Override void updateService(@Nonnull String service, @Nonnull Node serviceSpec, @Nonnull BigInteger version); @Override void restartContainer(@Nonnull String containerId); @Nonnull @Override Node inspectContainer(@Nonnull String container); @Nonnull @Override Node inspectImage(@Nonnull String image); @Override @Nonnull NodeStream createImage(@Nonnull String from, @Nullable String tag, @Nonnull DockerRegistryCredentials credentials); StreamHandler attach(@Nonnull String containerId, boolean demuxStdio); @Nonnull @Override StreamHandler streamLogs(@Nonnull String containerId, int lineCount, @Nonnull Set<StdioType> stdioTypes,
                                    boolean follow, boolean demuxStdio); @Nonnull @Override StreamHandler streamServiceLogs(@Nonnull String serviceId, int lineCount, @Nonnull Set<StdioType>
            stdioTypes, boolean follow, boolean demuxStream); @Override void stopContainer(@Nonnull String container, Duration timeout); @Override void removeContainer(@Nonnull String container, boolean removeVolumes, boolean force); @Override void removeService(@Nonnull String service); @Nonnull @Override Node listContainersWithLabel(@Nonnull Map<String, String> labelFilters); @Nonnull @Override Node listServicesWithLabel(@Nonnull Map<String, String> labelFilters); @Nonnull @Override Node listTasks(@Nonnull String serviceId); @Nonnull static DefaultDockerClient newInstance(DockerClientConfig clientConfig);  }
@Test public void openValidInput() { DefaultDockerClient.newInstance(createConfig(URI.create("tcp: DefaultDockerClient.newInstance(createConfig(URI.create("tcp: DefaultDockerClient.newInstance(createConfig(URI.create("tcp: }
@Test @SuppressWarnings("ConstantConditions") public void openAllInvalidInput() { assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> DefaultDockerClient.newInstance(createConfig(URI.create("/a/relative/uri"), false))); assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> DefaultDockerClient.newInstance(createConfig(URI.create("tcp:an.opaque.url"), false))); assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> DefaultDockerClient.newInstance(createConfig(URI.create("http: }
@Test public void platformDependentSettings() { if (DockerCloudUtils.isWindowsHost()) { assertThatExceptionOfType(IllegalArgumentException.class). isThrownBy(() -> DefaultDockerClient.newInstance(createConfig(DockerCloudUtils .DOCKER_DEFAULT_SOCKET_URI, false))); DefaultDockerClient.newInstance(createConfig(DockerCloudUtils.DOCKER_DEFAULT_NAMED_PIPE_URI, false)) .close(); DefaultDockerClient.newInstance(createConfig(URI.create("npipe: false)).close(); } else { assertThatExceptionOfType(IllegalArgumentException.class). isThrownBy(() -> DefaultDockerClient.newInstance(createConfig(DockerCloudUtils .DOCKER_DEFAULT_NAMED_PIPE_URI, false))); DefaultDockerClient.newInstance(createConfig(DockerCloudUtils.DOCKER_DEFAULT_SOCKET_URI, false)).close(); DefaultDockerClient.newInstance(createConfig(URI.create("unix:/some/non/sandard/location.sock"), false)) .close(); DefaultDockerClient.newInstance(createConfig(URI.create("unix: ).close(); } }
@Test @SuppressWarnings("ConstantConditions") public void invalidUnixSocketSettings() { assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> DefaultDockerClient.newInstance(createConfig(DockerCloudUtils.DOCKER_DEFAULT_SOCKET_URI, true))); assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> DefaultDockerClient.newInstance(createConfig(URI.create("unix: false))); assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> DefaultDockerClient.newInstance(createConfig(URI.create("unix: false))); }
@Test @SuppressWarnings("ConstantConditions") public void invalidNPipeSettings() { assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> DefaultDockerClient.newInstance(createConfig(DockerCloudUtils.DOCKER_DEFAULT_NAMED_PIPE_URI, true))); assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> DefaultDockerClient.newInstance(createConfig(URI.create("npipe: false))); assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> DefaultDockerClient.newInstance(createConfig(URI.create("npipe: "./pipe/non_standard_location?param=value"), false))); }
@Test @SuppressWarnings("ConstantConditions") public void openInvalidInput() { assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> DefaultDockerClient.newInstance(createConfig(URI.create("tcp:/127.0.0.1:2375"), false))); assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> DefaultDockerClient.newInstance(createConfig(URI.create("tcp: assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> DefaultDockerClient.newInstance(createConfig(URI.create("tcp: assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> DefaultDockerClient.newInstance(createConfig(URI.create("tcp: assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> DefaultDockerClient.newInstance(createConfig(URI.create("tcp: }