Skip to content

Commit

Permalink
Merge pull request spotify#349 from NrgXnat/master
Browse files Browse the repository at this point in the history
Refactored ListImagesParam and ListContainersParam
  • Loading branch information
davidxia committed Mar 4, 2016
2 parents e54a9ff + b5a40af commit 1bba0ca
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 149 deletions.
95 changes: 70 additions & 25 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,49 +364,92 @@ 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"]}. If filters is an empty map,
// urlEncodeFilters will return null and queryParam() will remove that query parameter.
resource = resource.queryParam("filters", urlEncodeFilters(filters));

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


/**
* Takes a map of filters and URL-encodes them. If the map is empty or an exception occurs,
* return null.
*
* @param filters A map of filters.
* @return String
* @throws DockerException
*/
private String urlEncodeFilters(final Map<String, List<String>> filters) throws DockerException {
final StringWriter writer = new StringWriter();
try (final JsonGenerator generator = objectMapper().getFactory().createGenerator(writer)) {
if (!filters.isEmpty()) {
generator.writeObject(filters);
generator.close();
// We must URL encode the string, otherwise Jersey chokes on the double-quotes in the json.
return URLEncoder.encode(writer.toString(), UTF_8.name());
}
} catch (IOException e) {
throw new DockerException(e);
}
return null;
}

@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);
}
}
}

// 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.writeStartObject();
for (Map.Entry<String, String> entry : filters.entrySet()) {
generator.writeArrayFieldStart(entry.getKey());
generator.writeString(entry.getValue());
generator.writeEndArray();
}
generator.writeEndObject();
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);
}
// 'filters' query param like this: filters={"dangling":["true"]}. If filters is an empty map,
// urlEncodeFilters will return null and queryParam() will remove that query parameter.
resource = resource.queryParam("filters", urlEncodeFilters(filters));

return request(GET, IMAGE_LIST, resource, resource.request(APPLICATION_JSON_TYPE));
}
Expand Down
Loading

0 comments on commit 1bba0ca

Please sign in to comment.