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

Ensure that File is properly handled in native mode in RESTEasy Reactive #26021

Merged
merged 1 commit into from
Jun 9, 2022
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
Expand Up @@ -6,6 +6,7 @@
import static java.util.stream.Collectors.toList;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.DATE_FORMAT;

import java.io.File;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -182,6 +183,7 @@ public class ResteasyReactiveProcessor {
DotName.createSimple(HttpServerRequest.class.getName()),
DotName.createSimple(HttpServerResponse.class.getName()),
DotName.createSimple(RoutingContext.class.getName()));
private static final DotName FILE = DotName.createSimple(File.class.getName());

private static final int SECURITY_EXCEPTION_MAPPERS_PRIORITY = Priorities.USER + 1;

Expand Down Expand Up @@ -501,6 +503,10 @@ public void accept(EndpointIndexer.ResourceMethodCallbackData entry) {
.source(source)
.build());
}
if (parameterType.name().equals(FILE)) {
reflectiveClass.produce(new ReflectiveClassBuildItem(false, true, false,
entry.getActualEndpointInfo().name().toString()));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
@RegisterRestClient(configKey = "multipart-client")
public interface MultipartClient {

@POST
@Path("/octet-stream")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
@Produces(MediaType.TEXT_PLAIN)
String octetStreamFile(File body);

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ public class MultipartResource {
@RestClient
MultipartClient client;

@GET
@Path("/client/octet-stream")
@Produces(MediaType.TEXT_PLAIN)
@Blocking
public String sendOctetStreamFile() throws IOException {
java.nio.file.Path tempFile = Files.createTempFile("dummy", ".txt");
Files.write(tempFile, "test".getBytes(UTF_8));
return client.octetStreamFile(tempFile.toFile());
}

@GET
@Path("/client/byte-array-as-binary-file-with-pojo")
@Consumes(MediaType.TEXT_PLAIN)
Expand Down Expand Up @@ -228,6 +238,13 @@ public String sendPathAsText() throws IOException {
return client.sendPathAsTextFile(data);
}

@POST
@Path("/echo/octet-stream")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public String consumeOctetStream(File file) throws IOException {
return Files.readString(file.toPath());
}

@POST
@Path("/echo/binary")
@Consumes(MediaType.MULTIPART_FORM_DATA)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ public void shouldProducesMultipartForm() {
assertMultipartResponseContains(response, "file", MediaType.TEXT_PLAIN, HELLO_WORLD);
}

@Test
public void shouldProperlyHandleOctetStreamFile() {
// @formatter:off
given()
.header("Content-Type", "text/plain")
.when().get("/client/octet-stream")
.then()
.statusCode(200)
.body(equalTo("test"));
// @formatter:on
}

private void assertMultipartResponseContains(String response, String name, String contentType, Object value) {
String[] lines = response.split("--");
assertThat(lines).anyMatch(line -> line.contains(String.format(EXPECTED_CONTENT_DISPOSITION_PART, name))
Expand Down