Skip to content

Commit

Permalink
[RESTEASY-3390] Use the request entity parts if a multipart/form-data…
Browse files Browse the repository at this point in the history
… request is sent and the injection parameter type is a string.

https://issues.redhat.com/browse/RESTEASY-3390
Signed-off-by: James R. Perkins <jperkins@redhat.com>
  • Loading branch information
jamezp committed Oct 5, 2023
1 parent 05d853a commit 5b15bce
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,61 @@ public void multiInjection() throws Exception {
}
}

/**
* Tests sending {@code multipart/form-data} content as a {@link EntityPart List<EntityPart>}. Three parts are sent
* and injected as {@link FormParam @FormParam} method parameters. Each part send is different and injected as a
* specific type.
* <p>
* The result from the REST endpoint is {@code multipart/form-data} content with a new name and the content for the
* injected field.
* </p>
*
* @throws Exception if an error occurs in the test
*/
@Test
public void multiAllFilesInjection() throws Exception {
try (Client client = ClientBuilder.newClient()) {
final List<EntityPart> multipart = List.of(
EntityPart.withName("entity-part")
.fileName("file1.txt")
.content("test entity part file1.txt")
.mediaType(MediaType.TEXT_PLAIN_TYPE)
.build(),
EntityPart.withName("string-part")
.fileName("file2.txt")
.content("test string file2.txt")
.mediaType(MediaType.TEXT_PLAIN_TYPE)
.build(),
EntityPart.withName("input-stream-part")
.fileName("file3.txt")
.content("test input stream file3.txt".getBytes(StandardCharsets.UTF_8))
.mediaType(MediaType.APPLICATION_OCTET_STREAM_TYPE)
.build());
try (
Response response = client.target(INSTANCE.configuration()
.baseUriBuilder()
.path("test/multi-injected"))
.request(MediaType.MULTIPART_FORM_DATA_TYPE)
.post(Entity.entity(new GenericEntity<>(multipart) {
}, MediaType.MULTIPART_FORM_DATA))) {
Assert.assertEquals(Response.Status.OK, response.getStatusInfo());
final List<EntityPart> entityParts = response.readEntity(new GenericType<>() {
});
if (entityParts.size() != 3) {
final String msg = "Expected 3 entries got " +
entityParts.size() +
'.' +
System.lineSeparator() +
getMessage(entityParts);
Assert.fail(msg);
}
checkEntity(entityParts, "received-entity-part", "test entity part file1.txt");
checkEntity(entityParts, "received-string", "test string file2.txt");
checkEntity(entityParts, "received-input-stream", "test input stream file3.txt");
}
}
}

/**
* Tests sending {@code multipart/form-data} content as a {@link EntityPart List<EntityPart>}. One part is sent
* and injected as {@link FormParam @FormParam} method parameters. The same part should be injected in different
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jboss.resteasy.core;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Type;
Expand All @@ -11,6 +13,7 @@

import jakarta.ws.rs.FormParam;
import jakarta.ws.rs.core.EntityPart;
import jakarta.ws.rs.core.MediaType;

import org.jboss.resteasy.resteasy_jaxrs.i18n.Messages;
import org.jboss.resteasy.spi.HttpRequest;
Expand Down Expand Up @@ -45,6 +48,17 @@ public Object inject(HttpRequest request, HttpResponse response, boolean unwrapA
} else if (InputStream.class.isAssignableFrom(type)) {
final Optional<EntityPart> part = request.getFormEntityPart(paramName);
return part.map(EntityPart::getContent).orElse(null);
} else if (String.class.isAssignableFrom(type) &&
// request.getHttpHeaders().getMediaType() may return null, but the isCompatible handles this check
MediaType.MULTIPART_FORM_DATA_TYPE.isCompatible(request.getHttpHeaders().getMediaType())) {
final Optional<EntityPart> part = request.getFormEntityPart(paramName);
return part.map(p -> {
try {
return p.getContent(String.class);
} catch (IOException e) {
throw new UncheckedIOException(Messages.MESSAGES.unableToExtractParameter(paramName, null), e);
}
}).orElse(null);
}
List<String> list = request.getDecodedFormParameters().get(paramName);
if (list != null && encode) {
Expand Down

0 comments on commit 5b15bce

Please sign in to comment.