Skip to content

Commit

Permalink
Ensure that File is properly handled in native mode in RESTEasy Reactive
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Jun 9, 2022
1 parent 1e25972 commit 91cd439
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
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
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
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
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

0 comments on commit 91cd439

Please sign in to comment.