Skip to content

Commit

Permalink
Add test coverage for improvement multipart encoded mode in rest-client
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarranzan committed Jun 27, 2024
1 parent 4b1a5cf commit 650cd90
Show file tree
Hide file tree
Showing 12 changed files with 435 additions and 1 deletion.
5 changes: 5 additions & 0 deletions http/rest-client-reactive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-fault-tolerance</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.quarkus.ts.http.restclient.reactive.multipart;

import static jakarta.ws.rs.core.MediaType.MULTIPART_FORM_DATA;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.FormParam;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import org.jboss.resteasy.reactive.PartType;
import org.jboss.resteasy.reactive.RestForm;

@Path("/encoder-mode")
public interface EncoderModeRestClient {

@POST
@Consumes(MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
MyMultipartDTO doAPostRequestToThisResource(
@PartType(MediaType.TEXT_PLAIN) @FormParam("file1") java.nio.file.Path file1,
@PartType(MediaType.TEXT_PLAIN) @FormParam("file2") java.nio.file.Path file2,
@RestForm String otherField);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.quarkus.ts.http.restclient.reactive.multipart;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

@RegisterRestClient(configKey = "encoder-mode-html5")
public interface Html5EncoderModeRestClient extends EncoderModeRestClient {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.quarkus.ts.http.restclient.reactive.multipart;

import java.util.List;
import java.util.Map;

public class Item {
public final String name;
public final long size;
public final String charset;
public final String fileContent;

public String getFileContent() {
return fileContent;
}

public String getName() {
return name;
}

public long getSize() {
return size;
}

public String getCharset() {
return charset;
}

public String getFileName() {
return fileName;
}

public boolean isFileItem() {
return isFileItem;
}

public Map<String, List<String>> getHeaders() {
return headers;
}

public final String fileName;
public final boolean isFileItem;
public final Map<String, List<String>> headers;
public boolean fileItem;

public Item(String name, long size, String charset, String fileName, boolean isFileItem,
Map<String, List<String>> headers, boolean fileItem, String fileContent) {
this.name = name;
this.size = size;
this.charset = charset;
this.fileName = fileName;
this.isFileItem = isFileItem;
this.headers = headers;
this.fileItem = fileItem;
this.fileContent = fileContent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.quarkus.ts.http.restclient.reactive.multipart;

import java.util.List;

public class MyMultipartDTO {
private List<Item> items;

public MyMultipartDTO() {
}

public MyMultipartDTO(List<Item> items) {
this.items = items;
}

public List<Item> getItems() {
return items;
}

public void setItems(List<Item> items) {
this.items = items;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.quarkus.ts.http.restclient.reactive.multipart;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

@RegisterRestClient(configKey = "encoder-mode-rfc1738")
public interface Rfc1738EncoderModeRestClient extends EncoderModeRestClient {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.quarkus.ts.http.restclient.reactive.multipart;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

@RegisterRestClient(configKey = "encoder-mode-rfc3986")
public interface Rfc3986EncoderModeRestClient extends EncoderModeRestClient {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.quarkus.ts.http.restclient.reactive.resources;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.jboss.resteasy.reactive.server.multipart.FormValue;
import org.jboss.resteasy.reactive.server.multipart.MultipartFormDataInput;

import io.quarkus.ts.http.restclient.reactive.multipart.EncoderModeRestClient;
import io.quarkus.ts.http.restclient.reactive.multipart.Html5EncoderModeRestClient;
import io.quarkus.ts.http.restclient.reactive.multipart.Item;
import io.quarkus.ts.http.restclient.reactive.multipart.MyMultipartDTO;
import io.quarkus.ts.http.restclient.reactive.multipart.Rfc1738EncoderModeRestClient;
import io.quarkus.ts.http.restclient.reactive.multipart.Rfc3986EncoderModeRestClient;

@Path("/encode")
public class MultipartPostEncoderModeResource {

private final Map<String, EncoderModeRestClient> modeToRestClient;

public MultipartPostEncoderModeResource(@RestClient Html5EncoderModeRestClient html5Client,
@RestClient Rfc1738EncoderModeRestClient rfc1738Client,
@RestClient Rfc3986EncoderModeRestClient rfc3986Client) {
this.modeToRestClient = Map.of("HTML5", html5Client, "RFC1738", rfc1738Client, "RFC3986", rfc3986Client);
}

@Path("{encoder-mode}")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public MyMultipartDTO doAPostRequestToSimpleEncodeResource(MultipartFormDataInput input,
@PathParam("encoder-mode") String encoderMode) {

EncoderModeRestClient client = modeToRestClient.get(encoderMode);
if (!modeToRestClient.containsKey(encoderMode)) {
throw new WebApplicationException("Unsupported encoder mode" + encoderMode, Response.Status.BAD_REQUEST);
}

Map<String, Collection<FormValue>> formValues = input.getValues();

if (!formValues.containsKey("file1") || !formValues.containsKey("file2") || !formValues.containsKey("otherField")) {
throw new IllegalArgumentException("Missing mandatory fields!");
}

java.nio.file.Path file1 = formValues.get("file1").stream().findFirst().get().getFileItem().getFile();
java.nio.file.Path file2 = formValues.get("file2").stream().findFirst().get().getFileItem().getFile();
String otherField = formValues.get("otherField").stream().findFirst().get().getValue();

MyMultipartDTO response = client.doAPostRequestToThisResource(file1, file2, otherField);

List<Item> items = response.getItems();

return new MyMultipartDTO(items);
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
quarkus.tls.trust-all=true
quarkus.http.ssl.certificate.key-store-file=META-INF/keystore.jks
quarkus.http.ssl.certificate.key-store-password=password
quarkus.http.ssl.certificate.key-store-password=password
vertx-server-url=http://${quarkus.http.host:localhost}:${vertx-server-port}/
quarkus.rest-client.encoder-mode-html5.url=${vertx-server-url}
quarkus.rest-client.encoder-mode-html5.multipart-post-encoder-mode=HTML5
quarkus.rest-client.encoder-mode-rfc1738.url=${vertx-server-url}
quarkus.rest-client.encoder-mode-rfc1738.multipart-post-encoder-mode=RFC1738
quarkus.rest-client.encoder-mode-rfc3986.url=${vertx-server-url}
quarkus.rest-client.encoder-mode-rfc3986.multipart-post-encoder-mode=RFC3986
Loading

0 comments on commit 650cd90

Please sign in to comment.