From c386178841674650f6d16425a0a5073f016dbd56 Mon Sep 17 00:00:00 2001 From: Joel Balcaen Date: Wed, 3 Apr 2024 14:58:09 -0300 Subject: [PATCH] Revert "add a method to get s3 object as file, add utils to parse pdf file to text." This reverts commit 9226a22d95c7e8d790ab970c5989e3386597b2eb. --- .../FormRequestProcessorFunction/pom.xml | 6 ---- .../awsdemo/formrequestprocessor/App.java | 32 +++---------------- .../service/S3Service.java | 22 ++----------- .../formrequestprocessor/utils/PDF.java | 27 ---------------- 4 files changed, 7 insertions(+), 80 deletions(-) delete mode 100644 lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/utils/PDF.java diff --git a/lambdas/FormProcessor/FormRequestProcessorFunction/pom.xml b/lambdas/FormProcessor/FormRequestProcessorFunction/pom.xml index f936855..1f26b86 100644 --- a/lambdas/FormProcessor/FormRequestProcessorFunction/pom.xml +++ b/lambdas/FormProcessor/FormRequestProcessorFunction/pom.xml @@ -88,12 +88,6 @@ jackson-datatype-joda 2.15.3 - - org.apache.pdfbox - pdfbox - 3.0.2 - - diff --git a/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/App.java b/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/App.java index 1eb3819..11bfccd 100644 --- a/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/App.java +++ b/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/App.java @@ -7,17 +7,13 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.joda.JodaModule; import com.levio.awsdemo.formrequestprocessor.service.*; -import com.levio.awsdemo.formrequestprocessor.utils.PDF; import jakarta.mail.MessagingException; import jakarta.mail.internet.InternetAddress; import jakarta.mail.internet.MimeMessage; -import org.apache.pdfbox.cos.COSDocument; -import org.apache.pdfbox.io.RandomAccessRead; -import org.apache.pdfbox.pdfparser.PDFParser; -import org.apache.pdfbox.pdmodel.PDDocument; -import org.apache.pdfbox.text.PDFTextStripper; -import java.io.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -73,14 +69,14 @@ public Void handleRequest(final SQSEvent input, final Context context) { var formKey = formFillRequest.getFormKey(); var questionsMapper = retrieveDocumentMapper(formKey); - String email = s3Service.getObjectAsString(formKey + "/email/" + formFillRequest.getEmailId()); + String email = s3Service.getFile(formKey + "/email/" + formFillRequest.getEmailId()); try { MimeMessage message = mailService.getMimeMessage(new ByteArrayInputStream(email.getBytes(StandardCharsets.UTF_8))); String emailBody = "Formulaire response"; String sender = ((InternetAddress) message.getFrom()[0]).getAddress(); String subject = message.getSubject(); - String content = getS3ObjectContent(attachmentKey); + String content = s3Service.getFile(attachmentKey); questionsMapper.entrySet().parallelStream() .forEach(positionQuestionAnswerMapper -> { @@ -122,24 +118,6 @@ private static Map getMessageAttributes(Strin return messageAttributes; } - - private String getS3ObjectContent(String key) { - final var isPDF = key.endsWith(".pdf"); - - try { - if (isPDF) { - final var file = s3Service.getObjectAsFile(key); - return PDF.generateTextFromPDF(file); - } else { - return s3Service.getObjectAsString(key); - } - } catch(IOException e) { - System.out.print(e); - } - - return s3Service.getObjectAsString(key); - } - private HashMap> retrieveDocumentMapper(String formKey) { try { return documentService.retrieveQuestionsMapper(formKey); diff --git a/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/service/S3Service.java b/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/service/S3Service.java index d25b8b3..b3e4a3a 100644 --- a/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/service/S3Service.java +++ b/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/service/S3Service.java @@ -9,7 +9,7 @@ import software.amazon.awssdk.services.s3.model.PutObjectRequest; import software.amazon.awssdk.services.s3.model.PutObjectResponse; -import java.io.*; +import java.io.InputStream; public class S3Service { @@ -18,27 +18,11 @@ public class S3Service { private final S3Client s3 = S3Client.builder() .region(Region.US_EAST_1) .build(); - - public String getObjectAsString(String key) { + public String getFile(String key) { ResponseBytes objectBytes = getObjectResponseBytes(key); return new String(objectBytes.asByteArray()); } - public File getObjectAsFile(String key) { - try { - ResponseBytes objectBytes = getObjectResponseBytes(key); - final var file = new File("/tmp/"+key); - OutputStream os = new FileOutputStream(file); - os.write(objectBytes.asByteArray()); - os.close(); - return file; - } catch (IOException e) { - e.printStackTrace(); - } - - return null; - } - public InputStream getInputFileStream(String key) { ResponseBytes objectBytes = getObjectResponseBytes(key); return objectBytes.asInputStream(); @@ -52,10 +36,8 @@ private ResponseBytes getObjectResponseBytes(String key) { .build(); return s3.getObjectAsBytes(objectRequest); - } - public String saveFile(String fileKey, byte[] fileContent) { PutObjectResponse objectResponse = s3.putObject( PutObjectRequest.builder() diff --git a/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/utils/PDF.java b/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/utils/PDF.java deleted file mode 100644 index 7b2b460..0000000 --- a/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/utils/PDF.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.levio.awsdemo.formrequestprocessor.utils; - -import org.apache.pdfbox.cos.COSDocument; -import org.apache.pdfbox.io.RandomAccessRead; -import org.apache.pdfbox.pdfparser.PDFParser; -import org.apache.pdfbox.pdmodel.PDDocument; -import org.apache.pdfbox.text.PDFTextStripper; - -import java.io.File; -import java.io.IOException; -import java.io.RandomAccessFile; - -public class PDF { - - public static String generateTextFromPDF(File file) throws IOException { - String parsedText; - PDFParser parser = new PDFParser((RandomAccessRead) new RandomAccessFile(file, "r")); - parser.parse(); - - COSDocument cosDoc = parser.parse().getDocument(); - PDFTextStripper pdfStripper = new PDFTextStripper(); - PDDocument pdDoc = new PDDocument(cosDoc); - parsedText = pdfStripper.getText(pdDoc); - - return parsedText; - } -}