multipartHello(@Context final ConnectionContext ctx,
(collector, item) -> ((CompositeBuffer) collector).addBuffer(item));
}
+ /**
+ * Resource that streams response content from a file via {@link Publisher}.
+ *
+ * Test with:
+ *
{@code
+ * curl -v http://localhost:8080/greetings/file-hello
+ * }
+ *
+ * @param ctx the {@link ConnectionContext}.
+ * @return greetings as a {@link Single} {@link Buffer}.
+ */
+ @GET
+ @Path("file-hello")
+ @Produces(TEXT_PLAIN)
+ public Publisher multipartHello(@Context final ConnectionContext ctx) {
+ final InputStream responseStream = HelloWorldJaxRsResource.class.getClassLoader()
+ .getResourceAsStream("response_payload.txt");
+ final BufferAllocator allocator = ctx.executionContext().bufferAllocator();
+ // InputStream lifetime ownership is transferred to ServiceTalk (e.g. it will call close) because
+ // we create a new InputStream per request and always pass it to ServiceTalk as the response payload
+ // body (if not null).
+ // Note that File APIs are blocking. ServiceTalk by default will call the File APIs on a non-IoExecutor thread
+ // and it isn't recommended to disable offloading for code paths that interact with blocking File APIs.
+ return responseStream == null ?
+ from(allocator.fromAscii("file not found")) :
+ fromInputStream(responseStream, allocator::wrap);
+ }
+
/**
* Resource that only relies on {@link Single}/{@link Publisher} for consuming and producing data,
* and returns a JAX-RS {@link Response} in order to set its status.
diff --git a/servicetalk-examples/http/jaxrs/src/main/resources/response_payload.txt b/servicetalk-examples/http/jaxrs/src/main/resources/response_payload.txt
new file mode 100644
index 0000000000..a8686cbfc5
--- /dev/null
+++ b/servicetalk-examples/http/jaxrs/src/main/resources/response_payload.txt
@@ -0,0 +1,5 @@
+response payload from file!
+
+warning: serving arbitrary files from your server maybe a security risk. You
+should take care if user input is used to infer file paths or names and ensure
+appropriate access control and auditing is in place.
\ No newline at end of file
diff --git a/settings.gradle b/settings.gradle
index f99fcd6b63..646bd8e241 100755
--- a/settings.gradle
+++ b/settings.gradle
@@ -78,6 +78,7 @@ include "servicetalk-annotations",
"servicetalk-examples:http:debugging",
"servicetalk-examples:http:timeout",
"servicetalk-examples:http:defaultloadbalancer",
+ "servicetalk-examples:http:files",
"servicetalk-gradle-plugin-internal",
"servicetalk-grpc-api",
"servicetalk-grpc-health",
@@ -160,3 +161,4 @@ project(":servicetalk-examples:http:uds").name = "servicetalk-examples-http-uds"
project(":servicetalk-examples:http:mutual-tls").name = "servicetalk-examples-http-mutual-tls"
project(":servicetalk-examples:http:redirects").name = "servicetalk-examples-http-redirects"
project(":servicetalk-examples:http:compression").name = "servicetalk-examples-http-compression"
+project(":servicetalk-examples:http:files").name = "servicetalk-examples-http-files"