Skip to content

Commit 7f97462

Browse files
authored
pass request body as byte[] where possible (#1495)
* pass request body as `byte[]` where possible Helps with #1467 * Add missing content-type header
1 parent f6de60d commit 7f97462

File tree

5 files changed

+51
-26
lines changed

5 files changed

+51
-26
lines changed

docker-java-core/src/main/java/com/github/dockerjava/core/DefaultInvocationBuilder.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import com.github.dockerjava.transport.DockerHttpClient;
1818
import org.apache.commons.io.IOUtils;
1919

20-
import java.io.ByteArrayInputStream;
2120
import java.io.FilterInputStream;
2221
import java.io.IOException;
2322
import java.io.InputStream;
@@ -97,7 +96,7 @@ public InputStream post(Object entity) {
9796
DockerHttpClient.Request request = requestBuilder
9897
.method(DockerHttpClient.Request.Method.POST)
9998
.putHeader("content-type", "application/json")
100-
.body(encode(entity))
99+
.bodyBytes(encode(entity))
101100
.build();
102101

103102
DockerHttpClient.Response response = execute(request);
@@ -119,7 +118,7 @@ public <T> T post(Object entity, TypeReference<T> typeReference) {
119118
DockerHttpClient.Request request = requestBuilder
120119
.method(DockerHttpClient.Request.Method.POST)
121120
.putHeader("content-type", "application/json")
122-
.body(new ByteArrayInputStream(objectMapper.writeValueAsBytes(entity)))
121+
.bodyBytes(encode(entity))
123122
.build();
124123

125124
try (DockerHttpClient.Response response = execute(request)) {
@@ -132,11 +131,17 @@ public <T> T post(Object entity, TypeReference<T> typeReference) {
132131

133132
@Override
134133
public <T> void post(Object entity, TypeReference<T> typeReference, ResultCallback<T> resultCallback) {
135-
try {
136-
post(typeReference, resultCallback, new ByteArrayInputStream(objectMapper.writeValueAsBytes(entity)));
137-
} catch (JsonProcessingException e) {
138-
throw new RuntimeException(e);
139-
}
134+
DockerHttpClient.Request request = requestBuilder
135+
.method(DockerHttpClient.Request.Method.POST)
136+
.putHeader("content-type", "application/json")
137+
.bodyBytes(encode(entity))
138+
.build();
139+
140+
executeAndStream(
141+
request,
142+
resultCallback,
143+
new JsonSink<>(typeReference, resultCallback)
144+
);
140145
}
141146

142147
@Override
@@ -150,17 +155,12 @@ public <T> T post(TypeReference<T> typeReference, InputStream body) {
150155

151156
@Override
152157
public void post(Object entity, InputStream stdin, ResultCallback<Frame> resultCallback) {
153-
final DockerHttpClient.Request request;
154-
try {
155-
request = requestBuilder
156-
.method(DockerHttpClient.Request.Method.POST)
157-
.putHeader("content-type", "application/json")
158-
.body(new ByteArrayInputStream(objectMapper.writeValueAsBytes(entity)))
159-
.hijackedInput(stdin)
160-
.build();
161-
} catch (JsonProcessingException e) {
162-
throw new RuntimeException(e);
163-
}
158+
DockerHttpClient.Request request = requestBuilder
159+
.method(DockerHttpClient.Request.Method.POST)
160+
.putHeader("content-type", "application/json")
161+
.bodyBytes(encode(entity))
162+
.hijackedInput(stdin)
163+
.build();
164164

165165
executeAndStream(
166166
request,
@@ -283,13 +283,13 @@ protected <T> void executeAndStream(
283283
thread.start();
284284
}
285285

286-
private InputStream encode(Object entity) {
286+
private byte[] encode(Object entity) {
287287
if (entity == null) {
288288
return null;
289289
}
290290

291291
try {
292-
return new ByteArrayInputStream(objectMapper.writeValueAsBytes(entity));
292+
return objectMapper.writeValueAsBytes(entity);
293293
} catch (JsonProcessingException e) {
294294
throw new RuntimeException(e);
295295
}

docker-java-transport-httpclient5/src/main/java/com/github/dockerjava/httpclient5/ApacheDockerHttpClientImpl.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.hc.core5.http.config.RegistryBuilder;
2222
import org.apache.hc.core5.http.impl.DefaultContentLengthStrategy;
2323
import org.apache.hc.core5.http.impl.io.EmptyInputStream;
24+
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
2425
import org.apache.hc.core5.http.io.entity.InputStreamEntity;
2526
import org.apache.hc.core5.http.protocol.BasicHttpContext;
2627
import org.apache.hc.core5.http.protocol.HttpContext;
@@ -137,9 +138,14 @@ public Response execute(Request request) {
137138

138139
request.headers().forEach(httpUriRequest::addHeader);
139140

140-
InputStream body = request.body();
141-
if (body != null) {
142-
httpUriRequest.setEntity(new InputStreamEntity(body, null));
141+
byte[] bodyBytes = request.bodyBytes();
142+
if (bodyBytes != null) {
143+
httpUriRequest.setEntity(new ByteArrayEntity(bodyBytes, null));
144+
} else {
145+
InputStream body = request.body();
146+
if (body != null) {
147+
httpUriRequest.setEntity(new InputStreamEntity(body, null));
148+
}
143149
}
144150

145151
if (request.hijackedInput() != null) {

docker-java-transport-jersey/src/main/java/com/github/dockerjava/jaxrs/JerseyDockerHttpClient.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,11 @@ public Response execute(Request request) {
304304
}
305305
}
306306

307-
private Entity<InputStream> toEntity(Request request) {
307+
private Entity<?> toEntity(Request request) {
308+
byte[] bodyBytes = request.bodyBytes();
309+
if (bodyBytes != null) {
310+
return Entity.json(bodyBytes);
311+
}
308312
InputStream body = request.body();
309313
if (body != null) {
310314
return Entity.entity(body, MediaType.APPLICATION_JSON_TYPE);

docker-java-transport-okhttp/src/main/java/com/github/dockerjava/okhttp/OkDockerHttpClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ private OkDockerHttpClient(
177177
}
178178

179179
private RequestBody toRequestBody(Request request) {
180+
byte[] bodyBytes = request.bodyBytes();
181+
if (bodyBytes != null) {
182+
return RequestBody.create(null, bodyBytes);
183+
}
184+
180185
InputStream body = request.body();
181186
if (body != null) {
182187
return new RequestBody() {

docker-java-transport/src/main/java/com/github/dockerjava/transport/DockerHttpClient.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import javax.annotation.Nonnull;
66
import javax.annotation.Nullable;
7+
import java.io.ByteArrayInputStream;
78
import java.io.Closeable;
89
import java.io.InputStream;
910
import java.util.List;
@@ -70,7 +71,16 @@ public static Builder builder() {
7071
public abstract String path();
7172

7273
@Nullable
73-
public abstract InputStream body();
74+
@Value.Default
75+
public InputStream body() {
76+
byte[] bodyBytes = bodyBytes();
77+
return bodyBytes != null
78+
? new ByteArrayInputStream(bodyBytes)
79+
: null;
80+
}
81+
82+
@Nullable
83+
public abstract byte[] bodyBytes();
7484

7585
@Nullable
7686
public abstract InputStream hijackedInput();

0 commit comments

Comments
 (0)