Skip to content

Commit

Permalink
Fix handling multipart requests manually
Browse files Browse the repository at this point in the history
Fixes #19404
  • Loading branch information
stuartwdouglas committed Aug 16, 2021
1 parent 701fd3e commit aafbbf5
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,14 @@ && isContextType(paramType.asClassType())) {
// in this case it is safe to assume that we are consuming multipart data
// we already don't allow multipart to be used along with body in the same method,
// so this is completely safe
builder.setType(ParameterType.MULTI_PART_FORM);
var type = toClassName(paramType, currentClassInfo, actualEndpointInfo, index);
var typeInfo = index.getClassByName(DotName.createSimple(type));
if (typeInfo != null && typeInfo.annotations().containsKey(REST_FORM_PARAM)) {
builder.setType(ParameterType.MULTI_PART_FORM);
} else {
//if the paramater does not have @RestForm annotations we treat it as a normal body
builder.setType(ParameterType.BODY);
}
} else {
builder.setType(ParameterType.BODY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public class ResourceReader {
private volatile List<MediaType> mediaTypes;
private volatile MessageBodyReader<?> instance;

public void setFactory(BeanFactory<MessageBodyReader<?>> factory) {
public ResourceReader setFactory(BeanFactory<MessageBodyReader<?>> factory) {
this.factory = factory;
return this;
}

public BeanFactory<MessageBodyReader<?>> getFactory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
import java.util.logging.Logger;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.jboss.resteasy.reactive.common.ResteasyReactiveConfig;
import org.jboss.resteasy.reactive.common.model.ResourceClass;
import org.jboss.resteasy.reactive.common.model.ResourceReader;
import org.jboss.resteasy.reactive.common.model.ResourceWriter;
import org.jboss.resteasy.reactive.common.processor.JandexUtil;
import org.jboss.resteasy.reactive.common.processor.scanning.ApplicationScanningResult;
Expand All @@ -46,13 +48,15 @@
import org.jboss.resteasy.reactive.server.core.Deployment;
import org.jboss.resteasy.reactive.server.core.DeploymentInfo;
import org.jboss.resteasy.reactive.server.core.ServerSerialisers;
import org.jboss.resteasy.reactive.server.core.startup.CustomServerRestHandlers;
import org.jboss.resteasy.reactive.server.core.startup.RuntimeDeploymentManager;
import org.jboss.resteasy.reactive.server.handlers.RestInitialHandler;
import org.jboss.resteasy.reactive.server.processor.ServerEndpointIndexer;
import org.jboss.resteasy.reactive.server.processor.scanning.ResteasyReactiveContextResolverScanner;
import org.jboss.resteasy.reactive.server.processor.scanning.ResteasyReactiveExceptionMappingScanner;
import org.jboss.resteasy.reactive.server.processor.scanning.ResteasyReactiveFeatureScanner;
import org.jboss.resteasy.reactive.server.processor.scanning.ResteasyReactiveParamConverterScanner;
import org.jboss.resteasy.reactive.server.providers.serialisers.ServerByteArrayMessageBodyHandler;
import org.jboss.resteasy.reactive.server.providers.serialisers.ServerStringMessageBodyHandler;
import org.jboss.resteasy.reactive.server.vertx.ResteasyReactiveVertxHandler;
import org.jboss.resteasy.reactive.server.vertx.VertxRequestContextFactory;
Expand Down Expand Up @@ -248,12 +252,31 @@ public MessageBodyWriter<?> getInstance() {
@Override
public void close() {

}
};
}
}));
serialisers.addReader(byte[].class, new ResourceReader()
.setMediaTypeStrings(Collections.singletonList(MediaType.WILDCARD))
.setFactory(new BeanFactory<MessageBodyReader<?>>() {
@Override
public BeanInstance<MessageBodyReader<?>> createInstance() {
return new BeanInstance<MessageBodyReader<?>>() {
@Override
public MessageBodyReader<?> getInstance() {
return new ServerByteArrayMessageBodyHandler();
}

@Override
public void close() {

}
};
}
}));
DeploymentInfo info = new DeploymentInfo()
.setApplicationPath("/")
.setConfig(new ResteasyReactiveConfig())
.setFeatures(ResteasyReactiveFeatureScanner.createFeatures(index, applicationScanningResult))
.setInterceptors(
ResteasyReactiveInterceptorScanner.createResourceInterceptors(index, applicationScanningResult))
Expand Down Expand Up @@ -284,7 +307,8 @@ public Application get() {
}
}
});
RuntimeDeploymentManager runtimeDeploymentManager = new RuntimeDeploymentManager(info, () -> executor, null,
RuntimeDeploymentManager runtimeDeploymentManager = new RuntimeDeploymentManager(info, () -> executor,
new CustomServerRestHandlers(null),
closeable -> closeTasks.add(closeable), new VertxRequestContextFactory(), ThreadSetupAction.NOOP, "/");
Deployment deployment = runtimeDeploymentManager.deploy();
RestInitialHandler initialHandler = new RestInitialHandler(deployment);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.jboss.resteasy.reactive.server.vertx.test.multipart;

import io.smallrye.common.annotation.NonBlocking;
import java.nio.charset.StandardCharsets;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/echo")
public class EchoResource {

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
@NonBlocking
public String echo(byte[] request) {
return new String(request, StandardCharsets.UTF_8);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.jboss.resteasy.reactive.server.vertx.test.multipart;

import io.restassured.RestAssured;
import java.util.function.Supplier;
import org.hamcrest.Matchers;
import org.jboss.resteasy.reactive.server.vertx.test.framework.ResteasyReactiveUnitTest;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class RawMultipartTestCase {

@RegisterExtension
static ResteasyReactiveUnitTest test = new ResteasyReactiveUnitTest()
.setArchiveProducer(new Supplier<JavaArchive>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class).addClass(EchoResource.class);
}
});

@Test
public void testHelloEndpoint() {
RestAssured.given()
.when()
.multiPart("testName", "testValue")
.post("/echo")
.then()
.statusCode(200)
.body(Matchers.containsString("testValue"));
}

}

0 comments on commit aafbbf5

Please sign in to comment.