Repository: docker-java/docker-java
Pull request #269 (2015-07-13 08:12:02)

Add filters option to events operation


Patch:
diff --git a/src/main/java/com/github/dockerjava/api/command/EventsCmd.java b/src/main/java/com/github/dockerjava/api/command/EventsCmd.java
index 057bed64..e691bbc9 100644
--- a/src/main/java/com/github/dockerjava/api/command/EventsCmd.java
+++ b/src/main/java/com/github/dockerjava/api/command/EventsCmd.java
@@ -1,6 +1,7 @@
 package com.github.dockerjava.api.command;
 
 import com.github.dockerjava.api.model.Event;
+import com.github.dockerjava.api.model.Filters;
 
 /**
  * Get events
@@ -19,6 +20,10 @@ public interface EventsCmd extends AsyncDockerCmd<EventsCmd, Event, Void> {
 
     public String getUntil();
 
+    public Filters getFilters();
+
+    public EventsCmd withFilters(Filters filters);
+
     public static interface Exec extends DockerCmdExec<EventsCmd, Void> {
     }
 }
diff --git a/src/main/java/com/github/dockerjava/api/model/EventFilters.java b/src/main/java/com/github/dockerjava/api/model/EventFilters.java
new file mode 100644
index 00000000..08cb7ad1
--- /dev/null
+++ b/src/main/java/com/github/dockerjava/api/model/EventFilters.java
@@ -0,0 +1,37 @@
+package com.github.dockerjava.api.model;
+
+import java.util.List;
+
+/**
+ * Representation of a Docker event filter.
+ *
+ * @author Carlos Sanchez <carlos@apache.org>
+ * 
+ */
+public class EventFilters extends Filters {
+
+    /**
+     * Default constructor for the deserialization.
+     */
+    public EventFilters() {
+    }
+
+    /**
+     * Constructor.
+     * 
+     * @param event
+     *            event to filter
+     */
+    public EventFilters(String... event) {
+        super();
+        withEvent(event);
+    }
+
+    public Filters withEvent(String... event) {
+        return withFilter("event", event);
+    }
+
+    public List<String> getEvent() {
+        return getFilter("event");
+    }
+}
diff --git a/src/main/java/com/github/dockerjava/core/async/ResultCallbackTemplate.java b/src/main/java/com/github/dockerjava/core/async/ResultCallbackTemplate.java
index 50cc1fd9..94d199a6 100644
--- a/src/main/java/com/github/dockerjava/core/async/ResultCallbackTemplate.java
+++ b/src/main/java/com/github/dockerjava/core/async/ResultCallbackTemplate.java
@@ -8,6 +8,9 @@ import java.io.IOException;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 import com.github.dockerjava.api.async.ResultCallback;
 
 /**
@@ -18,6 +21,8 @@ import com.github.dockerjava.api.async.ResultCallback;
  */
 public abstract class ResultCallbackTemplate<T> implements ResultCallback<T> {
 
+    private final static Logger LOGGER = LoggerFactory.getLogger(ResultCallbackTemplate.class);
+
     private final CountDownLatch finished = new CountDownLatch(1);
 
     private Closeable stream;
@@ -34,6 +39,7 @@ public abstract class ResultCallbackTemplate<T> implements ResultCallback<T> {
     @Override
     public void onError(Throwable throwable) {
         try {
+            LOGGER.error("Error during callback", throwable);
             throw new RuntimeException(throwable);
         } finally {
             try {
diff --git a/src/main/java/com/github/dockerjava/core/command/EventsCmdImpl.java b/src/main/java/com/github/dockerjava/core/command/EventsCmdImpl.java
index d1de2e60..50cf34ff 100644
--- a/src/main/java/com/github/dockerjava/core/command/EventsCmdImpl.java
+++ b/src/main/java/com/github/dockerjava/core/command/EventsCmdImpl.java
@@ -1,8 +1,11 @@
 package com.github.dockerjava.core.command;
 
+import static com.google.common.base.Preconditions.*;
+
 import com.github.dockerjava.api.async.ResultCallback;
 import com.github.dockerjava.api.command.EventsCmd;
 import com.github.dockerjava.api.model.Event;
+import com.github.dockerjava.api.model.Filters;
 
 /**
  * Stream docker events
@@ -13,6 +16,8 @@ public class EventsCmdImpl extends AbstrDockerCmd<EventsCmd, Void> implements Ev
 
     private String until;
 
+    private Filters filters;
+
     private ResultCallback<Event> resultCallback;
 
     public EventsCmdImpl(EventsCmd.Exec exec, ResultCallback<Event> resultCallback) {
@@ -38,6 +43,13 @@ public class EventsCmdImpl extends AbstrDockerCmd<EventsCmd, Void> implements Ev
         return this;
     }
 
+    @Override
+    public EventsCmd withFilters(Filters filters) {
+        checkNotNull(filters, "filters have not been specified");
+        this.filters = filters;
+        return this;
+    }
+
     @Override
     public String getSince() {
         return since;
@@ -53,9 +65,15 @@ public class EventsCmdImpl extends AbstrDockerCmd<EventsCmd, Void> implements Ev
         return resultCallback;
     }
 
+    @Override
+    public Filters getFilters() {
+        return filters;
+    }
+
     @Override
     public String toString() {
         return new StringBuilder("events").append(since != null ? " --since=" + since : "")
-                .append(until != null ? " --until=" + until : "").toString();
+                .append(until != null ? " --until=" + until : "")
+                .append(filters != null ? " --filters=" + filters : "").toString();
     }
 }
diff --git a/src/main/java/com/github/dockerjava/jaxrs/EventsCmdExec.java b/src/main/java/com/github/dockerjava/jaxrs/EventsCmdExec.java
index e6f27ae6..0b3d998a 100644
--- a/src/main/java/com/github/dockerjava/jaxrs/EventsCmdExec.java
+++ b/src/main/java/com/github/dockerjava/jaxrs/EventsCmdExec.java
@@ -1,5 +1,7 @@
 package com.github.dockerjava.jaxrs;
 
+import static com.google.common.net.UrlEscapers.*;
+
 import javax.ws.rs.client.WebTarget;
 
 import org.slf4j.Logger;
@@ -25,6 +27,11 @@ public class EventsCmdExec extends AbstrDockerCmdExec<EventsCmd, Void> implement
         WebTarget webTarget = getBaseResource().path("/events").queryParam("since", command.getSince())
                 .queryParam("until", command.getUntil());
 
+        if (command.getFilters() != null) {
+            webTarget = webTarget.queryParam("filters",
+                    urlPathSegmentEscaper().escape(command.getFilters().toString()));
+        }
+
         LOGGER.trace("GET: {}", webTarget);
 
         GETCallbackNotifier<Event> callbackNotifier = new GETCallbackNotifier<Event>(new JsonStreamProcessor<Event>(
diff --git a/src/main/java/com/github/dockerjava/jaxrs/async/AbstractCallbackNotifier.java b/src/main/java/com/github/dockerjava/jaxrs/async/AbstractCallbackNotifier.java
index 6cfc7a76..a010a87a 100644
--- a/src/main/java/com/github/dockerjava/jaxrs/async/AbstractCallbackNotifier.java
+++ b/src/main/java/com/github/dockerjava/jaxrs/async/AbstractCallbackNotifier.java
@@ -48,6 +48,11 @@ public abstract class AbstractCallbackNotifier<T> implements Callable<Void> {
                 resultCallback.onError(e.getCause());
             }
             return null;
+        } catch (Exception e) {
+            if (resultCallback != null) {
+                resultCallback.onError(e);
+            }
+            return null;
         }
 
         try {


Test patch:
diff --git a/src/test/java/com/github/dockerjava/core/command/EventsCmdImplTest.java b/src/test/java/com/github/dockerjava/core/command/EventsCmdImplTest.java
index eb490d7e..7da6a100 100644
--- a/src/test/java/com/github/dockerjava/core/command/EventsCmdImplTest.java
+++ b/src/test/java/com/github/dockerjava/core/command/EventsCmdImplTest.java
@@ -18,6 +18,7 @@ import com.github.dockerjava.api.DockerException;
 import com.github.dockerjava.api.command.CreateContainerResponse;
 import com.github.dockerjava.api.command.EventsCmd;
 import com.github.dockerjava.api.model.Event;
+import com.github.dockerjava.api.model.EventFilters;
 import com.github.dockerjava.client.AbstractDockerClientTest;
 import com.github.dockerjava.core.async.ResultCallbackTemplate;
 
@@ -94,6 +95,25 @@ public class EventsCmdImplTest extends AbstractDockerClientTest {
         assertTrue(zeroCount, "Received only: " + eventCallback.getEvents());
     }
 
+    @Test
+    public void testEventStreamingWithFilter() throws InterruptedException, IOException {
+        // Don't include other tests events
+        TimeUnit.SECONDS.sleep(1);
+
+        CountDownLatch countDownLatch = new CountDownLatch(1);
+        EventCallbackTest eventCallback = new EventCallbackTest(countDownLatch);
+
+        EventsCmd eventsCmd = dockerClient.eventsCmd(eventCallback).withFilters(new EventFilters().withEvent("start"));
+        eventsCmd.exec();
+
+        generateEvents();
+
+        boolean zeroCount = countDownLatch.await(10, TimeUnit.SECONDS);
+
+        eventCallback.close();
+        assertTrue(zeroCount, "Received only: " + eventCallback.getEvents());
+    }
+
     @Test
     public void testEventStreaming2() throws InterruptedException, IOException {
         // Don't include other tests events