Skip to content

Prepare a StreamingOutput response when serving file downloads.#1829

Merged
ssalinas merged 7 commits into
masterfrom
proper-download-streaming
Aug 16, 2018
Merged

Prepare a StreamingOutput response when serving file downloads.#1829
ssalinas merged 7 commits into
masterfrom
proper-download-streaming

Conversation

@baconmania

Copy link
Copy Markdown
Contributor

This prepares a 4k buffer and pipes the HTTP response InputStream to a jax-rs compatible StreamingOutput object.

.get()
.getResponseBodyAsStream();
PerRequestConfig unlimitedTimeout = new PerRequestConfig();
unlimitedTimeout.setRequestTimeoutInMs(-1);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default 60 second timeout on our AsyncHttpClient is insufficient for large file downloads.


}

private static class NingOutputToJaxRsStreamingOutputWrapper implements AsyncHandler<Void>, StreamingOutput {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fully open to suggestions on a better name for this, heh.

if (wrappedOutputStream == null) {
wrappedOutputStream = output;
try {
requestBuilder.execute(this).get();

@baconmania baconmania Aug 8, 2018

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to do it this way because the OutputStream doesn't become available to us until Jersey actually calls write() on this implementation. The alternative would be to begin the proxied request before waiting for Jersey, in which case we'd be back to buffering a potentially large response in memory.

The get() here on the Future is also necessary because Jersey expects write() to be a blocking call, and closes the provided OutputStream after this method returns.

@ssalinas

ssalinas commented Aug 9, 2018

Copy link
Copy Markdown
Contributor

🚢

@ssalinas ssalinas added this to the 0.21.0 milestone Aug 9, 2018
@ssalinas ssalinas merged commit fa2a287 into master Aug 16, 2018
@ssalinas ssalinas deleted the proper-download-streaming branch August 16, 2018 12:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants