From 9bffa68c63041b0c1ac7132b7bdbd83bed9eff86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20Guti=C3=A9rrez?= Date: Fri, 13 Mar 2020 15:32:30 +0100 Subject: [PATCH] Make HTTP post requests repeatable by wrapping entities into a repeatable entity (#856) --- .../briefcase/reused/http/CommonsHttp.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/org/opendatakit/briefcase/reused/http/CommonsHttp.java b/src/org/opendatakit/briefcase/reused/http/CommonsHttp.java index d510765df..02f91af38 100644 --- a/src/org/opendatakit/briefcase/reused/http/CommonsHttp.java +++ b/src/org/opendatakit/briefcase/reused/http/CommonsHttp.java @@ -21,6 +21,7 @@ import static org.opendatakit.briefcase.reused.http.RequestMethod.POST; import java.io.IOException; +import java.io.InputStream; import java.io.UncheckedIOException; import java.net.SocketTimeoutException; import java.net.URL; @@ -30,6 +31,7 @@ import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.conn.HttpHostConnectException; import org.apache.http.entity.BasicHttpEntity; +import org.apache.http.entity.BufferedHttpEntity; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.InputStreamBody; @@ -128,10 +130,9 @@ private Response uncheckedExecute(Request request, Executor executor) part.getName(), new InputStreamBody(part.getBody(), ContentType.create(part.getContentType()), part.getAttachmentName()) ); - body = bodyBuilder.build(); + body = makeRepeatable(bodyBuilder.build()); } else { - body = new BasicHttpEntity(); - ((BasicHttpEntity) body).setContent(request.getBody()); + body = makeRepeatable(buildBasicEntity(request.getBody())); } commonsRequest.body(body); } @@ -149,6 +150,20 @@ private Response uncheckedExecute(Request request, Executor executor) } } + private BasicHttpEntity buildBasicEntity(InputStream contents) { + BasicHttpEntity entity = new BasicHttpEntity(); + entity.setContent(contents); + return entity; + } + + private BufferedHttpEntity makeRepeatable(HttpEntity entity) { + try { + return new BufferedHttpEntity(entity); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + private static org.apache.http.client.fluent.Request getCommonsRequest(Request request) { switch (request.getMethod()) { case GET: