Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file handling in REST Client #45630

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package io.quarkus.rest.client.reactive;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.URI;
import java.time.Duration;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.rest.client.reactive.redirect.RedirectingResource;
import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
import io.smallrye.mutiny.Uni;

public class FileDownloadTest {

private static final long FIFTY_MEGA = 50 * 1024L * 1024L;

@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(Client.class, RedirectingResource.class))
.overrideRuntimeConfigKey("quarkus.rest-client.follow-redirects", "true");

@TestHTTPResource
URI uri;

@Test
void test() {
Client client = RestClientBuilder.newBuilder().baseUri(uri).build(Client.class);
File file = client.file();
assertThat(file).exists().hasSize(FIFTY_MEGA);

java.nio.file.Path path = client.path();
assertThat(path).exists().hasSize(FIFTY_MEGA);

file = client.uniFile().await().atMost(Duration.ofSeconds(10));
assertThat(file).exists().hasSize(FIFTY_MEGA);

path = client.uniPath().await().atMost(Duration.ofSeconds(10));
assertThat(path).exists().hasSize(FIFTY_MEGA);
}

@Path("/test")
public interface Client {
@GET
@Path("file")
File file();

@GET
@Path("file")
java.nio.file.Path path();

@GET
@Path("file")
Uni<File> uniFile();

@GET
@Path("file")
Uni<java.nio.file.Path> uniPath();
}

@Path("test")
public static class Resource {

@Path("file")
@GET
public File file() throws IOException {
File file = createTempFileToDownload();
RandomAccessFile f = new RandomAccessFile(file, "rw");
f.setLength(FIFTY_MEGA);
return file;
}

private static File createTempFileToDownload() throws IOException {
File file = File.createTempFile("toDownload", ".txt");
file.deleteOnExit();
return file;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -311,24 +311,20 @@ public void handle(AsyncResult<AsyncFile> asyncFileOpened) {
new Handler<>() {
@Override
public void handle(AsyncResult<Void> event) {
tmpAsyncFile.flush(new Handler<>() {
public void handle(AsyncResult<Void> flushed) {
if (flushed.failed()) {
reportFinish(flushed.cause(),
requestContext);
requestContext.resume(flushed.cause());
return;
}

if (loggingScope != LoggingScope.NONE) {
clientLogger.logRequest(
httpClientRequest, null, false);
}

requestContext.setTmpFilePath(tmpFilePath);
requestContext.resume();
}
});
if (event.failed()) {
reportFinish(event.cause(),
requestContext);
requestContext.resume(event.cause());
return;
}

if (loggingScope != LoggingScope.NONE) {
clientLogger.logRequest(
httpClientRequest, null, false);
}

requestContext.setTmpFilePath(tmpFilePath);
requestContext.resume();
}
});
clientResponse.resume();
Expand Down
Loading