-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 as a JAX-RS body parameter #35659
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...n/java/io/quarkus/resteasy/reactive/server/deployment/BuiltInReaderOverrideBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, String> toMap(List<BuiltInReaderOverrideBuildItem> items) { | ||
if (items.isEmpty()) { | ||
return Collections.emptyMap(); | ||
} | ||
Map<String, String> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...test/java/io/quarkus/resteasy/reactive/server/test/multipart/FileInputWithDeleteTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...t/java/io/quarkus/resteasy/reactive/server/test/multipart/FileInputWithoutDeleteTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤢