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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.github.dockerjava.transport.DockerHttpClient;
import org.apache.commons.io.IOUtils;

import java.io.ByteArrayInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -97,7 +96,7 @@ public InputStream post(Object entity) {
DockerHttpClient.Request request = requestBuilder
.method(DockerHttpClient.Request.Method.POST)
.putHeader("content-type", "application/json")
.body(encode(entity))
.bodyBytes(encode(entity))
.build();

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

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

@Override
public <T> void post(Object entity, TypeReference<T> typeReference, ResultCallback<T> resultCallback) {
try {
post(typeReference, resultCallback, new ByteArrayInputStream(objectMapper.writeValueAsBytes(entity)));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
DockerHttpClient.Request request = requestBuilder
.method(DockerHttpClient.Request.Method.POST)
.putHeader("content-type", "application/json")
.bodyBytes(encode(entity))
.build();

executeAndStream(
request,
resultCallback,
new JsonSink<>(typeReference, resultCallback)
);
}

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

@Override
public void post(Object entity, InputStream stdin, ResultCallback<Frame> resultCallback) {
final DockerHttpClient.Request request;
try {
request = requestBuilder
.method(DockerHttpClient.Request.Method.POST)
.putHeader("content-type", "application/json")
.body(new ByteArrayInputStream(objectMapper.writeValueAsBytes(entity)))
.hijackedInput(stdin)
.build();
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
DockerHttpClient.Request request = requestBuilder
.method(DockerHttpClient.Request.Method.POST)
.putHeader("content-type", "application/json")
.bodyBytes(encode(entity))
.hijackedInput(stdin)
.build();

executeAndStream(
request,
Expand Down Expand Up @@ -283,13 +283,13 @@ protected <T> void executeAndStream(
thread.start();
}

private InputStream encode(Object entity) {
private byte[] encode(Object entity) {
if (entity == null) {
return null;
}

try {
return new ByteArrayInputStream(objectMapper.writeValueAsBytes(entity));
return objectMapper.writeValueAsBytes(entity);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.hc.core5.http.config.RegistryBuilder;
import org.apache.hc.core5.http.impl.DefaultContentLengthStrategy;
import org.apache.hc.core5.http.impl.io.EmptyInputStream;
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
import org.apache.hc.core5.http.io.entity.InputStreamEntity;
import org.apache.hc.core5.http.protocol.BasicHttpContext;
import org.apache.hc.core5.http.protocol.HttpContext;
Expand Down Expand Up @@ -134,9 +135,14 @@ public Response execute(Request request) {

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

InputStream body = request.body();
if (body != null) {
httpUriRequest.setEntity(new InputStreamEntity(body, null));
byte[] bodyBytes = request.bodyBytes();
if (bodyBytes != null) {
httpUriRequest.setEntity(new ByteArrayEntity(bodyBytes, null));
} else {
InputStream body = request.body();
if (body != null) {
httpUriRequest.setEntity(new InputStreamEntity(body, null));
}
}

if (request.hijackedInput() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,11 @@ public Response execute(Request request) {
}
}

private Entity<InputStream> toEntity(Request request) {
private Entity<?> toEntity(Request request) {
byte[] bodyBytes = request.bodyBytes();
if (bodyBytes != null) {
return Entity.json(bodyBytes);
}
InputStream body = request.body();
if (body != null) {
return Entity.entity(body, MediaType.APPLICATION_JSON_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ private OkDockerHttpClient(
}

private RequestBody toRequestBody(Request request) {
byte[] bodyBytes = request.bodyBytes();
if (bodyBytes != null) {
return RequestBody.create(null, bodyBytes);
}

InputStream body = request.body();
if (body != null) {
return new RequestBody() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.InputStream;
import java.util.List;
Expand Down Expand Up @@ -70,7 +71,16 @@ public static Builder builder() {
public abstract String path();

@Nullable
public abstract InputStream body();
@Value.Default
public InputStream body() {
byte[] bodyBytes = bodyBytes();
return bodyBytes != null
? new ByteArrayInputStream(bodyBytes)
: null;
}

@Nullable
public abstract byte[] bodyBytes();

@Nullable
public abstract InputStream hijackedInput();
Expand Down