Skip to content

Commit ffe3301

Browse files
author
Sean Fitts
committed
Some tweaks for use in gradle-docker.
- Change the default Docker port to 2375 to match current Docker version. - Add toString() to all commands which prints out a form similar to the corresponding Docker command line. Useful in tracing. - Create client logging filter variant which allows us to avoid logging requests whose content type is likely to cause issues for both the Windows and OSX consoles. This is an adaptation of a change from a while back (alexec@887a112) which seems to have be lost in the move.
1 parent 1f84a6e commit ffe3301

29 files changed

+257
-31
lines changed

src/main/java/com/github/dockerjava/client/DockerClient.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.dockerjava.client;
22

3-
import static org.apache.commons.io.IOUtils.closeQuietly;
3+
import static org.apache.commons.io.IOUtils.*;
44

55
import java.io.File;
66
import java.io.IOException;
@@ -54,7 +54,6 @@
5454
import com.sun.jersey.api.client.WebResource;
5555
import com.sun.jersey.api.client.config.ClientConfig;
5656
import com.sun.jersey.api.client.config.DefaultClientConfig;
57-
import com.sun.jersey.api.client.filter.LoggingFilter;
5857
import com.sun.jersey.client.apache4.ApacheHttpClient4;
5958
import com.sun.jersey.client.apache4.ApacheHttpClient4Handler;
6059

@@ -63,7 +62,7 @@
6362
*/
6463
public class DockerClient {
6564

66-
private Client client;
65+
private Client client;
6766
private WebResource baseResource;
6867
private AuthConfig authConfig;
6968

@@ -110,7 +109,7 @@ private DockerClient(Config config) {
110109
// client = new UnixSocketClient(clientConfig);
111110

112111
client.addFilter(new JsonClientFilter());
113-
client.addFilter(new LoggingFilter());
112+
client.addFilter(new SelectiveLoggingFilter());
114113

115114
baseResource = client.resource(config.url + "/v" + config.version);
116115
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.github.dockerjava.client;
2+
3+
import java.util.Set;
4+
5+
import javax.ws.rs.core.HttpHeaders;
6+
import javax.ws.rs.core.MediaType;
7+
8+
import com.google.common.collect.ImmutableSet;
9+
import com.sun.jersey.api.client.ClientHandlerException;
10+
import com.sun.jersey.api.client.ClientRequest;
11+
import com.sun.jersey.api.client.ClientResponse;
12+
import com.sun.jersey.api.client.filter.LoggingFilter;
13+
14+
/**
15+
* A version of the logging filter that will avoid trying to log entities which can cause
16+
* issues with the console.
17+
*
18+
* @author sfitts
19+
*
20+
*/
21+
public class SelectiveLoggingFilter extends LoggingFilter {
22+
23+
private static final Set<String> SKIPPED_CONTENT = ImmutableSet.<String>builder()
24+
.add(MediaType.APPLICATION_OCTET_STREAM)
25+
.add("application/tar")
26+
.build();
27+
28+
@Override
29+
public ClientResponse handle(ClientRequest request) throws ClientHandlerException {
30+
// Unless the content type is in the list of those we want to ellide, then just have
31+
// our super-class handle things.
32+
MediaType contentType = (MediaType) request.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
33+
if (SKIPPED_CONTENT.contains(contentType.toString())) {
34+
// Skip logging this.
35+
//
36+
// N.B. -- I'd actually love to reproduce (or better yet just use) the logging code from
37+
// our super-class. However, everything is private (so we can't use it) and the code
38+
// is under a modified GPL which means we can't pull it into an ASL project. Right now
39+
// I don't have the energy to do a clean implementation.
40+
return getNext().handle(request);
41+
}
42+
43+
// Do what we normally would
44+
return super.handle(request);
45+
}
46+
47+
}

src/main/java/com/github/dockerjava/client/command/AuthCmd.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ protected Void impl() throws DockerException {
3434
throw new DockerException(e);
3535
}
3636
}
37+
38+
@Override
39+
public String toString() {
40+
return "authenticate using " + this.authConfig;
41+
}
3742
}

src/main/java/com/github/dockerjava/client/command/BuildImgCmd.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ public BuildImgCmd withNoCache(boolean noCache) {
6161
return this;
6262
}
6363

64+
@Override
65+
public String toString() {
66+
return new StringBuilder("build ")
67+
.append(tag != null ? "-t " + tag + " " : "")
68+
.append(noCache ? "--nocache=true " : "")
69+
.append(dockerFolder != null ? dockerFolder.getPath() : "-")
70+
.toString();
71+
}
72+
6473
protected ClientResponse impl() {
6574
if (tarInputStream == null) {
6675
File dockerFolderTar = buildDockerFolderTar();

src/main/java/com/github/dockerjava/client/command/CommitCmd.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,17 @@ public CommitCmd withRun(String run) {
6767
return this;
6868
}
6969

70-
70+
@Override
71+
public String toString() {
72+
return new StringBuilder("commit ")
73+
.append(commitConfig.getAuthor() != null ? "--author " + commitConfig.getAuthor() + " " : "")
74+
.append(commitConfig.getMessage() != null ? "--message " + commitConfig.getMessage() + " " : "")
75+
.append(commitConfig.getContainerId())
76+
.append(commitConfig.getRepo() != null ? " " + commitConfig.getRepo() + ":" : " ")
77+
.append(commitConfig.getTag() != null ? commitConfig.getTag() : "")
78+
.toString();
79+
}
80+
7181
private void checkCommitConfig(CommitConfig commitConfig) {
7282
Preconditions.checkNotNull(commitConfig, "CommitConfig was not specified");
7383
}

src/main/java/com/github/dockerjava/client/command/ContainerDiffCmd.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@ public ContainerDiffCmd withContainerId(String containerId) {
3737
return this;
3838
}
3939

40-
protected List<ChangeLog> impl() throws DockerException {
40+
@Override
41+
public String toString() {
42+
return new StringBuilder("diff ")
43+
.append(containerId)
44+
.toString();
45+
}
46+
47+
protected List<ChangeLog> impl() throws DockerException {
4148
WebResource webResource = baseResource.path(String.format("/containers/%s/changes", containerId));
4249

4350
try {

src/main/java/com/github/dockerjava/client/command/CopyFileFromContainerCmd.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ public CopyFileFromContainerCmd withResource(String resource) {
4040
return this;
4141
}
4242

43+
@Override
44+
public String toString() {
45+
return new StringBuilder("cp ")
46+
.append(containerId)
47+
.append(":")
48+
.append(resource)
49+
.toString();
50+
}
51+
4352
protected ClientResponse impl() throws DockerException {
4453
CopyConfig copyConfig = new CopyConfig();
4554
copyConfig.setResource(resource);

src/main/java/com/github/dockerjava/client/command/CreateContainerCmd.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ public CreateContainerCmd withExposedPorts(ExposedPort... exposedPorts) {
8181
return this;
8282
}
8383

84+
@Override
85+
public String toString() {
86+
return new StringBuilder("create container ")
87+
.append(name != null ? "name=" + name + " " : "")
88+
.append(containerCreateConfig)
89+
.toString();
90+
}
91+
8492
protected ContainerCreateResponse impl() {
8593
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
8694
if (name != null) {

src/main/java/com/github/dockerjava/client/command/ImportImageCmd.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ public ImportImageCmd withTag(String tag) {
6262
return this;
6363
}
6464

65+
@Override
66+
public String toString() {
67+
return new StringBuilder("import - ")
68+
.append(repository != null ? repository + ":" : "")
69+
.append(tag != null ? tag : "")
70+
.toString();
71+
}
72+
6573
protected ImageCreateResponse impl() {
6674
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
6775
params.add("repo", repository);

src/main/java/com/github/dockerjava/client/command/InfoCmd.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212

1313

1414
/**
15-
*
16-
*
17-
*
15+
* Return Docker server info
1816
*/
1917
public class InfoCmd extends AbstrDockerCmd<InfoCmd, Info> {
2018

2119
private static final Logger LOGGER = LoggerFactory.getLogger(InfoCmd.class);
2220

23-
21+
@Override
22+
public String toString() {
23+
return "info";
24+
}
25+
2426
protected Info impl() throws DockerException {
2527
WebResource webResource = baseResource.path("/info");
2628

0 commit comments

Comments
 (0)