diff --git a/docker-java-transport-httpclient5/src/main/java/com/github/dockerjava/httpclient5/ApacheDockerHttpClientImpl.java b/docker-java-transport-httpclient5/src/main/java/com/github/dockerjava/httpclient5/ApacheDockerHttpClientImpl.java index c3fbc6755..c19c222c7 100644 --- a/docker-java-transport-httpclient5/src/main/java/com/github/dockerjava/httpclient5/ApacheDockerHttpClientImpl.java +++ b/docker-java-transport-httpclient5/src/main/java/com/github/dockerjava/httpclient5/ApacheDockerHttpClientImpl.java @@ -1,9 +1,9 @@ package com.github.dockerjava.httpclient5; import com.github.dockerjava.transport.DockerHttpClient; -import com.github.dockerjava.transport.DomainSocket; import com.github.dockerjava.transport.NamedPipeSocket; import com.github.dockerjava.transport.SSLConfig; +import com.github.dockerjava.transport.UnixSocket; import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase; import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; @@ -134,7 +134,7 @@ private Registry createConnectionSocketFactoryRegistry( .register("unix", new PlainConnectionSocketFactory() { @Override public Socket createSocket(HttpContext context) throws IOException { - return DomainSocket.get(dockerHost.getPath()); + return UnixSocket.get(dockerHost.getPath()); } }) .register("npipe", new PlainConnectionSocketFactory() { diff --git a/docker-java-transport-okhttp/src/main/java/com/github/dockerjava/okhttp/UnixSocketFactory.java b/docker-java-transport-okhttp/src/main/java/com/github/dockerjava/okhttp/UnixSocketFactory.java index fd7499386..dc19b1351 100644 --- a/docker-java-transport-okhttp/src/main/java/com/github/dockerjava/okhttp/UnixSocketFactory.java +++ b/docker-java-transport-okhttp/src/main/java/com/github/dockerjava/okhttp/UnixSocketFactory.java @@ -1,6 +1,6 @@ package com.github.dockerjava.okhttp; -import com.github.dockerjava.transport.DomainSocket; +import com.github.dockerjava.transport.UnixSocket; import javax.net.SocketFactory; import java.io.IOException; @@ -18,7 +18,7 @@ class UnixSocketFactory extends SocketFactory { @Override public Socket createSocket() { try { - return DomainSocket.get(socketPath); + return UnixSocket.get(socketPath); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/docker-java-transport/src/main/java/com/github/dockerjava/transport/DomainSocket.java b/docker-java-transport/src/main/java/com/github/dockerjava/transport/DomainSocket.java index ff77c3830..a2a3503f5 100644 --- a/docker-java-transport/src/main/java/com/github/dockerjava/transport/DomainSocket.java +++ b/docker-java-transport/src/main/java/com/github/dockerjava/transport/DomainSocket.java @@ -130,7 +130,9 @@ public void close() throws IOException { * @param path the path to the domain socket * @return a {@link DomainSocket} instance * @throws IOException if the socket cannot be opened + * @deprecated use {@link UnixSocket#get(String)} */ + @Deprecated public static DomainSocket get(String path) throws IOException { if (Platform.isMac() || isBsdPlatform()) { return new BsdDomainSocket(path); diff --git a/docker-java-transport/src/main/java/com/github/dockerjava/transport/UnixSocket.java b/docker-java-transport/src/main/java/com/github/dockerjava/transport/UnixSocket.java new file mode 100644 index 000000000..0a9338771 --- /dev/null +++ b/docker-java-transport/src/main/java/com/github/dockerjava/transport/UnixSocket.java @@ -0,0 +1,88 @@ +package com.github.dockerjava.transport; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; +import java.net.SocketAddress; +import java.net.SocketException; +import java.nio.channels.Channels; +import java.nio.channels.SocketChannel; + +public class UnixSocket extends AbstractSocket { + + /** + * Return a new {@link Socket} for the given path. Will use JDK's {@link java.net.UnixDomainSocketAddress} + * if available and fallback to {@link DomainSocket} otherwise. + * + * @param path the path to the domain socket + * @return a {@link Socket} instance + * @throws IOException if the socket cannot be opened + */ + public static Socket get(String path) throws IOException { + try { + return new UnixSocket(path); + } catch (Exception e) { + //noinspection deprecation + return DomainSocket.get(path); + } + } + + private final SocketAddress socketAddress; + + private final SocketChannel socketChannel; + + private UnixSocket(String path) throws Exception { + Class unixDomainSocketAddress = Class.forName("java.net.UnixDomainSocketAddress"); + this.socketAddress = + (SocketAddress) unixDomainSocketAddress.getMethod("of", String.class) + .invoke(null, path); + this.socketChannel = SocketChannel.open(this.socketAddress); + } + + @Override + public InputStream getInputStream() throws IOException { + if (isClosed()) { + throw new SocketException("Socket is closed"); + } + if (!isConnected()) { + throw new SocketException("Socket is not connected"); + } + if (isInputShutdown()) { + throw new SocketException("Socket input is shutdown"); + } + + return Channels.newInputStream(socketChannel); + } + + @Override + public OutputStream getOutputStream() throws IOException { + if (isClosed()) { + throw new SocketException("Socket is closed"); + } + if (!isConnected()) { + throw new SocketException("Socket is not connected"); + } + if (isOutputShutdown()) { + throw new SocketException("Socket output is shutdown"); + } + + return Channels.newOutputStream(socketChannel); + } + + @Override + public SocketAddress getLocalSocketAddress() { + return socketAddress; + } + + @Override + public SocketAddress getRemoteSocketAddress() { + return socketAddress; + } + + @Override + public void close() throws IOException { + super.close(); + this.socketChannel.close(); + } +}