| 35 | import org.openqa.selenium.remote.http.HttpResponse; |
| 36 | |
| 37 | class VersionCommand { |
| 38 | |
| 39 | private static final Json JSON = new Json(); |
| 40 | // Insertion order matters. |
| 41 | // Map Docker API versions to their implementations |
| 42 | // 1.48 is for Docker Engine v28+ with multi-platform and gateway priority support |
| 43 | // 1.44 is for Docker Engine v25+ with multi-network and modern features |
| 44 | // 1.40 is maintained for backward compatibility with legacy engines (Docker v19.03+) |
| 45 | // All use the same generic implementation with version-specific adapters |
| 46 | private static final Map<Version, Function<HttpHandler, DockerProtocol>> SUPPORTED_VERSIONS = |
| 47 | sequencedMapOf( |
| 48 | new Version("1.48"), client -> new DockerClient(client, "1.48"), |
| 49 | new Version("1.44"), client -> new DockerClient(client, "1.44"), |
| 50 | new Version("1.40"), client -> new DockerClient(client, "1.40")); |
| 51 | |
| 52 | private final HttpHandler handler; |
| 53 | |
| 54 | public VersionCommand(HttpHandler handler) { |
| 55 | this.handler = Require.nonNull("HTTP client", handler); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Gets the Docker protocol implementation for a user-specified API version. This allows users to |
| 60 | * override the automatic version detection and force a specific API version (e.g., 1.40 for |
| 61 | * legacy Docker engines). |
| 62 | * |
| 63 | * @param requestedVersion The API version to use (e.g., "1.40" or "1.44") |
| 64 | * @return Optional containing the DockerProtocol implementation if the version is supported |
| 65 | */ |
| 66 | public Optional<DockerProtocol> getDockerProtocol(@Nullable String requestedVersion) { |
| 67 | if (requestedVersion == null || requestedVersion.isEmpty()) { |
| 68 | return getDockerProtocol(); |
| 69 | } |
| 70 | |
| 71 | Version version = new Version(requestedVersion); |
| 72 | Function<HttpHandler, DockerProtocol> factory = SUPPORTED_VERSIONS.get(version); |
| 73 | |
| 74 | if (factory != null) { |
| 75 | return Optional.of(factory.apply(handler)); |
| 76 | } |
| 77 | |
| 78 | // If exact version not found, try to find a compatible version |
| 79 | return SUPPORTED_VERSIONS.entrySet().stream() |
| 80 | .filter(entry -> entry.getKey().equalTo(version)) |
| 81 | .map(Map.Entry::getValue) |
| 82 | .map(func -> func.apply(handler)) |
| 83 | .findFirst(); |
| 84 | } |
| 85 | |
| 86 | public Optional<DockerProtocol> getDockerProtocol() { |
| 87 | try { |
| 88 | HttpResponse res = handler.execute(new HttpRequest(GET, "/version")); |
| 89 | |
| 90 | if (!res.isSuccessful()) { |
| 91 | return Optional.empty(); |
| 92 | } |
| 93 | |
| 94 | Map<String, Object> raw = JSON.toType(res.contentAsString(), MAP_TYPE); |
nothing calls this directly
no test coverage detected