Skip to content

Commit

Permalink
🚧 (195): Continue developing base for proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Klaus committed Dec 16, 2024
1 parent ebfbfe8 commit c0a3773
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 9 deletions.
4 changes: 4 additions & 0 deletions backend/app.hopps.fin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@
<groupId>io.quarkiverse.amazonservices</groupId>
<artifactId>quarkus-amazon-s3</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>url-connection-client</artifactId>
</dependency>

<!-- openfga -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

Expand Down Expand Up @@ -43,13 +42,12 @@ public InputStream getFile(String documentKey) {
public void saveFile(DocumentForm documentForm) {
try {
byte[] bytes = IOUtils.toByteArray(documentForm.file());
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);

s3.putObject(PutObjectRequest.builder()
.bucket(bucketName)
.key(documentForm.filename())
.contentType(documentForm.mimetype())
.build(), RequestBody.fromInputStream(byteArrayInputStream, bytes.length));
.build(), RequestBody.fromBytes(bytes));
} catch (IOException e) {
LOG.warn("Could not upload file");
throw new IllegalArgumentException("Could not upload file to s3");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
import app.hopps.fin.jpa.TransactionRecordRepository;
import app.hopps.fin.jpa.entities.TransactionRecord;
import app.hopps.fin.kafka.DocumentProducer;
import io.quarkus.security.Authenticated;
import jakarta.inject.Inject;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;

import java.io.InputStream;
import java.math.BigDecimal;

@Authenticated
@Path("/document")
public class DocumentResource {
private static final Logger LOG = LoggerFactory.getLogger(DocumentResource.class);

private final DocumentProducer documentProducer;
private final S3Handler s3Handler;
private final TransactionRecordRepository repository;
Expand All @@ -33,7 +36,12 @@ public DocumentResource(DocumentProducer documentProducer, S3Handler s3Handler,
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public InputStream getDocumentByKey(@PathParam("documentKey") String documentKey) {
// S3 download
return s3Handler.getFile(documentKey);
try {
return s3Handler.getFile(documentKey);
} catch (NoSuchKeyException ignored) {
LOG.info("File with key {} not found", documentKey);
throw new NotFoundException(Response.status(Response.Status.NOT_FOUND).entity("Document with key " + documentKey + " not found").build());
}
}

@POST
Expand All @@ -46,7 +54,9 @@ public Response uploadDocument(DocumentForm documentForm) {
// Save in database
TransactionRecord transactionRecord = new TransactionRecord(BigDecimal.ZERO);
transactionRecord.setDocumentKey(documentForm.filename());
transactionRecord.setBommelId(documentForm.bommelId());
if (documentForm.bommelId() != null) {
transactionRecord.setBommelId(documentForm.bommelId());
}

repository.persist(transactionRecord);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.io.InputStream;

public record DocumentForm(
@RestForm("file") InputStream file,
@RestForm("file") @PartType(MediaType.APPLICATION_OCTET_STREAM) InputStream file,
@RestForm @PartType(MediaType.TEXT_PLAIN) String filename,
@RestForm @PartType(MediaType.TEXT_PLAIN) String mimetype,
@RestForm @PartType(MediaType.TEXT_PLAIN) Long bommelId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ kogito.service.url=http://localhost:8180
######################################
# S3
######################################
app.hopps.fin.bucket.name=documents
app.hopps.fin.bucket.name=documents
quarkus.s3.path-style-access=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package app.hopps.fin.endpoint;

import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import org.apache.commons.io.IOUtils;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.flywaydb.core.Flyway;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;

import java.io.InputStream;

import static io.restassured.RestAssured.given;

@QuarkusTest
@TestHTTPEndpoint(DocumentResource.class)
class DocumentResourceTest {
@Inject
Flyway flyway;

@Inject
S3Client s3Client;

@ConfigProperty(name = "app.hopps.fin.bucket.name")
String bucketName;

@BeforeEach
void setup() {
flyway.clean();
flyway.migrate();

try (InputStream zugferdInputStream = getClass().getClassLoader().getResourceAsStream("ZUGFeRD.pdf")) {
assert zugferdInputStream != null;
byte[] byteArray = IOUtils.toByteArray(zugferdInputStream);

s3Client.putObject(PutObjectRequest.builder()
.key("imageKey")
.bucket(bucketName)
.build(), RequestBody.fromBytes(byteArray));
} catch (Exception ignored) {
// Do nothing
}
}

@Test
void shouldNotGetFile() {
given()
.when()
.get("{documentKey}", "randomKey")
.then()
.statusCode(Response.Status.NOT_FOUND.getStatusCode());
}

@Test
void shouldGetFile() {
given()
.when()
.get("{documentKey}", "imageKey")
.then()
.statusCode(Response.Status.OK.getStatusCode());
}

@Test
@Disabled("ByteArrayInputStream serialization error")
void shouldUploadFile() {
InputStream zugferdInputStream = getClass().getClassLoader().getResourceAsStream("ZUGFeRD.pdf");

given()
.when()
.multiPart("file", zugferdInputStream)
.multiPart("filename", "ZUGFeRD.pdf")
.multiPart("mimetype", "application/pdf")
.post()
.then()
.statusCode(Response.Status.ACCEPTED.getStatusCode());
}
}
Binary file not shown.

0 comments on commit c0a3773

Please sign in to comment.