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
33 changes: 8 additions & 25 deletions src/main/java/com/amihaiemil/docker/LocalDocker.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
package com.amihaiemil.docker;

import org.apache.http.impl.client.HttpClientBuilder;
import java.io.File;

/**
* Local Docker API. Use this when you want to communicate with the local
Expand All @@ -43,38 +43,21 @@ public final class LocalDocker extends RtDocker {

/**
* Local Docker engine.
* @param unixSocket Path to the unix socket
* (e.g. unix:///var/run/docker.sock).
* @param unixSocket Unix socket File on disk.
* (most likely /var/run/docker.sock).
*/
public LocalDocker(final String unixSocket){
public LocalDocker(final File unixSocket){
this(unixSocket, "v1.35");
}

/**
* Local Docker engine.
* @param unixSocket Path to the unix socket
* (e.g. unix:///var/run/docker.sock).
* @param unixSocket Unix socket File on disk.
* (most likely /var/run/docker.sock).
* @param version API version (e.g. v1.30).
*/
public LocalDocker(final String unixSocket, final String version){
super(HttpClientBuilder.create().build());
}


/**
* Sanitize the path to the unix socket.
* @param unixSocket Path to the unix socket.
* @return Sanitized path.
*/
private static String sanitize(final String unixSocket) {
String sanitized = unixSocket;
if(unixSocket.startsWith("unix://")) {
sanitized = unixSocket.substring("unix://".length());
}
if(!sanitized.startsWith("/")) {
sanitized = "/" + sanitized;
}
return sanitized;
public LocalDocker(final File unixSocket, final String version){
super(new UnixHttpClient(unixSocket));
}

}
3 changes: 3 additions & 0 deletions src/main/java/com/amihaiemil/docker/RtDocker.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
* @since 0.0.1
* @todo #11:30min Implement RemoteDocker which will make the requests over
* a tcp socket and TLS if certificates are provided.
* @todo #32:30min Reimplement ping() with the new HttpClient architecture.
* It should create an HttpGet method and send it with the encapsulated
* client.
*/
abstract class RtDocker implements Docker {

Expand Down
55 changes: 48 additions & 7 deletions src/main/java/com/amihaiemil/docker/UnixHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,25 @@
*/
package com.amihaiemil.docker;

import jnr.unixsocket.UnixSocketAddress;
import jnr.unixsocket.UnixSocketChannel;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
* An HttpClient which works over a UnixSocket.
Expand All @@ -44,10 +52,9 @@
* @version $Id$
* @since 0.0.1
* @checkstyle ParameterNumber (150 lines)
* @todo #29:30min Implement a ConnectionSocketFactory which builds
* UnixSocket objects.
* @checkstyle AnonInnerLength (150 lines)
*/
public final class UnixHttpClient implements HttpClient {
final class UnixHttpClient implements HttpClient {

/**
* Decorated HttpClient.
Expand All @@ -56,17 +63,51 @@ public final class UnixHttpClient implements HttpClient {

/**
* Ctor.
* @param socketFile Path to the unix socket on disk.
* @param socketFile Unix socket on disk.
*/
public UnixHttpClient(final String socketFile) {
this(HttpClientBuilder.create().build());
UnixHttpClient(final File socketFile) {
this(
HttpClientBuilder.create().setConnectionManager(
new BasicHttpClientConnectionManager(
RegistryBuilder
.<ConnectionSocketFactory>create()
.register(
"unix",
new ConnectionSocketFactory() {
@Override
public Socket createSocket(
final HttpContext httpContext
) throws IOException {
return UnixSocketChannel.open().socket();
}

@Override
public Socket connectSocket(
final int connectionTimeout,
final Socket socket,
final HttpHost host,
final InetSocketAddress remoteAddress,
final InetSocketAddress localAddress,
final HttpContext context
) throws IOException {
socket.setSoTimeout(connectionTimeout);
socket.getChannel().connect(
new UnixSocketAddress(socketFile)
);
return socket;
}
})
.build()
)
).build()
);
}

/**
* Ctor.
* @param client Decorated HttpClient.
*/
public UnixHttpClient(final HttpClient client) {
UnixHttpClient(final HttpClient client) {
this.client = client;
}

Expand Down
6 changes: 5 additions & 1 deletion src/test/java/com/amihaiemil/docker/LocalDockerITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.hamcrest.Matchers;
import org.junit.Test;

import java.io.File;

/**
* Integration tests for LocalDocker.
* @author Mihai Andronache (amihaiemil@gmail.com)
Expand All @@ -43,7 +45,9 @@ public final class LocalDockerITCase {
*/
@Test
public void pingsDocker() throws Exception {
final Docker docker = new LocalDocker("/var/run/docker.sock");
final Docker docker = new LocalDocker(
new File("/var/run/docker.sock")
);
MatcherAssert.assertThat(docker.ping(), Matchers.is(Boolean.TRUE));
}

Expand Down
10 changes: 8 additions & 2 deletions src/test/java/com/amihaiemil/docker/LocalDockerTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.hamcrest.Matchers;
import org.junit.Test;

import java.io.File;

/**
* Unit tests for LocalDocker.
* @author Mihai Andronache (amihaiemil@gmail.com)
Expand All @@ -42,7 +44,9 @@ public final class LocalDockerTestCase {
*/
@Test
public void canInstantiateWithScheme() {
final Docker docker = new LocalDocker("unix:///var/run/docker.sock");
final Docker docker = new LocalDocker(
new File("/var/run/docker.sock")
);
MatcherAssert.assertThat(docker, Matchers.notNullValue());
}

Expand All @@ -51,7 +55,9 @@ public void canInstantiateWithScheme() {
*/
@Test
public void canInstantiateWithoutScheme() {
final Docker docker = new LocalDocker("/var/run/docker.sock");
final Docker docker = new LocalDocker(
new File("/var/run/docker.sock")
);
MatcherAssert.assertThat(docker, Matchers.notNullValue());
}
}
17 changes: 15 additions & 2 deletions src/test/java/com/amihaiemil/docker/UnixHttpClientTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import org.hamcrest.Matchers;
import org.junit.Test;
import org.mockito.Mockito;

import java.io.File;
import java.io.IOException;

/**
Expand All @@ -49,7 +51,18 @@
public final class UnixHttpClientTestCase {

/**
* UnixHttoClient returns its HttpParams.
* UnixHttpClient can be instantiated with a socket File.
*/
@Test
public void instantiatesWithSocket() {
MatcherAssert.assertThat(
new UnixHttpClient(new File("/var/run/docker.sock")),
Matchers.notNullValue()
);
}

/**
* UnixHttpClient returns its HttpParams.
*/
@Test
public void getsHttpParams() {
Expand All @@ -65,7 +78,7 @@ public void getsHttpParams() {
}

/**
* UnixHttoClient returns its ClientConnectionManager.
* UnixHttpClient returns its ClientConnectionManager.
*/
@Test
public void getsClientConnectionManager() {
Expand Down