X Tutup
Skip to content

Commit 163ee8f

Browse files
committed
Merge pull request #58 from yuxuanchiadm/master
Code clear and bug fix
2 parents 17c7a35 + 04c528e commit 163ee8f

File tree

4 files changed

+38
-24
lines changed

4 files changed

+38
-24
lines changed

src/main/java/com/github/dockerjava/core/DockerClientImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import com.github.dockerjava.api.command.*;
1212
import com.github.dockerjava.api.model.AuthConfig;
1313
import com.github.dockerjava.core.command.*;
14-
import com.github.dockerjava.jaxrs.DockerCmdExecFactoryImpl;
1514
import com.google.common.base.Preconditions;
1615

1716
/**

src/main/java/com/github/dockerjava/core/command/AbstrDockerCmd.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,11 @@
33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
55

6-
import com.github.dockerjava.api.BadRequestException;
7-
import com.github.dockerjava.api.ConflictException;
86
import com.github.dockerjava.api.DockerException;
9-
import com.github.dockerjava.api.InternalServerErrorException;
10-
import com.github.dockerjava.api.NotAcceptableException;
11-
import com.github.dockerjava.api.NotFoundException;
12-
import com.github.dockerjava.api.NotModifiedException;
137
import com.github.dockerjava.api.command.DockerCmd;
148
import com.github.dockerjava.api.command.DockerCmdExec;
15-
169
import com.google.common.base.Preconditions;
1710

18-
import javax.ws.rs.ClientErrorException;
19-
2011
public abstract class AbstrDockerCmd<CMD_T extends DockerCmd<RES_T>, RES_T> implements DockerCmd<RES_T> {
2112

2213
private final static Logger LOGGER = LoggerFactory.getLogger(AbstrDockerCmd.class);
@@ -28,14 +19,10 @@ public AbstrDockerCmd(DockerCmdExec<CMD_T, RES_T> execution) {
2819
this.execution = execution;
2920
}
3021

31-
@Override
22+
@Override
23+
@SuppressWarnings("unchecked")
3224
public RES_T exec() throws DockerException {
3325
LOGGER.debug("Cmd: {}", this);
3426
return execution.exec((CMD_T)this);
3527
}
36-
37-
protected DockerException toDockerException(ClientErrorException exception) {
38-
LOGGER.info("toDockerException");
39-
return new DockerException(exception.getMessage(), exception.getResponse().getStatus(), exception);
40-
}
4128
}

src/main/java/com/github/dockerjava/jaxrs/InspectContainerCmdExec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public InspectContainerCmdExec(WebTarget baseResource) {
2121

2222
@Override
2323
protected InspectContainerResponse execute(InspectContainerCmd command) {
24-
WebTarget webResource = getBaseResource().path(String.format("/containers/%s/json", command.getContainerId()));
24+
WebTarget webResource = getBaseResource().path("/containers/{id}/json").resolveTemplate("id", command.getContainerId());
2525

2626
LOGGER.debug("GET: {}", webResource);
2727
return webResource.request().accept(MediaType.APPLICATION_JSON).get(InspectContainerResponse.class);

src/main/java/com/github/dockerjava/jaxrs/util/ResponseStatusExceptionFilter.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.github.dockerjava.jaxrs.util;
22

3+
import java.io.EOFException;
34
import java.io.IOException;
5+
import java.nio.charset.Charset;
46

57
import javax.ws.rs.client.ClientRequestContext;
68
import javax.ws.rs.client.ClientResponseContext;
79
import javax.ws.rs.client.ClientResponseFilter;
10+
import javax.ws.rs.core.MediaType;
811

912
import org.apache.commons.io.IOUtils;
1013

@@ -46,20 +49,45 @@ public void filter(ClientRequestContext requestContext, ClientResponseContext re
4649
throw new NotAcceptableException(getBodyAsMessage(responseContext));
4750
case 409:
4851
throw new ConflictException(getBodyAsMessage(responseContext));
49-
case 500: {
50-
52+
case 500:
5153
throw new InternalServerErrorException(getBodyAsMessage(responseContext));
52-
}
5354
default:
5455
throw new DockerException(getBodyAsMessage(responseContext), status);
5556
}
5657
}
5758

5859
public String getBodyAsMessage(ClientResponseContext responseContext)
5960
throws IOException {
60-
byte[] buffer = new byte[1000];
61-
IOUtils.read(responseContext.getEntityStream(), buffer);
62-
String message = new String(buffer);
63-
return message;
61+
if (responseContext.hasEntity()) {
62+
int contentLength = responseContext.getLength();
63+
if (contentLength != -1) {
64+
byte[] buffer = new byte[contentLength];
65+
try {
66+
IOUtils.readFully(responseContext.getEntityStream(), buffer);
67+
}
68+
catch (EOFException e) {
69+
return null;
70+
}
71+
Charset charset = null;
72+
MediaType mediaType = responseContext.getMediaType();
73+
if (mediaType != null) {
74+
String charsetName = mediaType.getParameters().get("charset");
75+
if (charsetName != null) {
76+
try {
77+
charset = Charset.forName(charsetName);
78+
}
79+
catch (Exception e) {
80+
//Do noting...
81+
}
82+
}
83+
}
84+
if (charset == null) {
85+
charset = Charset.defaultCharset();
86+
}
87+
String message = new String(buffer, charset);
88+
return message;
89+
}
90+
}
91+
return null;
6492
}
6593
}

0 commit comments

Comments
 (0)
X Tutup