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 @@ -21,6 +21,7 @@
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 +165,8 @@ 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));

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

if (response.isSuccess())
tracker.trackEndSendingSubmissionAndAttachments(submissionNumber, totalSubmissions, part, parts);
else
Expand Down
10 changes: 10 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,16 @@ public static InputStream newInputStream(Path path, OpenOption... options) {
}
}

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

public static long size(Path path) {
try {
return Files.size(path);
Expand Down
14 changes: 14 additions & 0 deletions src/org/opendatakit/briefcase/reused/http/CommonsHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.opendatakit.briefcase.reused.BriefcaseException;
import org.opendatakit.briefcase.reused.UncheckedFiles;
import org.opendatakit.briefcase.reused.http.response.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CommonsHttp implements Http {
private Executor executor;
private final int maxConnections;
private final BasicCookieStore cookieStore;
private static final Logger log = LoggerFactory.getLogger(CommonsHttp.class);

private CommonsHttp(Executor executor, int maxConnections, BasicCookieStore cookieStore) {
this.executor = executor;
Expand Down Expand Up @@ -147,6 +151,16 @@ private <T> Response<T> uncheckedExecute(Request<T> request, Executor executor)
throw new HttpException("The connection has timed out", e);
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
try {
UncheckedFiles.closeInputStream(request.getBody());
} catch (final BriefcaseException exception) {
log.error("Error: Attempted to close Request.body InputStream, but failed", exception);
}
if (request.multipartMessages != null)
request.multipartMessages.stream()
.map(MultipartMessage::getBody)
.forEach(UncheckedFiles::closeInputStream);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.opendatakit.briefcase.reused;

import static org.opendatakit.briefcase.reused.UncheckedFiles.createFile;
import static org.opendatakit.briefcase.reused.UncheckedFiles.createTempDirectory;
import static org.opendatakit.briefcase.reused.UncheckedFiles.deleteRecursive;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Path;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class UncheckedFilesInputStreamTest {
private Path tempDir;
private Path temp;


@Before
public void setUp() {
tempDir = createTempDirectory("briefcase");
temp = tempDir.resolve("test.txt");
createFile(temp);
}

@After
public void tearDown() {
deleteRecursive(tempDir);
}

@Test
public void closeInputStream_should_handle_null() {
UncheckedFiles.closeInputStream(null);
}

@Test(expected = IOException.class)
public void closeInputStream_should_close() throws IOException {
InputStream inputStream = UncheckedFiles.newInputStream(temp);

UncheckedFiles.closeInputStream(inputStream);

// after close, .available() should throw exception
inputStream.available();
}

@Test(expected = UncheckedIOException.class)
public void closeInputStream_should_throw_exception() throws IOException {
InputStream is = new InputStream() {
@Override
public int read() throws IOException {
return 0;
}
@Override
public void close() throws IOException {
throw new IOException("chuchu");
}
};
UncheckedFiles.closeInputStream(is);
}
}