diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive-jackson/deployment/src/test/java/io/quarkus/resteasy/reactive/jackson/deployment/test/MessageBodyReaderTests.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive-jackson/deployment/src/test/java/io/quarkus/resteasy/reactive/jackson/deployment/test/MessageBodyReaderTests.java index d4e1238aadc60..68bd47c204deb 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive-jackson/deployment/src/test/java/io/quarkus/resteasy/reactive/jackson/deployment/test/MessageBodyReaderTests.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive-jackson/deployment/src/test/java/io/quarkus/resteasy/reactive/jackson/deployment/test/MessageBodyReaderTests.java @@ -9,17 +9,20 @@ import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Objects; import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.container.CompletionCallback; import jakarta.ws.rs.container.ConnectionCallback; +import jakarta.ws.rs.core.HttpHeaders; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; import org.jboss.resteasy.reactive.common.providers.serialisers.AbstractJsonMessageBodyReader; import org.jboss.resteasy.reactive.server.jackson.JacksonBasicMessageBodyReader; +import org.jboss.resteasy.reactive.server.jaxrs.HttpHeadersImpl; import org.jboss.resteasy.reactive.server.spi.ContentType; import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo; import org.jboss.resteasy.reactive.server.spi.ServerHttpResponse; @@ -266,6 +269,11 @@ public ResteasyReactiveResourceInfo getResteasyReactiveResourceInfo() { return null; } + @Override + public HttpHeaders getRequestHeaders() { + return new HttpHeadersImpl(Collections.emptyList()); + } + @Override public void abortWith(Response response) { diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/BuiltInReaderOverrideBuildItem.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/BuiltInReaderOverrideBuildItem.java new file mode 100644 index 0000000000000..13b3ce8eb68da --- /dev/null +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/BuiltInReaderOverrideBuildItem.java @@ -0,0 +1,42 @@ +package io.quarkus.resteasy.reactive.server.deployment; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import io.quarkus.builder.item.MultiBuildItem; + +public final class BuiltInReaderOverrideBuildItem extends MultiBuildItem { + + private final String readerClassName; + private final String overrideClassName; + + public BuiltInReaderOverrideBuildItem(String readerClassName, String overrideClassName) { + this.readerClassName = readerClassName; + this.overrideClassName = overrideClassName; + } + + public String getReaderClassName() { + return readerClassName; + } + + public String getOverrideClassName() { + return overrideClassName; + } + + public static Map toMap(List items) { + if (items.isEmpty()) { + return Collections.emptyMap(); + } + Map result = new HashMap<>(); + for (BuiltInReaderOverrideBuildItem item : items) { + String previousOverride = result.put(item.getReaderClassName(), item.getOverrideClassName()); + if (previousOverride != null) { + throw new IllegalStateException( + "Providing multiple BuiltInReaderOverrideBuildItem for the same readerClassName is not supported"); + } + } + return result; + } +} diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveProcessor.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveProcessor.java index 8802a8f7f576f..742bec269c29b 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveProcessor.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/ResteasyReactiveProcessor.java @@ -12,6 +12,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Path; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; @@ -110,6 +111,7 @@ import org.jboss.resteasy.reactive.server.processor.scanning.ResponseHeaderMethodScanner; import org.jboss.resteasy.reactive.server.processor.scanning.ResponseStatusMethodScanner; import org.jboss.resteasy.reactive.server.processor.util.ResteasyReactiveServerDotNames; +import org.jboss.resteasy.reactive.server.providers.serialisers.ServerFileBodyHandler; import org.jboss.resteasy.reactive.server.spi.RuntimeConfiguration; import org.jboss.resteasy.reactive.server.spi.ServerRestHandler; import org.jboss.resteasy.reactive.server.vertx.serializers.ServerMutinyAsyncFileMessageBodyWriter; @@ -165,6 +167,8 @@ import io.quarkus.resteasy.reactive.common.deployment.ServerDefaultProducesHandlerBuildItem; import io.quarkus.resteasy.reactive.common.runtime.ResteasyReactiveConfig; import io.quarkus.resteasy.reactive.server.EndpointDisabled; +import io.quarkus.resteasy.reactive.server.runtime.QuarkusServerFileBodyHandler; +import io.quarkus.resteasy.reactive.server.runtime.QuarkusServerPathBodyHandler; import io.quarkus.resteasy.reactive.server.runtime.ResteasyReactiveInitialiser; import io.quarkus.resteasy.reactive.server.runtime.ResteasyReactiveRecorder; import io.quarkus.resteasy.reactive.server.runtime.ResteasyReactiveRuntimeRecorder; @@ -1021,6 +1025,16 @@ private static String determineHandledGenericTypeOfProviderInterface(Class pr } } + @BuildStep + public void fileHandling(BuildProducer overrideProducer, + BuildProducer readerProducer) { + overrideProducer.produce(new BuiltInReaderOverrideBuildItem(ServerFileBodyHandler.class.getName(), + QuarkusServerFileBodyHandler.class.getName())); + readerProducer.produce( + new MessageBodyReaderBuildItem(QuarkusServerPathBodyHandler.class.getName(), Path.class.getName(), List.of( + MediaType.WILDCARD), RuntimeType.SERVER, true, Priorities.USER)); + } + @BuildStep @Record(value = ExecutionTime.STATIC_INIT, useIdentityComparisonForParameters = false) public void serverSerializers(ResteasyReactiveRecorder recorder, @@ -1030,6 +1044,7 @@ public void serverSerializers(ResteasyReactiveRecorder recorder, List additionalMessageBodyWriters, List messageBodyReaderOverrideBuildItems, List messageBodyWriterOverrideBuildItems, + List builtInReaderOverrideBuildItems, BuildProducer reflectiveClass, BuildProducer serverSerializersProducer) { @@ -1047,11 +1062,16 @@ public void serverSerializers(ResteasyReactiveRecorder recorder, reflectiveClass.produce(ReflectiveClassBuildItem.builder(builtinWriter.writerClass.getName()) .build()); } + Map builtInReaderOverrides = BuiltInReaderOverrideBuildItem.toMap(builtInReaderOverrideBuildItems); for (Serialisers.BuiltinReader builtinReader : ServerSerialisers.BUILTIN_READERS) { - registerReader(recorder, serialisers, builtinReader.entityClass.getName(), builtinReader.readerClass.getName(), + String effectiveReaderClassName = builtinReader.readerClass.getName(); + if (builtInReaderOverrides.containsKey(effectiveReaderClassName)) { + effectiveReaderClassName = builtInReaderOverrides.get(effectiveReaderClassName); + } + registerReader(recorder, serialisers, builtinReader.entityClass.getName(), effectiveReaderClassName, beanContainerBuildItem.getValue(), builtinReader.mediaType, builtinReader.constraint); - reflectiveClass.produce(ReflectiveClassBuildItem.builder(builtinReader.readerClass.getName()) + reflectiveClass.produce(ReflectiveClassBuildItem.builder(effectiveReaderClassName) .build()); } diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/AbstractMultipartTest.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/AbstractMultipartTest.java index 86348aa16e4b8..9bcb1a8dd1d7c 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/AbstractMultipartTest.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/AbstractMultipartTest.java @@ -3,6 +3,8 @@ import static org.awaitility.Awaitility.await; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; import java.time.Duration; import java.util.concurrent.Callable; @@ -40,4 +42,8 @@ public Boolean call() { } }); } + + protected String fileSizeAsStr(File file) throws IOException { + return "" + Files.readAllBytes(file.toPath()).length; + } } diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/FileInputWithDeleteTest.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/FileInputWithDeleteTest.java new file mode 100644 index 0000000000000..a3d43bad36ab3 --- /dev/null +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/FileInputWithDeleteTest.java @@ -0,0 +1,80 @@ +package io.quarkus.resteasy.reactive.server.test.multipart; + +import static org.hamcrest.CoreMatchers.equalTo; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.function.Supplier; + +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; + +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusUnitTest; +import io.restassured.RestAssured; + +public class FileInputWithDeleteTest extends AbstractMultipartTest { + + private static final java.nio.file.Path uploadDir = Paths.get("file-uploads"); + + @RegisterExtension + static QuarkusUnitTest test = new QuarkusUnitTest() + .setArchiveProducer(new Supplier<>() { + @Override + public JavaArchive get() { + return ShrinkWrap.create(JavaArchive.class) + .addClasses(Resource.class) + .addAsResource(new StringAsset( + "quarkus.http.body.uploads-directory=" + + uploadDir.toString() + "\n"), + "application.properties"); + } + + }); + + private final File HTML_FILE = new File("./src/test/resources/test.html"); + private final File HTML_FILE2 = new File("./src/test/resources/test2.html"); + + @Test + public void test() throws IOException { + RestAssured.given() + .contentType("application/octet-stream") + .body(HTML_FILE) + .when() + .post("/test") + .then() + .statusCode(200) + .body(equalTo(fileSizeAsStr(HTML_FILE))); + + awaitUploadDirectoryToEmpty(uploadDir); + + RestAssured.given() + .contentType("application/octet-stream") + .body(HTML_FILE2) + .when() + .post("/test") + .then() + .statusCode(200) + .body(equalTo(fileSizeAsStr(HTML_FILE2))); + + awaitUploadDirectoryToEmpty(uploadDir); + } + + @Path("test") + public static class Resource { + + @POST + @Consumes("application/octet-stream") + public long size(File file) throws IOException { + return Files.size(file.toPath()); + } + } +} diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/FileInputWithoutDeleteTest.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/FileInputWithoutDeleteTest.java new file mode 100644 index 0000000000000..318b8602e9167 --- /dev/null +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/FileInputWithoutDeleteTest.java @@ -0,0 +1,96 @@ +package io.quarkus.resteasy.reactive.server.test.multipart; + +import static org.hamcrest.CoreMatchers.equalTo; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.function.Supplier; + +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; + +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusUnitTest; +import io.restassured.RestAssured; + +public class FileInputWithoutDeleteTest extends AbstractMultipartTest { + + private static final java.nio.file.Path uploadDir = Paths.get("file-uploads"); + + @RegisterExtension + static QuarkusUnitTest test = new QuarkusUnitTest() + .setArchiveProducer(new Supplier<>() { + @Override + public JavaArchive get() { + return ShrinkWrap.create(JavaArchive.class) + .addClasses(Resource.class) + .addAsResource(new StringAsset( + // keep the files around so we can assert the outcome + "quarkus.http.body.delete-uploaded-files-on-end=false\nquarkus.http.body.uploads-directory=" + + uploadDir.toString() + "\n"), + "application.properties"); + } + + }); + + private final File HTML_FILE = new File("./src/test/resources/test.html"); + private final File HTML_FILE2 = new File("./src/test/resources/test2.html"); + + @BeforeEach + public void assertEmptyUploads() { + Assertions.assertTrue(isDirectoryEmpty(uploadDir)); + } + + @AfterEach + public void clearDirectory() { + clearDirectory(uploadDir); + } + + @Test + public void test() throws IOException { + RestAssured.given() + .contentType("application/octet-stream") + .body(HTML_FILE) + .when() + .post("/test") + .then() + .statusCode(200) + .body(equalTo(fileSizeAsStr(HTML_FILE))); + + // ensure that the 3 uploaded files where created on disk + Assertions.assertEquals(1, uploadDir.toFile().listFiles().length); + + RestAssured.given() + .contentType("application/octet-stream") + .body(HTML_FILE2) + .when() + .post("/test") + .then() + .statusCode(200) + .body(equalTo(fileSizeAsStr(HTML_FILE2))); + + // ensure that the 3 uploaded files where created on disk + Assertions.assertEquals(2, uploadDir.toFile().listFiles().length); + } + + @Path("test") + public static class Resource { + + @POST + @Consumes("application/octet-stream") + public long size(File file) throws IOException { + return Files.size(file.toPath()); + } + } +} diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/MultipartInputBodyHandlerTest.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/MultipartInputBodyHandlerTest.java index f75e0a416573d..9b371cb02996e 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/MultipartInputBodyHandlerTest.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/MultipartInputBodyHandlerTest.java @@ -5,7 +5,6 @@ import java.io.File; import java.io.IOException; -import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.function.Consumer; @@ -164,8 +163,4 @@ private String filePath(File file) { return file.toPath().toAbsolutePath().toString(); } - private String fileSizeAsStr(File file) throws IOException { - return "" + Files.readAllBytes(file.toPath()).length; - } - } diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/MultipartInputTest.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/MultipartInputTest.java index 3b3bc76ad6b54..e29c3791fda69 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/MultipartInputTest.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/MultipartInputTest.java @@ -5,7 +5,6 @@ import java.io.File; import java.io.IOException; -import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.function.Supplier; @@ -245,8 +244,4 @@ private String filePath(File file) { return file.toPath().toAbsolutePath().toString(); } - private String fileSizeAsStr(File file) throws IOException { - return "" + Files.readAllBytes(file.toPath()).length; - } - } diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/PathInputWithDeleteTest.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/PathInputWithDeleteTest.java new file mode 100644 index 0000000000000..b03d853f2fcab --- /dev/null +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/PathInputWithDeleteTest.java @@ -0,0 +1,80 @@ +package io.quarkus.resteasy.reactive.server.test.multipart; + +import static org.hamcrest.CoreMatchers.equalTo; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.function.Supplier; + +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; + +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusUnitTest; +import io.restassured.RestAssured; + +public class PathInputWithDeleteTest extends AbstractMultipartTest { + + private static final java.nio.file.Path uploadDir = Paths.get("file-uploads"); + + @RegisterExtension + static QuarkusUnitTest test = new QuarkusUnitTest() + .setArchiveProducer(new Supplier<>() { + @Override + public JavaArchive get() { + return ShrinkWrap.create(JavaArchive.class) + .addClasses(Resource.class) + .addAsResource(new StringAsset( + "quarkus.http.body.uploads-directory=" + + uploadDir.toString() + "\n"), + "application.properties"); + } + + }); + + private final File HTML_FILE = new File("./src/test/resources/test.html"); + private final File HTML_FILE2 = new File("./src/test/resources/test2.html"); + + @Test + public void test() throws IOException { + RestAssured.given() + .contentType("application/octet-stream") + .body(HTML_FILE) + .when() + .post("/test") + .then() + .statusCode(200) + .body(equalTo(fileSizeAsStr(HTML_FILE))); + + awaitUploadDirectoryToEmpty(uploadDir); + + RestAssured.given() + .contentType("application/octet-stream") + .body(HTML_FILE2) + .when() + .post("/test") + .then() + .statusCode(200) + .body(equalTo(fileSizeAsStr(HTML_FILE2))); + + awaitUploadDirectoryToEmpty(uploadDir); + } + + @Path("test") + public static class Resource { + + @POST + @Consumes("application/octet-stream") + public long size(java.nio.file.Path file) throws IOException { + return Files.size(file); + } + } +} diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/PathInputWithoutDeleteTest.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/PathInputWithoutDeleteTest.java new file mode 100644 index 0000000000000..08b7f7181da6b --- /dev/null +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/multipart/PathInputWithoutDeleteTest.java @@ -0,0 +1,96 @@ +package io.quarkus.resteasy.reactive.server.test.multipart; + +import static org.hamcrest.CoreMatchers.equalTo; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.function.Supplier; + +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; + +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusUnitTest; +import io.restassured.RestAssured; + +public class PathInputWithoutDeleteTest extends AbstractMultipartTest { + + private static final java.nio.file.Path uploadDir = Paths.get("file-uploads"); + + @RegisterExtension + static QuarkusUnitTest test = new QuarkusUnitTest() + .setArchiveProducer(new Supplier<>() { + @Override + public JavaArchive get() { + return ShrinkWrap.create(JavaArchive.class) + .addClasses(Resource.class) + .addAsResource(new StringAsset( + // keep the files around so we can assert the outcome + "quarkus.http.body.delete-uploaded-files-on-end=false\nquarkus.http.body.uploads-directory=" + + uploadDir.toString() + "\n"), + "application.properties"); + } + + }); + + private final File HTML_FILE = new File("./src/test/resources/test.html"); + private final File HTML_FILE2 = new File("./src/test/resources/test2.html"); + + @BeforeEach + public void assertEmptyUploads() { + Assertions.assertTrue(isDirectoryEmpty(uploadDir)); + } + + @AfterEach + public void clearDirectory() { + clearDirectory(uploadDir); + } + + @Test + public void test() throws IOException { + RestAssured.given() + .contentType("application/octet-stream") + .body(HTML_FILE) + .when() + .post("/test") + .then() + .statusCode(200) + .body(equalTo(fileSizeAsStr(HTML_FILE))); + + // ensure that the 3 uploaded files where created on disk + Assertions.assertEquals(1, uploadDir.toFile().listFiles().length); + + RestAssured.given() + .contentType("application/octet-stream") + .body(HTML_FILE2) + .when() + .post("/test") + .then() + .statusCode(200) + .body(equalTo(fileSizeAsStr(HTML_FILE2))); + + // ensure that the 3 uploaded files where created on disk + Assertions.assertEquals(2, uploadDir.toFile().listFiles().length); + } + + @Path("test") + public static class Resource { + + @POST + @Consumes("application/octet-stream") + public long size(java.nio.file.Path file) throws IOException { + return Files.size(file); + } + } +} diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/runtime/src/main/java/io/quarkus/resteasy/reactive/server/runtime/QuarkusServerFileBodyHandler.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/runtime/src/main/java/io/quarkus/resteasy/reactive/server/runtime/QuarkusServerFileBodyHandler.java new file mode 100644 index 0000000000000..e8c8effc7300a --- /dev/null +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/runtime/src/main/java/io/quarkus/resteasy/reactive/server/runtime/QuarkusServerFileBodyHandler.java @@ -0,0 +1,55 @@ +package io.quarkus.resteasy.reactive.server.runtime; + +import static io.quarkus.resteasy.reactive.server.runtime.QuarkusServerPathBodyHandler.createFile; +import static org.jboss.resteasy.reactive.common.providers.serialisers.FileBodyHandler.PREFIX; +import static org.jboss.resteasy.reactive.common.providers.serialisers.FileBodyHandler.SUFFIX; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.nio.file.Files; +import java.nio.file.Path; + +import jakarta.ws.rs.WebApplicationException; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.MultivaluedMap; + +import org.jboss.logging.Logger; +import org.jboss.resteasy.reactive.common.providers.serialisers.FileBodyHandler; +import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo; +import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyReader; +import org.jboss.resteasy.reactive.server.spi.ServerRequestContext; + +public class QuarkusServerFileBodyHandler implements ServerMessageBodyReader { + + private static final Logger log = Logger.getLogger(QuarkusServerFileBodyHandler.class); + + @Override + public boolean isReadable(Class type, Type genericType, ResteasyReactiveResourceInfo lazyMethod, + MediaType mediaType) { + return File.class.equals(type); + } + + @Override + public File readFrom(Class type, Type genericType, MediaType mediaType, ServerRequestContext context) + throws WebApplicationException, IOException { + Path file = createFile(context); + return FileBodyHandler.doRead(context.getRequestHeaders().getRequestHeaders(), context.getInputStream(), file.toFile()); + } + + @Override + public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return File.class.equals(type); + } + + @Override + public File readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, + MultivaluedMap httpHeaders, InputStream entityStream) + throws IOException, WebApplicationException { + // unfortunately we don't do much here to avoid the file leak + // however this should never be called in a real world scenario + return FileBodyHandler.doRead(httpHeaders, entityStream, Files.createTempFile(PREFIX, SUFFIX).toFile()); + } +} diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive/runtime/src/main/java/io/quarkus/resteasy/reactive/server/runtime/QuarkusServerPathBodyHandler.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive/runtime/src/main/java/io/quarkus/resteasy/reactive/server/runtime/QuarkusServerPathBodyHandler.java new file mode 100644 index 0000000000000..e6dcb8ff7dfa9 --- /dev/null +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive/runtime/src/main/java/io/quarkus/resteasy/reactive/server/runtime/QuarkusServerPathBodyHandler.java @@ -0,0 +1,96 @@ +package io.quarkus.resteasy.reactive.server.runtime; + +import static org.jboss.resteasy.reactive.common.providers.serialisers.FileBodyHandler.PREFIX; +import static org.jboss.resteasy.reactive.common.providers.serialisers.FileBodyHandler.SUFFIX; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.UncheckedIOException; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; +import java.nio.file.Path; +import java.nio.file.Paths; + +import jakarta.ws.rs.WebApplicationException; +import jakarta.ws.rs.container.CompletionCallback; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.MultivaluedMap; + +import org.jboss.logging.Logger; +import org.jboss.resteasy.reactive.common.providers.serialisers.FileBodyHandler; +import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo; +import org.jboss.resteasy.reactive.server.spi.RuntimeConfiguration; +import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyReader; +import org.jboss.resteasy.reactive.server.spi.ServerRequestContext; + +public class QuarkusServerPathBodyHandler implements ServerMessageBodyReader { + + private static final Logger log = Logger.getLogger(QuarkusServerPathBodyHandler.class); + + @Override + public boolean isReadable(Class type, Type genericType, ResteasyReactiveResourceInfo lazyMethod, + MediaType mediaType) { + return Path.class.equals(type); + } + + @Override + public Path readFrom(Class type, Type genericType, MediaType mediaType, ServerRequestContext context) + throws WebApplicationException, IOException { + Path file = createFile(context); + return FileBodyHandler.doRead(context.getRequestHeaders().getRequestHeaders(), context.getInputStream(), file.toFile()) + .toPath(); + } + + @Override + public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return File.class.equals(type); + } + + @Override + public Path readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, + MultivaluedMap httpHeaders, InputStream entityStream) + throws IOException, WebApplicationException { + // unfortunately we don't do much here to avoid the file leak + // however this should never be called in a real world scenario + return FileBodyHandler.doRead(httpHeaders, entityStream, Files.createTempFile(PREFIX, SUFFIX).toFile()).toPath(); + } + + static Path createFile(ServerRequestContext context) throws IOException { + RuntimeConfiguration.Body runtimeBodyConfiguration = ResteasyReactiveRecorder.getCurrentDeployment() + .getRuntimeConfiguration().body(); + boolean deleteUploadedFilesOnEnd = runtimeBodyConfiguration.deleteUploadedFilesOnEnd(); + String uploadsDirectoryStr = runtimeBodyConfiguration.uploadsDirectory(); + Path uploadDirectory = Paths.get(uploadsDirectoryStr); + try { + Files.createDirectories(uploadDirectory); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + + Path file = Files.createTempFile(uploadDirectory, PREFIX, SUFFIX); + if (deleteUploadedFilesOnEnd) { + context.registerCompletionCallback(new CompletionCallback() { + @Override + public void onComplete(Throwable throwable) { + ResteasyReactiveRecorder.EXECUTOR_SUPPLIER.get().execute(new Runnable() { + @Override + public void run() { + if (Files.exists(file)) { + try { + Files.delete(file); + } catch (NoSuchFileException e) { // ignore + } catch (IOException e) { + log.error("Cannot remove uploaded file " + file, e); + } + } + } + }); + } + }); + } + return file; + } +} diff --git a/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/providers/serialisers/FileBodyHandler.java b/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/providers/serialisers/FileBodyHandler.java index 82d2045179024..1f502c66555a2 100644 --- a/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/providers/serialisers/FileBodyHandler.java +++ b/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/providers/serialisers/FileBodyHandler.java @@ -21,8 +21,8 @@ import org.jboss.resteasy.reactive.common.headers.HeaderUtil; public class FileBodyHandler implements MessageBodyReader, MessageBodyWriter { - protected static final String PREFIX = "pfx"; - protected static final String SUFFIX = "sfx"; + public static final String PREFIX = "pfx"; + public static final String SUFFIX = "sfx"; @Override public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { @@ -33,16 +33,20 @@ public boolean isReadable(Class type, Type genericType, Annotation[] annotati public File readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, InputStream entityStream) throws IOException { - File downloadedFile = Files.createTempFile(PREFIX, SUFFIX).toFile(); + return doRead(httpHeaders, entityStream, Files.createTempFile(PREFIX, SUFFIX).toFile()); + } + + public static File doRead(MultivaluedMap httpHeaders, InputStream entityStream, + File file) throws IOException { if (HeaderUtil.isContentLengthZero(httpHeaders)) { - return downloadedFile; + return file; } - try (OutputStream output = new BufferedOutputStream(new FileOutputStream(downloadedFile))) { + try (OutputStream output = new BufferedOutputStream(new FileOutputStream(file))) { entityStream.transferTo(output); } - return downloadedFile; + return file; } public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { diff --git a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/ResteasyReactiveRequestContext.java b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/ResteasyReactiveRequestContext.java index 2821582f8cd01..cb3cec7776e43 100644 --- a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/ResteasyReactiveRequestContext.java +++ b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/ResteasyReactiveRequestContext.java @@ -19,6 +19,7 @@ import jakarta.ws.rs.core.Cookie; import jakarta.ws.rs.core.GenericEntity; +import jakarta.ws.rs.core.HttpHeaders; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.PathSegment; import jakarta.ws.rs.core.Request; @@ -149,6 +150,11 @@ public ResteasyReactiveRequestContext(Deployment deployment, @Override public abstract ServerHttpResponse serverResponse(); + @Override + public HttpHeaders getRequestHeaders() { + return getHttpHeaders(); + } + public Deployment getDeployment() { return deployment; } diff --git a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/spi/ServerRequestContext.java b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/spi/ServerRequestContext.java index 50fba8210e0da..4825de1472c11 100644 --- a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/spi/ServerRequestContext.java +++ b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/spi/ServerRequestContext.java @@ -3,6 +3,7 @@ import java.io.InputStream; import java.io.OutputStream; +import jakarta.ws.rs.core.HttpHeaders; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; @@ -22,5 +23,7 @@ public interface ServerRequestContext extends ResteasyReactiveCallbackContext { ResteasyReactiveResourceInfo getResteasyReactiveResourceInfo(); + HttpHeaders getRequestHeaders(); + void abortWith(Response response); }