Skip to content

Commit

Permalink
Merge pull request #74 from FloRul/feature/formulaire
Browse files Browse the repository at this point in the history
Response with form.
  • Loading branch information
brunoabreu-levio authored Mar 22, 2024
2 parents cda2e3c + 51c3bc1 commit c0850cc
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,9 +28,15 @@ public class App implements RequestHandler<SQSEvent, Void> {

private final S3Service s3Service;

private final SqsProducerService sqsProducerService;

private final MailService mailService;

private final HashMap<Integer, Map<String, String>> 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());
Expand All @@ -37,11 +49,13 @@ public App() {

public App(S3Service s3Service,
DocumentService documentService,
ClaudeService claudeService,
ClaudeService claudeService, SqsProducerService sqsProducerService, MailService mailService,
HashMap<Integer, Map<String, String>> questionsMapper) {
this.s3Service = s3Service;
this.documentService = documentService;
this.claudeService = claudeService;
this.sqsProducerService = sqsProducerService;
this.mailService = mailService;
this.questionsMapper = questionsMapper;
}

Expand All @@ -51,22 +65,50 @@ 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<String, String> questionAnswerMap = positionQuestionAnswerMapper.getValue();
String answer = claudeService.getResponse(questionAnswerMap.get("question"), content);
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<String, SQSEvent.MessageAttribute> getMessageAttributes(String sender, String subject, String... attachmentsUri) {
Map<String, SQSEvent.MessageAttribute> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class DocumentService {
public HashMap<Integer, Map<String, String>> retrieveQuestionsMapper() throws IOException {
HashMap<Integer, Map<String, String>> 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<XWPFParagraph> paragraphs = document.getParagraphs();
Expand All @@ -47,7 +47,7 @@ public HashMap<Integer, Map<String, String>> retrieveQuestionsMapper() throws IO
}

public ByteArrayOutputStream fillFile(HashMap<Integer, Map<String, String>> 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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetObjectResponse> objectBytes = getObjectResponseBytes(key);
return new String(objectBytes.asByteArray());
}

public InputStream getFile(String key) {
public InputStream getInputFileStream(String key) {
ResponseBytes<GetObjectResponse> objectBytes = getObjectResponseBytes(key);
return objectBytes.asInputStream();
}

private ResponseBytes<GetObjectResponse> getObjectResponseBytes(String key) {
GetObjectRequest objectRequest = GetObjectRequest
.builder()
.key(key)
.bucket(BUCKET_NAME)
.build();

ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
return objectBytes.asInputStream();
return s3.getObjectAsBytes(objectRequest);
}

public String saveFile(String fileKey, byte[] fileContent) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, SQSEvent.MessageAttribute> messageAttributes, String messageId) {
Map<String, MessageAttributeValue> 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);
}
}

0 comments on commit c0850cc

Please sign in to comment.