Skip to content

Commit fe10593

Browse files
author
Marcus Linke
committed
Fix issue docker-java#8
1 parent 6b042a9 commit fe10593

18 files changed

+559
-371
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
import org.apache.http.conn.ssl.SSLSocketFactory;
1818
import org.apache.http.impl.client.DefaultHttpClient;
1919
import org.apache.http.impl.conn.PoolingClientConnectionManager;
20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
2220

2321
import com.github.dockerjava.client.command.AbstrDockerCmd;
2422
import com.github.dockerjava.client.command.AuthCmd;
@@ -34,6 +32,7 @@
3432
import com.github.dockerjava.client.command.KillContainerCmd;
3533
import com.github.dockerjava.client.command.ListContainersCmd;
3634
import com.github.dockerjava.client.command.ListImagesCmd;
35+
import com.github.dockerjava.client.command.AttachContainerCmd;
3736
import com.github.dockerjava.client.command.LogContainerCmd;
3837
import com.github.dockerjava.client.command.PullImageCmd;
3938
import com.github.dockerjava.client.command.PushImageCmd;
@@ -287,6 +286,11 @@ public WaitContainerCmd waitContainerCmd(String containerId) {
287286
return new WaitContainerCmd(containerId).withBaseResource(baseResource);
288287
}
289288

289+
public AttachContainerCmd attachContainerCmd(String containerId) {
290+
return new AttachContainerCmd(containerId).withBaseResource(baseResource);
291+
}
292+
293+
290294
public LogContainerCmd logContainerCmd(String containerId) {
291295
return new LogContainerCmd(containerId).withBaseResource(baseResource);
292296
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.github.dockerjava.client.command;
2+
3+
import javax.ws.rs.core.MediaType;
4+
import javax.ws.rs.core.MultivaluedMap;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import com.github.dockerjava.client.DockerException;
10+
import com.github.dockerjava.client.NotFoundException;
11+
import com.google.common.base.Preconditions;
12+
import com.sun.jersey.api.client.ClientResponse;
13+
import com.sun.jersey.api.client.UniformInterfaceException;
14+
import com.sun.jersey.api.client.WebResource;
15+
import com.sun.jersey.core.util.MultivaluedMapImpl;
16+
17+
/**
18+
* Attach to container
19+
*
20+
* @param logs - true or false, includes logs. Defaults to false.
21+
*
22+
* @param followStream
23+
* - true or false, return stream. Defaults to false.
24+
* @param stdout
25+
* - true or false, includes stdout log. Defaults to false.
26+
* @param stderr
27+
* - true or false, includes stderr log. Defaults to false.
28+
* @param timestamps
29+
* - true or false, if true, print timestamps for every log line.
30+
* Defaults to false.
31+
*/
32+
public class AttachContainerCmd extends AbstrDockerCmd<AttachContainerCmd, ClientResponse> {
33+
34+
private static final Logger LOGGER = LoggerFactory
35+
.getLogger(AttachContainerCmd.class);
36+
37+
private String containerId;
38+
39+
private boolean logs, followStream, timestamps, stdout, stderr;
40+
41+
public AttachContainerCmd(String containerId) {
42+
withContainerId(containerId);
43+
}
44+
45+
public AttachContainerCmd withContainerId(String containerId) {
46+
Preconditions.checkNotNull(containerId, "containerId was not specified");
47+
this.containerId = containerId;
48+
return this;
49+
}
50+
51+
public AttachContainerCmd withFollowStream() {
52+
return withFollowStream(true);
53+
}
54+
55+
public AttachContainerCmd withFollowStream(boolean followStream) {
56+
this.followStream = followStream;
57+
return this;
58+
}
59+
60+
public AttachContainerCmd withTimestamps(boolean timestamps) {
61+
this.timestamps = timestamps;
62+
return this;
63+
}
64+
65+
public AttachContainerCmd withStdOut() {
66+
return withStdOut(true);
67+
}
68+
69+
public AttachContainerCmd withStdOut(boolean stdout) {
70+
this.stdout = stdout;
71+
return this;
72+
}
73+
74+
public AttachContainerCmd withStdErr() {
75+
return withStdErr(true);
76+
}
77+
78+
public AttachContainerCmd withStdErr(boolean stderr) {
79+
this.stderr = stderr;
80+
return this;
81+
}
82+
83+
public AttachContainerCmd withLogs(boolean logs) {
84+
this.logs = logs;
85+
return this;
86+
}
87+
88+
protected ClientResponse impl() throws DockerException {
89+
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
90+
params.add("logs", logs ? "1" : "0");
91+
params.add("timestamps", timestamps ? "1" : "0");
92+
params.add("stdout", stdout ? "1" : "0");
93+
params.add("stderr", stderr ? "1" : "0");
94+
params.add("follow", followStream ? "1" : "0");
95+
96+
WebResource webResource = baseResource.path(
97+
String.format("/containers/%s/attach", containerId))
98+
.queryParams(params);
99+
100+
try {
101+
LOGGER.trace("POST: {}", webResource);
102+
return webResource.accept(MediaType.APPLICATION_OCTET_STREAM_TYPE)
103+
.post(ClientResponse.class, params);
104+
} catch (UniformInterfaceException exception) {
105+
if (exception.getResponse().getStatus() == 400) {
106+
throw new DockerException("bad parameter");
107+
} else if (exception.getResponse().getStatus() == 404) {
108+
throw new NotFoundException(String.format(
109+
"No such container %s", containerId));
110+
} else if (exception.getResponse().getStatus() == 500) {
111+
throw new DockerException("Server error", exception);
112+
} else {
113+
throw new DockerException(exception);
114+
}
115+
}
116+
}
117+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class BuildImgCmd extends AbstrDockerCmd<BuildImgCmd, ClientResponse> {
3535

3636
private File dockerFolder;
3737
private String tag;
38-
private boolean noCache= false;
38+
private boolean noCache;
3939

4040

4141
public BuildImgCmd(File dockerFolder) {

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@
1717
/**
1818
* Get container logs
1919
*
20-
* @param followStream - true or false, return stream. Defaults to false.
21-
* @param stdout - true or false, includes stdout log. Defaults to false.
22-
* @param stderr - true or false, includes stderr log. Defaults to false.
23-
* @param timestamps - true or false, if true, print timestamps for every log line. Defaults to false.
20+
* @param followStream
21+
* - true or false, return stream. Defaults to false.
22+
* @param stdout
23+
* - true or false, includes stdout log. Defaults to false.
24+
* @param stderr
25+
* - true or false, includes stderr log. Defaults to false.
26+
* @param timestamps
27+
* - true or false, if true, print timestamps for every log line.
28+
* Defaults to false.
2429
*/
2530
public class LogContainerCmd extends AbstrDockerCmd<LogContainerCmd, ClientResponse> {
2631

27-
private static final Logger LOGGER = LoggerFactory.getLogger(LogContainerCmd.class);
32+
private static final Logger LOGGER = LoggerFactory
33+
.getLogger(LogContainerCmd.class);
2834

2935
private String containerId;
3036

@@ -39,7 +45,7 @@ public LogContainerCmd withContainerId(String containerId) {
3945
this.containerId = containerId;
4046
return this;
4147
}
42-
48+
4349
public LogContainerCmd withFollowStream() {
4450
return withFollowStream(true);
4551
}
@@ -53,7 +59,7 @@ public LogContainerCmd withTimestamps(boolean timestamps) {
5359
this.timestamps = timestamps;
5460
return this;
5561
}
56-
62+
5763
public LogContainerCmd withStdOut() {
5864
return withStdOut(true);
5965
}
@@ -62,7 +68,7 @@ public LogContainerCmd withStdOut(boolean stdout) {
6268
this.stdout = stdout;
6369
return this;
6470
}
65-
71+
6672
public LogContainerCmd withStdErr() {
6773
return withStdErr(true);
6874
}
@@ -72,20 +78,23 @@ public LogContainerCmd withStdErr(boolean stderr) {
7278
return this;
7379
}
7480

81+
82+
7583
protected ClientResponse impl() throws DockerException {
7684
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
77-
params.add("logs", "1");
7885
params.add("timestamps", timestamps ? "1" : "0");
7986
params.add("stdout", stdout ? "1" : "0");
8087
params.add("stderr", stderr ? "1" : "0");
81-
params.add("follow", followStream ? "1" : "0"); // this parameter keeps stream open indefinitely
88+
params.add("follow", followStream ? "1" : "0");
8289

83-
WebResource webResource = baseResource.path(String.format("/containers/%s/attach", containerId))
90+
WebResource webResource = baseResource.path(
91+
String.format("/containers/%s/logs", containerId))
8492
.queryParams(params);
8593

8694
try {
87-
LOGGER.trace("POST: {}", webResource);
88-
return webResource.accept(MediaType.APPLICATION_OCTET_STREAM_TYPE).post(ClientResponse.class, params);
95+
LOGGER.trace("GET: {}", webResource);
96+
return webResource.accept(MediaType.APPLICATION_OCTET_STREAM_TYPE)
97+
.get(ClientResponse.class);
8998
} catch (UniformInterfaceException exception) {
9099
if (exception.getResponse().getStatus() == 400) {
91100
throw new DockerException("bad parameter");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import com.github.dockerjava.client.DockerException;
99
import com.github.dockerjava.client.NotFoundException;
1010
import com.github.dockerjava.client.model.LxcConf;
11-
import com.github.dockerjava.client.model.StartContainerConfig;
1211
import com.github.dockerjava.client.model.Ports;
12+
import com.github.dockerjava.client.model.StartContainerConfig;
1313
import com.google.common.base.Preconditions;
1414
import com.sun.jersey.api.client.UniformInterfaceException;
1515
import com.sun.jersey.api.client.WebResource;

src/main/java/com/github/dockerjava/client/model/Container.java

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,8 @@ public class Container {
2828
@JsonProperty("Status")
2929
private String status;
3030

31-
/* Example:
32-
"Ports": {
33-
"22/tcp": [
34-
{
35-
"HostIp": "0.0.0.0",
36-
"HostPort": "8022"
37-
}
38-
]
39-
}
40-
*/
41-
4231
@JsonProperty("Ports")
43-
public Ports ports;
32+
public Port[] ports;
4433

4534
@JsonProperty("SizeRw")
4635
private int size;
@@ -71,11 +60,11 @@ public String getStatus() {
7160
return status;
7261
}
7362

74-
public Ports getPorts() {
63+
public Port[] getPorts() {
7564
return ports;
7665
}
7766

78-
public void setPorts(Ports ports) {
67+
public void setPorts(Port[] ports) {
7968
this.ports = ports;
8069
}
8170

@@ -91,37 +80,6 @@ public String[] getNames() {
9180
return names;
9281
}
9382

94-
public void setId(String id) {
95-
this.id = id;
96-
}
97-
98-
public void setCommand(String command) {
99-
this.command = command;
100-
}
101-
102-
public void setImage(String image) {
103-
this.image = image;
104-
}
105-
106-
public void setCreated(long created) {
107-
this.created = created;
108-
}
109-
110-
public void setStatus(String status) {
111-
this.status = status;
112-
}
113-
114-
public void setSize(int size) {
115-
this.size = size;
116-
}
117-
118-
public void setSizeRootFs(int sizeRootFs) {
119-
this.sizeRootFs = sizeRootFs;
120-
}
121-
122-
public void setNames(String[] names) {
123-
this.names = names;
124-
}
12583

12684
@Override
12785
public String toString() {
@@ -131,10 +89,52 @@ public String toString() {
13189
", image='" + image + '\'' +
13290
", created=" + created +
13391
", status='" + status + '\'' +
134-
", ports=" + ports +
92+
", ports=" + Arrays.toString(ports) +
13593
", size=" + size +
13694
", sizeRootFs=" + sizeRootFs +
13795
", names=" + Arrays.toString(names) +
13896
'}';
13997
}
98+
99+
@JsonIgnoreProperties(ignoreUnknown = true)
100+
public static class Port {
101+
102+
@JsonProperty("IP")
103+
private String ip;
104+
105+
@JsonProperty("PrivatePort")
106+
private Integer privatePort;
107+
108+
@JsonProperty("PublicPort")
109+
private Integer publicPort;
110+
111+
@JsonProperty("Type")
112+
private String type;
113+
114+
public String getIp() {
115+
return ip;
116+
}
117+
118+
public Integer getPrivatePort() {
119+
return privatePort;
120+
}
121+
122+
public Integer getPublicPort() {
123+
return publicPort;
124+
}
125+
126+
public String getType() {
127+
return type;
128+
}
129+
130+
@Override
131+
public String toString() {
132+
return "Port{" +
133+
"IP='" + ip + '\'' +
134+
", privatePort='" + privatePort + '\'' +
135+
", publicPort='" + publicPort + '\'' +
136+
", type='" + type + '\'' +
137+
'}';
138+
}
139+
}
140140
}

0 commit comments

Comments
 (0)