Skip to content

Commit

Permalink
Refactored ListImageParams and ListContainerParams
Browse files Browse the repository at this point in the history
  • Loading branch information
johnflavin committed Mar 4, 2016
1 parent e54a9ff commit 11cfa3e
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 136 deletions.
67 changes: 55 additions & 12 deletions src/main/java/com/spotify/docker/client/DefaultDockerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.io.CharStreams;
import com.google.common.net.HostAndPort;

Expand Down Expand Up @@ -75,6 +76,7 @@
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URLEncoder;
Expand Down Expand Up @@ -362,25 +364,72 @@ public List<Container> listContainers(final ListContainersParam... params)
WebTarget resource = resource()
.path("containers").path("json");

final Map<String, List<String>> filters = newHashMap();
for (ListContainersParam param : params) {
resource = resource.queryParam(param.name(), param.value());
if (param instanceof ListContainersFilterParam) {
List<String> filterValueList;
if (filters.containsKey(param.name())) {
filterValueList = filters.get(param.name());
} else {
filterValueList = Lists.newArrayList();
}
filterValueList.add(param.value());
filters.put(param.name(), filterValueList);
} else {
try {
final String encodedName = URLEncoder.encode(param.name(), UTF_8.name());
final String encodedValue = URLEncoder.encode(param.value(), UTF_8.name());
resource = resource.queryParam(encodedName, encodedValue);
} catch (UnsupportedEncodingException e) {
throw new DockerException(e);
}
}
}

// If filters were specified, we must put them in a JSON object and pass them using the
// 'filters' query param like this: filters={"dangling":["true"]}
try {
if (!filters.isEmpty()) {
final StringWriter writer = new StringWriter();
final JsonGenerator generator = objectMapper().getFactory().createGenerator(writer);
generator.writeObject(filters);
generator.close();
// We must URL encode the string, otherwise Jersey chokes on the double-quotes in the json.
final String encoded = URLEncoder.encode(writer.toString(), UTF_8.name());
resource = resource.queryParam("filters", encoded);
}
} catch (IOException e) {
throw new DockerException(e);
}

return request(GET, CONTAINER_LIST, resource, resource.request(APPLICATION_JSON_TYPE));
}

@Override
public List<Image> listImages(ListImagesParam... params)
public List<Image> listImages(final ListImagesParam... params)
throws DockerException, InterruptedException {
WebTarget resource = resource()
.path("images").path("json");

final Map<String, String> filters = newHashMap();
final Map<String, List<String>> filters = newHashMap();
for (ListImagesParam param : params) {
if (param instanceof ListImagesFilterParam) {
filters.put(param.name(), param.value());
List<String> filterValueList;
if (filters.containsKey(param.name())) {
filterValueList = filters.get(param.name());
} else {
filterValueList = Lists.newArrayList();
}
filterValueList.add(param.value());
filters.put(param.name(), filterValueList);
} else {
resource = resource.queryParam(param.name(), param.value());
try {
final String encodedName = URLEncoder.encode(param.name(), UTF_8.name());
final String encodedValue = URLEncoder.encode(param.value(), UTF_8.name());
resource = resource.queryParam(encodedName, encodedValue);
} catch (UnsupportedEncodingException e) {
throw new DockerException(e);
}
}
}

Expand All @@ -390,13 +439,7 @@ public List<Image> listImages(ListImagesParam... params)
if (!filters.isEmpty()) {
final StringWriter writer = new StringWriter();
final JsonGenerator generator = objectMapper().getFactory().createGenerator(writer);
generator.writeStartObject();
for (Map.Entry<String, String> entry : filters.entrySet()) {
generator.writeArrayFieldStart(entry.getKey());
generator.writeString(entry.getValue());
generator.writeEndArray();
}
generator.writeEndObject();
generator.writeObject(filters);
generator.close();
// We must URL encode the string, otherwise Jersey chokes on the double-quotes in the json.
final String encoded = URLEncoder.encode(writer.toString(), UTF_8.name());
Expand Down
Loading

0 comments on commit 11cfa3e

Please sign in to comment.