Skip to content

Commit

Permalink
REST Client Reactive: support POJO as non-file fields in multipart me…
Browse files Browse the repository at this point in the history
…ssages

fixes #19892
  • Loading branch information
michalszynkiewicz committed Sep 14, 2021
1 parent 895a87c commit 106b744
Show file tree
Hide file tree
Showing 12 changed files with 639 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.jboss.resteasy.reactive.client.impl.MultiInvoker;
import org.jboss.resteasy.reactive.client.impl.UniInvoker;
import org.jboss.resteasy.reactive.client.impl.WebTargetImpl;
import org.jboss.resteasy.reactive.client.impl.multipart.QuarkusMultipartForm;
import org.jboss.resteasy.reactive.client.processor.beanparam.BeanParamItem;
import org.jboss.resteasy.reactive.client.processor.beanparam.ClientBeanParamInfo;
import org.jboss.resteasy.reactive.client.processor.beanparam.CookieParamItem;
Expand Down Expand Up @@ -121,7 +122,6 @@
import io.quarkus.gizmo.TryBlock;
import io.quarkus.jaxrs.client.reactive.runtime.ClientResponseBuilderFactory;
import io.quarkus.jaxrs.client.reactive.runtime.JaxrsClientReactiveRecorder;
import io.quarkus.jaxrs.client.reactive.runtime.MultipartFormUtils;
import io.quarkus.jaxrs.client.reactive.runtime.ToObjectArray;
import io.quarkus.resteasy.reactive.common.deployment.ApplicationResultBuildItem;
import io.quarkus.resteasy.reactive.common.deployment.QuarkusFactoryCreator;
Expand All @@ -137,7 +137,6 @@
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import io.vertx.core.buffer.Buffer;
import io.vertx.ext.web.multipart.MultipartForm;

public class JaxrsClientReactiveProcessor {

Expand Down Expand Up @@ -963,11 +962,9 @@ A more full example of generated client (with sub-resource) can is at the bottom
*/
private ResultHandle createMultipartForm(MethodCreator methodCreator, ResultHandle methodParam, Type formClassType,
IndexView index) {
AssignableResultHandle multipartForm = methodCreator.createVariable(MultipartForm.class);
AssignableResultHandle multipartForm = methodCreator.createVariable(QuarkusMultipartForm.class);
methodCreator.assign(multipartForm,
methodCreator
.invokeStaticMethod(
MethodDescriptor.ofMethod(MultipartFormUtils.class, "create", MultipartForm.class)));
methodCreator.newInstance(MethodDescriptor.ofConstructor(QuarkusMultipartForm.class)));

ClassInfo formClass = index.getClassByName(formClassType.name());

Expand Down Expand Up @@ -1016,10 +1013,8 @@ private ResultHandle createMultipartForm(MethodCreator methodCreator, ResultHand
} else if (is(BUFFER, fieldClass, index)) {
// and buffer
addBuffer(methodCreator, multipartForm, formParamName, partType, fieldValue, field);
} else {
throw new IllegalArgumentException("Unsupported multipart form field on: " + formClassType.name()
+ "." + fieldType.name() +
". Supported types are: java.lang.String, java.io.File, java.nio.Path and io.vertx.core.Buffer");
} else { // assume POJO:
addPojo(methodCreator, multipartForm, formParamName, partType, fieldValue, field);
}
break;
case ARRAY:
Expand All @@ -1030,8 +1025,8 @@ private ResultHandle createMultipartForm(MethodCreator methodCreator, ResultHand
throw new IllegalArgumentException("Array of unsupported type: " + componentType.name()
+ " on " + formClassType.name() + "." + field.name());
}
ResultHandle buffer = methodCreator.invokeStaticMethod(
MethodDescriptor.ofMethod(MultipartFormUtils.class, "buffer", Buffer.class, byte[].class),
ResultHandle buffer = methodCreator.invokeStaticInterfaceMethod(
MethodDescriptor.ofMethod(Buffer.class, "buffer", Buffer.class, byte[].class),
fieldValue);
addBuffer(methodCreator, multipartForm, formParamName, partType, buffer, field);
break;
Expand All @@ -1053,9 +1048,18 @@ private ResultHandle createMultipartForm(MethodCreator methodCreator, ResultHand
return multipartForm;
}

private void addPojo(MethodCreator methodCreator, AssignableResultHandle multipartForm, String formParamName,
String partType, ResultHandle fieldValue, FieldInfo field) {
methodCreator.assign(multipartForm,
methodCreator.invokeVirtualMethod(MethodDescriptor.ofMethod(QuarkusMultipartForm.class, "entity",
QuarkusMultipartForm.class, String.class, Object.class, String.class, Class.class),
multipartForm, methodCreator.load(field.name()), fieldValue, methodCreator.load(partType),
methodCreator.loadClass(field.type().name().toString())));
}

/**
* add file upload, see {@link MultipartForm#binaryFileUpload(String, String, String, String)} and
* {@link MultipartForm#textFileUpload(String, String, String, String)}
* add file upload, see {@link QuarkusMultipartForm#binaryFileUpload(String, String, String, String)} and
* {@link QuarkusMultipartForm#textFileUpload(String, String, String, String)}
*/
private void addFile(MethodCreator methodCreator, AssignableResultHandle multipartForm, String formParamName,
String partType, ResultHandle filePath) {
Expand All @@ -1066,19 +1070,19 @@ private void addFile(MethodCreator methodCreator, AssignableResultHandle multipa
methodCreator.assign(multipartForm,
// MultipartForm#binaryFileUpload(String name, String filename, String pathname, String mediaType);
// filename = name
methodCreator.invokeInterfaceMethod(
MethodDescriptor.ofMethod(MultipartForm.class, "binaryFileUpload",
MultipartForm.class, String.class, String.class, String.class,
methodCreator.invokeVirtualMethod(
MethodDescriptor.ofMethod(QuarkusMultipartForm.class, "binaryFileUpload",
QuarkusMultipartForm.class, String.class, String.class, String.class,
String.class),
multipartForm, methodCreator.load(formParamName), fileName,
pathString, methodCreator.load(partType)));
} else {
methodCreator.assign(multipartForm,
// MultipartForm#textFileUpload(String name, String filename, String pathname, String mediaType);;
// filename = name
methodCreator.invokeInterfaceMethod(
MethodDescriptor.ofMethod(MultipartForm.class, "textFileUpload",
MultipartForm.class, String.class, String.class, String.class,
methodCreator.invokeVirtualMethod(
MethodDescriptor.ofMethod(QuarkusMultipartForm.class, "textFileUpload",
QuarkusMultipartForm.class, String.class, String.class, String.class,
String.class),
multipartForm, methodCreator.load(formParamName), fileName,
pathString, methodCreator.load(partType)));
Expand Down Expand Up @@ -1115,8 +1119,8 @@ private ResultHandle primitiveToString(MethodCreator methodCreator, ResultHandle
private void addString(MethodCreator methodCreator, AssignableResultHandle multipartForm, String formParamName,
ResultHandle fieldValue) {
methodCreator.assign(multipartForm,
methodCreator.invokeInterfaceMethod(
MethodDescriptor.ofMethod(MultipartForm.class, "attribute", MultipartForm.class,
methodCreator.invokeVirtualMethod(
MethodDescriptor.ofMethod(QuarkusMultipartForm.class, "attribute", QuarkusMultipartForm.class,
String.class, String.class),
multipartForm, methodCreator.load(formParamName), fieldValue));
}
Expand All @@ -1132,19 +1136,19 @@ private void addBuffer(MethodCreator methodCreator, AssignableResultHandle multi
methodCreator.assign(multipartForm,
// MultipartForm#binaryFileUpload(String name, String filename, String pathname, String mediaType);
// filename = name
methodCreator.invokeInterfaceMethod(
MethodDescriptor.ofMethod(MultipartForm.class, "binaryFileUpload",
MultipartForm.class, String.class, String.class, Buffer.class,
methodCreator.invokeVirtualMethod(
MethodDescriptor.ofMethod(QuarkusMultipartForm.class, "binaryFileUpload",
QuarkusMultipartForm.class, String.class, String.class, Buffer.class,
String.class),
multipartForm, methodCreator.load(formParamName), methodCreator.load(formParamName),
buffer, methodCreator.load(partType)));
} else {
methodCreator.assign(multipartForm,
// MultipartForm#textFileUpload(String name, String filename, io.vertx.mutiny.core.buffer.Buffer content, String mediaType)
// filename = name
methodCreator.invokeInterfaceMethod(
MethodDescriptor.ofMethod(MultipartForm.class, "textFileUpload",
MultipartForm.class, String.class, String.class, Buffer.class,
methodCreator.invokeVirtualMethod(
MethodDescriptor.ofMethod(QuarkusMultipartForm.class, "textFileUpload",
QuarkusMultipartForm.class, String.class, String.class, Buffer.class,
String.class),
multipartForm, methodCreator.load(formParamName), methodCreator.load(formParamName),
buffer, methodCreator.load(partType)));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.RequestOptions;
import io.vertx.core.streams.Pipe;
import io.vertx.ext.web.client.impl.MultipartFormUpload;
import io.vertx.ext.web.multipart.MultipartForm;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
Expand All @@ -28,6 +26,8 @@
import org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties;
import org.jboss.resteasy.reactive.client.impl.AsyncInvokerImpl;
import org.jboss.resteasy.reactive.client.impl.RestClientRequestContext;
import org.jboss.resteasy.reactive.client.impl.multipart.QuarkusMultipartForm;
import org.jboss.resteasy.reactive.client.impl.multipart.QuarkusMultipartFormUpload;
import org.jboss.resteasy.reactive.client.spi.ClientRestHandler;
import org.jboss.resteasy.reactive.common.core.Serialisers;

Expand Down Expand Up @@ -62,7 +62,7 @@ public void handle(HttpClientRequest httpClientRequest) {
Future<HttpClientResponse> sent;
if (requestContext.isMultipart()) {
Promise<HttpClientRequest> requestPromise = Promise.promise();
MultipartFormUpload actualEntity;
QuarkusMultipartFormUpload actualEntity;
try {
actualEntity = ClientSendRequestHandler.this.setMultipartHeadersAndPrepareBody(httpClientRequest,
requestContext);
Expand Down Expand Up @@ -173,20 +173,24 @@ public Future<HttpClientRequest> createRequest(RestClientRequestContext state) {
return httpClient.request(requestOptions);
}

private MultipartFormUpload setMultipartHeadersAndPrepareBody(HttpClientRequest httpClientRequest,
private QuarkusMultipartFormUpload setMultipartHeadersAndPrepareBody(HttpClientRequest httpClientRequest,
RestClientRequestContext state) throws Exception {
if (!(state.getEntity().getEntity() instanceof MultipartForm)) {
if (!(state.getEntity().getEntity() instanceof QuarkusMultipartForm)) {
throw new IllegalArgumentException(
"Multipart form upload expects an entity of type MultipartForm, got: " + state.getEntity().getEntity());
}
MultivaluedMap<String, String> headerMap = state.getRequestHeaders().asMap();
MultipartForm entity = (MultipartForm) state.getEntity().getEntity();
QuarkusMultipartForm multipartForm = (QuarkusMultipartForm) state.getEntity().getEntity();
multipartForm.preparePojos(state);

Object property = state.getConfiguration().getProperty(QuarkusRestClientProperties.MULTIPART_ENCODER_MODE);
HttpPostRequestEncoder.EncoderMode mode = HttpPostRequestEncoder.EncoderMode.RFC1738;
if (property != null) {
mode = (HttpPostRequestEncoder.EncoderMode) property;
}
MultipartFormUpload multipartFormUpload = new MultipartFormUpload(Vertx.currentContext(), entity, true, mode);
QuarkusMultipartFormUpload multipartFormUpload = new QuarkusMultipartFormUpload(Vertx.currentContext(), multipartForm,
true,
mode);
setEntityRelatedHeaders(headerMap, state.getEntity());

// multipart has its own headers:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpClientResponse;
import io.vertx.ext.web.multipart.MultipartForm;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
Expand All @@ -32,6 +31,7 @@
import javax.ws.rs.ext.WriterInterceptor;
import org.jboss.resteasy.reactive.ClientWebApplicationException;
import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.client.impl.multipart.QuarkusMultipartForm;
import org.jboss.resteasy.reactive.client.spi.ClientRestHandler;
import org.jboss.resteasy.reactive.common.core.AbstractResteasyReactiveContext;
import org.jboss.resteasy.reactive.common.core.Serialisers;
Expand Down Expand Up @@ -156,10 +156,14 @@ public <T> T readEntity(InputStream in,
configuration);
}

ReaderInterceptor[] getReaderInterceptors() {
public ReaderInterceptor[] getReaderInterceptors() {
return configuration.getReaderInterceptors().toArray(Serialisers.NO_READER_INTERCEPTOR);
}

public Map<String, Object> getProperties() {
return properties;
}

public void initialiseResponse(HttpClientResponse vertxResponse) {
MultivaluedMap<String, String> headers = new CaseInsensitiveMap<>();
MultiMap vertxHeaders = vertxResponse.headers();
Expand Down Expand Up @@ -413,7 +417,7 @@ public RestClientRequestContext setAbortedWith(Response abortedWith) {
}

public boolean isMultipart() {
return entity != null && entity.getEntity() instanceof MultipartForm;
return entity != null && entity.getEntity() instanceof QuarkusMultipartForm;
}

public Map<String, Object> getClientFilterProperties() {
Expand Down
Loading

0 comments on commit 106b744

Please sign in to comment.