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 05985f8..5ea3be4 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 @@ -6,11 +6,17 @@ import com.levio.awsdemo.formrequestprocessor.service.ClaudeService; import com.levio.awsdemo.formrequestprocessor.service.DocumentService; import com.levio.awsdemo.formrequestprocessor.service.LambdaService; +import com.levio.awsdemo.formrequestprocessor.service.MailService; import com.levio.awsdemo.formrequestprocessor.service.S3Service; +import com.levio.awsdemo.formrequestprocessor.service.SqsProducerService; +import jakarta.mail.MessagingException; +import jakarta.mail.internet.InternetAddress; +import jakarta.mail.internet.MimeMessage; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -22,9 +28,15 @@ public class App implements RequestHandler { private final S3Service s3Service; + private final SqsProducerService sqsProducerService; + + private final MailService mailService; + private final HashMap> questionsMapper; public App() { + this.mailService = new MailService(); + this.sqsProducerService = new SqsProducerService(); this.s3Service = new S3Service(); this.documentService = new DocumentService(s3Service); this.claudeService = new ClaudeService(new LambdaService()); @@ -37,11 +49,13 @@ public App() { public App(S3Service s3Service, DocumentService documentService, - ClaudeService claudeService, + ClaudeService claudeService, SqsProducerService sqsProducerService, MailService mailService, HashMap> questionsMapper) { this.s3Service = s3Service; this.documentService = documentService; this.claudeService = claudeService; + this.sqsProducerService = sqsProducerService; + this.mailService = mailService; this.questionsMapper = questionsMapper; } @@ -51,10 +65,14 @@ public Void handleRequest(final SQSEvent input, final Context context) { String keyId = record.getBody(); - InputStream fileInputStream = s3Service.getFile("formulaire/attachment/" + keyId + ".txt"); + String email = s3Service.getFile("formulaire/email/" + keyId); try { - byte[] fileByteArray = fileInputStream.readAllBytes(); - String content = new String(fileByteArray); + 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 = s3Service.getFile("formulaire/attachment/" + keyId + ".txt"); questionsMapper.entrySet().parallelStream() .forEach(positionQuestionAnswerMapper -> { Map questionAnswerMap = positionQuestionAnswerMapper.getValue(); @@ -62,11 +80,35 @@ public Void handleRequest(final SQSEvent input, final Context context) { questionAnswerMap.put("answer", answer); }); ByteArrayOutputStream fileOutputStream = documentService.fillFile(questionsMapper); - s3Service.saveFile("formulaire/" + keyId + ".docx", fileOutputStream.toByteArray()); - } catch (IOException e) { + String formDocxUri = s3Service.saveFile("formulaire/" + keyId + ".docx", fileOutputStream.toByteArray()); + sqsProducerService.send(emailBody, getMessageAttributes(sender, subject, formDocxUri), keyId); + } catch (IOException | MessagingException e) { throw new RuntimeException(e); } }); return null; } + + private static Map getMessageAttributes(String sender, String subject, String... attachmentsUri) { + Map messageAttributes = new HashMap<>(); + + SQSEvent.MessageAttribute senderAttribute = new SQSEvent.MessageAttribute(); + senderAttribute.setStringValue(sender); + senderAttribute.setDataType("String"); + messageAttributes.put("sender", senderAttribute); + + SQSEvent.MessageAttribute subjectAttribute = new SQSEvent.MessageAttribute(); + subjectAttribute.setStringValue(subject); + subjectAttribute.setDataType("String"); + messageAttributes.put("subject", subjectAttribute); + + for (int i = 0; i < attachmentsUri.length; i++) { + SQSEvent.MessageAttribute attachmentAttribute = new SQSEvent.MessageAttribute(); + attachmentAttribute.setStringValue(attachmentsUri[i]); + attachmentAttribute.setDataType("String"); + messageAttributes.put("attachment" + i, attachmentAttribute); + } + + return messageAttributes; + } } diff --git a/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/service/DocumentService.java b/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/service/DocumentService.java index 0caccce..3457457 100644 --- a/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/service/DocumentService.java +++ b/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/service/DocumentService.java @@ -23,7 +23,7 @@ public class DocumentService { public HashMap> retrieveQuestionsMapper() throws IOException { HashMap> questionsMapper = new HashMap<>(); - InputStream fileInputStream = s3Service.getFile(STANDARD_FORM_FILE_KEY); + InputStream fileInputStream = s3Service.getInputFileStream(STANDARD_FORM_FILE_KEY); try (XWPFDocument document = new XWPFDocument(fileInputStream)) { List paragraphs = document.getParagraphs(); @@ -47,7 +47,7 @@ public HashMap> retrieveQuestionsMapper() throws IO } public ByteArrayOutputStream fillFile(HashMap> questionsMapper) throws IOException { - InputStream fileInputStream = s3Service.getFile(STANDARD_FORM_FILE_KEY); + InputStream fileInputStream = s3Service.getInputFileStream(STANDARD_FORM_FILE_KEY); try (XWPFDocument document = new XWPFDocument(fileInputStream)) { questionsMapper.entrySet().stream() diff --git a/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/service/MailService.java b/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/service/MailService.java new file mode 100644 index 0000000..f7463af --- /dev/null +++ b/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/service/MailService.java @@ -0,0 +1,18 @@ +package com.levio.awsdemo.formrequestprocessor.service; + +import jakarta.mail.MessagingException; +import jakarta.mail.Session; +import jakarta.mail.internet.MimeMessage; + +import java.io.InputStream; +import java.util.Properties; + +public class MailService { + + public MimeMessage getMimeMessage(InputStream inputStream) throws MessagingException { + Properties props = new Properties(); + Session session = Session.getDefaultInstance(props, null); + + return new MimeMessage(session, inputStream); + } +} 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 7b83e71..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 @@ -18,16 +18,24 @@ public class S3Service { private final S3Client s3 = S3Client.builder() .region(Region.US_EAST_1) .build(); + public String getFile(String key) { + ResponseBytes objectBytes = getObjectResponseBytes(key); + return new String(objectBytes.asByteArray()); + } - public InputStream getFile(String key) { + public InputStream getInputFileStream(String key) { + ResponseBytes objectBytes = getObjectResponseBytes(key); + return objectBytes.asInputStream(); + } + + private ResponseBytes getObjectResponseBytes(String key) { GetObjectRequest objectRequest = GetObjectRequest .builder() .key(key) .bucket(BUCKET_NAME) .build(); - ResponseBytes objectBytes = s3.getObjectAsBytes(objectRequest); - return objectBytes.asInputStream(); + return s3.getObjectAsBytes(objectRequest); } public String saveFile(String fileKey, byte[] fileContent) { diff --git a/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/service/SqsProducerService.java b/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/service/SqsProducerService.java new file mode 100644 index 0000000..05c4974 --- /dev/null +++ b/lambdas/FormProcessor/FormRequestProcessorFunction/src/main/java/com/levio/awsdemo/formrequestprocessor/service/SqsProducerService.java @@ -0,0 +1,38 @@ +package com.levio.awsdemo.formrequestprocessor.service; + +import com.amazonaws.services.lambda.runtime.events.SQSEvent; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.sqs.SqsClient; +import software.amazon.awssdk.services.sqs.model.MessageAttributeValue; +import software.amazon.awssdk.services.sqs.model.SendMessageRequest; + +import java.util.HashMap; +import java.util.Map; + +public class SqsProducerService { + private static final String QUEUE_URL = System.getenv("QUEUE_URL"); + private final SqsClient sqs = SqsClient.builder() + .region(Region.US_EAST_1) + .build(); + + public void send(String message, Map messageAttributes, String messageId) { + Map messageAttributeValues = new HashMap<>(); + messageAttributes.forEach((key, value) -> { + MessageAttributeValue mav = MessageAttributeValue.builder() + .stringValue(value.getStringValue()) + .dataType(value.getDataType()) + .build(); + messageAttributeValues.put(key, mav); + }); + + SendMessageRequest sendMessageRequest = SendMessageRequest.builder() + .queueUrl(QUEUE_URL) + .messageBody(message) + .messageAttributes(messageAttributeValues) + .messageGroupId(messageAttributes.get("sender").getStringValue()) + .messageDeduplicationId(messageId) + .build(); + + sqs.sendMessage(sendMessageRequest); + } +}