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
10 changes: 10 additions & 0 deletions src/main/java/com/amihaiemil/docker/Images.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
* @since 0.0.1
* @todo #98:30min Continue implementing the rest of the operations for the
* Images interface. See the docs referenced above for more details.
* @todo #144:30min Add the filter(Map<String, String>) method which will filter
* the given instance of Images. For instance:
* <pre>
* final Images imgs = docker.imageS();//all listed images.
* final Images filtered = imgs.filter(...);
* final Images again = filtered.filter(...); //respects both filters.
* </pre>
* @todo #144:30min Add the save() method, which will save the given Images,
* an InputStream representing the created tarball will be created (see method
* "Export several images" from the docs.
*/
public interface Images extends Iterable<Image> {

Expand Down
71 changes: 71 additions & 0 deletions src/main/java/com/amihaiemil/docker/ListedImages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright (c) 2018, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of docker-java-api nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.amihaiemil.docker;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import java.net.URI;
import java.util.Iterator;

/**
* Listed images, which may have a filter applied.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.3
* @todo #144:30min Finish implementation here, add a Map to this class, that
* would hold the actual filters and apply them when making the call in the
* iterator() method. Also, more ctors should be available, at least one with
* filters and one without filters.
*/
final class ListedImages extends RtImages {

/**
* Ctor.
* @param client The http client.
* @param uri The URI for this Images API.
* @param dkr The docker entry point.
* @checkstyle ParameterNumber (10 lines)
*/
ListedImages(final HttpClient client, final URI uri, final Docker dkr) {
super(client, uri, dkr);
}

@Override
public Iterator<Image> iterator() {
return new ResourcesIterator<>(
super.client(),
new HttpGet(super.baseUri().toString().concat("/json")),
img -> new RtImage(
img,
super.client(),
URI.create(
super.baseUri().toString() + "/" + img.getString("Id")
),
super.docker()
)
);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/amihaiemil/docker/RtDocker.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public final Containers containers() {

@Override
public final Images images() {
return new RtImages(
return new ListedImages(
this.client,
URI.create(this.baseUri.toString() + "/images"),
this
Expand Down
40 changes: 19 additions & 21 deletions src/main/java/com/amihaiemil/docker/RtImages.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.Iterator;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;

import javax.json.Json;
Expand All @@ -42,14 +40,14 @@
* @version $Id$
* @since 0.0.1
*/
final class RtImages implements Images {
abstract class RtImages implements Images {
/**
* Apache HttpClient which sends the requests.
*/
private final HttpClient client;

/**
* Base URI.
* Base URI for Images API.
*/
private final URI baseUri;

Expand All @@ -63,6 +61,7 @@ final class RtImages implements Images {
* @param client The http client.
* @param uri The URI for this Images API.
* @param dkr The docker entry point.
* @checkstyle ParameterNumber (10 lines)
*/
RtImages(final HttpClient client, final URI uri, final Docker dkr) {
this.client = client;
Expand Down Expand Up @@ -123,25 +122,24 @@ public void prune() throws IOException, UnexpectedResponseException {
}
}

@Override
public Iterator<Image> iterator() {
return new ResourcesIterator<>(
this.client,
new HttpGet(this.baseUri.toString().concat("/json")),
json-> new RtImage(
json,
this.client,
URI.create(
this.baseUri.toString() + "/" + json.getString("Id")
),
this.docker
)
);
}

@Override
public Docker docker() {
return this.docker;
}


/**
* Get the (protected) HttpClient for subclasses.
* @return HttpClient.
*/
HttpClient client() {
return this.client;
}

/**
* Get the (protected) base URI for subclasses.
* @return URI.
*/
URI baseUri() {
return this.baseUri;
}
}
18 changes: 9 additions & 9 deletions src/test/java/com/amihaiemil/docker/RtImagesTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public final class RtImagesTestCase {
@Test
public void iteratesImages() {
final AtomicInteger count = new AtomicInteger();
new RtImages(
new ListedImages(
new AssertRequest(
new Response(
HttpStatus.SC_OK,
Expand Down Expand Up @@ -87,7 +87,7 @@ public void iteratesImages() {
@Test
public void iteratesZeroImages() throws Exception {
final AtomicInteger count = new AtomicInteger();
new RtImages(
new ListedImages(
new AssertRequest(
new Response(
HttpStatus.SC_OK,
Expand All @@ -109,7 +109,7 @@ public void iteratesZeroImages() throws Exception {
*/
@Test(expected = UnexpectedResponseException.class)
public void iterateFailsIfResponseIs500() throws Exception {
new RtImages(
new ListedImages(
new AssertRequest(
new Response(HttpStatus.SC_INTERNAL_SERVER_ERROR)
),
Expand All @@ -127,7 +127,7 @@ public void iterateFailsIfResponseIs500() throws Exception {
*/
@Test
public void createSetsGivenParameters() throws Exception {
new RtImages(
new ListedImages(
new AssertRequest(
new Response(HttpStatus.SC_OK),
new Condition(
Expand All @@ -154,7 +154,7 @@ public void createSetsGivenParameters() throws Exception {
*/
@Test(expected = UnexpectedResponseException.class)
public void createErrorOnStatus404() throws Exception {
new RtImages(
new ListedImages(
new AssertRequest(
new Response(HttpStatus.SC_NOT_FOUND)
),
Expand All @@ -170,7 +170,7 @@ public void createErrorOnStatus404() throws Exception {
*/
@Test(expected = UnexpectedResponseException.class)
public void createErrorOnStatus500() throws Exception {
new RtImages(
new ListedImages(
new AssertRequest(
new Response(HttpStatus.SC_INTERNAL_SERVER_ERROR)
),
Expand All @@ -186,7 +186,7 @@ public void createErrorOnStatus500() throws Exception {
*/
@Test
public void prunesOk() throws Exception {
new RtImages(
new ListedImages(
new AssertRequest(
new Response(HttpStatus.SC_OK),
new Condition(
Expand All @@ -211,7 +211,7 @@ public void prunesOk() throws Exception {
*/
@Test(expected = UnexpectedResponseException.class)
public void pruneThrowsErrorOnResponse500() throws Exception {
new RtImages(
new ListedImages(
new AssertRequest(
new Response(HttpStatus.SC_INTERNAL_SERVER_ERROR)
),
Expand All @@ -226,7 +226,7 @@ public void pruneThrowsErrorOnResponse500() throws Exception {
@Test
public void returnsDocker() {
MatcherAssert.assertThat(
new RtImages(
new ListedImages(
new AssertRequest(
new Response(
HttpStatus.SC_OK,
Expand Down