Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -134,7 +134,7 @@ private Registry<ConnectionSocketFactory> 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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}