Skip to content
This repository was archived by the owner on Apr 16, 2022. It is now read-only.

Close response InputStreams #865

Merged
merged 11 commits into from
Nov 2, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import static org.opendatakit.briefcase.reused.UncheckedFiles.closeInputStream;
import static org.opendatakit.briefcase.reused.UncheckedFiles.list;
import static org.opendatakit.briefcase.reused.UncheckedFiles.size;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -164,7 +166,13 @@ void pushFormAndAttachments(FormStatus form, List<Path> attachments, RunnerStatu
}

tracker.trackStartSendingFormAndAttachments(part, parts);
Response response = http.execute(server.getPushFormRequest(form.getFormFile(briefcaseDir), attachments));
Response<InputStream> response = http.execute(server.getPushFormRequest(form.getFormFile(briefcaseDir), attachments));

for (InputStream stream : server.getFileStreams()) {
closeInputStream(stream);
}
server.getFileStreams().clear();

if (response.isSuccess())
tracker.trackEndSendingFormAndAttachments(part, parts);
else
Expand All @@ -186,6 +194,12 @@ void pushSubmissionAndAttachments(Path submissionFile, List<Path> attachments, R
submissionFile,
attachments
));

for (InputStream stream : server.getFileStreams()) {
closeInputStream(stream);
}
server.getFileStreams().clear();

if (response.isSuccess())
tracker.trackEndSendingSubmissionAndAttachments(submissionNumber, totalSubmissions, part, parts);
else
Expand Down
8 changes: 8 additions & 0 deletions src/org/opendatakit/briefcase/reused/UncheckedFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ public static InputStream newInputStream(Path path, OpenOption... options) {
}
}

public static void closeInputStream(InputStream inputStream) {
try {
inputStream.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public static long size(Path path) {
try {
return Files.size(path);
Expand Down
21 changes: 17 additions & 4 deletions src/org/opendatakit/briefcase/reused/transfer/AggregateServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.net.URL;
import java.nio.file.Path;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -54,10 +55,12 @@
public class AggregateServer implements RemoteServer {
private final URL baseUrl;
private final Optional<Credentials> credentials;
private List<InputStream> fileStreams;

public AggregateServer(URL baseUrl, Optional<Credentials> credentials) {
this.baseUrl = baseUrl;
this.credentials = credentials;
this.fileStreams = new ArrayList<>();
}

public static AggregateServer authenticated(URL baseUrl, Credentials credentials) {
Expand Down Expand Up @@ -151,20 +154,24 @@ public Request<XmlElement> getInstanceIdBatchRequest(String formId, int entriesP
}

public Request<InputStream> getPushFormRequest(Path formFile, List<Path> attachments) {
InputStream formFileStream = newInputStream(formFile);
RequestBuilder<InputStream> builder = RequestBuilder.post(baseUrl)
.withPath("/formUpload")
.withMultipartMessage(
"form_def_file",
"application/xml",
formFile.getFileName().toString(),
newInputStream(formFile)
formFileStream
);
fileStreams.add(formFileStream);
for (Path attachment : attachments) {
InputStream attachmentStream = newInputStream(attachment);
fileStreams.add(attachmentStream);
builder = builder.withMultipartMessage(
attachment.getFileName().toString(),
getContentType(attachment),
attachment.getFileName().toString(),
newInputStream(attachment)
attachmentStream
);
}
return builder
Expand All @@ -175,21 +182,25 @@ public Request<InputStream> getPushFormRequest(Path formFile, List<Path> attachm
}

public Request<XmlElement> getPushSubmissionRequest(Path submissionFile, List<Path> attachments) {
InputStream submissionFileStream = newInputStream(submissionFile);
RequestBuilder<XmlElement> builder = RequestBuilder.post(baseUrl)
.asXmlElement()
.withPath("/submission")
.withMultipartMessage(
"xml_submission_file",
"application/xml",
submissionFile.getFileName().toString(),
newInputStream(submissionFile)
submissionFileStream
);
fileStreams.add(submissionFileStream);
for (Path attachment : attachments) {
InputStream attachmentStream = newInputStream(attachment);
fileStreams.add(attachmentStream);
builder = builder.withMultipartMessage(
attachment.getFileName().toString(),
getContentType(attachment),
attachment.getFileName().toString(),
newInputStream(attachment)
attachmentStream
);
}
return builder
Expand All @@ -199,6 +210,8 @@ public Request<XmlElement> getPushSubmissionRequest(Path submissionFile, List<Pa
.build();
}

public List<InputStream> getFileStreams() { return fileStreams; }

private String getContentType(Path file) {
return getFileExtension(file.getFileName().toString())
.map(extension -> {
Expand Down