CybercentreCanada / CybercentreCanada/assemblyline-java-client

downloadFile() does not gracefully handle error responses

Open
#72 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
3
Forks
0
PR merge metrics
No merged PRs in 30d

Description

`downloadFile()` attempts to to convert an HTTP response body (represented as a `Flux`) into an `InputStream`. This works fine when AssemblyLine successfully returns the expected response. However, the asynchronous nature of RxJava and `PipedInputStream`/`PipedOutputStream` means that an error like a 404 will only happen on a separate thread, after `downloadFile()` has returned the `PipedInputStream`. The result is that if an exception is thrown from "within" RxJava, the only external indication is a truncated `InputStream` (or even an empty one, if we got a 404).

Two solutions I can think of off the top of my head:
1. Buffer the entire file in memory on the main thread (e.g. by using `block()`), so that any exception can be caught within the method.
2. Some kind of frankenstein wrapper around the piped streams that allows an exception on the write end to be propagated to the read end (e.g. code calling `read()` on the `InputStream` could throw a "404")

Both solutions have some major drawbacks. Since the main exception I have encountered is 404 File Not Found from AssemblyLine, an easy workaround is for a user to call `getFileInfo()` first, to check if the file exists.

```java
try {
client.getFileInfo(sha256).block();
} catch (WebClientResponseException e) {
if (e.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
throw new RuntimeException("File does not exist.", e);
} else {
throw e;
}
}

client.downloadFile(sha256);
```

I'm not sure that finding a "proper" solution is ever going to be worth the effort, but I'm recording this issue here because someone else could run into the same issue in the future.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.