Skip to content

Commit b9027fa

Browse files
authored
Merge pull request #50343 from geoand/streaming-docs
Add example of sending back a file with `RestResponse
2 parents 39b9f4b + 0608da9 commit b9027fa

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

.github/workflows/jdk-early-access-build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ env:
2121
# Workaround testsuite locale issue
2222
LANG: en_US.UTF-8
2323
MAVEN_OPTS: -Xmx2g -XX:MaxMetaspaceSize=1g
24-
JVM_TEST_MAVEN_OPTS: "-e -B --settings .github/mvn-settings.xml -Dtest-containers -Dstart-containers -Dtest-resteasy-reactive-large-files -Dformat.skip"
24+
JVM_TEST_MAVEN_OPTS: "-e -B --settings .github/mvn-settings.xml -Dtest-containers -Dstart-containers -Dtest-resteasy-reactive-large-files -Dquarkus.test.rest-assured.enable-logging-on-failure=false -Dformat.skip"
2525
DB_USER: hibernate_orm_test
2626
DB_PASSWORD: hibernate_orm_test
2727
DB_NAME: hibernate_orm_test
@@ -139,4 +139,4 @@ jobs:
139139
issue-repository: ${{ github.repository }}
140140
issue-number: 15867
141141
quarkus-sha: ${{ steps.quarkus-sha.outputs.sha }}
142-
project-sha: ${{ steps.quarkus-sha.outputs.sha }}
142+
project-sha: ${{ steps.quarkus-sha.outputs.sha }}

docs/src/main/asciidoc/rest.adoc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,34 @@ public class Endpoint {
778778
NOTE: You can also use the Jakarta REST type link:{jaxrsapi}/jakarta/ws/rs/core/Response.html[`Response`] but it is
779779
not strongly typed to your entity.
780780

781+
==== Responding with files
782+
783+
When a file needs to be returned, the following code ensures that Quarkus properly streams the file contents to the client.
784+
785+
[source,java]
786+
----
787+
package org.acme.rest;
788+
789+
import jakarta.ws.rs.GET;
790+
import jakarta.ws.rs.Path;
791+
import org.jboss.resteasy.reactive.RestResponse;
792+
import org.jboss.resteasy.reactive.RestResponse.ResponseBuilder;
793+
794+
@Path("")
795+
public class Endpoint {
796+
797+
@Path("file")
798+
@GET
799+
public RestResponse<java.nio.file.Path> largePathRestResponse() throws IOException {
800+
java.nio.file.Path path = ...
801+
802+
return RestResponse.ResponseBuilder.ok(path)
803+
.header(HttpHeaders.CONTENT_DISPOSITION, "some-file-name")
804+
.build();
805+
}
806+
}
807+
----
808+
781809
==== Using annotations
782810

783811
Alternatively, if you only need to set the status code and / or HTTP headers with static values, you can use `@org.jboss.resteasy.reactive.ResponseStatus` and /or `ResponseHeader` respectively.

extensions/resteasy-reactive/rest/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/providers/FileResource.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package io.quarkus.resteasy.reactive.server.test.providers;
22

33
import java.io.File;
4+
import java.io.IOException;
5+
import java.io.RandomAccessFile;
46
import java.nio.file.Paths;
57

68
import jakarta.ws.rs.GET;
79
import jakarta.ws.rs.Path;
10+
import jakarta.ws.rs.core.HttpHeaders;
811

912
import org.jboss.resteasy.reactive.FilePart;
1013
import org.jboss.resteasy.reactive.PathPart;
@@ -91,4 +94,19 @@ public Uni<AsyncFile> getAsyncFilePartial(RoutingContext vertxRequest) {
9194
});
9295
});
9396
}
97+
98+
static final long ONE_GIGA = 1024L * 1024L * 1024L;
99+
100+
@Path("large-path-rest-response")
101+
@GET
102+
public RestResponse<java.nio.file.Path> largePathRestResponse() throws IOException {
103+
File largeFile = File.createTempFile("rr-large-file-rest-response", ".tmp");
104+
largeFile.deleteOnExit();
105+
RandomAccessFile f = new RandomAccessFile(largeFile, "rw");
106+
f.setLength(ONE_GIGA);
107+
108+
return RestResponse.ResponseBuilder.ok(largeFile.toPath())
109+
.header(HttpHeaders.CONTENT_DISPOSITION, "large-file")
110+
.build();
111+
}
94112
}

extensions/resteasy-reactive/rest/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/providers/FileTestCase.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.jboss.resteasy.reactive.PathPart;
1515
import org.junit.jupiter.api.Assertions;
1616
import org.junit.jupiter.api.Test;
17+
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
1718
import org.junit.jupiter.api.extension.RegisterExtension;
1819

1920
import io.quarkus.test.QuarkusUnitTest;
@@ -168,4 +169,16 @@ public void testChecks() throws IOException {
168169
} catch (IllegalArgumentException x) {
169170
}
170171
}
172+
173+
@EnabledIfSystemProperty(named = "test-resteasy-reactive-large-files", matches = "true")
174+
@Test
175+
public void testWithLargeFile() {
176+
RestAssured.given()
177+
.get("/providers/file/large-path-rest-response")
178+
.then()
179+
.statusCode(200)
180+
.contentType("application/octet-stream")
181+
.header(HttpHeaders.CONTENT_DISPOSITION, "large-file")
182+
.header(HttpHeaders.CONTENT_LENGTH, "1073741824");
183+
}
171184
}

0 commit comments

Comments
 (0)