Skip to content

Commit

Permalink
migrate to resteasy reactive
Browse files Browse the repository at this point in the history
  • Loading branch information
Postremus committed Apr 10, 2021
1 parent 910e297 commit 0b1216b
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 41 deletions.
6 changes: 1 addition & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,7 @@
<!-- Quarkus Extensions -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus </groupId>
<artifactId>quarkus-resteasy-multipart</artifactId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponses;
import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
import org.jboss.resteasy.reactive.MultipartForm;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.charset.Charset;
import java.util.List;

@Path(RecordJarFullResource.RESOURCE_PATH)
@RequestScoped
@ApplicationScoped
public class RecordJarFullResource {
public static final String RESOURCE_PATH = "record/jar/full/";

Expand All @@ -47,8 +48,9 @@ public class RecordJarFullResource {
responseCode = "400", description = "400 Bad Request. Violations are present in the body.", content = @Content(schema = @Schema(implementation = Violation.class, type = SchemaType.ARRAY))
)
})
public Response uploadMultipartFile(@MultipartForm RecordJarFile recordJarFile) {
List<Record> records = service.convert(recordJarFile.getFile(), recordJarFile.getEncoding());
public Response uploadMultipartFile(@MultipartForm RecordJarFile recordJarFile) throws FileNotFoundException {

List<Record> records = service.convert(new FileInputStream(recordJarFile.getFile().uploadedFile().toFile()), recordJarFile.getEncoding());
return Response.ok().entity(records).build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.pro_crafting.tools.recordjarconverter.rest;

import com.pro_crafting.tools.recordjarconverter.RestApplication;
import org.eclipse.microprofile.health.Health;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.Liveness;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

@Health
@Liveness
@ApplicationScoped
public class RecordJarHealth implements HealthCheck {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponses;
import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
import org.jboss.resteasy.reactive.MultipartForm;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.charset.Charset;
import java.util.*;
import java.util.stream.Collectors;

@Path(RecordJarResource.RESOURCE_PATH)
@RequestScoped
@ApplicationScoped
public class RecordJarResource {
public static final String RESOURCE_PATH = "record/jar/";

Expand All @@ -47,8 +49,8 @@ public class RecordJarResource {
responseCode = "400", description = "400 Bad Request. Violations are present in the body.", content = @Content(schema = @Schema(implementation = Violation.class, type = SchemaType.ARRAY))
)
})
public Response uploadMultipartFile(@MultipartForm RecordJarFile recordJarFile) {
List<Record> records = service.convert(recordJarFile.getFile(), recordJarFile.getEncoding());
public Response uploadMultipartFile(@MultipartForm RecordJarFile recordJarFile) throws FileNotFoundException {
List<Record> records = service.convert(new FileInputStream(recordJarFile.getFile().uploadedFile().toFile()), recordJarFile.getEncoding());
return Response.ok().entity(map(records)).build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.pro_crafting.tools.recordjarconverter.rest.model;

import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.jboss.resteasy.annotations.providers.multipart.PartType;
import org.jboss.resteasy.reactive.PartType;
import org.jboss.resteasy.reactive.multipart.FileUpload;

import javax.ws.rs.FormParam;
import java.io.InputStream;
Expand All @@ -10,18 +11,18 @@ public class RecordJarFile {
@FormParam("file")
@PartType("application/octect-stream")
@Schema(description = "Record Jar formatted file", required = true)
private InputStream file;
private FileUpload file;

@FormParam("encoding")
@PartType("text/plain")
@Schema(description = "Encoding of the specified record-jar formatted file.", example="UTF-8", defaultValue = "UTF-8")
private String encoding;

public InputStream getFile() {
public FileUpload getFile() {
return file;
}

public void setFile(InputStream file) {
public void setFile(FileUpload file) {
this.file = file;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.pro_crafting.tools.recordjarconverter.rest.model;

import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.jboss.resteasy.annotations.providers.multipart.PartType;
import org.jboss.resteasy.reactive.PartType;

import javax.ws.rs.FormParam;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package com.pro_crafting.tools.recordjarconverter.rest;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.pro_crafting.tools.recordjarconverter.IntegrationTestBase;
import com.pro_crafting.tools.recordjarconverter.RestApplication;
import io.restassured.builder.MultiPartSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.internal.multipart.MultiPartSpecificationImpl;
import io.restassured.response.Response;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.nio.charset.StandardCharsets;

import static io.restassured.RestAssured.given;
import static org.apache.commons.io.IOUtils.resourceToByteArray;
Expand All @@ -22,14 +18,14 @@

class RecordJarResourceIT extends IntegrationTestBase {

private static String MULTIPART_FORM_FILE_PATH = RestApplication.VERSION_PATH + RecordJarResource.RESOURCE_PATH + "multipart/file";
private static String MULTIPART_FORM_TEXT_PATH = RestApplication.VERSION_PATH + RecordJarResource.RESOURCE_PATH + "multipart/text";
private static String TEXT_PATH = RestApplication.VERSION_PATH + RecordJarResource.RESOURCE_PATH + "text";
private static final String MULTIPART_FORM_FILE_PATH = RestApplication.VERSION_PATH + RecordJarResource.RESOURCE_PATH + "multipart/file";
private static final String MULTIPART_FORM_TEXT_PATH = RestApplication.VERSION_PATH + RecordJarResource.RESOURCE_PATH + "multipart/text";
private static final String TEXT_PATH = RestApplication.VERSION_PATH + RecordJarResource.RESOURCE_PATH + "text";

@Test
void testUploadMultipartFile() throws IOException {
byte[] bytes = resourceToByteArray("taoup-rj-example.rj", getClass().getClassLoader());
String expected = resourceToString("taoup-rj-converted.json", Charset.forName("UTF-8"), getClass().getClassLoader());
String expected = resourceToString("taoup-rj-converted.json", StandardCharsets.UTF_8, getClass().getClassLoader());
Response response = given()
.multiPart("file", "language-subtag-registry.rj", bytes)
.multiPart("encoding", "UTF-8")
Expand All @@ -45,7 +41,7 @@ void testUploadMultipartFile() throws IOException {
@Test
void testUploadMultipartFileLarge() throws IOException {
byte[] bytes = resourceToByteArray("language-subtag-registry.rj", getClass().getClassLoader());
String expected = resourceToString("language-subtag-registry-converted.json", Charset.forName("UTF-8"), getClass().getClassLoader());
String expected = resourceToString("language-subtag-registry-converted.json", StandardCharsets.UTF_8, getClass().getClassLoader());
Response response = given()
.multiPart("file", "language-subtag-registry.rj", bytes)
.multiPart("encoding", "UTF-8")
Expand All @@ -60,8 +56,8 @@ void testUploadMultipartFileLarge() throws IOException {

@Test
void testUploadMultipartText() throws IOException {
String body = resourceToString("taoup-rj-example.rj", Charset.forName("UTF-8"), getClass().getClassLoader());
String expected = resourceToString("taoup-rj-converted.json", Charset.forName("UTF-8"), getClass().getClassLoader());
String body = resourceToString("taoup-rj-example.rj", StandardCharsets.UTF_8, getClass().getClassLoader());
String expected = resourceToString("taoup-rj-converted.json", StandardCharsets.UTF_8, getClass().getClassLoader());
Response response = given()
.multiPart("text", body)
.multiPart("encoding", "UTF-8")
Expand All @@ -76,10 +72,10 @@ void testUploadMultipartText() throws IOException {

@Test
void testUploadMultipartTextLarge() throws IOException {
String body = resourceToString("language-subtag-registry.rj", Charset.forName("UTF-8"), getClass().getClassLoader());
String expected = resourceToString("language-subtag-registry-converted.json", Charset.forName("UTF-8"), getClass().getClassLoader());
String body = resourceToString("language-subtag-registry.rj", StandardCharsets.UTF_8, getClass().getClassLoader());
String expected = resourceToString("language-subtag-registry-converted.json", StandardCharsets.UTF_8, getClass().getClassLoader());
Response response = given()
.multiPart(new MultiPartSpecBuilder(body).controlName("text").mimeType("text/plain").charset("UTF-8").build())
.multiPart("text", body)
.multiPart("encoding", "UTF-8")
.expect()
.statusCode(200)
Expand All @@ -92,8 +88,8 @@ void testUploadMultipartTextLarge() throws IOException {

@Test
void testUploadText() throws IOException {
String body = resourceToString("taoup-rj-example.rj", Charset.forName("UTF-8"), getClass().getClassLoader());
String expected = resourceToString("taoup-rj-converted.json", Charset.forName("UTF-8"), getClass().getClassLoader());
String body = resourceToString("taoup-rj-example.rj", StandardCharsets.UTF_8, getClass().getClassLoader());
String expected = resourceToString("taoup-rj-converted.json", StandardCharsets.UTF_8, getClass().getClassLoader());
Response response = given()
.body(body)
.queryParam("encoding", "UTF-8")
Expand All @@ -108,8 +104,8 @@ void testUploadText() throws IOException {

@Test
void testUploadTextLarge() throws IOException {
String body = resourceToString("language-subtag-registry.rj", Charset.forName("UTF-8"), getClass().getClassLoader());
String expected = resourceToString("language-subtag-registry-converted.json", Charset.forName("UTF-8"), getClass().getClassLoader());
String body = resourceToString("language-subtag-registry.rj", StandardCharsets.UTF_8, getClass().getClassLoader());
String expected = resourceToString("language-subtag-registry-converted.json", StandardCharsets.UTF_8, getClass().getClassLoader());
Response response = given()
.body(body)
.contentType("text/plain; charset=UTF-8")
Expand Down

0 comments on commit 0b1216b

Please sign in to comment.